`
#define BLYNK_DEBUG
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
SimpleTimer timer;
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
char auth[] = "a94be1f3b38b46ed8ca65f8e74f96055";
char ssid[] = "Motel Trang";
char pass[] = "nhanghitrang257";
WidgetLED led1(0);
WidgetLED led2(1);
WidgetLED led3(2);
WidgetLED led4(3);
int ChkOutputStateLed1 = 0;
int ChkOutputStateLed2 = 0;
int ChkOutputStateLed3 = 0;
int ChkOutputStateLed4 = 0;
void setup()
{
Serial.begin(112500);
Blynk.begin(auth,ssid, pass);
dht.begin();
timer.setInterval(10, sendDHT);
pinMode(14, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
digitalWrite(14, LOW); // active High
digitalWrite(12, LOW); // active High
digitalWrite(13, LOW); // active High
digitalWrite(15, LOW); // active High
while(Blynk.connect()==false){}
}
void sendDHT()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Blynk.virtualWrite(V4, t);
Blynk.virtualWrite(V5, h);
}
void LEDLastStatus() // LED Last status to display the status when Mobile App is started
{
ChkOutputStateLed1 = digitalRead(14); // reading output pin 14
ChkOutputStateLed2 = digitalRead(12); // reading output pin 12
ChkOutputStateLed3 = digitalRead(13); // reading output pin 13
ChkOutputStateLed4 = digitalRead(15); // reading output pin 15
if (ChkOutputStateLed1 == HIGH){led1.on();} else{led1.off();} // actice high
if (ChkOutputStateLed2 == HIGH){led2.on();} else{led2.off();} // actice high
if (ChkOutputStateLed3 == HIGH){led3.on();} else{led3.off();} // actice high
if (ChkOutputStateLed4 == HIGH){led4.on();} else{led4.off();} // actice high
}
BLYNK_WRITE(0) // virtual pin0 control on App
{
if (param.asInt()) {
digitalWrite(14, LOW); // control D5 PIN14 NodeMCU
led1.off();
} else {
digitalWrite(14, HIGH);
led1.on();
}
}
BLYNK_WRITE(1) // virtual pin1 control on App
{
if (param.asInt()) {
digitalWrite(12, LOW); // control D6 PIN12
led2.off();
} else {
digitalWrite(12, HIGH);
led2.on();
}
}
BLYNK_WRITE(2) // virtual pin2 control on App
{
if (param.asInt()) {
digitalWrite(13, LOW); // control D7 PIN13
led3.off();
} else {
digitalWrite(13, HIGH);
led3.on();
}
}
BLYNK_WRITE(3) // virtual pin3 control on App
{
if (param.asInt()) {
digitalWrite(15, LOW); // control D8 PIN15
led4.off();
} else {
digitalWrite(15, HIGH);
led4.on();
}
}
void loop()
{
Blynk.run();
timer.run();
LEDLastStatus();
}
`