#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
//LCD BANG////////////////////////////////////////////////////////
#include <LiquidCrystal_I2C.h>
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// if you don't know your display address, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
/////////////////////////////////////////////////////////////////
#define ECHO_PIN 2
#define TRIG_PIN 4
int x=0;
int y=0;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// +international_country_code + phone number
// Portugal +351, example: +351912345678
String phoneNumber = "905437251435";
String apiKey = "115530";
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 indicator(){
float distance = readDistanceCM();
if(distance>=100 && x==0){
// Send Message to WhatsAPP
String message = "Distance: ";
message += String(distance);
sendMessage(message);
digitalWrite(13,HIGH);
x=1;
y=0;
}
if(x==1){}
if(y==1){}
if(distance<=100 && y==0){
// Send Message to WhatsAPP
String message = "Distance: ";
message += String(distance);
sendMessage(message);
digitalWrite(13,LOW);
y=1;
x=0;
}}
void baca_sensor(){
float distance = readDistanceCM();
Serial.print("distance(CM): ");
Serial.println(readDistanceCM());
Serial.println();
delay(100);
}
float readDistanceCM() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
int duration = pulseIn(ECHO_PIN, HIGH);
return duration * 0.034 / 2;
}
void LCD_setting(){
if(digitalRead(13)==HIGH){
lcd.setCursor(5, 0);
lcd.print("FULL");
delay(1000);
lcd.clear();
lcd.setCursor(5,0);
lcd.print("FULL");
delay(1000);
lcd.clear(); }
if(digitalRead(13)==LOW){
lcd.setCursor(4, 0);
lcd.print("EMPTY");
delay(1000);
lcd.clear();
lcd.setCursor(4,0);
lcd.print("EMPTY");
delay(1000);
lcd.clear(); }
}
void setup() {
Serial.begin(115200);
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());
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
pinMode(13, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
}
void loop() {
baca_sensor();
indicator();
LCD_setting();
}