Fix esp32 build

This commit is contained in:
Magnus Persson
2022-01-24 07:54:59 +01:00
parent 1a9283b719
commit 080820f10f
2 changed files with 87 additions and 66 deletions

View File

@ -23,7 +23,6 @@ SOFTWARE.
*/ */
#if defined(ESP8266) #if defined(ESP8266)
#include <ESP8266HTTPClient.h> #include <ESP8266HTTPClient.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h> #include <ESP8266mDNS.h>
#else // defined (ESP32) #else // defined (ESP32)
#include <HTTPClient.h> #include <HTTPClient.h>
@ -55,13 +54,15 @@ void PushTarget::send(float angle, float gravitySG, float corrGravitySG,
_ms = millis(); _ms = millis();
#if defined(ESP8266)
if (ESP.getFreeContStack() < 1500) { if (ESP.getFreeContStack() < 1500) {
Log.error(F("PUSH: Low on memory, skipping push since it will crasch. (stack=%d, heap=%d)." CR), ESP.getFreeContStack(), ESP.getFreeHeap()); Log.error(F("PUSH: Low on memory, skipping push since it will crasch. "
"(stack=%d, heap=%d)." CR),
ESP.getFreeContStack(), ESP.getFreeHeap());
myWifi.closeWifiClient(); myWifi.closeWifiClient();
return; return;
} }
#endif
//MDNS.close();
TemplatingEngine engine; TemplatingEngine engine;
engine.initialize(angle, gravitySG, corrGravitySG, tempC, runTime); engine.initialize(angle, gravitySG, corrGravitySG, tempC, runTime);
@ -179,7 +180,8 @@ void PushTarget::sendBrewfather(TemplatingEngine& engine) {
// //
void PushTarget::sendHttp(TemplatingEngine& engine, int index) { void PushTarget::sendHttp(TemplatingEngine& engine, int index) {
#if !defined(PUSH_DISABLE_LOGGING) #if !defined(PUSH_DISABLE_LOGGING)
Log.notice(F("PUSH: Sending values to http (%s)" CR), index ? "http2" : "http1"); Log.notice(F("PUSH: Sending values to http (%s)" CR),
index ? "http2" : "http1");
#endif #endif
String serverPath, doc; String serverPath, doc;
@ -187,8 +189,7 @@ void PushTarget::sendHttp(TemplatingEngine& engine, int index) {
if (index == 0) { if (index == 0) {
serverPath = myConfig.getHttpPushUrl(); serverPath = myConfig.getHttpPushUrl();
doc = engine.create(TemplatingEngine::TEMPLATE_HTTP1); doc = engine.create(TemplatingEngine::TEMPLATE_HTTP1);
} } else {
else {
serverPath = myConfig.getHttpPushUrl2(); serverPath = myConfig.getHttpPushUrl2();
doc = engine.create(TemplatingEngine::TEMPLATE_HTTP2); doc = engine.create(TemplatingEngine::TEMPLATE_HTTP2);
} }
@ -258,10 +259,10 @@ void PushTarget::sendMqtt(TemplatingEngine& engine) {
mqtt.setTimeout(10); // 10 seconds timeout mqtt.setTimeout(10); // 10 seconds timeout
int lines = 1; int lines = 1;
// Find out how many lines are in the document. Each line is one topic/message. | is used as new line. // Find out how many lines are in the document. Each line is one
// topic/message. | is used as new line.
for (unsigned int i = 0; i < doc.length() - 1; i++) { for (unsigned int i = 0; i < doc.length() - 1; i++) {
if (doc.charAt(i) == '|') if (doc.charAt(i) == '|') lines++;
lines++;
} }
int index = 0; int index = 0;
@ -273,7 +274,8 @@ void PushTarget::sendMqtt(TemplatingEngine& engine) {
String topic = line.substring(0, line.indexOf(":")); String topic = line.substring(0, line.indexOf(":"));
String value = line.substring(line.indexOf(":") + 1); String value = line.substring(line.indexOf(":") + 1);
#if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING) #if LOG_LEVEL == 6 && !defined(PUSH_DISABLE_LOGGING)
Log.verbose(F("PUSH: topic '%s', value '%s'." CR), topic.c_str(), value.c_str()); Log.verbose(F("PUSH: topic '%s', value '%s'." CR), topic.c_str(),
value.c_str());
#endif #endif
if (mqtt.publish(topic, value)) { if (mqtt.publish(topic, value)) {
Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str()); Log.notice(F("PUSH: MQTT publish successful on %s" CR), topic.c_str());

View File

@ -27,9 +27,9 @@ SOFTWARE.
#include <helper.hpp> #include <helper.hpp>
#include <main.hpp> #include <main.hpp>
#include <resources.hpp> #include <resources.hpp>
#include <templating.hpp>
#include <tempsensor.hpp> #include <tempsensor.hpp>
#include <webserver.hpp> #include <webserver.hpp>
#include <templating.hpp>
#include <wifi.hpp> #include <wifi.hpp>
WebServerHandler myWebServerHandler; // My wrapper class fr webserver functions WebServerHandler myWebServerHandler; // My wrapper class fr webserver functions
@ -612,12 +612,14 @@ void WebServerHandler::webHandleConfigFormatWrite() {
} else if (_server->hasArg(PARAM_FORMAT_HTTP2)) { } else if (_server->hasArg(PARAM_FORMAT_HTTP2)) {
success = writeFile(TPL_FNAME_HTTP2, _server->arg(PARAM_FORMAT_HTTP2)); success = writeFile(TPL_FNAME_HTTP2, _server->arg(PARAM_FORMAT_HTTP2));
} else if (_server->hasArg(PARAM_FORMAT_INFLUXDB)) { } else if (_server->hasArg(PARAM_FORMAT_INFLUXDB)) {
success = writeFile(TPL_FNAME_INFLUXDB, _server->arg(PARAM_FORMAT_INFLUXDB)); success =
writeFile(TPL_FNAME_INFLUXDB, _server->arg(PARAM_FORMAT_INFLUXDB));
} else if (_server->hasArg(PARAM_FORMAT_MQTT)) { } else if (_server->hasArg(PARAM_FORMAT_MQTT)) {
success = writeFile(TPL_FNAME_MQTT, _server->arg(PARAM_FORMAT_MQTT)); success = writeFile(TPL_FNAME_MQTT, _server->arg(PARAM_FORMAT_MQTT));
} }
/*else if (_server->hasArg(PARAM_FORMAT_BREWFATHER)) { /*else if (_server->hasArg(PARAM_FORMAT_BREWFATHER)) {
success = writeFile(TPL_FNAME_BREWFATHER, _server->arg(PARAM_FORMAT_BREWFATHER)); success = writeFile(TPL_FNAME_BREWFATHER,
_server->arg(PARAM_FORMAT_BREWFATHER));
}*/ }*/
if (success) { if (success) {
@ -632,7 +634,8 @@ void WebServerHandler::webHandleConfigFormatWrite() {
} }
// //
// Write file to disk, if there is no data then delete the current file (if it exists) = reset to default. // Write file to disk, if there is no data then delete the current file (if it
// exists) = reset to default.
// //
bool WebServerHandler::writeFile(String fname, String data) { bool WebServerHandler::writeFile(String fname, String data) {
if (data.length()) { if (data.length()) {
@ -640,12 +643,18 @@ bool WebServerHandler::writeFile(String fname, String data) {
File file = LittleFS.open(fname, "w"); File file = LittleFS.open(fname, "w");
if (file) { if (file) {
Log.notice(F("WEB : Storing template data in %s." CR), fname.c_str()); Log.notice(F("WEB : Storing template data in %s." CR), fname.c_str());
#if defined(ESP8266)
file.write(data.c_str()); file.write(data.c_str());
#else // defined (ESP32)
file.write((unsigned char*)data.c_str(), data.length());
#endif
file.close(); file.close();
return true; return true;
} }
} else { } else {
Log.notice(F("WEB : No template data to store in %s, reverting to default." CR), fname.c_str()); Log.notice(
F("WEB : No template data to store in %s, reverting to default." CR),
fname.c_str());
LittleFS.remove(fname); LittleFS.remove(fname);
return true; return true;
} }
@ -673,7 +682,6 @@ String WebServerHandler::readFile(String fname) {
// Get format templates // Get format templates
// //
void WebServerHandler::webHandleConfigFormatRead() { void WebServerHandler::webHandleConfigFormatRead() {
LOG_PERF_START("webserver-api-config-format-read"); LOG_PERF_START("webserver-api-config-format-read");
Log.notice(F("WEB : webServer callback for /api/config/formula(get)." CR)); Log.notice(F("WEB : webServer callback for /api/config/formula(get)." CR));
@ -863,13 +871,18 @@ bool WebServerHandler::setupWebServer() {
// Static content // Static content
#if defined(EMBED_HTML) #if defined(EMBED_HTML)
_server->on("/", std::bind(&WebServerHandler::webReturnIndexHtm, this)); _server->on("/", std::bind(&WebServerHandler::webReturnIndexHtm, this));
_server->on("/index.htm", std::bind(&WebServerHandler::webReturnIndexHtm, this)); _server->on("/index.htm",
_server->on("/device.htm", std::bind(&WebServerHandler::webReturnDeviceHtm, this)); std::bind(&WebServerHandler::webReturnIndexHtm, this));
_server->on("/config.htm", std::bind(&WebServerHandler::webReturnConfigHtm, this)); _server->on("/device.htm",
std::bind(&WebServerHandler::webReturnDeviceHtm, this));
_server->on("/config.htm",
std::bind(&WebServerHandler::webReturnConfigHtm, this));
_server->on("/calibration.htm", _server->on("/calibration.htm",
std::bind(&WebServerHandler::webReturnCalibrationHtm, this)); std::bind(&WebServerHandler::webReturnCalibrationHtm, this));
_server->on("/format.htm", std::bind(&WebServerHandler::webReturnFormatHtm, this)); _server->on("/format.htm",
_server->on("/about.htm", std::bind(&WebServerHandler::webReturnAboutHtm, this)); std::bind(&WebServerHandler::webReturnFormatHtm, this));
_server->on("/about.htm",
std::bind(&WebServerHandler::webReturnAboutHtm, this));
#else #else
// Show files in the filessytem at startup // Show files in the filessytem at startup
@ -900,7 +913,8 @@ bool WebServerHandler::setupWebServer() {
// Also add the static upload view in case we we have issues that needs to // Also add the static upload view in case we we have issues that needs to
// be fixed. // be fixed.
_server->on("/upload.htm", std::bind(&WebServerHandler::webReturnUploadHtm, this)); _server->on("/upload.htm",
std::bind(&WebServerHandler::webReturnUploadHtm, this));
} else { } else {
Log.error(F("WEB : Missing html files, starting with upload UI." CR)); Log.error(F("WEB : Missing html files, starting with upload UI." CR));
_server->on("/", std::bind(&WebServerHandler::webReturnUploadHtm, this)); _server->on("/", std::bind(&WebServerHandler::webReturnUploadHtm, this));
@ -908,9 +922,11 @@ bool WebServerHandler::setupWebServer() {
#endif #endif
// Dynamic content // Dynamic content
_server->on("/api/config", HTTP_GET, _server->on(
"/api/config", HTTP_GET,
std::bind(&WebServerHandler::webHandleConfig, this)); // Get config.json std::bind(&WebServerHandler::webHandleConfig, this)); // Get config.json
_server->on("/api/device", HTTP_GET, _server->on(
"/api/device", HTTP_GET,
std::bind(&WebServerHandler::webHandleDevice, this)); // Get device.json std::bind(&WebServerHandler::webHandleDevice, this)); // Get device.json
_server->on("/api/formula", HTTP_GET, _server->on("/api/formula", HTTP_GET,
std::bind(&WebServerHandler::webHandleFormulaRead, std::bind(&WebServerHandler::webHandleFormulaRead,
@ -921,21 +937,23 @@ bool WebServerHandler::setupWebServer() {
_server->on("/api/calibrate", HTTP_POST, _server->on("/api/calibrate", HTTP_POST,
std::bind(&WebServerHandler::webHandleCalibrate, std::bind(&WebServerHandler::webHandleCalibrate,
this)); // Run calibration routine (param id) this)); // Run calibration routine (param id)
_server->on("/api/factory", HTTP_GET,
std::bind(&WebServerHandler::webHandleFactoryReset,
this)); // Reset the device
_server->on("/api/status", HTTP_GET,
std::bind(&WebServerHandler::webHandleStatus,
this)); // Get the status.json
_server->on("/api/clearwifi", HTTP_GET,
std::bind(&WebServerHandler::webHandleClearWIFI,
this)); // Clear wifi settings
_server->on( _server->on(
"/api/factory", HTTP_GET, "/api/upload", HTTP_GET,
std::bind(&WebServerHandler::webHandleFactoryReset, this)); // Reset the device
_server->on(
"/api/status", HTTP_GET,
std::bind(&WebServerHandler::webHandleStatus, this)); // Get the status.json
_server->on(
"/api/clearwifi", HTTP_GET,
std::bind(&WebServerHandler::webHandleClearWIFI, this)); // Clear wifi settings
_server->on("/api/upload", HTTP_GET,
std::bind(&WebServerHandler::webHandleUpload, this)); // Get upload.json std::bind(&WebServerHandler::webHandleUpload, this)); // Get upload.json
_server->on( _server->on("/api/upload", HTTP_POST,
"/api/upload", HTTP_POST, std::bind(&WebServerHandler::webReturnOK, this), std::bind(&WebServerHandler::webReturnOK, this),
std::bind(&WebServerHandler::webHandleUploadFile, this)); // File upload data std::bind(&WebServerHandler::webHandleUploadFile,
this)); // File upload data
_server->on("/api/status/sleepmode", HTTP_POST, _server->on("/api/status/sleepmode", HTTP_POST,
std::bind(&WebServerHandler::webHandleStatusSleepmode, std::bind(&WebServerHandler::webHandleStatusSleepmode,
this)); // Change sleep mode this)); // Change sleep mode
@ -961,7 +979,8 @@ bool WebServerHandler::setupWebServer() {
std::bind(&WebServerHandler::webHandleDeviceParam, std::bind(&WebServerHandler::webHandleDeviceParam,
this)); // Change device params this)); // Change device params
_server->onNotFound(std::bind(&WebServerHandler::webHandlePageNotFound, this)); _server->onNotFound(
std::bind(&WebServerHandler::webHandlePageNotFound, this));
_server->begin(); _server->begin();
Log.notice(F("WEB : Web server started." CR)); Log.notice(F("WEB : Web server started." CR));
return true; return true;