瀏覽代碼

Some formatting changes. Switched SDA and SCL.

Thomas Buck 8 年之前
父節點
當前提交
e0f84d6b11
共有 2 個文件被更改,包括 42 次插入26 次删除
  1. 31
    21
      ESP-Weather.ino
  2. 11
    5
      README.md

+ 31
- 21
ESP-Weather.ino 查看文件

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

+ 11
- 5
README.md 查看文件

@@ -1,16 +1,22 @@
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 5
 ## Dependencies
6 6
 
7 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 12
 * [Websocket Server](https://github.com/morrissinger/ESP8266-Websocket)
11 13
 * [WiFiManager](https://github.com/tzapu/WiFiManager)
12 14
 
13 15
 ### JavaScript
16
+
17
+The JavaScript client code includes the following libraries:
18
+
14 19
 * [Bootstrap](http://getbootstrap.com/)
15 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…
取消
儲存