Browse Source

Some formatting changes. Switched SDA and SCL.

Thomas Buck 8 years ago
parent
commit
e0f84d6b11
2 changed files with 42 additions and 26 deletions
  1. 31
    21
      ESP-Weather.ino
  2. 11
    5
      README.md

+ 31
- 21
ESP-Weather.ino View File

42
 };
42
 };
43
 
43
 
44
 struct __attribute__((__packed__)) Header {
44
 struct __attribute__((__packed__)) Header {
45
-  uint8_t count;
45
+  uint16_t count;
46
   uint16_t checksum;
46
   uint16_t checksum;
47
 };
47
 };
48
 
48
 
131
   lastTime = millis();
131
   lastTime = millis();
132
   waitingForReplies = true;
132
   waitingForReplies = true;
133
   
133
   
134
-  Serial.println("broadcast send");
134
+  Serial.println("Sending UDP Broadcast...");
135
 }
135
 }
136
 
136
 
137
 // URL nicht vorhanden
137
 // URL nicht vorhanden
151
 }
151
 }
152
 
152
 
153
 void setup(void) {
153
 void setup(void) {
154
-  EEPROM.begin(512);
154
+  EEPROM.begin(EEPROM_SIZE);
155
    
155
    
156
   // Debugging
156
   // Debugging
157
   Serial.begin(115200);
157
   Serial.begin(115200);
158
-  Serial.println("");
158
+  Serial.println();
159
+  Serial.println("ESP-Weather init...");
159
 
160
 
160
-  SHT21.begin();
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);
161
   
165
   
162
   WiFiManager wifiManager;
166
   WiFiManager wifiManager;
163
   // use one or the other, never both!
167
   // use one or the other, never both!
190
   sendNTPpacket(timeServerIP); 
194
   sendNTPpacket(timeServerIP); 
191
   
195
   
192
   Udp.begin(localPort);
196
   Udp.begin(localPort);
193
-  Serial.println("HTTP server and UDP started");
194
-  
197
+  Serial.println("ESP-Weather ready!");
195
 }
198
 }
196
 
199
 
