Sfoglia il codice sorgente

Moved Persistent Storage stuff into own h/cpp files.

Thomas Buck 7 anni fa
parent
commit
35a4c48c4a
3 ha cambiato i file con 111 aggiunte e 85 eliminazioni
  1. 37
    85
      ESP-Weather.ino
  2. 46
    0
      storage.cpp
  3. 28
    0
      storage.h

+ 37
- 85
ESP-Weather.ino Vedi File

6
 #include <vector>
6
 #include <vector>
7
 #include <WebSocketServer.h>
7
 #include <WebSocketServer.h>
8
 #include <WiFiServer.h>
8
 #include <WiFiServer.h>
9
-#include <EEPROM.h>
10
 #include <DNSServer.h>
9
 #include <DNSServer.h>
11
 #include <WiFiManager.h>
10
 #include <WiFiManager.h>
11
+#include "storage.h"
12
 
12
 
13
+#define WEB_PORT 80
14
+#define BROADCAST_PORT 2390
15
+#define WEBSOCKET_PORT 2391
13
 #define NTP_PORT 2392
16
 #define NTP_PORT 2392
14
-#define MAX_WAIT_TIME 550
15
-#define EEPROM_SIZE 512
17
+
18
+#define DEFAULT_SSID "ESP-Weather"
19
+#define DEFAULT_PASS "testtest"
20
+
21
+#define MAX_BROADCAST_WAIT_TIME 550
22
+#define NTP_RETRY_TIMEOUT 2000
16
 
23
 
17
 // NTP-Client
24
 // NTP-Client
18
 IPAddress timeServerIP; 
25
 IPAddress timeServerIP; 
27
 unsigned long lastNTP = 0;
34
 unsigned long lastNTP = 0;
28
 
35
 
29
 SHT21 SHT21;
36
 SHT21 SHT21;
30
-ESP8266WebServer server(80);
31
-WiFiServer serverSocket(2391);
37
+ESP8266WebServer server(WEB_PORT);
38
+WiFiServer serverSocket(WEBSOCKET_PORT);
32
 WebSocketServer webSocketServer;
39
 WebSocketServer webSocketServer;
33
 
40
 
34
-#define DEFAULT_SSID "ESP-Weather"
35
-#define DEFAULT_PASS "testtest"
36
-
37
 IPAddress broadcastIP;
41
 IPAddress broadcastIP;
38
 
42
 
39
-struct __attribute__((__packed__)) Measurement {
40
-    float temperature;
41
-    float humidity;
42
-};
43
-
44
-struct __attribute__((__packed__)) Header {
45
-    uint16_t count;
46
-    uint16_t checksum;
47
-};
48
-
49
-#define MAX_STORAGE (EEPROM_SIZE - sizeof(Header)) / sizeof(Measurement)
50
-
51
-struct __attribute__((__packed__)) PersistentStorage {
52
-    Measurement data[MAX_STORAGE];
53
-    Header header;
54
-};
55
-
56
 PersistentStorage storage;
43
 PersistentStorage storage;
57
 
44
 
58
-void writeMemory(PersistentStorage &s) {
59
-    Serial.println("write Memory");
60
-    unsigned char* r = (unsigned char*) &s;
61
-    uint16_t a = 0, b = 0;
62
-    for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
63
-        a = (a + r[i]) % 255;
64
-        b = (b + a) % 255;
65
-    }
66
-    s.header.checksum = (b << 8) | a;
67
-    for (int i = 0; i < sizeof(PersistentStorage); i++) {
68
-        EEPROM.write(i, r[i]);
69
-    }
70
-    EEPROM.commit();
71
-}
72
-
73
-PersistentStorage readMemory() {
74
-    PersistentStorage s;
75
-    unsigned char* r = (unsigned char*) &s;
76
-    for (int i = 0; i < sizeof(PersistentStorage); i++) {
77
-        r[i] = EEPROM.read(i);
78
-    }
79
-    uint16_t a = 0, b = 0;
80
-    for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
81
-        a = (a + r[i]) % 255;
82
-        b = (b + a) % 255;
83
-    }
84
-    if (s.header.checksum != ((b << 8) | a)) {
85
-        Serial.print("Checksum error ");
86
-        Serial.print(s.header.checksum);
87
-        Serial.print(" ");
88
-        Serial.println((b << 8) | a);
89
-        s.header.count = 0;
90
-    } else {
91
-        Serial.println("Checksum ok");
92
-    }
93
-    return s;
94
-}
95
-
96
 std::vector<IPAddress> vecClients; 
