ソースを参照

Moved Persistent Storage stuff into own h/cpp files.

Thomas Buck 8年前
コミット
35a4c48c4a
3個のファイルの変更111行の追加85行の削除
  1. 37
    85
      ESP-Weather.ino
  2. 46
    0
      storage.cpp
  3. 28
    0
      storage.h

+ 37
- 85
ESP-Weather.ino ファイルの表示

@@ -6,13 +6,20 @@
6 6
 #include <vector>
7 7
 #include <WebSocketServer.h>
8 8
 #include <WiFiServer.h>
9
-#include <EEPROM.h>
10 9
 #include <DNSServer.h>
11 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 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 24
 // NTP-Client
18 25
 IPAddress timeServerIP; 
@@ -27,79 +34,21 @@ byte storeAtBoot = 1;
27 34
 unsigned long lastNTP = 0;
28 35
 
29 36
 SHT21 SHT21;
30
-ESP8266WebServer server(80);
31
-WiFiServer serverSocket(2391);
37
+ESP8266WebServer server(WEB_PORT);
38
+WiFiServer serverSocket(WEBSOCKET_PORT);
32 39
 WebSocketServer webSocketServer;
33 40
 
34
-#define DEFAULT_SSID "ESP-Weather"
35
-#define DEFAULT_PASS "testtest"
36
-
37 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 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 45
 std::vector<IPAddress> vecClients; 
97 46
 
98 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 52
 WiFiUDP Udp;
104 53
 
105 54
 unsigned long lastTime;
@@ -116,21 +65,19 @@ const char* htmlBegin = "<html><head>\
116 65
 <script type=\"text/javascript\">";
117 66
 const char* htmlEnd = "</script></body></html>";
118 67
 
119
-// root-URL
120 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 73
     Udp.write(pingBuffer);
124 74
     Udp.endPacket();
125 75
 
126
-    // Timer starten
76
+    // Start reply wait timer
127 77
     lastTime = millis();
128 78
     waitingForReplies = true;
129
-
130
-    Serial.println("Sending UDP Broadcast...");
131 79
 }
132 80
 
133
-// URL nicht vorhanden
134 81
 void handleNotFound(){
135 82
     String message = "File Not Found\n\n";
136 83
     message += "URI: ";
@@ -147,8 +94,6 @@ void handleNotFound(){
147 94
 }
148 95
 
149 96
 void setup(void) {
150
-    EEPROM.begin(EEPROM_SIZE);
151
-
152 97
     // Debugging
153 98
     Serial.begin(115200);
154 99
     Serial.println();
@@ -159,19 +104,26 @@ void setup(void) {
159 104
     // config does not match the pins i'm using (sda - 2; scl - 0)
160 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 111
     WiFiManager wifiManager;
163
-    // use one or the other, never both!
164 112
     wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
165 113
     //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
166 114
 
115
+    initMemory();
167 116
     storage = readMemory();
168 117
 
169 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 127
     Serial.print("IP address: ");
176 128
     Serial.println(WiFi.localIP());
177 129
 
@@ -189,7 +141,7 @@ void setup(void) {
189 141
     lastNTP = millis();
190 142
     sendNTPpacket(timeServerIP); 
191 143
 
192
-    Udp.begin(localPort);
144
+    Udp.begin(BROADCAST_PORT);
193 145
     Serial.println("ESP-Weather ready!");
194 146
 }
195 147
 
@@ -229,7 +181,7 @@ void loop(void){
229 181
     }
230 182
 
231 183
     // NTP wiederholen falls keine Antwort
232
-    if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
184
+    if ((timestamp == 0) && ((millis() - lastNTP) > NTP_RETRY_TIMEOUT)) {
233 185
         Serial.println("NTP packet retry...");
234 186
         WiFi.hostByName(ntpServerName, timeServerIP);
235 187
         lastNTP = millis();
@@ -273,7 +225,7 @@ void loop(void){
273 225
     if (packetSize) {
274 226
         IPAddress remoteIp = Udp.remoteIP();
275 227
         // read the packet into packetBufffer
276
-        int len = Udp.read(packetBuffer, 255);
228
+        int len = Udp.read(packetBuffer, UDP_PACKET_BUFFER_SIZE);
277 229
         if (len > 0) {
278 230
             packetBuffer[len] = 0;
279 231
         }
@@ -291,7 +243,7 @@ void loop(void){
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 247
         Serial.println("Timeout, sending response...");
296 248
         waitingForReplies = false;
297 249
         String message = htmlBegin;

+ 46
- 0
storage.cpp ファイルの表示

@@ -0,0 +1,46 @@
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 ファイルの表示

@@ -0,0 +1,28 @@
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
+

読み込み中…
キャンセル
保存