diff --git a/Esp8266EasyIoT/examples/esp8266easyiot_temperature_humidity/esp8266easyiot_temperature_humidity.ino b/Esp8266EasyIoT/examples/esp8266easyiot_temperature_humidity/esp8266easyiot_temperature_humidity.ino index c31efb7..8958d3a 100644 --- a/Esp8266EasyIoT/examples/esp8266easyiot_temperature_humidity/esp8266easyiot_temperature_humidity.ino +++ b/Esp8266EasyIoT/examples/esp8266easyiot_temperature_humidity/esp8266easyiot_temperature_humidity.ino @@ -1,7 +1,7 @@ /* - V1.0 - first version + V2.0 - small fix - Created by Igor Jarc + Created by Igor Jarc See http://iot-playground.com for details This program is free software; you can redistribute it and/or @@ -17,6 +17,9 @@ #define HUMIDITY_SENSOR_DIGITAL_PIN 2 +#define SEND_INTERVAL 1000 * 30 // 30S + + Esp8266EasyIoT esp; SoftwareSerial serialEsp(10, 11); @@ -29,6 +32,7 @@ float lastHum; Esp8266EasyIoTMsg msgHum(CHILD_ID_HUM, V_HUM); Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP); +unsigned long startTime; void setup() { @@ -37,7 +41,6 @@ void setup() Serial.println("EasyIoTEsp init"); - esp.begin(NULL, 3, &serialEsp, &Serial); //esp.begin(NULL, &serialEsp); dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); @@ -49,6 +52,9 @@ void setup() // Serial.println("present S_TEMP"); esp.present(CHILD_ID_TEMP, S_TEMP); + + + startTime = millis()+SEND_INTERVAL; } @@ -56,33 +62,53 @@ void loop() { while(!esp.process()); - delay(dht.getMinimumSamplingPeriod()); - - while(!esp.process()); - - float temperature = dht.getTemperature(); - if (isnan(temperature)) { - Serial.println("Failed reading temperature from DHT"); - } - else if (temperature != lastTemp) + if (IsTimeout()) { - lastTemp = temperature; - esp.send(msgTemp.set(temperature, 1)); - Serial.print("T: "); - Serial.println(temperature); + startTime = millis(); + + float temperature = dht.getTemperature(); + if (isnan(temperature)) { + Serial.println("Failed reading temperature from DHT"); + } + else if (temperature != lastTemp) + { + lastTemp = temperature; + esp.send(msgTemp.set(temperature, 1)); + Serial.print("T: "); + Serial.println(temperature); + } + + while(!esp.process()); + + float humidity = dht.getHumidity(); + if (isnan(humidity)) { + Serial.println("Failed reading humidity from DHT"); + } + else if (humidity != lastHum) + { + lastHum = humidity; + esp.send(msgHum.set(humidity, 1)); + Serial.print("H: "); + Serial.println(humidity); + } } +} - float humidity = dht.getHumidity(); - if (isnan(humidity)) { - Serial.println("Failed reading humidity from DHT"); - } - else if (humidity != lastHum) +boolean IsTimeout() +{ + unsigned long now = millis(); + if (startTime <= now) { - lastHum = humidity; - esp.send(msgHum.set(humidity, 1)); - Serial.print("H: "); - Serial.println(humidity); + if ( (unsigned long)(now - startTime ) < SEND_INTERVAL ) + return false; } + else + { + if ( (unsigned long)(startTime - now) < SEND_INTERVAL ) + return false; + } + + return true; }