ソースを参照

Unified indentation and removed trailing whitespace

Thomas Buck 8年前
コミット
444a21f2c0
1個のファイルの変更238行の追加242行の削除
  1. 238
    242
      ESP-Weather.ino

+ 238
- 242
ESP-Weather.ino ファイルの表示

@@ -37,60 +37,60 @@ WebSocketServer webSocketServer;
37 37
 IPAddress broadcastIP;
38 38
 
39 39
 struct __attribute__((__packed__)) Measurement {
40
-  float temperature;
41
-  float humidity;
40
+    float temperature;
41
+    float humidity;
42 42
 };
43 43
 
44 44
 struct __attribute__((__packed__)) Header {
45
-  uint16_t count;
46
-  uint16_t checksum;
45
+    uint16_t count;
46
+    uint16_t checksum;
47 47
 };
48 48
 
49 49
 #define MAX_STORAGE (EEPROM_SIZE - sizeof(Header)) / sizeof(Measurement)
50 50
 
51 51
 struct __attribute__((__packed__)) PersistentStorage {
52
-  Measurement data[MAX_STORAGE];
53
-  Header header;
52
+    Measurement data[MAX_STORAGE];
53
+    Header header;
54 54
 };
55 55
 
56 56
 PersistentStorage storage;
57 57
 
58 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();
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 71
 }
72 72
 
73 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;
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 94
 }
95 95
 
96 96
 std::vector<IPAddress> vecClients; 
@@ -105,234 +105,230 @@ WiFiUDP Udp;
105 105
 unsigned long lastTime;
106 106
 bool waitingForReplies = false;
107 107
 
108
-const char* htmlBegin = "<html>\
109
-  <head>\
110
-    <title>Sysadmin</title>\
111
-    <script src=\"http://hoegerle-home.de/sysAdmin/js/jquery-3.1.1.min.js\"></script>\
112
-    <script src=\"http://hoegerle-home.de/sysAdmin/js/bootstrap.min.js\"></script>\
113
-    <script src=\"http://hoegerle-home.de/sysAdmin/js/Chart.bundle.min.js\"></script>\
114
-    <script src=\"http://hoegerle-home.de/sysAdmin/js/script.js\"></script>\
115
-    <link rel=\"stylesheet\" href=\"http://hoegerle-home.de/sysAdmin/css/bootstrap.min.css\" />\
116
-  </head>\
117
-  <body>\
118
-    <script type=\"text/javascript\">";
119
-const char* htmlEnd = "</script>\
120
-  </body>\
121
-</html>";
108
+const char* htmlBegin = "<html><head>\
109
+<title>Sysadmin</title>\
110
+<script src=\"http://hoegerle-home.de/sysAdmin/js/jquery-3.1.1.min.js\"></script>\
111
+<script src=\"http://hoegerle-home.de/sysAdmin/js/bootstrap.min.js\"></script>\
112
+<script src=\"http://hoegerle-home.de/sysAdmin/js/Chart.bundle.min.js\"></script>\
113
+<script src=\"http://hoegerle-home.de/sysAdmin/js/script.js\"></script>\
114
+<link rel=\"stylesheet\" href=\"http://hoegerle-home.de/sysAdmin/css/bootstrap.min.css\" />\
115
+</head><body>\
116
+<script type=\"text/javascript\">";
117
+const char* htmlEnd = "</script></body></html>";
122 118
 
123 119
 // root-URL
124 120
 void handleRoot() {
125
-  // send a reply, to the IP address and port that sent us the packet we received
126
-  Udp.beginPacket(broadcastIP, 2390);
127
-  Udp.write(pingBuffer);
128
-  Udp.endPacket();
129
-
130
-  // Timer starten
131
-  lastTime = millis();
132
-  waitingForReplies = true;
133
-  
134
-  Serial.println("Sending UDP Broadcast...");
121
+    // send a reply, to the IP address and port that sent us the packet we received
122
+    Udp.beginPacket(broadcastIP, 2390);
123
+    Udp.write(pingBuffer);
124
+    Udp.endPacket();
125
+
126
+    // Timer starten
127
+    lastTime = millis();
128
+    waitingForReplies = true;
129
+
130
+    Serial.println("Sending UDP Broadcast...");
135 131
 }
136 132
 
137 133
 // URL nicht vorhanden
