#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#include "DHTesp.h"
const int DHT_PIN = 15;
const int redPin = 4;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const int trigPin = 15;
const int echoPin = 2;
long duration;
int distance;
DHTesp dhtSensor;
//international country code + phone number
String phoneNumber = "+97337722062";
String apiKey = "1298471";
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");
digitalWrite (redPin, HIGH);
}
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());
pinMode (redPin, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
// Send Message to WhatsAPP "Add your name + NV number + Class"
// Example: "Hello From Ebrahim - NV21058 - 12.CCP !"
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distance = (duration*0.034/2)+1;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distance);
if (distance <= 50) {
sendMessage(R"('Hello from Sayed Mohammed Mustafa - 12.CCP
WARNING! Someone enters the class!
Distance: ')" + String(distance) + "cm");
}
delay(3600000);
}
/*
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 Sayed Mohammed - NV21060 - 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)
}
*/