Overflow error prevention and minor improvements for the esp8266

This commit is contained in:
2017-08-27 17:28:48 +02:00
parent a518a69d9a
commit eda866b36d

View File

@@ -1,10 +1,13 @@
#include "DHT.h" #include "DHT.h"
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include "aREST.h" #include "aREST.h"
#include <climits>
//DHT settings: //DHT settings:
#define DHTPIN 14 // what digital pin we're connected to #define DHTPIN 14 // what digital pin we're connected to
#define MEASURESECONDS 60 //shouldn't be < 2sec
// Uncomment whatever type you're using! // Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 #define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
@@ -27,8 +30,8 @@ DHT dht(DHTPIN, DHTTYPE);
aREST rest = aREST(); aREST rest = aREST();
// WiFi settings: // WiFi settings:
const char* ssid = "Signal im Regal"; const char* ssid = "Your_SSID";
const char* password = "42003489"; const char* password = "Your_Password";
#define LISTEN_PORT 80 #define LISTEN_PORT 80
@@ -42,8 +45,10 @@ float humidity;
//milli counter //milli counter
long millitotal = 0; unsigned long millitotal = 0;
//First measurement
bool firstmeasurement = true;
void setup() { void setup() {
@@ -57,7 +62,7 @@ void setup() {
rest.variable("temperature", &temperature); rest.variable("temperature", &temperature);
rest.variable("humidity", &humidity); 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_id("1");
rest.set_name("sensornode"); rest.set_name("sensornode");
@@ -82,6 +87,7 @@ void setup() {
void loop() { void loop() {
// Wait a few seconds between measurements. // Wait a few seconds between measurements.
float millicounter = millis(); float millicounter = millis();
if (millicounter >= millitotal) { if (millicounter >= millitotal) {
// Reading temperature or humidity takes about 250 milliseconds! // Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) // 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 //set the new values
humidity = h; humidity = h;
temperature = t; temperature = t;
} }
//set new milli counter //set new milli counter
//millis will overflow after approx. 52 days. To prevent errors we're checking the limits
if (millis() + MEASURESECONDS * 1000 <= ULONG_MAX) {
millitotal = millis() + 2000; millitotal = millis() + 2000;
} else {
millitotal = 0;
}
} }
// Handle REST calls // Handle REST calls
WiFiClient client = server.available(); WiFiClient client = server.available();
@@ -112,4 +122,7 @@ void loop() {
delay(1); delay(1);
} }
rest.handle(client); rest.handle(client);
//Let the esp chill a bit
delay(100);
} }