138 134
 void handleNotFound(){
139
-  String message = "File Not Found\n\n";
140
-  message += "URI: ";
141
-  message += server.uri();
142
-  message += "\nMethod: ";
143
-  message += (server.method() == HTTP_GET)?"GET":"POST";
144
-  message += "\nArguments: ";
145
-  message += server.args();
146
-  message += "\n";
147
-  for (uint8_t i=0; i<server.args(); i++){
148
-    message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
149
-  }
150
-  server.send(404, "text/plain", message);
135
+    String message = "File Not Found\n\n";
136
+    message += "URI: ";
137
+    message += server.uri();
138
+    message += "\nMethod: ";
139
+    message += (server.method() == HTTP_GET)?"GET":"POST";
140
+    message += "\nArguments: ";
141
+    message += server.args();
142
+    message += "\n";
143
+    for (uint8_t i = 0; i < server.args(); i++){
144
+        message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
145
+    }
146
+    server.send(404, "text/plain", message);
151 147
 }
152 148
 
153 149
 void setup(void) {
154
-  EEPROM.begin(EEPROM_SIZE);
155
-   
156
-  // Debugging
157
-  Serial.begin(115200);
158
-  Serial.println();
159
-  Serial.println("ESP-Weather init...");
160
-
161
-  //SHT21.begin();
162
-  // The SHT library is simpy calling Wire.begin(), but the default
163
-  // config does not match the pins i'm using (sda - 2; scl - 0)
164
-  Wire.begin(2, 0);
165
-  
166
-  WiFiManager wifiManager;
167
-  // use one or the other, never both!
168
-  wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
169
-  //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
170
-
171
-  storage = readMemory();
172
-
173
-  // Wait for connection
174
-  while (WiFi.status() != WL_CONNECTED) {
175
-    delay(500);
176
-    Serial.print(".");
177
-  }
178
-  Serial.println("");
179
-  Serial.print("IP address: ");
180
-  Serial.println(WiFi.localIP());
181
-  
182
-  broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
183
-  
184
-  server.on("/", handleRoot);
185
-  server.onNotFound(handleNotFound);
186
-  server.begin();
187
-
188
-  serverSocket.begin();
189
-
190
-  // NTP-Client
191
-  ntp.begin(NTP_PORT);
192
-  WiFi.hostByName(ntpServerName, timeServerIP); 
193
-  lastNTP = millis();
194
-  sendNTPpacket(timeServerIP); 
195
-  
196
-  Udp.begin(localPort);
197
-  Serial.println("ESP-Weather ready!");
198
-}
150
+    EEPROM.begin(EEPROM_SIZE);
199 151
 
