/*SPEED BREAKER NOTIFIER
problem solved: while traveling in highspeed, sometimes the speedbreakers are not noticed,
here, ultrasonic sensors detect the speedbreaker at a particular distance prior and notifies
the driver which can be viewed in car display
here ,when the first and second sensor get their minimum distance less then
16M and 14M respectively(which shows the upper slope of speed breaker) , a message
will be sent to user through twilio
note:
the speedbreaker prediction cannot be highly accurate in real life
when using ultrasonic sensor , so I concluded assuming we will use IR sensor in
reallife applications for more accuracy in this case
*/
#include <WiFi.h>
#include "base64.h"
#include <HTTPClient.h>
const char* WIFI_NAME="Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// Define pin numbers
const int ledPin = 6; // LED pin
// Pins for first ultrasonic sensor
const int trigPin1 = 28; // Trigger pin of first ultrasonic sensor
const int echoPin1 = 27; // Echo pin of first ultrasonic sensor
// Pins for second ultrasonic sensor
const int trigPin2 = 22; // Trigger pin of second ultrasonic sensor
const int echoPin2 = 21; // Echo pin of second ultrasonic sensor
//Crenditals for Twilio (Messaging)
const char* accountSid = "AC377a3bfd71346982e58684676c026faa";
const char* authToken = "ee75f40174abea28b292f94e949f2b94";
const char* fromPhoneNumber = "+13343779718";
const char* toPhoneNumber = "+918122667959";
void setup() {
// Initialize serial communication
Serial1.begin(115200);
// Set the LED pin as an OUTPUT
pinMode(ledPin, OUTPUT);
// Set the pins for first ultrasonic sensor
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
// Set the pins for second ultrasonic sensor
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
WiFi.begin(WIFI_NAME,WIFI_PASSWORD);
while (WiFi.status()!=WL_CONNECTED){
delay(1000);
Serial1.print("Wifi not connected...\n");
}
Serial1.println("\nWifi connected");
Serial1.println("IP Address: "+(WiFi.localIP()));
WiFi.mode(WIFI_STA);
}
void loop() {
// Read distance from first ultrasonic sensor
long duration1, distance1;
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distance1 = duration1 * 0.034 / 2;
// Read distance from second ultrasonic sensor
long duration2, distance2;
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distance2 = duration2 * 0.034 / 2;
// Print ultrasonic sensor readings
Serial1.print("Distance 1: ");
Serial1.print(distance1);
Serial1.println(" m");
Serial1.print("Distance 2: ");
Serial1.print(distance2);
Serial1.println(" m");
// Turn on the LED if conditions are met
if (distance1 < 16 && distance2 < 15) {
digitalWrite(ledPin, HIGH);
Serial1.println("speed breaker ahead by 15 M");
// Encode the Twilio credentials
String credentials = String(accountSid) + ":" + String(authToken);
String encodedCredentials = base64::encode(credentials);
String messageBody = "speed breaker ahead by 15 M";
// Create the URL for the Twilio API
String url = "https://api.twilio.com/2010-04-01/Accounts/" + String(accountSid) + "/Messages.json";
// Create the POST data
String postData = "To=" + String(toPhoneNumber) + "&From=" + String(fromPhoneNumber) + "&Body=" + messageBody;
// Create and configure the HTTP client for Twilio
HTTPClient http;
http.begin(url);
http.addHeader("Authorization", "Basic " + encodedCredentials);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
int httpCode = http.POST(postData); //Sending the message once the heartRate is above the threshold.
if (httpCode == HTTP_CODE_OK) {
;
}
// Close the connection
http.end();
} else {
digitalWrite(ledPin, LOW);
}
// Delay for a second
delay(1000);
}