#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
#include <ESP32_MailClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const String apiKey = "32294d689f754a069dc25001232309";
const String writeApiKey = "63VW3WKRGX525MWH";
String serverName = "http://api.weatherapi.com/v1/current.json";
String thingSpeakServerName = "https://api.thingspeak.com/update";
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4);
//Your Domain name with URL path or IP address with path
const int relayPin = 19;
#define emailSenderAccount "[email protected]"
#define emailSenderPassword "tnma juwe nxxo tlym"
#define emailRecipient "[email protected]"
#define smtpServer "74.125.143.108"
#define emailSubject "NodeWifi32 Test"
#define smtpServerPort 465
SMTPData smtpData;
void writeToThingspeak(float temp, float humid, float windSpeed, float winDegree, float precip_mm){
HTTPClient newHttp;
String requestData = "api_key="+ writeApiKey +
"&field1="+temp +
"&field2="+humid+
"&field3="+windSpeed+
"&field4="+winDegree+
"&field5="+precip_mm;
newHttp.begin(thingSpeakServerName);
newHttp.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = newHttp.POST(requestData);
newHttp.end();
}
void sendMail(){
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("NodeWifi32", emailSenderAccount);
smtpData.setPriority("Rain");
smtpData.setSubject(emailSubject);
smtpData.setMessage("Đã có mưa", true);
smtpData.addRecipient(emailRecipient);
if (!MailClient.sendMail(smtpData)) {
Serial.println("Error sending Email: " + MailClient.smtpErrorReason());
}
smtpData.empty();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(relayPin,OUTPUT);
WiFi.begin(ssid, password);
lcd.init();
lcd.backlight();
}
void loop() {
if(WiFi.status()!= WL_CONNECTED)
return;
HTTPClient http;
String serverPath = serverName + "?key=" + apiKey + "&q=Angiang&aqi=no";
http.begin(serverPath);
int httpCode = http.GET();
if(httpCode == 200){
DynamicJsonDocument doc(1024);
String payload = http.getString();
deserializeJson(doc, payload);
String is_day = doc["current"]["condition"];
String weatherText = doc["current"]["condition"]["text"];
lcd.println(weatherText);
Serial.println(is_day);
int weatherCode = doc["current"]["condition"]["code"];
double temperature = doc["current"]["temp_c"];
double humid = doc["current"]["humidity"];
double windSpeed = doc["current"]["wind_kph"];
double windDegree = doc["current"]["wind_degree"];
double precip = doc["current"]["precip_mm"];
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("*C");
if(weatherCode > 1150){ //Có mưa
digitalWrite( relayPin, HIGH); //bơm nước ra
sendMail();
}
if(temperature > 30){ //Nhiệt độ trên 30
digitalWrite( relayPin, LOW); //bơm nước vô
}
writeToThingspeak(temperature,humid,windSpeed,windDegree,precip);
}
http.end();
// put your main code here, to run repeatedly:
delay(5000); // this speeds up the simulation
lcd.clear();
}