Skip to content

Commit

Permalink
small fix ESP8266 Arduino DHT22 example
Browse files Browse the repository at this point in the history
small fix ESP8266 Arduino DHT22 example
  • Loading branch information
iot-playground committed May 4, 2015
1 parent 8c9fa8f commit f0fe3b5
Showing 1 changed file with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
V1.0 - first version
V2.0 - small fix
Created by Igor Jarc <igor.jarc1@gmail.com>
Created by Igor Jarc <admin@iot-playground.com>
See http://iot-playground.com for details
This program is free software; you can redistribute it and/or
Expand All @@ -17,6 +17,9 @@
#define HUMIDITY_SENSOR_DIGITAL_PIN 2


#define SEND_INTERVAL 1000 * 30 // 30S


Esp8266EasyIoT esp;

SoftwareSerial serialEsp(10, 11);
Expand All @@ -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()
{
Expand All @@ -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);
Expand All @@ -49,40 +52,63 @@ void setup()

// Serial.println("present S_TEMP");
esp.present(CHILD_ID_TEMP, S_TEMP);


startTime = millis()+SEND_INTERVAL;

}

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;
}


Expand Down

0 comments on commit f0fe3b5

Please sign in to comment.