|
| 1 | +#define BLYNK_PRINT Serial |
| 2 | + |
| 3 | +#include <BlynkSimpleEsp8266_WM.h> |
| 4 | +#include <Ticker.h> |
| 5 | +#include <DHT.h> |
| 6 | + |
| 7 | +#define DHT_PIN 2 |
| 8 | +#define DHT_TYPE DHT11 |
| 9 | + |
| 10 | +#define DHT_DEBUG 1 |
| 11 | + |
| 12 | +DHT dht(DHT_PIN, DHT_TYPE); |
| 13 | +BlynkTimer timer; |
| 14 | +Ticker aux_ticker; |
| 15 | + |
| 16 | +void readAndSendData() |
| 17 | +{ |
| 18 | + float temperature = dht.readTemperature(); |
| 19 | + float humidity = dht.readHumidity(); |
| 20 | + |
| 21 | + |
| 22 | + if (!isnan(temperature) && !isnan(humidity)) |
| 23 | + { |
| 24 | + Blynk.virtualWrite(V17, temperature); |
| 25 | + Blynk.virtualWrite(V18, humidity); |
| 26 | + } |
| 27 | + else |
| 28 | + { |
| 29 | + Blynk.virtualWrite(V17, -100); |
| 30 | + Blynk.virtualWrite(V18, -100); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#define PIN_LED 2 // Pin D4 mapped to pin GPIO2/TXD1 of ESP8266, NodeMCU and WeMoS, control on-board LED |
| 35 | + |
| 36 | +void set_led(byte status) |
| 37 | +{ |
| 38 | + digitalWrite(PIN_LED /*LED_BUILTIN*/, status); |
| 39 | + } |
| 40 | + |
| 41 | +void check_status() |
| 42 | +{ |
| 43 | + static unsigned long checkstatus_timeout = 0; |
| 44 | + |
| 45 | +#define STATUS_CHECK_INTERVAL 15000L |
| 46 | + |
| 47 | + // Send status report every STATUS_REPORT_INTERVAL (10) seconds: we don't need to send updates frequently if there is no status change. |
| 48 | + if ((millis() > checkstatus_timeout) || (checkstatus_timeout == 0)) |
| 49 | + { |
| 50 | + // report status to Blynk |
| 51 | + if (Blynk.connected()) |
| 52 | + { |
| 53 | + set_led(LOW); |
| 54 | + aux_ticker.once_ms(111, set_led, (byte) HIGH); |
| 55 | + |
| 56 | + Serial.println("B"); |
| 57 | + } |
| 58 | + else |
| 59 | + { |
| 60 | + Serial.println("F"); |
| 61 | + } |
| 62 | + |
| 63 | + checkstatus_timeout = millis() + STATUS_CHECK_INTERVAL; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void setup() |
| 68 | +{ |
| 69 | + // Debug console |
| 70 | + Serial.begin(115200); |
| 71 | + dht.begin(); |
| 72 | + Blynk.begin(); |
| 73 | + timer.setInterval(60 * 1000, readAndSendData); |
| 74 | + |
| 75 | + if (Blynk.connected()) |
| 76 | + { |
| 77 | + Serial.println("Blynk connected. Board Name : " + Blynk.getBoardName()); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void loop() |
| 82 | +{ |
| 83 | + Blynk.run(); |
| 84 | + timer.run(); |
| 85 | + check_status(); |
| 86 | +} |
0 commit comments