/* ESP32 HTTP IoT Server Example for Wokwi.com
https://wokwi.com/projects/320964045035274834
To test, you need the Wokwi IoT Gateway, as explained here:
https://docs.wokwi.com/guides/esp32-wifi#the-private-gateway
Then start the simulation, and open http://localhost:9080
in another browser tab.
Note that the IoT Gateway requires a Wokwi Club subscription.
To purchase a Wokwi Club subscription, go to https://wokwi.com/club
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#include <uri/UriBraces.h>
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
// Defining the WiFi channel speeds up the connection:
#define WIFI_CHANNEL 6
WebServer server(80);
const int LED1 = 26;
const int LED2 = 27;
bool led1State = false;
bool led2State = false;
void sendHtml() {
String response = R"(
<!DOCTYPE html><html>
<head>
<title>ESP32 Web Server Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html { font-family: sans-serif; text-align: center; }
body { display: inline-flex; flex-direction: column; }
h1 { margin-bottom: 1.2em; }
h2 { margin: 0; }
div { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto auto; grid-auto-flow: column; grid-gap: 1em; }
.btn { background-color: #5B5; border: none; color: #fff; padding: 0.5em 1em;
font-size: 2em; text-decoration: none }
.btn.OFF { background-color: #333; }
</style>
</head>
<body>
<h1>ESP32 Web Server</h1>
<div>
<h2>LED 1</h2>
<a href="/toggle/1" class="btn LED1_TEXT">LED1_TEXT</a>
<h2>LED 2</h2>
<a href="/toggle/2" class="btn LED2_TEXT">LED2_TEXT</a>
</div>
</body>
</html>
)";
response.replace("LED1_TEXT", led1State ? "ON" : "OFF");
response.replace("LED2_TEXT", led2State ? "ON" : "OFF");
server.send(200, "text/html", response);
}
void setup(void) {
Serial.begin(115200);
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD, WIFI_CHANNEL);
Serial.print("Connecting to WiFi ");
Serial.print(WIFI_SSID);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.macAddress());
server.on("/", sendHtml);
server.on(UriBraces("/toggle/{}"), []() {
String led = server.pathArg(0);
Serial.print("Toggle LED #");
Serial.println(led);
switch (led.toInt()) {
case 1:
led1State = !led1State;
digitalWrite(LED1, led1State);
break;
case 2:
led2State = !led2State;
digitalWrite(LED2, led2State);
break;
}
sendHtml();
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(2);
}
// #include <ESP8266WiFi.h>
// #include <ESP8266HTTPClient.h>
// // Define pin connections
// const int ENA = D1; // Enable pin for motor driver 1
// const int IN1 = D2; // Input 1 for motor driver 1
// const int IN2 = D3; // Input 2 for motor driver 1
// const int triggerPin1 = D5; // Trigger pin for sensor 1
// const int echoPin1 = D6; // Echo pin for sensor 1
// const int triggerPin2 = D7; // Trigger pin for sensor 2
// const int echoPin2 = D8; // Echo pin for sensor 2
// // Variables
// long duration1, distance1; // Variables to store ultrasonic sensor 1 data
// long duration2, distance2; // Variables to store ultrasonic sensor 2 data
// const char* ssid = "YOUR_SSID"; // Your WiFi SSID
// const char* password = "YOUR_PASSWORD"; // Your WiFi password
// const char* serverAddress = "YOUR_SERVER_ADDRESS"; // Your server address
// void setup() {
// // Motor pins setup
// pinMode(ENA, OUTPUT);
// pinMode(IN1, OUTPUT);
// pinMode(IN2, OUTPUT);
// // Ultrasonic sensor pins setup
// pinMode(triggerPin1, OUTPUT);
// pinMode(echoPin1, INPUT);
// pinMode(triggerPin2, OUTPUT);
// pinMode(echoPin2, INPUT);
// // Initialize Serial Monitor
// Serial.begin(9600);
// delay(100);
// // Connect to Wi-Fi network
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) {
// delay(1000);
// Serial.println("Connecting to WiFi...");
// }
// Serial.println("Connected to WiFi");
// Serial.println(WiFi.localIP());
// Serial.println(WiFi.macAddress());
// }
// void loop() {
// // Read data from ultrasonic sensor 1
// digitalWrite(triggerPin1, LOW);
// delayMicroseconds(2);
// digitalWrite(triggerPin1, HIGH);
// delayMicroseconds(10);
// digitalWrite(triggerPin1, LOW);
// duration1 = pulseIn(echoPin1, HIGH);
// distance1 = (duration1 * 0.0343) / 2;
// // Read data from ultrasonic sensor 2
// digitalWrite(triggerPin2, LOW);
// delayMicroseconds(2);
// digitalWrite(triggerPin2, HIGH);
// delayMicroseconds(10);
// digitalWrite(triggerPin2, LOW);
// duration2 = pulseIn(echoPin2, HIGH);
// distance2 = (duration2 * 0.0343) / 2;
// // Motor control based on sensor data
// if (distance1 < 20 && distance2 < 20) {
// // Move motor forward
// digitalWrite(IN1, HIGH);
// digitalWrite(IN2, LOW);
// analogWrite(ENA, 255); // Adjust PWM value for motor speed
// } else {
// // Stop motor
// digitalWrite(IN1, LOW);
// digitalWrite(IN2, LOW);
// analogWrite(ENA, 0);
// }
// // Print sensor data to Serial Monitor
// Serial.print("Sensor 1: ");
// Serial.print(distance1);
// Serial.print(" cm Sensor 2: ");
// Serial.print(distance2);
// Serial.println(" cm");
// // Check for HTTP GET request to control the device
// if (WiFi.status() == WL_CONNECTED) {
// HTTPClient http;
// http.begin(serverAddress);
// int httpResponseCode = http.GET();
// if (httpResponseCode > 0) {
// if (httpResponseCode == HTTP_CODE_OK) {
// String payload = http.getString();
// if (payload == "OFF") {
// // Turn off the device
// digitalWrite(IN1, LOW);
// digitalWrite(IN2, LOW);
// analogWrite(ENA, 0);
// } else if (payload == "ON") {
// // Turn on the device
// digitalWrite(IN1, HIGH);
// digitalWrite(IN2, LOW);
// analogWrite(ENA, 255);
// }
// Serial.println("Device status updated");
// }
// } else {
// Serial.println("Error in HTTP request");
// }
// http.end();
// } else {
// Serial.println("WiFi Disconnected, unable to send data to server");
// }
// delay(500); // Adjust delay time as needed
// }