Skip to content

Commit f0fe3b5

Browse files
small fix ESP8266 Arduino DHT22 example
small fix ESP8266 Arduino DHT22 example
1 parent 8c9fa8f commit f0fe3b5

File tree

1 file changed

+51
-25
lines changed

1 file changed

+51
-25
lines changed

Esp8266EasyIoT/examples/esp8266easyiot_temperature_humidity/esp8266easyiot_temperature_humidity.ino

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
V1.0 - first version
2+
V2.0 - small fix
33
4-
Created by Igor Jarc <igor.jarc1@gmail.com>
4+
Created by Igor Jarc <admin@iot-playground.com>
55
See http://iot-playground.com for details
66
77
This program is free software; you can redistribute it and/or
@@ -17,6 +17,9 @@
1717
#define HUMIDITY_SENSOR_DIGITAL_PIN 2
1818

1919

20+
#define SEND_INTERVAL 1000 * 30 // 30S
21+
22+
2023
Esp8266EasyIoT esp;
2124

2225
SoftwareSerial serialEsp(10, 11);
@@ -29,6 +32,7 @@ float lastHum;
2932
Esp8266EasyIoTMsg msgHum(CHILD_ID_HUM, V_HUM);
3033
Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP);
3134

35+
unsigned long startTime;
3236

3337
void setup()
3438
{
@@ -37,7 +41,6 @@ void setup()
3741

3842
Serial.println("EasyIoTEsp init");
3943

40-
4144
esp.begin(NULL, 3, &serialEsp, &Serial);
4245
//esp.begin(NULL, &serialEsp);
4346
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);
@@ -49,40 +52,63 @@ void setup()
4952

5053
// Serial.println("present S_TEMP");
5154
esp.present(CHILD_ID_TEMP, S_TEMP);
55+
56+
57+
startTime = millis()+SEND_INTERVAL;
5258

5359
}
5460

5561
void loop()
5662
{
5763
while(!esp.process());
5864

59-
delay(dht.getMinimumSamplingPeriod());
60-
61-
while(!esp.process());
62-
63-
float temperature = dht.getTemperature();
64-
if (isnan(temperature)) {
65-
Serial.println("Failed reading temperature from DHT");
66-
}
67-
else if (temperature != lastTemp)
65+
if (IsTimeout())
6866
{
69-
lastTemp = temperature;
70-
esp.send(msgTemp.set(temperature, 1));
71-
Serial.print("T: ");
72-
Serial.println(temperature);
67+
startTime = millis();
68+
69+
float temperature = dht.getTemperature();
70+
if (isnan(temperature)) {
71+
Serial.println("Failed reading temperature from DHT");
72+
}
73+
else if (temperature != lastTemp)
74+
{
75+
lastTemp = temperature;
76+
esp.send(msgTemp.set(temperature, 1));
77+
Serial.print("T: ");
78+
Serial.println(temperature);
79+
}
80+
81+
while(!esp.process());
82+
83+
float humidity = dht.getHumidity();
84+
if (isnan(humidity)) {
85+
Serial.println("Failed reading humidity from DHT");
86+
}
87+
else if (humidity != lastHum)
88+
{
89+
lastHum = humidity;
90+
esp.send(msgHum.set(humidity, 1));
91+
Serial.print("H: ");
92+
Serial.println(humidity);
93+
}
7394
}
95+
}
7496

75-
float humidity = dht.getHumidity();
76-
if (isnan(humidity)) {
77-
Serial.println("Failed reading humidity from DHT");
78-
}
79-
else if (humidity != lastHum)
97+
boolean IsTimeout()
98+
{
99+
unsigned long now = millis();
100+
if (startTime <= now)
80101
{
81-
lastHum = humidity;
82-
esp.send(msgHum.set(humidity, 1));
83-
Serial.print("H: ");
84-
Serial.println(humidity);
102+
if ( (unsigned long)(now - startTime ) < SEND_INTERVAL )
103+
return false;
85104
}
105+
else
106+
{
107+
if ( (unsigned long)(startTime - now) < SEND_INTERVAL )
108+
return false;
109+
}
110+
111+
return true;
86112
}
87113

88114

0 commit comments

Comments
 (0)