#include <WiFi.h>
#include <ESP32Servo.h>
#include <SevSeg.h> // Include SevSeg library
const char* ssid = "Wokwi-GUEST";
const char* password = "";
WiFiServer server(80);
Servo myServo;
SevSeg sevseg; // Create a SevSeg object
int servoPin = 27;
int positions[] = {0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150, 165, 180};
float currentLevel = 3;
void setup() {
Serial.begin(9600);
// Connect to Wokwi-GUEST WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(ssid, password, 6);
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);
// SevSeg configuration
byte numDigits = 2;
byte digitPins[] = {17, 5}; // DIG1 and DIG2
byte segmentPins[] = {22, 23, 33, 19, 16, 18, 21, 26}; // A, B, C, D, E, F, G, DP
bool resistorsOnSegments = true;
byte hardwareConfig = COMMON_ANODE;
bool updateWithDelays = false;
bool leadingZeros = false;
bool disableDecPoint = false;
sevseg.begin(hardwareConfig, numDigits, digitPins, segmentPins, resistorsOnSegments, updateWithDelays, leadingZeros, disableDecPoint);
sevseg.setBrightness(90); // Set brightness
// Initial display setup
displayCurrentLevel();
}
void moveToLevel(float level) {
int index = (int)(level * 2);
if (index >= 0 && index < 13) {
int position = positions[index];
myServo.write(position);
Serial.print("Moved to position: ");
Serial.print(position);
Serial.print(" degrees (Level: ");
Serial.print(level);
Serial.println(")");
currentLevel = level;
displayCurrentLevel(); // Update the display with the current level
} else {
Serial.println("Level out of bounds!");
}
}
void handleIncrement(float increment) {
float newLevel = currentLevel + increment;
if (newLevel >= 0 && newLevel <= 6) {
moveToLevel(newLevel);
} else {
Serial.println("Level increment out of bounds!");
}
}
void displayCurrentLevel() {
int displayValue = (int)(currentLevel * 10); // Multiply by 10 to show 1 decimal place
sevseg.setNumber(displayValue, 1); // Display the value with 1 decimal point
sevseg.refreshDisplay(); // Refresh the display
}
void loop() {
sevseg.refreshDisplay(); // Continuously refresh the display
WiFiClient client = server.available();
if (client) {
String currentLine = "";
boolean levelReceived = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
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>");
client.println("<button onclick=\"sendData('dec')\">-</button>");
client.println("</div>");
client.println("<p class=\"status\" id=\"status\">Current Level: 3</p>");
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}`);");
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);
moveToLevel(level);
levelReceived = true;
}
}
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}
client.stop();
}
}