#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define PIN_TRIG 2
#define PIN_ECHO 15
#define PIN_LED 5
#define SDA_PIN 12
#define SCL_PIN 13
#define SOUND_SPEED 0.034 // Speed of sound in cm per microsecond
long duration; // Global variable to store the pulse duration
float distanceCm; // Variable to store the calculated distance in centimeters
// Replace with your network credentials
const char* ssid = "Wokwi_Guest";
const char* password = "your_PASSWORD";
// Telegram Bot credentials
#define BOT_TOKEN "8009275723:AAE7DnKJrG94o0F_F-FItq3ZqYd-OTndS-M"
#define CHAT_ID "1418696775"
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD setup with I2C address 0x27, 16 columns, 2 rows
void setup() {
Serial.begin(115200); // Start serial communication at 115200 baud rate
pinMode(PIN_TRIG, OUTPUT); // Set the TRIG pin as an output
pinMode(PIN_LED, OUTPUT); // Set the LED pin as an output
pinMode(PIN_ECHO, INPUT); // Set the ECHO pin as an input
// Initialize I2C communication
Wire.begin(SDA_PIN, SCL_PIN);
// Initialize the LCD
lcd.init();
lcd.backlight(); // Turn on the LCD backlight
// Initialize Wi-Fi
connectToWiFi();
}
void loop() {
// Trigger the ultrasonic sensor
digitalWrite(PIN_TRIG, LOW); // Make sure TRIG is low initially
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(PIN_TRIG, HIGH); // Send a 10 microsecond pulse
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW); // End the pulse
// Measure the time taken for the pulse to return
duration = pulseIn(PIN_ECHO, HIGH); // Read the pulse duration on ECHO pin
// Calculate the distance based on the duration of the pulse
distanceCm = duration * SOUND_SPEED / 2; // Distance = time * speed of sound / 2 (for round trip)
// Control the LED based on the distance
if (distanceCm < 10) { // If the distance is less than 10 cm
digitalWrite(PIN_LED, HIGH); // Turn on LED
sendTelegramMessage("Warning! Object is closer than 10 cm!"); // Send Telegram message
} else {
digitalWrite(PIN_LED, LOW); // Turn off LED
}
// Output the distance to the serial monitor
Serial.print("Distance in CM: ");
Serial.println(distanceCm);
// Update the LCD display
lcd.clear(); // Clear the previous text on the LCD
lcd.setCursor(0, 0); // Set cursor to the first row, first column
lcd.print("Distance: ");
lcd.print(distanceCm); // Print the distance on the LCD
delay(1000); // Wait for 1 second before the next reading
}
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
WiFi.begin(ssid, password);
// Wait for the WiFi connection to be established
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("Connected to WiFi!");
}
void sendTelegramMessage(String message) {
HTTPClient http;
String url = "https://api.telegram.org/bot" + String(BOT_TOKEN) + "/sendMessage?chat_id=" + String(CHAT_ID) + "&text=" + message;
http.begin(url); // Start HTTP request
int httpCode = http.GET(); // Send GET request to Telegram API
if (httpCode > 0) { // If the request was successful
Serial.println("Message sent to Telegram.");
} else {
Serial.println("Failed to send message.");
}
http.end(); // End HTTP request
}