#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
DHTesp dhtSensor;
//international country code + phone number
String phoneNumber = "+919920016230";
String apiKey = "9726995";
void sendMessage(String message) {
// Data to send with HTTP POST
String url = "https://api.callmebot.com/whatsapp.php?phone=" + phoneNumber + "&apikey=" + apiKey + "&text=" + urlEncode(message);
HTTPClient http;
http.begin(url);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Send HTTP POST request
int httpResponseCode = http.POST(url);
if (httpResponseCode == 200) {
Serial.print("Message sent successfully");
}
else {
Serial.println("Error sending the message");
Serial.print("HTTP response code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
WiFi.begin(ssid, password);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// Send Message to WhatsAPP "Add your name + NV number + Class"
// Example: "Hello From Ebrahim - NV21058 - 12.CCP !"
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
if (data.temperature > 40) {
sendMessage("Hello from Anas Khalid Hamdan - NV21061 - 12.CCP WARNING! Temperature is too high! Temperature: " + String(data.temperature, 2) + "°C");
}
delay(3600000); // Wait for a new reading from the sensor (DHT22 has ~0.5Hz sample rate)
}