#include <Stepper.h>
#include <Wire.h>
#include <ESP32Servo.h>
#include <WiFi.h>
#include <ESPAsyncWebSrv.h>
#include <AsyncTCP.h>
char webpage[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<script>
var socket = new WebSocket("ws://" + window.location.host + "/ws");
socket.onmessage = function(event) {
var data = JSON.parse(event.data);
document.getElementById("distance1").innerText = "Distance 1: " + data.distance1 + " cm";
document.getElementById("distance2").innerText = "Distance 2: " + data.distance2 + " cm";
document.getElementById("distance3").innerText = "Distance 3: " + data.distance3 + " cm";
document.getElementById("distance4").innerText = "Distance 4: " + data.distance4 + " cm";
};
</script>
</head>
<body>
<h1>Ultrasonic Sensor Data</h1>
<p id="distance1">Distance 1: - cm</p>
<p id="distance2">Distance 2: - cm</p>
<p id="distance3">Distance 3: - cm</p>
<p id="distance4">Distance 4: - cm</p>
</body>
</html>
)=====";
Servo myservo;
#define TRIG_PIN 19
#define ECHO_PIN 4
#define TRIG_PIN1 5
#define ECHO_PIN1 15
#define TRIG_PIN2 2
#define ECHO_PIN2 23
#define TRIG_PIN3 18
#define ECHO_PIN3 21
#define LED_R 32
#define LED_G 27
#define LED_Y 25
#define LED_B 26
const char *ssid = "Wokwi-GUEST";
const char *password = "";
const int stepsPerRevolution = 200;
const int motorPin1 = 14;
const int motorPin2 = 12;
Stepper myStepper(stepsPerRevolution, motorPin1, motorPin2);
int currentAngle = 0;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
void calculateRotation(int currentAngle, int targetAngle, int &direction, int &rotationAngle) {
int clockwiseDistance = (targetAngle - currentAngle + 360) % 360;
int counterclockwiseDistance = (currentAngle - targetAngle + 360) % 360;
if (clockwiseDistance <= counterclockwiseDistance) {
direction = 1; // 1 for clockwise
rotationAngle = clockwiseDistance;
} else {
direction = -1; // -1 for counterclockwise
rotationAngle = counterclockwiseDistance;
}
}
long measureDistance(int trigPin, int echoPin) {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
long duration = pulseIn(echoPin, HIGH);
return duration / 58;
}
void setup() {
Serial.begin(115200);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(TRIG_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIG_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
pinMode(TRIG_PIN3, OUTPUT);
pinMode(ECHO_PIN3, INPUT);
pinMode(LED_R, OUTPUT);
pinMode(LED_G, OUTPUT);
pinMode(LED_Y, OUTPUT);
pinMode(LED_B, OUTPUT);
myStepper.setSpeed(500);
myservo.attach(22);
myservo.write(0);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Serve the HTML page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send_P(200, "text/html", webpage);
});
// Attach the WebSocket event handler
ws.onEvent([](AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) {
if (type == WS_EVT_CONNECT) {
Serial.println("WebSocket client connected");
} else if (type == WS_EVT_DISCONNECT) {
Serial.println("WebSocket client disconnected");
}
});
server.begin();
}
void loop() {
int direction, rotationAngle;
int v;
int ca = currentAngle;
int ra;
long distance = measureDistance(TRIG_PIN, ECHO_PIN);
long distance1 = measureDistance(TRIG_PIN1, ECHO_PIN1);
long distance2 = measureDistance(TRIG_PIN2, ECHO_PIN2);
long distance3 = measureDistance(TRIG_PIN3, ECHO_PIN3);
digitalWrite(LED_R, distance <= 50 ? HIGH : LOW);
digitalWrite(LED_G, distance1 <= 50 ? HIGH : LOW);
digitalWrite(LED_Y, distance2 <= 50 ? HIGH : LOW);
digitalWrite(LED_B, distance3 <= 50 ? HIGH : LOW);
Serial.println("Distance 1: " + String(distance) + " cm");
Serial.println("Distance 2: " + String(distance1) + " cm");
Serial.println("Distance 3: " + String(distance2) + " cm");
Serial.println("Distance 4: " + String(distance3) + " cm");
// Prepare and send JSON data over WebSocket
String json = "{\"distance1\":" + String(distance) + ",\"distance2\":" + String(distance1) + ",\"distance3\":" + String(distance2) + ",\"distance4\":" + String(distance3) + "}";
ws.textAll(json);
// Rest of your code to handle stepper and servo motor control
}