/* 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>
#include <ESP8266mDNS.h> // Include the mDNS library
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
WebServer server(80);
const int RELAY = 15;
bool _isClosed = false;
void setup(void) {
Serial.begin(115200);
pinMode(RELAY, OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
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());
server.on(UriBraces("/status"), []() {
Serial.println("Reporting status...");
String response = R"(
<!DOCTYPE html><html><head><title>Status</title></head>
<body>OPEN_CLOSED_STATUS</body>
</html>
)";
response.replace("OPEN_CLOSED_STATUS", _isClosed ? "Closed" : "Open");
server.send(200, "text/html", response);
});
server.on(UriBraces("/pulse"), []() {
Serial.println("Pulse relay...");
digitalWrite(RELAY, HIGH);
delay(1000);
digitalWrite(RELAY, LOW);
String response = R"(
<!DOCTYPE html><html><head><title>Response</title></head>
<body>Pulsed</body>
</html>
)";
server.send(200, "text/html", response);
});
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
delay(100);
}