45
 std::vector<IPAddress> vecClients; 
97
 
46
 
98
 // UDP-Config
47
 // UDP-Config
99
-unsigned int localPort = 2390;
100
-char packetBuffer[255];                                       
101
-char pingBuffer[] = "pingESP8266v0.1";
102
-char echoBuffer[] = "echoESP8266v0.1";
48
+#define UDP_PACKET_BUFFER_SIZE 255
49
+char packetBuffer[UDP_PACKET_BUFFER_SIZE];
50
+const char pingBuffer[] = "pingESP8266v0.1";
51
+const char echoBuffer[] = "echoESP8266v0.1";
103
 WiFiUDP Udp;
52
 WiFiUDP Udp;
104
 
53
 
105
 unsigned long lastTime;
54
 unsigned long lastTime;
116
 <script type=\"text/javascript\">";
65
 <script type=\"text/javascript\">";
117
 const char* htmlEnd = "</script></body></html>";
66
 const char* htmlEnd = "</script></body></html>";
118
 
67
 
119
-// root-URL
120
 void handleRoot() {
68
 void handleRoot() {
121
-    // send a reply, to the IP address and port that sent us the packet we received
122
-    Udp.beginPacket(broadcastIP, 2390);
69
+    Serial.println("Sending UDP Broadcast...");
70
+
71
+    // Send UDP broadcast to other modules
72
+    Udp.beginPacket(broadcastIP, BROADCAST_PORT);
123
     Udp.write(pingBuffer);
73
     Udp.write(pingBuffer);
124
     Udp.endPacket();
74
     Udp.endPacket();
125
 
75
 
126
-    // Timer starten
76
+    // Start reply wait timer
127
     lastTime = millis();
77
     lastTime = millis();
128
     waitingForReplies = true;
78
     waitingForReplies = true;
129
-
130
-    Serial.println("Sending UDP Broadcast...");
131
 }
79
 }
132
 
80
 
133
-// URL nicht vorhanden
134
 void handleNotFound(){
81
 void handleNotFound(){
135
     String message = "File Not Found\n\n";
82
     String message = "File Not Found\n\n";
136
     message += "URI: ";
83
     message += "URI: ";
147
 }
94
 }
148
 
95
 
149
 void setup(void) {
96
 void setup(void) {
150
-    EEPROM.begin(EEPROM_SIZE);
151
-
152
     // Debugging
97
     // Debugging
153
     Serial.begin(115200);
98
     Serial.begin(115200);
154
     Serial.println();
99
     Serial.println();
159
     // config does not match the pins i'm using (sda - 2; scl - 0)
104
     // config does not match the pins i'm using (sda - 2; scl - 0)
160
     Wire.begin(2, 0);
105
     Wire.begin(2, 0);
161
 
106
 
107
+    // Here you can override the WiFiManager configuration. Leave
108
+    // the default autoConnect in use for the default behaviour.
109
+    // To force the config portal, even though the module was connected before,
110
+    // comment-out autoConnect and use startConfigPortal instead.
162
     WiFiManager wifiManager;
111
     WiFiManager wifiManager;
163
-    // use one or the other, never both!
164
     wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
112
     wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
165
     //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
113
     //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
166
 
114
 
115
+    initMemory();
167
     storage = readMemory();
116
     storage = readMemory();
168
 
117
 
169
     // Wait for connection
118
     // Wait for connection
170
-    while (WiFi.status() != WL_CONNECTED) {
171
-        delay(500);
172
-        Serial.print(".");
119
+    if (WiFi.status() != WL_CONNECTED) {
120
+        while (WiFi.status() != WL_CONNECTED) {
121
+            delay(500);
122
+            Serial.print(".");
123
+        }
124
+        Serial.println("");
173
     }
125
     }
174
-    Serial.println("");
126
+
175
     Serial.print("IP address: ");
127
     Serial.print("IP address: ");
176
     Serial.println(WiFi.localIP());
128
     Serial.println(WiFi.localIP());
177
 
129
 
189
     lastNTP = millis();
141
     lastNTP = millis();
190
     sendNTPpacket(timeServerIP); 
142
     sendNTPpacket(timeServerIP); 
191
 
143
 
192
-    Udp.begin(localPort);
144
+    Udp.begin(BROADCAST_PORT);
193
     Serial.println("ESP-Weather ready!");
145
     Serial.println("ESP-Weather ready!");
194
 }
146
 }
