Refactor webUpdater to remove logic from firmware.ino

This commit is contained in:
Florian Eitel 2019-08-04 12:46:42 +02:00
parent ed913d740e
commit 007d2e4e73
No known key found for this signature in database
GPG key ID: 9987EAFEF6F686BB
2 changed files with 21 additions and 31 deletions

View file

@ -55,9 +55,6 @@ uint16_t update_webserver_cnt = 0;
WiFiManager wifiManager; WiFiManager wifiManager;
#ifdef WEBUPDATER_FEATURE
String localIP = "127.0.0.1";
#endif
//*************************************************************************// //*************************************************************************//
void debug(String x) { void debug(String x) {
@ -144,8 +141,7 @@ void setup() {
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
#ifndef BATTERY_POWERED #ifndef BATTERY_POWERED
setupWebUpdater(); setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString());
localIP = WiFi.localIP().toString();
#endif #endif
#endif #endif
@ -252,7 +248,7 @@ void _loop() {
#endif #endif
#ifdef WEBUPDATER_FEATURE #ifdef WEBUPDATER_FEATURE
setSensorData(DEVICE_NAME, localIP, currentSensorData); setSensorData(currentSensorData);
#endif #endif
} }

View file

@ -15,16 +15,15 @@
ESP8266WebServer httpServer(8080); ESP8266WebServer httpServer(8080);
ESP8266HTTPUpdateServer httpUpdater; ESP8266HTTPUpdateServer httpUpdater;
String ip = "127.0.0.1"; String _webUpdater_ip = "127.0.0.1";
String dev = "unknown"; String _webUpdater_dev = "unknown";
float sensValues[6]; float _webUpdater_sensValues[VALUES];
void setupWebUpdater(void) void setupWebUpdater(String device, String ip)
{ {
#ifdef DEBUG debug("Starting WebUpdater... " + ip);
Serial.println(); _webUpdater_ip = ip;
Serial.println("Starting WebUpdater..."); _webUpdater_dev = device;
#endif
httpUpdater.setup(&httpServer); httpUpdater.setup(&httpServer);
@ -32,9 +31,7 @@ void setupWebUpdater(void)
httpServer.begin(); httpServer.begin();
#ifdef DEBUG debug("HTTPUpdateServer ready!");
Serial.println("HTTPUpdateServer ready!");
#endif
} }
void doWebUpdater(void) void doWebUpdater(void)
@ -43,27 +40,24 @@ void doWebUpdater(void)
httpServer.handleClient(); httpServer.handleClient();
} }
void setSensorData(String device, String localip, float sensorValues[]) { void setSensorData(float sensorValues[]) {
dev = device; for (uint8_t i = 0; i < VALUES; i++) {
ip = localip; _webUpdater_sensValues[i] = sensorValues[i];
for (uint8_t i = 0; i < VALUES; i++)
{
sensValues[i] = sensorValues[i];
} }
} }
void showHTMLMain(void) { void showHTMLMain(void) {
String message = "<html><head><title>OKO Weatherstation - " + String(dev) + "</title>" String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
"<meta http-equiv=\"refresh\" content=\"20\">" "<meta http-equiv=\"refresh\" content=\"20\">"
"</head><body>" "</head><body>"
"<br><a href=\"http://" + ip + ":8080/update\">firmware update</a><br><br>" "<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>"
"<table>" "<table>"
"<tr><td>temperature</td><td>" + String(sensValues[SENSOR_TEMPERATURE]) + "</td></tr>" "<tr><td>temperature</td><td>" + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + "</td></tr>"
"<tr><td>humidity</td><td>" + String(sensValues[SENSOR_HUMIDITY]) + "</td></tr>" "<tr><td>humidity</td><td>" + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + "</td></tr>"
"<tr><td>light</td><td>" + String(sensValues[SENSOR_LIGHT]) + "</td></tr>" "<tr><td>light</td><td>" + String(_webUpdater_sensValues[SENSOR_LIGHT]) + "</td></tr>"
"<tr><td>windspeed</td><td>" + String(sensValues[SENSOR_WINDSPEED]) + "</td></tr>" "<tr><td>windspeed</td><td>" + String(_webUpdater_sensValues[SENSOR_WINDSPEED]) + "</td></tr>"
"<tr><td>pressure</td><td>" + String(sensValues[SENSOR_PRESSURE]) + "</td></tr>" "<tr><td>pressure</td><td>" + String(_webUpdater_sensValues[SENSOR_PRESSURE]) + "</td></tr>"
"<tr><td>batvoltage</td><td>" + String(sensValues[SENSOR_BAT_VOLTAGE]) + "</td></tr>" "<tr><td>batvoltage</td><td>" + String(_webUpdater_sensValues[SENSOR_BAT_VOLTAGE]) + "</td></tr>"
"</table>" "</table>"
"</body></html>"; "</body></html>";