#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#define echoPin 27
#define trigPin 26
#define ledPin 21
const char* ssid = "nepatronix_2.4";//Enter your SSID
const char* password = "CLB269DA03";//Enter your password
String phoneNumber = "+34644867049"; //country_code + phone number
String apiKey = "2957777";
void setup() {
Serial.begin(115200);
pinMode(echoPin, INPUT);
pinMode(trigPin,OUTPUT);
pinMode(ledPin,OUTPUT);
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
sendAlert("Your system is ready!");
}
void loop() {
// Start a new measurement:
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the result:
int duration = pulseIn(echoPin, HIGH);
Serial.print("Distance in CM: ");
Serial.println(duration / 58);
int sensorValue = duration/58;
if (sensorValue < 15) {
digitalWrite(ledPIN, HIGH);
sendAlert("Warning!");
} else {
digitalWrite(ledPIN, LOW);
}
}
void sendAlert(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() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}