// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <ESP32_MailClient.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apiKey = "bfa0847748cb48868c172843232809";
// for thingspeak
const char* thingspeakApiKey = "ZCGU0KDY11YCGHKQ";
float temperatureCondition=0;
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 20, 4);
#define LED_PIN 23
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 "Current weather"
#define smtpServerPort 465
SMTPData smtpData;
void sendMail(){
Serial.println("Co vo gui mail r ma ko gui duoc");
smtpData.setLogin(smtpServer, smtpServerPort, emailSenderAccount, emailSenderPassword);
smtpData.setSender("NodeWifi32", emailSenderAccount);
smtpData.setPriority("Rain");
smtpData.setSubject(emailSubject);
smtpData.setMessage("Thong bao thoi tiet hien tai dang mua", true);
smtpData.addRecipient(emailRecipient);
if (!MailClient.sendMail(smtpData)) {
Serial.println("Error sending Email: " + MailClient.smtpErrorReason());
}
smtpData.empty();
}
void sendDataToThingspeak(float field1Value, float field2Value, float field3Value) {
String url = "https://api.thingspeak.com/update?api_key=" + String(thingspeakApiKey) +
"&field1=" + String(field1Value) +
"&field2=" + String(field2Value) +
"&field3=" + String(field3Value);
HTTPClient http;
http.begin(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
Serial.println("Data sent successfully");
} else {
Serial.println("Error sending data");
}
http.end();
}
void printWeatherAnGiang() {
String url = "http://api.weatherapi.com/v1/forecast.json?key=" + String(apiKey) + "&q=Long%20Xuyen";
HTTPClient http;
http.begin(url);
Serial.println(url);
int httpCode = http.GET();
if (httpCode == HTTP_CODE_OK) {
String payload = http.getString();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload);
const char* weatherCondition = doc["current"]["condition"]["text"];
temperatureCondition = doc["current"]["temp_c"];
int weatherCode = doc["current"]["condition"]["code"];
float humidity = doc["current"]["humidity"];
float precipitation = doc["current"]["precip_mm"];
Serial.print("Tình trạng thời tiết: ");
Serial.println(weatherCondition);
Serial.print("Tình trạng thời tiết: ");
Serial.println(weatherCode);
Serial.print("Nhiệt độ hiện tại: ");
Serial.print(temperatureCondition);
Serial.print(" độ C");
Serial.print("humidity hiện tại: ");
Serial.print(humidity);
Serial.print("precipitation hiện tại: ");
Serial.print(precipitation);
LCD.setCursor(0, 0);
LCD.println(weatherCondition);
LCD.setCursor(0, 1);
LCD.println(temperatureCondition);
if (temperatureCondition >= 30 ){
Serial.print(">= 30");
digitalWrite( relayPin, LOW); // đỏ ( bơm vào)
}else if(weatherCode >= 1063) {//1063
Serial.print("có mưa");
digitalWrite( relayPin, HIGH); // xanh lá (bơm ra)
sendMail();
} else {
Serial.print("Không có mưa và bé hơn 30%"); // đỏ ( bơm vào)
}
sendDataToThingspeak(temperatureCondition,humidity,precipitation);
} else {
Serial.println("HTTP Error Code: " + String(httpCode));
}
}
void setup() {
Serial.begin(115200);
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting to ");
LCD.setCursor(0, 1);
LCD.print("WiFi ");
pinMode(relayPin,OUTPUT);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
LCD.clear();
}
void loop() {
printWeatherAnGiang();
delay(300000);
}