#include <WiFi.h>
#include <ESP32Servo.h> // Use ESP32Servo library instead of Arduino Servo
const char* ssid = "Wokwi-GUEST";
const char* password = ""; // No password required for Wokwi-GUEST
WiFiServer server(80);
Servo myServo; // Create a servo object
int servoPin = 27; // GPIO pin connected to the servo
int positions[] = {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180}; // Defined positions for each level
float currentLevel = 3; // Start at level 3
void setup() {
Serial.begin(9600);
// Connect to Wokwi-GUEST WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, 6); // Use channel 6 for Wokwi-GUEST
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Print the local IP address
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
// Start the server
server.begin();
Serial.println("Server started");
// Attach the servo to the GPIO pin
myServo.attach(servoPin);
moveToLevel(currentLevel); // Move to the initial level
Serial.println("Current Level: 3");
}
void moveToLevel(float level) {
// Mapping the level to the correct index
int index = (int)(level * 2); // Multiply by 2 to map 0.5 increments to 1 index
if (index >= 0 && index < 13) { // Ensure the index is within bounds
int position = positions[index]; // Get the corresponding position
myServo.write(position); // Move the servo
Serial.print("Moved to position: ");
Serial.print(position);
Serial.print(" degrees (Level: ");
Serial.print(level);
Serial.println(")");
currentLevel = level; // Update the current level
} else {
Serial.println("Level out of bounds!"); // Error handling if level is out of range
}
}
void handleIncrement(float increment) {
float newLevel = currentLevel + increment;
if (newLevel >= 0 && newLevel <= 6) {
moveToLevel(newLevel); // Move to the new level if within bounds
} else {
Serial.println("Level increment out of bounds!");
}
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) {
String currentLine = "";
boolean levelReceived = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
// HTTP response
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
// Start of HTML content
client.println("<!DOCTYPE html>");
client.println("<html lang=\"en\">");
client.println("<head>");
client.println("<meta charset=\"UTF-8\">");
client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
client.println("<title>Oxygen Control System</title>");
client.println("<style>");
client.println("body { font-family: Arial, sans-serif; text-align: center; }");
client.println("h1 { margin-bottom: 20px; }");
client.println(".button-container { display: flex; flex-direction: column; align-items: center; gap: 15px; }");
client.println("button { padding: 15px 30px; font-size: 24px; width: 200px; }");
client.println(".status { margin-top: 20px; font-size: 20px; color: green; }");
client.println("</style>");
client.println("</head>");
client.println("<body>");
client.println("<h1>Oxygen Controller</h1>");
client.println("<div class=\"button-container\">");
client.println("<button onclick=\"sendData(0)\">Off</button>");
client.println("<button onclick=\"sendData(1)\">1</button>");
client.println("<button onclick=\"sendData(2)\">2</button>");
client.println("<button onclick=\"sendData(3)\">3</button>");
client.println("<button onclick=\"sendData(4)\">4</button>");
client.println("<button onclick=\"sendData(5)\">5</button>");
client.println("<button onclick=\"sendData(6)\">6</button>");
client.println("<button onclick=\"sendData('reset')\">Reset</button>");
client.println("<button onclick=\"sendData('inc')\">+</button>"); // Increment button
client.println("<button onclick=\"sendData('dec')\">-</button>"); // Decrement button
client.println("</div>");
client.println("<p class=\"status\" id=\"status\">Current Level: 3</p>");
// JavaScript for handling button clicks and feedback
client.println("<script>");
client.println("function sendData(level) {");
client.println("let currentLevel = parseFloat(document.getElementById('status').textContent.split(' ')[2]);");
client.println("if (level === 'inc') {");
client.println(" level = (currentLevel < 6) ? currentLevel + 0.5 : currentLevel;");
client.println("} else if (level === 'dec') {");
client.println(" level = (currentLevel > 0) ? currentLevel - 0.5 : currentLevel;");
client.println("} else if (level === 'reset') {");
client.println(" level = 3;");
client.println("}");
client.println("document.getElementById('status').textContent = `Current Level: ${level}`;");
client.println("fetch(`/sendData?level=${level}`);"); // Send the request to update the level (no need to handle response)
client.println("}");
client.println("</script>");
client.println("</body>");
client.println("</html>");
client.println();
break;
} else {
if (currentLine.indexOf("GET /sendData?level=") >= 0) {
int levelStart = currentLine.indexOf("level=") + 6;
String levelStr = currentLine.substring(levelStart);
if (!levelReceived) {
float level = levelStr.toFloat();
Serial.print("Received level: ");
Serial.println(level);
// Move the servo to the corresponding position
moveToLevel(level); // Call the mapping function to move the servo
levelReceived = true;
}
}
currentLine = ""; // Clear the current line after processing
}
} else if (c != '\r') {
currentLine += c; // Add the character to the current line string
}
}
}
// Close the connection
client.stop();
}
}