///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //PROGRAM 1
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
const char* ssid = "Motel Trang";
const char* password = "nhanghitrang257";
DHT *dht;
void connectWiFi();
void reconnectWiFiIfLinkDown();
void initDht(DHT **dht, uint8_t pin, uint8_t dht_type);
void readDht(DHT *dht, float *temp, float *humid);
void uploadThingspeak(float t,float );
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
connectWiFi();
initDht(&dht, DHTPIN, DHTTYPE);
}
void loop() {
// put your main code here, to run repeatedly:
static float t_ds;
static float t_dht;
static float h_dht;
readDht(dht, &t_dht, &h_dht);
uploadThingspeak(t_dht,h_dht);
//Serial.println(t_ds);
delay(10*1000);
reconnectWiFiIfLinkDown();
}
void reconnectWiFiIfLinkDown() {
if(WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi Disconnected");
connectWiFi();
}
}
void connectWiFi() {
Serial.println();
Serial.println();
Serial.println("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
void initDht(DHT **dht, uint8_t pin, uint8_t dht_type) {
*dht = new DHT(pin, dht_type, 30);
(*dht)->begin();
}
void uploadThingspeak(float t, float h) {
// unsigned long DHT11_upload = 127100;
static const char* host = "api.thingspeak.com";
static const char* apiKey = "NAZ2KNBYDDWSA6LZ";
// use wificlient clas to creat TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("Connection Failed");
return;
}
//we now create a URI for the requet
String url = "/update/";
// url += streamID;
url += "?key=";
url += apiKey;
url += "&field1=";
url += t;
url += "&field2=";
url += h;
Serial.print("Requesting URL : ");Serial.println(url);Serial.println();Serial.println();
//this will send request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +"Host:" + host + "\r\n" + "Connection : close\r\n\r\n");
}
void readDht(DHT *dht, float *temp, float *humid) {
if (dht == NULL) {
Serial.println("[dht11] is not initialised, please call initDht() first.");
return;
}
//reading temperature or humidity takes about 250 miliseconds
//sensor reading say also be up to 2 seconds 'old'
float h = dht->readHumidity();
float t = dht->readTemperature();
float f = dht->readTemperature(true);
if (isnan(h)||isnan(t)||isnan(f)) {
Serial.println("Failed to read from DHT sensor");
return;
}
Serial.print("Temperature : "); Serial.print(t); Serial.print(" *C\t");
Serial.print("Humidity : "); Serial.print(h); Serial.print(" %\t");
*temp = t;
*humid = h;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////PROGRAM 2
#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){}
LEDLastStatus();
}
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();
}