#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <Adafruit_Sensor.h>
#include <NTPtimeESP.h> // get time from internet
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <String.h>
#define DHTPIN 12 // what digital pin the DHT22 is conected to
#define DHTTYPE DHT22 // there are multiple kinds of DHT sensors
NTPtime NTPch("ch.pool.ntp.org"); // Server NTP
// Tao server
WiFiServer server(80);
const char* network="XuanSon" ;
const char* password="md240196" ;
LiquidCrystal_I2C lcd(0x27,20,4); // Library LCD 2004
strDateTime dateTime;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
pinMode(0,OUTPUT); // Quạt làm mát
pinMode(2,OUTPUT); // Quạt thường
pinMode(13,OUTPUT); // Đèn
pinMode(14,OUTPUT); // Chuông
pinMode(15,OUTPUT); //Bơm
delay(100);
lcd.init(); //initialize the lcd
lcd.backlight();
lcd.clear();
dht.begin();
//Kết nối wifi
Serial.begin(9600);// set baundRate esp8266 nodemcu with computer
Serial.println();//xuong hang
Serial.println("DA SAN SANG!");
Serial.print("Ket noi toi mang wifi ");
Serial.println(network);
//bắt đầu sever
WiFi.begin(network, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi da ket noi"); // connected wifi
Serial.println("Dia chi IP: ");
Serial.println(WiFi.localIP()); //print ra dia chi IP
}
void loop()
{
float h = dht.readHumidity();
float t = dht.readTemperature();
int value = analogRead(A0);
while (isnan(t) || isnan(h)) { // Kiểm tra xem thử việc đọc giá trị có bị thất bại hay không. Hàm isnan bạn xem tại đây http://arduino.vn/reference/isnan
Serial.println("FALL");
}
lcd.setCursor(0,0);
Serial.println(t);
lcd.print("ND=");
lcd.print(t);
lcd.print("oC");
lcd.print(" ");
lcd.setCursor(0,1);
Serial.println(h);
lcd.print("DA=");
lcd.print(h);
lcd.print("%");
lcd.print(" ");
Serial.println(value);
lcd.setCursor(12,1);
lcd.print("KK=");
lcd.print(value);
lcd.print(" ");
delay(1000);
/* gui du ieu len webserver */
if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status
{
// gủi dữ liệu lên webserver
HTTPClient http; //Declare an object of class HTTPClient
String url = "http://ns-ptit.site/Load.php?";
url+="field1=";
url+=String(t);
url+="&field2=";
url+= String(h);
url+= "&field3=";
url+=String(value);
http.begin(url); //Specify request destination
http.GET();
http.end();
delay(300);
//nhận dữ liệu từ webserver
//điều khiển quạt làm mát
http.begin("http://ns-ptit.site/Quat1.php"); //Specify request destination
int httpCode1=http.GET();
if (httpCode1 > 0) //Check the returning code
{
String payload1 = http.getString(); //Get the request response payload
if(payload1 =="ON")
{
digitalWrite(0, HIGH);//bat quat lam mat
Serial.println(payload1); //Print the response payload
//delay(20);
}
else if(payload1=="OFF")
{
digitalWrite(0,LOW);//tat quat lam mat
Serial.println(payload1);
}
}
http.end(); //Close connection
delay(300);
//dieu khien quat thuong
http.begin("http://ns-ptit.site/Quat2.php");
int httpCode2=http.GET();
if (httpCode2 > 0) //Check the returning code
{
String payload2 = http.getString(); //Get the request response payload
if(payload2 =="ON")
{
digitalWrite(2, HIGH);//bat quat thuong
Serial.println(payload2); //Print the response payload
// delay(20);
}
else if(payload2=="OFF")
{
digitalWrite(2,LOW);//tat quat thuong
Serial.println(payload2);
}
}
http.end(); //Close connection
delay(300);
// điều khiển đèn
http.begin("http://ns-ptit.site/Den.php");
int httpCode3=http.GET();
if (httpCode3 > 0) //Check the returning code
{
String payload3 = http.getString(); //Get the request response payload
if(payload3 =="ON")
{
digitalWrite(13, HIGH);//bat bom phan
Serial.println(payload3); //Print the response payload
// delay(20);
}
else if(payload3=="OFF")
{
digitalWrite(13,LOW);//tat den
Serial.println(payload3);
}
}
http.end(); //Close connection
delay(300);
dateTime = NTPch.getNTPtime(7.0, 0);
// Kiem tra dateTime.valid truoc khi dua ra gia tri thoi gian
if(dateTime.valid)
{
NTPch.printDateTime(dateTime);
byte actualday =dateTime.day; // Ngay
lcd.setCursor(1,2);
if( actualday<10)
{
lcd.print(0);
lcd.print(actualday);
}
else
{
lcd.print(actualday);
}
lcd.print('/');
Serial.print(actualday, DEC); // Ngày
Serial.print('/');
byte actualMonth = dateTime.month; // Thang
if(actualMonth<10)
{
lcd.print(0);
lcd.print(actualMonth);
}
else
{
lcd.print(actualMonth);
}
lcd.print('/');
Serial.print(actualMonth, DEC); // Tháng
Serial.print('/');
int actualyear = dateTime.year; // Nam
lcd.print(actualyear);
lcd.print(' ');
Serial.print(actualyear, DEC); // Năm
Serial.print(' ');
byte actualHour = dateTime.hour; // Gio
if(actualHour<10)
{
lcd.print(0);
lcd.print(actualHour);
}
else
{
lcd.print(actualHour);
}
lcd.print(':');
Serial.print(actualHour, DEC); // Giờ
Serial.print(':');
byte actualMinute = dateTime.minute; // Phut
if( actualMinute<10)
{
lcd.print(0);
lcd.print(actualMinute);
}
else
{
lcd.print(actualMinute);
}
Serial.print(actualMinute, DEC); // Phút
}
}
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.