200
-void loop(void){
201
-  server.handleClient();
202
-
203
-  // Websocket fuer Browser
204
-  WiFiClient client = serverSocket.available();
205
-  if (client.connected() && webSocketServer.handshake(client)) {
206
-    Serial.println("Building WebSocket Response...");
207
-    
208
-    String json = "{\"H\":";
209
-    json += String(SHT21.getHumidity());
210
-    json += ",";
211
-    json += "\"T\":";
212
-    json += String(SHT21.getTemperature());
213
-    json += ", \"EEPROM\" : [";
214
-    for (int i = 0; i < storage.header.count; i++) {
215
-      json += "{\"H\":";
216
-      json += String(storage.data[i].humidity);
217
-      json += ",";
218
-      json += "\"T\":";
219
-      json += String(storage.data[i].temperature);
220
-      json += "}";
221
-      if (i < storage.header.count - 1) {
222
-        json += ",";
223
-      }
152
+    // Debugging
153
+    Serial.begin(115200);
154
+    Serial.println();
155
+    Serial.println("ESP-Weather init...");
156
+
157
+    //SHT21.begin();
158
+    // The SHT library is simpy calling Wire.begin(), but the default
159
+    // config does not match the pins i'm using (sda - 2; scl - 0)
160
+    Wire.begin(2, 0);
161
+
162
+    WiFiManager wifiManager;
163
+    // use one or the other, never both!
164
+    wifiManager.autoConnect(DEFAULT_SSID, DEFAULT_PASS);
165
+    //wifiManager.startConfigPortal(DEFAULT_SSID, DEFAULT_PASS);
166
+
167
+    storage = readMemory();
168
+
169
+    // Wait for connection
170
+    while (WiFi.status() != WL_CONNECTED) {
171
+        delay(500);
172
+        Serial.print(".");
224 173
     }
225
-    json += "]}";
226
-
227
-    Serial.println("WebSocket Response:");
228
-    Serial.println(json);
229
-    
230
-    webSocketServer.sendData(json);
231
-    client.flush();
232
-    client.stop();
233
-  }
234
-
235
-  // NTP wiederholen falls keine Antwort
236
-  if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
237
-    Serial.println("NTP packet retry...");
174
+    Serial.println("");
175
+    Serial.print("IP address: ");
176
+    Serial.println(WiFi.localIP());
177
+
178
+    broadcastIP = ~WiFi.subnetMask() | WiFi.gatewayIP();
179
+
180
+    server.on("/", handleRoot);
181
+    server.onNotFound(handleNotFound);
182
+    server.begin();
183
+
184
+    serverSocket.begin();
185
+
186
+    // NTP-Client
187
+    ntp.begin(NTP_PORT);
238 188
     WiFi.hostByName(ntpServerName, timeServerIP); 
239 189
     lastNTP = millis();
240 190
     sendNTPpacket(timeServerIP); 
241
-  }
242
-  
243
-  // NTP Paket vom Server erhalten
244
-  if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
245
-    ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);   
246
-    unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
247
-    unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
248
-    unsigned long secsSince1900 = highWord << 16 | lowWord;
249
-    const unsigned long seventyYears = 2208988800UL;
250
-    unsigned long epoch = secsSince1900 - seventyYears;
251
-    timestamp = epoch;
252
-    timeReceived = millis();
253
-    Serial.print("Got NTP time: ");
254
-    Serial.println(epoch);
255
-  }
256
-
257
-  // EEPROM-Schreiben jede Stunde
258
-  if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
259
-          && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
260
-      Serial.println("Storing new data packet...");
261
-      lastStorageTime = millis();
262
-      storeAtBoot = 0;
263
-      if (storage.header.count < MAX_STORAGE) {
264
-        storage.header.count++;
265
-      } else {
266
-        for(int i = 0; i < MAX_STORAGE - 1; i++) {
267
-          storage.data[i] = storage.data[i+1];
191
+
192
+    Udp.begin(localPort);
193
+    Serial.println("ESP-Weather ready!");
194
+}
195
+
196
+void loop(void){
197
+    server.handleClient();
198
+
199
+    // Websocket fuer Browser
200
+    WiFiClient client = serverSocket.available();
201
+    if (client.connected() && webSocketServer.handshake(client)) {
202
+        Serial.println("Building WebSocket Response...");
203
+
204
+        String json = "{\"H\":";
205
+        json += String(SHT21.getHumidity());
206
+        json += ",";
207
+        json += "\"T\":";
208
+        json += String(SHT21.getTemperature());
209
+        json += ", \"EEPROM\" : [";
210
+        for (int i = 0; i < storage.header.count; i++) {
211
+            json += "{\"H\":";
212
+            json += String(storage.data[i].humidity);
213
+            json += ",";
214
+            json += "\"T\":";
215
+            json += String(storage.data[i].temperature);
216
+            json += "}";
217
+            if (i < storage.header.count - 1) {
218
+                json += ",";
219
+            }
268 220
         }
269
-      }
270
-      storage.data[storage.header.count-1].temperature = SHT21.getTemperature();
271
-      storage.data[storage.header.count-1].humidity = SHT21.getHumidity(); 
272
-      writeMemory(storage); 
273
-  }
274
-  
275
-  // UDP
276
-  int packetSize = Udp.parsePacket();
277
-  if (packetSize) {
278
-    IPAddress remoteIp = Udp.remoteIP();
279
-    // read the packet into packetBufffer
280
-    int len = Udp.read(packetBuffer, 255);
281
-    if (len > 0) {
282
-      packetBuffer[len] = 0;
221
+        json += "]}";
222
+
223
+        Serial.println("WebSocket Response:");
224
+        Serial.println(json);
225
+
226
+        webSocketServer.sendData(json);
227
+        client.flush();
228
+        client.stop();
283 229
     }
284 230
 
285
-    Serial.print("Got UDP packet: ");
286
-    Serial.println(packetBuffer);
287
-       
288
-    if (strcmp(packetBuffer, pingBuffer) == 0) {
289
-      Serial.println("Broadcast");
290
-      Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
291
-      Udp.print(echoBuffer);
292
-      Udp.endPacket();
293
-    } else if((strcmp(packetBuffer, echoBuffer) == 0) && (waitingForReplies == true)) {
294
-      vecClients.push_back(Udp.remoteIP());
231
+    // NTP wiederholen falls keine Antwort
232
+    if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
233
+        Serial.println("NTP packet retry...");
234
+        WiFi.hostByName(ntpServerName, timeServerIP);
235
+        lastNTP = millis();
236
+        sendNTPpacket(timeServerIP);
295 237
     }
296
-  }
297
-  
298
-  if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
299
-    Serial.println("Timeout, sending response...");
300
-    waitingForReplies = false;
301
-    String message = htmlBegin;
302
-    message += "var clients = Array(";
303
-    message += "\"" + WiFi.localIP().toString() + "\"";
304
-    if(vecClients.size() > 0) {
305
-      message += ", ";
238
+
239
+    // NTP Paket vom Server erhalten
240
+    if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
241
+        ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);
242
+        unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
243
+        unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
244
+        unsigned long secsSince1900 = highWord << 16 | lowWord;
245
+        const unsigned long seventyYears = 2208988800UL;
246
+        unsigned long epoch = secsSince1900 - seventyYears;
247
+        timestamp = epoch;
248
+        timeReceived = millis();
249
+        Serial.print("Got NTP time: ");
250
+        Serial.println(epoch);
306 251
     }
