Browse Source

Refactor webUpdater to remove logic from firmware.ino

Florian Eitel 1 year ago
parent
commit
007d2e4e73
No account linked to committer's email address
2 changed files with 21 additions and 31 deletions
  1. 2
    6
      firmware/firmware.ino
  2. 19
    25
      firmware/webUpdater.ino

+ 2
- 6
firmware/firmware.ino View File

@@ -55,9 +55,6 @@ uint16_t update_webserver_cnt   = 0;
55 55
 
56 56
 WiFiManager wifiManager;
57 57
 
58
-#ifdef WEBUPDATER_FEATURE
59
-String localIP = "127.0.0.1";
60
-#endif
61 58
 //*************************************************************************//
62 59
 
63 60
 void debug(String x) {
@@ -144,8 +141,7 @@ void setup() {
144 141
 
145 142
 #ifdef WEBUPDATER_FEATURE
146 143
 #ifndef BATTERY_POWERED
147
-  setupWebUpdater();
148
-  localIP = WiFi.localIP().toString();
144
+  setupWebUpdater(DEVICE_NAME, WiFi.localIP().toString());
149 145
 #endif
150 146
 #endif
151 147
 
@@ -252,7 +248,7 @@ void _loop() {
252 248
 #endif
253 249
 
254 250
 #ifdef WEBUPDATER_FEATURE
255
-  setSensorData(DEVICE_NAME, localIP, currentSensorData);
251
+  setSensorData(currentSensorData);
256 252
 #endif
257 253
 }
258 254
 

+ 19
- 25
firmware/webUpdater.ino View File

@@ -15,16 +15,15 @@
15 15
 ESP8266WebServer httpServer(8080);
16 16
 ESP8266HTTPUpdateServer httpUpdater;
17 17
 
18
-String ip = "127.0.0.1";
19
-String dev = "unknown";
20
-float sensValues[6];
18
+String _webUpdater_ip = "127.0.0.1";
19
+String _webUpdater_dev = "unknown";
20
+float _webUpdater_sensValues[VALUES];
21 21
 
22
-void setupWebUpdater(void)
22
+void setupWebUpdater(String device, String ip)
23 23
 {
24
-#ifdef DEBUG
25
-  Serial.println();
26
-  Serial.println("Starting WebUpdater...");
27
-#endif
24
+  debug("Starting WebUpdater... " + ip);
25
+  _webUpdater_ip = ip;
26
+  _webUpdater_dev = device;
28 27
 
29 28
   httpUpdater.setup(&httpServer);
30 29
 
@@ -32,9 +31,7 @@ void setupWebUpdater(void)
32 31
 
33 32
   httpServer.begin();
34 33
 
35
-#ifdef DEBUG
36
-  Serial.println("HTTPUpdateServer ready!");
37
-#endif
34
+  debug("HTTPUpdateServer ready!");
38 35
 }
39 36
 
40 37
 void doWebUpdater(void)
@@ -43,27 +40,24 @@ void doWebUpdater(void)
43 40
   httpServer.handleClient();
44 41
 }
45 42
 
46
-void setSensorData(String device, String localip, float sensorValues[]) {
47
-  dev = device;
48
-  ip = localip;
49
-  for (uint8_t i = 0; i < VALUES; i++)
50
-  {
51
-    sensValues[i] = sensorValues[i];
43
+void setSensorData(float sensorValues[]) {
44
+  for (uint8_t i = 0; i < VALUES; i++) {
45
+    _webUpdater_sensValues[i] = sensorValues[i];
52 46
   }
53 47
 }
54 48
 
55 49
 void showHTMLMain(void) {
56
-  String message = "<html><head><title>OKO Weatherstation - " + String(dev) + "</title>"
50
+  String message = "<html><head><title>OKO Weatherstation - " + String(_webUpdater_dev) + "</title>"
57 51
 "<meta http-equiv=\"refresh\" content=\"20\">"
58 52
 "</head><body>"
59
-"<br><a href=\"http://" + ip + ":8080/update\">firmware update</a><br><br>"
53
+"<br><a href=\"http://" + _webUpdater_ip + ":8080/update\">firmware update</a><br><br>"
60 54
 "<table>"
61
-"<tr><td>temperature</td><td>" + String(sensValues[SENSOR_TEMPERATURE]) + "</td></tr>"
62
-"<tr><td>humidity</td><td>" + String(sensValues[SENSOR_HUMIDITY]) + "</td></tr>"
63
-"<tr><td>light</td><td>" + String(sensValues[SENSOR_LIGHT]) + "</td></tr>"
64
-"<tr><td>windspeed</td><td>" + String(sensValues[SENSOR_WINDSPEED]) + "</td></tr>"
65
-"<tr><td>pressure</td><td>" + String(sensValues[SENSOR_PRESSURE]) + "</td></tr>"
66
-"<tr><td>batvoltage</td><td>" + String(sensValues[SENSOR_BAT_VOLTAGE]) + "</td></tr>"
55
+"<tr><td>temperature</td><td>" + String(_webUpdater_sensValues[SENSOR_TEMPERATURE]) + "</td></tr>"
56
+"<tr><td>humidity</td><td>" + String(_webUpdater_sensValues[SENSOR_HUMIDITY]) + "</td></tr>"
57
+"<tr><td>light</td><td>" + String(_webUpdater_sensValues[SENSOR_LIGHT]) + "</td></tr>"
58
+"<tr><td>windspeed</td><td>" + String(_webUpdater_sensValues[SENSOR_WINDSPEED]) + "</td></tr>"
59
+"<tr><td>pressure</td><td>" + String(_webUpdater_sensValues[SENSOR_PRESSURE]) + "</td></tr>"
60
+"<tr><td>batvoltage</td><td>" + String(_webUpdater_sensValues[SENSOR_BAT_VOLTAGE]) + "</td></tr>"
67 61
 "</table>"
68 62
 "</body></html>";
69 63
 

Loading…
Cancel
Save