197
 void loop(void){
200
 void loop(void){
199
 
202
 
200
   // Websocket fuer Browser
203
   // Websocket fuer Browser
201
   WiFiClient client = serverSocket.available();
204
   WiFiClient client = serverSocket.available();
202
-  if(client.connected() && webSocketServer.handshake(client)) {
205
+  if (client.connected() && webSocketServer.handshake(client)) {
206
+    Serial.println("Building WebSocket Response...");
203
     
207
     
204
     String json = "{\"H\":";
208
     String json = "{\"H\":";
205
     json += String(SHT21.getHumidity());
209
     json += String(SHT21.getHumidity());
207
     json += "\"T\":";
211
     json += "\"T\":";
208
     json += String(SHT21.getTemperature());
212
     json += String(SHT21.getTemperature());
209
     json += ", \"EEPROM\" : [";
213
     json += ", \"EEPROM\" : [";
210
-    for(int i = 0; i < storage.header.count; i++) {
214
+    for (int i = 0; i < storage.header.count; i++) {
211
       json += "{\"H\":";
215
       json += "{\"H\":";
212
       json += String(storage.data[i].humidity);
216
       json += String(storage.data[i].humidity);
213
       json += ",";
217
       json += ",";
214
       json += "\"T\":";
218
       json += "\"T\":";
215
       json += String(storage.data[i].temperature);
219
       json += String(storage.data[i].temperature);
216
       json += "}";
220
       json += "}";
217
-      if(i < storage.header.count - 1) {
221
+      if (i < storage.header.count - 1) {
218
         json += ",";
222
         json += ",";
219
       }
223
       }
220
     }
224
     }
221
     json += "]}";
225
     json += "]}";
226
+
227
+    Serial.println("WebSocket Response:");
222
     Serial.println(json);
228
     Serial.println(json);
223
     
229
     
224
     webSocketServer.sendData(json);
230
     webSocketServer.sendData(json);
227
   }
233
   }
228
 
234
 
229
   // NTP wiederholen falls keine Antwort
235
   // NTP wiederholen falls keine Antwort
230
-  if((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
236
+  if ((timestamp == 0) && ((millis() - lastNTP) > 2000)) {
237
+    Serial.println("NTP packet retry...");
231
     WiFi.hostByName(ntpServerName, timeServerIP); 
238
     WiFi.hostByName(ntpServerName, timeServerIP); 
232
     lastNTP = millis();
239
     lastNTP = millis();
233
     sendNTPpacket(timeServerIP); 
240
     sendNTPpacket(timeServerIP); 
234
   }
241
   }
235
   
242
   
236
   // NTP Paket vom Server erhalten
243
   // NTP Paket vom Server erhalten
237
-  if(ntp.parsePacket() >= NTP_PACKET_SIZE) {
244
+  if (ntp.parsePacket() >= NTP_PACKET_SIZE) {
238
     ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);   
245
     ntp.read(ntpPacketBuffer, NTP_PACKET_SIZE);   
239
     unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
246
     unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
240
     unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
247
     unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
243
     unsigned long epoch = secsSince1900 - seventyYears;
250
     unsigned long epoch = secsSince1900 - seventyYears;
244
     timestamp = epoch;
251
     timestamp = epoch;
245
     timeReceived = millis();
252
     timeReceived = millis();
253
+    Serial.print("Got NTP time: ");
246
     Serial.println(epoch);
254
     Serial.println(epoch);
247
   }
255
   }
248
 
256
 
249
   // EEPROM-Schreiben jede Stunde
257
   // EEPROM-Schreiben jede Stunde
250
-  if((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0) && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
258
+  if ((((((millis() - timeReceived) / 1000) + timestamp) % 3600) == 0)
259
+          && (timestamp != 0) && (((millis() - lastStorageTime) > 100000) || storeAtBoot) ) {
260
+      Serial.println("Storing new data packet...");
251
       lastStorageTime = millis();
261
       lastStorageTime = millis();
252
       storeAtBoot = 0;
262
       storeAtBoot = 0;
253
-      if(storage.header.count < MAX_STORAGE) {
263
+      if (storage.header.count < MAX_STORAGE) {
254
         storage.header.count++;
264
         storage.header.count++;
255
       } else {
265
       } else {
256
         for(int i = 0; i < MAX_STORAGE - 1; i++) {
266
         for(int i = 0; i < MAX_STORAGE - 1; i++) {
264
   
274
   
265
   // UDP
275
   // UDP
266
   int packetSize = Udp.parsePacket();
276
   int packetSize = Udp.parsePacket();
267
-  if(packetSize) {
277
+  if (packetSize) {
268
     IPAddress remoteIp = Udp.remoteIP();
278
     IPAddress remoteIp = Udp.remoteIP();
269
     // read the packet into packetBufffer
279
     // read the packet into packetBufffer
270
     int len = Udp.read(packetBuffer, 255);
280
     int len = Udp.read(packetBuffer, 255);
271
     if (len > 0) {
281
     if (len > 0) {
272
       packetBuffer[len] = 0;
282
       packetBuffer[len] = 0;
273
     }
283
     }
274
-    
284
+
285
+    Serial.print("Got UDP packet: ");
275
     Serial.println(packetBuffer);
286
     Serial.println(packetBuffer);
276
        
287
        
277
-    if(strcmp(packetBuffer, pingBuffer) == 0) {
288
+    if (strcmp(packetBuffer, pingBuffer) == 0) {
278
       Serial.println("Broadcast");
289
       Serial.println("Broadcast");
279
       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
290
       Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
280
       Udp.print(echoBuffer);
291
       Udp.print(echoBuffer);
284
     }
295
     }
285
   }
296
   }
286
   
297
   
287
-  if(((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
288
-    Serial.println("waiting done");
298
+  if (((millis() - lastTime) >= MAX_WAIT_TIME) && (waitingForReplies == true)) {
299
+    Serial.println("Timeout, sending response...");
289
     waitingForReplies = false;
300
     waitingForReplies = false;
290
     String message = htmlBegin;
301
     String message = htmlBegin;
291
     message += "var clients = Array(";
302
     message += "var clients = Array(";
309
 }
320
 }
310
 
321
 
311
 void sendNTPpacket(IPAddress& address) {
322
 void sendNTPpacket(IPAddress& address) {
312
-  Serial.println("sending NTP packet...");
323
+  Serial.println("Sending NTP packet...");
313
   memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
324
   memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
314
   ntpPacketBuffer[0] = 0b11100011;   // LI, Version, Mode
325
   ntpPacketBuffer[0] = 0b11100011;   // LI, Version, Mode
315
   ntpPacketBuffer[1] = 0;    
326
   ntpPacketBuffer[1] = 0;    
325
   ntp.endPacket();
336
   ntp.endPacket();
326
 }
337
 }
327
 
338
 
328
-

+ 11
- 5
README.md View File

1
-# Systemadministration
1
+# ESP-Weather
2
 
2
 
3
-Projekt zur Systemadministration-Vorlesung im Wintersemester 2016/17 an der Hochschule Ravensburg Weingarten.
3
+This started out as a project for the "Systemadministration" course in the Hochschule Ravensburg-Weingarten in the winter of 2016 / 2017.
4
 
4
 
5
 ## Dependencies
5
 ## Dependencies
6
 
6
 
7
 ### ESP8266
7
 ### ESP8266
8
-* [Arduino core for ESP8266 WiFi chip](https://github.com/esp8266/Arduino)
9
-* [temperature and humidity sensor SHT21](https://github.com/markbeee/SHT21)
8
+
9
+Download and install the following libraries to your `Arduino/libraries` folder. Of course, you also need the [Arduino core for the ESP8266 WiFi chip](https://github.com/esp8266/Arduino) installed from the IDE Board Manager.
10
+
11
+* [Temperature and Humidity Sensor SHT21](https://github.com/markbeee/SHT21)
10
 * [Websocket Server](https://github.com/morrissinger/ESP8266-Websocket)
12
 * [Websocket Server](https://github.com/morrissinger/ESP8266-Websocket)
11
 * [WiFiManager](https://github.com/tzapu/WiFiManager)
13
 * [WiFiManager](https://github.com/tzapu/WiFiManager)
12
 
14
 
13
 ### JavaScript
15
 ### JavaScript
16
+
17
+The JavaScript client code includes the following libraries:
18
+
14
 * [Bootstrap](http://getbootstrap.com/)
19
 * [Bootstrap](http://getbootstrap.com/)
15
 * [jQuery](https://jquery.com/)
20
 * [jQuery](https://jquery.com/)
16
-* [Chart.js](https://github.com/chartjs/Chart.js)
21
+* [Chart.js](https://github.com/chartjs/Chart.js)
22
+

Loading…
Cancel
Save