307
-    for(int i = 0; i < vecClients.size(); i++) {
308
-      message += "\"" + vecClients[i].toString() + "\"";
309
-      if(i < (vecClients.size() - 1)) {
310
-        message += ", ";
311
-      }
252
+
253
+    // EEPROM-Schreiben jede Stunde
254
+    if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
255
+            && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
256
+        Serial.println("Storing new data packet...");
257
+        lastStorageTime = millis();
258
+        storeAtBoot = 0;
259
+        if (storage.header.count < MAX_STORAGE) {
260
+            storage.header.count++;
261
+        } else {
262
+            for(int i = 0; i < MAX_STORAGE - 1; i++) {
263
+                storage.data[i] = storage.data[i+1];
264
+            }
265
+        }
266
+        storage.data[storage.header.count - 1].temperature = SHT21.getTemperature();
267
+        storage.data[storage.header.count - 1].humidity = SHT21.getHumidity();
268
+        writeMemory(storage);
312 269
     }
313
-    message += ");";
314
-    message += htmlEnd;
315 270
 
316
-    vecClients.clear();
317
-    
318
-    server.send(200, "text/html", message);
319
-  }
271
+    // UDP
272
+    int packetSize = Udp.parsePacket();
273
+    if (packetSize) {
274
+        IPAddress remoteIp = Udp.remoteIP();
275
+        // read the packet into packetBufffer
276
+        int len = Udp.read(packetBuffer, 255);
277
+        if (len > 0) {
278
+            packetBuffer[len] = 0;
279
+        }
280
+
281
+        Serial.print("Got UDP packet: ");
282
+        Serial.println(packetBuffer);
283
+
284
+        if (strcmp(packetBuffer, pingBuffer) == 0) {
285
+            Serial.println("Broadcast");
286
+            Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
287
+            Udp.print(echoBuffer);
288
+            Udp.endPacket();
289
+        } else if((strcmp(packetBuffer, echoBuffer) == 0) && (waitingForReplies == true)) {
290
+            vecClients.push_back(Udp.remoteIP());
291
+        }
292
+    }
293
+
294
+    if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
295
+        Serial.println("Timeout, sending response...");
296
+        waitingForReplies = false;
297
+        String message = htmlBegin;
298
+        message += "var clients = Array(";
299
+        message += "\"" + WiFi.localIP().toString() + "\"";
300
+        if (vecClients.size() > 0) {
301
+            message += ", ";
302
+        }
303
+        for (int i = 0; i < vecClients.size(); i++) {
304
+            message += "\"" + vecClients[i].toString() + "\"";
305
+            if (i < (vecClients.size() - 1)) {
306
+                message += ", ";
307
+            }
308
+        }
309
+        message += ");";
310
+        message += htmlEnd;
311
+
312
+        vecClients.clear();
313
+
314
+        server.send(200, "text/html", message);
315
+    }
320 316
 }
321 317
 
322 318
 void sendNTPpacket(IPAddress& address) {
323
-  Serial.println("Sending NTP packet...");
324
-  memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
325
-  ntpPacketBuffer[0] = 0b11100011;   // LI, Version, Mode
326
-  ntpPacketBuffer[1] = 0;    
327
-  ntpPacketBuffer[2] = 6;     
328
-  ntpPacketBuffer[3] = 0xEC; 
329
-  ntpPacketBuffer[12]  = 49;
330
-  ntpPacketBuffer[13]  = 0x4E;
331
-  ntpPacketBuffer[14]  = 49;
332
-  ntpPacketBuffer[15]  = 52;
333
-
334
-  ntp.beginPacket(address, 123);
335
-  ntp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
336
-  ntp.endPacket();
319
+    Serial.println("Sending NTP packet...");
320
+    memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
321
+    ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
322
+    ntpPacketBuffer[1] = 0;
323
+    ntpPacketBuffer[2] = 6;
324
+    ntpPacketBuffer[3] = 0xEC;
325
+    ntpPacketBuffer[12]  = 49;
326
+    ntpPacketBuffer[13]  = 0x4E;
327
+    ntpPacketBuffer[14]  = 49;
328
+    ntpPacketBuffer[15]  = 52;
329
+
330
+    ntp.beginPacket(address, 123);
331
+    ntp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
332
+    ntp.endPacket();
337 333
 }
338 334
 

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