195
 
147
 
229
     }
181
     }
230
 
182
 
231
     // NTP wiederholen falls keine Antwort
183
     // NTP wiederholen falls keine Antwort
232
-    if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
184
+    if ((timestamp == 0) && ((millis() - lastNTP) > NTP_RETRY_TIMEOUT)) {
233
         Serial.println("NTP packet retry...");
185
         Serial.println("NTP packet retry...");
234
         WiFi.hostByName(ntpServerName, timeServerIP);
186
         WiFi.hostByName(ntpServerName, timeServerIP);
235
         lastNTP = millis();
187
         lastNTP = millis();
273
     if (packetSize) {
225
     if (packetSize) {
274
         IPAddress remoteIp = Udp.remoteIP();
226
         IPAddress remoteIp = Udp.remoteIP();
275
         // read the packet into packetBufffer
227
         // read the packet into packetBufffer
276
-        int len = Udp.read(packetBuffer, 255);
228
+        int len = Udp.read(packetBuffer, UDP_PACKET_BUFFER_SIZE);
277
         if (len > 0) {
229
         if (len > 0) {
278
             packetBuffer[len] = 0;
230
             packetBuffer[len] = 0;
279
         }
231
         }
291
         }
243
         }
292
     }
244
     }
293
 
245
 
294
-    if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
246
+    if (((millis() - lastTime) >= MAX_BROADCAST_WAIT_TIME) && (waitingForReplies == true)) {
295
         Serial.println("Timeout, sending response...");
247
         Serial.println("Timeout, sending response...");
296
         waitingForReplies = false;
248
         waitingForReplies = false;
297
         String message = htmlBegin;
249
         String message = htmlBegin;

+ 46
- 0
storage.cpp Vedi File

1
+#include <Arduino.h>
2
+#include <EEPROM.h>
3
+#include "storage.h"
4
+
5
+void initMemory(void) {
6
+    EEPROM.begin(EEPROM_SIZE);
7
+}
8
+
9
+void writeMemory(PersistentStorage &s) {
10
+    Serial.println("write Memory");
11
+    unsigned char* r = (unsigned char*) &s;
12
+    uint16_t a = 0, b = 0;
13
+    for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
14
+        a = (a + r[i]) % 255;
15
+        b = (b + a) % 255;
16
+    }
17
+    s.header.checksum = (b << 8) | a;
18
+    for (int i = 0; i < sizeof(PersistentStorage); i++) {
19
+        EEPROM.write(i, r[i]);
20
+    }
21
+    EEPROM.commit();
22
+}
23
+
24
+PersistentStorage readMemory() {
25
+    PersistentStorage s;
26
+    unsigned char* r = (unsigned char*) &s;
27
+    for (int i = 0; i < sizeof(PersistentStorage); i++) {
28
+        r[i] = EEPROM.read(i);
29
+    }
30
+    uint16_t a = 0, b = 0;
31
+    for (int i = 0; i < sizeof(PersistentStorage) - 2; i++) {
32
+        a = (a + r[i]) % 255;
33
+        b = (b + a) % 255;
34
+    }
35
+    if (s.header.checksum != ((b << 8) | a)) {
36
+        Serial.print("Checksum error ");
37
+        Serial.print(s.header.checksum);
38
+        Serial.print(" ");
39
+        Serial.println((b << 8) | a);
40
+        s.header.count = 0;
41
+    } else {
42
+        Serial.println("Checksum ok");
43
+    }
44
+    return s;
45
+}
46
+

+ 28
- 0
storage.h Vedi File

1
+#ifndef __STORAGE_H__
2
+#define __STORAGE_H__
3
+
4
+#define EEPROM_SIZE 512
5
+
6
+struct __attribute__((__packed__)) Measurement {
7
+    float temperature;
8
+    float humidity;
9
+};
10
+
11
+struct __attribute__((__packed__)) Header {
12
+    uint16_t count;
13
+    uint16_t checksum;
14
+};
15
+
16
+#define MAX_STORAGE (EEPROM_SIZE - sizeof(Header)) / sizeof(Measurement)
17
+
18
+struct __attribute__((__packed__)) PersistentStorage {
19
+    Measurement data[MAX_STORAGE];
20
+    Header header;
21
+};
22
+
23
+void initMemory(void);
24
+void writeMemory(PersistentStorage &s);
25
+PersistentStorage readMemory();
26
+
27
+#endif // __STORAGE_H__
28
+

Loading…
Annulla
Salva