/* School Bus Monitoring System
Data Visualisation - Speed over the period of time done with the help of Adafruit
Notification is sent to the number if speed exceeds 80km/hr.
Emergency Alert - If switch is turned on in case of emergency, Indicator is turned on
in Adafruit. Notification is sent to the number if switch is turned on.
*/
#include <WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "base64.h"
#include <HTTPClient.h>
// Wi-Fi credentials
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASS ""
// Adafruit IO credentials
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "Aswathvikram"
#define AIO_KEY "aio_awwx79uAeeHK8jkZRkM3cN3ON5j5"
// Twilio API details for sending SMS
const char* accountSid = "AC6272d8810310ef6dbc2b7f8a74e46b8f";
const char* authToken = "8205603e09df71408635f42d16d42241";
const char* twilioNumber = "+13204088969";
const char* recipientNumber = "+919486520274";
// MQTT Feeds for speed and emergency alerts
#define SPEED_FEED AIO_USERNAME "/feeds/Speed"
#define ALERT_FEED AIO_USERNAME "/feeds/EmergencyAlert"
// Pin definitions
#define SPEED 34 // Analog pin for reading speed sensor input
#define LED 33 // Digital pin for LED indicator
#define SWITCH 32 // Digital pin for emergency switch
// Initialize Wi-Fi and MQTT client
WiFiClient wifiClient;
Adafruit_MQTT_Client mqtt(&wifiClient, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// Adafruit IO feeds for publishing data
Adafruit_MQTT_Publish speedFeed(&mqtt, SPEED_FEED);
Adafruit_MQTT_Publish alertFeed(&mqtt, ALERT_FEED);
bool emergencyAlertSent = false;
void connectToWiFi() {
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
}
void connectToMQTT() {
// Reconnect to MQTT if disconnected
while (!mqtt.connected()) {
Serial.println("Connecting to MQTT...");
int8_t ret = mqtt.connect();
if (ret == 0) {
Serial.println("Connected to MQTT!");
} else {
Serial.print("MQTT Connection failed: ");
Serial.println(mqtt.connectErrorString(ret));
delay(5000);
}
}
}
void sendTwilioSMS(const char* message) {
// Construct the Basic Authentication header for Twilio API
String encodedAuth = base64::encode(String(accountSid) + ":" + String(authToken));
HTTPClient http;
http.begin("https://api.twilio.com/2010-04-01/Accounts/" + String(accountSid) + "/Messages.json");
http.addHeader("Authorization", "Basic " + encodedAuth);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare POST data
String postData = "To=" + String(recipientNumber) + "&From=" + String(twilioNumber) + "&Body=" + String(message);
int httpResponseCode = http.POST(postData);
if (httpResponseCode > 0) {
Serial.println("SMS Sent: " + String(message));
} else {
Serial.print("SMS Error: ");
Serial.println(httpResponseCode);
}
http.end();
}
void setup() {
Serial.begin(115200);
pinMode(SPEED, INPUT);
pinMode(LED, OUTPUT);
pinMode(SWITCH, INPUT);
connectToWiFi(); // Establish Wi-Fi connection
connectToMQTT(); // Connect to MQTT server
}
void loop() {
if (!mqtt.connected()) {
connectToMQTT(); // Reconnect to MQTT if necessary
}
mqtt.processPackets(10);
mqtt.ping();
// Read and map the speed sensor value
int speedValue = analogRead(SPEED);
int speed = map(speedValue, 0, 4095, 0, 120);
// Publish speed data to Adafruit IO
if (!speedFeed.publish((int32_t)speed)) {
Serial.println("Failed to publish speed");
}
// Check if speed exceeds threshold and send alert
if (speed > 80 && !emergencyAlertSent) {
sendTwilioSMS("Warning: Speed exceeded 80 km/hr!");
emergencyAlertSent = true;
}
// Monitor emergency switch state
int switchState = digitalRead(SWITCH);
if (switchState == LOW) { // Emergency switch activated
digitalWrite(LED, HIGH); // Turn on LED
if (!alertFeed.publish((int32_t)1)) { // Publish alert to Adafruit IO
Serial.println("Failed to publish alert");
}
// Send emergency SMS
if (!emergencyAlertSent) {
sendTwilioSMS("Emergency Alert: Switch Pressed!");
emergencyAlertSent = true;
}
} else { // Emergency switch deactivated
digitalWrite(LED, LOW); // Turn off LED
if (!alertFeed.publish((int32_t)0)) { // Publish normal state
Serial.println("Failed to publish alert");
}
emergencyAlertSent = false; // Reset alert flag
}
delay(2000); // 2-second delay for the next loop iteration
}