From eda866b36dbaf3dcb69a7f61ecf2ed000fbee127 Mon Sep 17 00:00:00 2001 From: structix Date: Sun, 27 Aug 2017 17:28:48 +0200 Subject: [PATCH] Overflow error prevention and minor improvements for the esp8266 --- ESP8266/sensornode/sensornode.ino | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/ESP8266/sensornode/sensornode.ino b/ESP8266/sensornode/sensornode.ino index 1eccfee..67269e7 100644 --- a/ESP8266/sensornode/sensornode.ino +++ b/ESP8266/sensornode/sensornode.ino @@ -1,10 +1,13 @@ #include "DHT.h" #include #include "aREST.h" +#include //DHT settings: #define DHTPIN 14 // what digital pin we're connected to +#define MEASURESECONDS 60 //shouldn't be < 2sec + // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 @@ -27,8 +30,8 @@ DHT dht(DHTPIN, DHTTYPE); aREST rest = aREST(); // WiFi settings: -const char* ssid = "Signal im Regal"; -const char* password = "42003489"; +const char* ssid = "Your_SSID"; +const char* password = "Your_Password"; #define LISTEN_PORT 80 @@ -42,8 +45,10 @@ float humidity; //milli counter -long millitotal = 0; +unsigned long millitotal = 0; +//First measurement +bool firstmeasurement = true; void setup() { @@ -57,7 +62,7 @@ void setup() { rest.variable("temperature", &temperature); rest.variable("humidity", &humidity); - // Give name & ID to the device (ID should be 6 characters long) + // Set a ID (ID must be greater than 0) rest.set_id("1"); rest.set_name("sensornode"); @@ -82,6 +87,7 @@ void setup() { void loop() { // Wait a few seconds between measurements. float millicounter = millis(); + if (millicounter >= millitotal) { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) @@ -97,11 +103,15 @@ void loop() { //set the new values humidity = h; temperature = t; - } //set new milli counter - millitotal = millis() + 2000; + //millis will overflow after approx. 52 days. To prevent errors we're checking the limits + if (millis() + MEASURESECONDS * 1000 <= ULONG_MAX) { + millitotal = millis() + 2000; + } else { + millitotal = 0; + } } // Handle REST calls WiFiClient client = server.available(); @@ -112,4 +122,7 @@ void loop() { delay(1); } rest.handle(client); + + //Let the esp chill a bit + delay(100); }