/*****************************************************************
ESP32 MASTER PROJECT (WOKWI)
HOW TO USE:
- Set ONLY ONE assignment to #if 1
- Keep others as #if 0
- Follow wiring steps inside each block
PIN PLAN (DO NOT RANDOMLY CHANGE):
LED (basic) -> GPIO 2
SERVO -> GPIO 18
DHT11 -> GPIO 4
HOME LEDS -> 16, 17, 19
BUTTON (mock) -> GPIO 5
*****************************************************************/
// ================== ASSIGNMENT 3 ==================
#if 1
// assignment - 3
// LED control using web interface
/*
WOKWI STEPS:
1. LED → GPIO 2 via resistor
2. Open Serial Monitor → copy IP
3. Open browser → control LED
*/
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
const int LED = 2;
void root() {
server.send(200, "text/html",
"<h1>LED Control</h1>"
"<a href='/on'>ON</a><br>"
"<a href='/off'>OFF</a>");
}
void ledOn() {
digitalWrite(LED, HIGH);
server.send(200, "text/html", "LED ON");
}
void ledOff() {
digitalWrite(LED, LOW);
server.send(200, "text/html", "LED OFF");
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
Serial.println(WiFi.localIP());
server.on("/", root);
server.on("/on", ledOn);
server.on("/off", ledOff);
server.begin();
}
void loop() {
server.handleClient();
}
#endif
// ================== ASSIGNMENT 4 ==================
#if 0
// assignment - 4
// LED toggle web UI
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
const int LED = 2;
bool state = false;
String page() {
return "<h2>Status: " + String(state ? "ON" : "OFF") +
"</h2><a href='/toggle'>Toggle</a>";
}
void toggle() {
state = !state;
digitalWrite(LED, state);
server.send(200, "text/html", page());
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
Serial.println(WiFi.localIP());
server.on("/", [](){ server.send(200,"text/html",page()); });
server.on("/toggle", toggle);
server.begin();
}
void loop() {
server.handleClient();
}
#endif
// ================== ASSIGNMENT 5 ==================
#if 0
// assignment - 5
// Servo control via web
/*
WOKWI:
Servo → GPIO 18
*/
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
Servo s;
int angle = 90;
String page() {
return "<form action='/set'>"
"<input type='range' name='a' min='0' max='180' value='" + String(angle) + "'>"
"<input type='submit'></form>";
}
void setAngle() {
angle = server.arg("a").toInt();
s.write(angle);
server.send(200,"text/html",page());
}
void setup() {
Serial.begin(115200);
s.attach(18);
WiFi.begin(ssid,password);
while (WiFi.status()!=WL_CONNECTED) delay(500);
Serial.println(WiFi.localIP());
server.on("/", [](){ server.send(200,"text/html",page()); });
server.on("/set", setAngle);
server.begin();
}
void loop() {
server.handleClient();
}
#endif
// ================== ASSIGNMENT 6 ==================
#if 0
// assignment - 6
// Basic web server
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WebServer server(80);
void setup() {
Serial.begin(115200);
WiFi.begin(ssid,password);
while (WiFi.status()!=WL_CONNECTED) delay(500);
Serial.println(WiFi.localIP());
server.on("/", [](){
server.send(200,"text/html","ESP32 Web Server Running");
});
server.begin();
}
void loop() {
server.handleClient();
}
#endif
// ================== ASSIGNMENT 7 ==================
#if 0
// assignment - 7
// ThingSpeak
/*
PUT YOUR API KEY
*/
#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT11
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* apiKey = "YOUR_API_KEY";
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid,password);
while (WiFi.status()!=WL_CONNECTED) delay(500);
}
void loop() {
float t = dht.readTemperature();
if (WiFi.status()==WL_CONNECTED) {
HTTPClient http;
String url = "http://api.thingspeak.com/update?api_key=" + String(apiKey) + "&field1=" + String(t);
http.begin(url);
http.GET();
http.end();
}
delay(20000);
}
#endif
// ================== ASSIGNMENT 9 ==================
#if 0
// assignment - 9
// MQTT
#include <WiFi.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
const char* ssid="Wokwi-GUEST";
const char* password="";
const char* server="broker.hivemq.com";
void callback(char* topic, byte* payload, unsigned int length) {
String msg;
for(int i=0;i<length;i++) msg+=(char)payload[i];
if(msg=="ON") digitalWrite(2,HIGH);
else digitalWrite(2,LOW);
}
void reconnect() {
while(!client.connected()) {
if(client.connect("esp32")) {
client.subscribe("wokwi/led");
} else delay(1000);
}
}
void setup() {
Serial.begin(115200);
pinMode(2,OUTPUT);
WiFi.begin(ssid,password);
while (WiFi.status()!=WL_CONNECTED) delay(500);
client.setServer(server,1883);
client.setCallback(callback);
}
void loop() {
if(!client.connected()) reconnect();
client.loop();
}
#endif
// ================== ASSIGNMENT 10 ==================
#if 0
// assignment - 10
// Home Automation
#include <WiFi.h>
#include <WebServer.h>
WebServer server(80);
void setup() {
Serial.begin(115200);
pinMode(16,OUTPUT);
pinMode(17,OUTPUT);
pinMode(19,OUTPUT);
WiFi.begin("Wokwi-GUEST","");
while(WiFi.status()!=WL_CONNECTED) delay(500);
Serial.println(WiFi.localIP());
server.on("/l1", [](){ digitalWrite(16,HIGH); server.send(200); });
server.on("/l2", [](){ digitalWrite(17,HIGH); server.send(200); });
server.on("/l3", [](){ digitalWrite(19,HIGH); server.send(200); });
server.begin();
}
void loop() {
server.handleClient();
}
#endif
// ================== ASSIGNMENT 13 ==================
#if 0
// assignment - 13
// Voice control (mock)
/*
Type ON / OFF in Serial Monitor
*/
const int LED = 2;
void setup() {
Serial.begin(115200);
pinMode(LED,OUTPUT);
}
void loop() {
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if(cmd=="ON") digitalWrite(LED,HIGH);
else if(cmd=="OFF") digitalWrite(LED,LOW);
}
}
#endif