#include <WiFi.h>
#include <Stepper.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#include <MPU6050.h>
// WiFi credentials
const char* ssid = "Virus";
const char* password = "Zxcvbnmlp";
// Azure IoT Hub credentials
const char* connectionString = "HostName=TransportationPro.azure-devices.net;DeviceId=esp32;SharedAccessKey=3zdRQiFBdr2K2w+QnKykCGjx0Q9pXBOjoAIoTNy0bkY=";
// Constants for the ultrasonic sensor
const int trigPin = 12; // Pin for triggering the ultrasonic pulse
const int echoPin = 14; // Pin for receiving the echo pulse
// Constants for the LEDs
const int greenLEDPin = 25;
const int redLEDPin = 26;
// Constants for the stepper motor
const int stepsPerRevolution = 200; // Number of steps per revolution of your stepper motor
Stepper myStepper(stepsPerRevolution, 32, 33, 26, 27);
// Constants for the DHT sensor
#define DHTPIN 13 // Pin connected to the DHT sensor
#define DHTTYPE DHT11 // Change to DHT22 if using the DHT22 sensor
DHT dht(DHTPIN, DHTTYPE);
// Create an instance of the MPU6050 class
MPU6050 mpu;
// Threshold distance in centimeters
const int thresholdDistance = 20; // Distance at which the motor stops and the red LED turns on
void setup() {
// Initialize serial communication
Serial.begin(115200);
// Connect to WiFi (simulation)
connectToWiFi();
// Set up the pins for the ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Set up the pins for the LEDs
pinMode(greenLEDPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
// Initialize the DHT sensor
dht.begin();
// Initialize the MPU6050 sensor
Wire.begin();
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed!");
while (1);
}
// Start with the green LED on and red LED off
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
}
void loop() {
// Measure the distance using the ultrasonic sensor
long duration;
int distance;
// Trigger the ultrasonic pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pulse duration
duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
distance = duration * 0.034 / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Measure the temperature using the DHT sensor
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temperature from DHT sensor!");
} else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" *C");
}
// Measure the orientation using the MPU6050 sensor
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Print the gyroscope data to the Serial Monitor
Serial.print("Gyroscope: X=");
Serial.print(gx);
Serial.print(" Y=");
Serial.print(gy);
Serial.print(" Z=");
Serial.println(gz);
// Perform specific actions based on gyroscope data
if (abs(gx) > 10000 || abs(gy) > 10000 || abs(gz) > 10000) {
Serial.println("Sudden movement detected! Possible traffic incident.");
}
// Check the distance and control the stepper motor and LEDs
if (distance < thresholdDistance) {
// If the object is too close, stop the motor and turn on the red LED
myStepper.step(0); // Stop the motor
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, HIGH);
Serial.println("Object too close! Stopping motor and turning on red LED.");
} else {
// If the distance is safe, rotate the motor and keep the green LED on
myStepper.step(stepsPerRevolution / 100); // Adjust the speed as needed
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, LOW);
Serial.println("Safe distance. Motor running and green LED on.");
}
// Simulated JSON message for Azure IoT Hub
char message[256];
snprintf(message, sizeof(message), "{\"distance\": %d, \"temperature\": %.2f, \"gyroscope\": {\"x\": %d, \"y\": %d, \"z\": %d}}", distance, temperature, gx, gy, gz);
// Print the message that would be sent to Azure
Serial.print("Simulated Azure Message: ");
Serial.println(message);
// Add a delay to stabilize the sensor readings
delay(1000);
}
// Simulate WiFi connection
void connectToWiFi() {
Serial.println("Simulating WiFi Connection...");
Serial.print("Connecting to WiFi: ");
Serial.print(ssid);
Serial.println("...Connected.");
}