#include #include #include #include // Pin configuration const int oneWirePin = 5; // GPIO5 for DS18B20 const int ledPin = 2; // Built-in LED const int buttonTempPin = 15; // GPIO15 for temperature button const int buttonLedPin = 16; // GPIO16 for LED control button const int pin1 = 17; // Additional pin 1 const int pin2 = 18; // Additional pin 2 // WiFi client settings const char* ssid = "Black hole"; const char* password = "753oe762"; // Access Point settings const char* apSSID = "ESP32_Direct"; const char* apPassword = "12345678"; // Minimum 8 characters // Web server on port 80 WebServer server(80); // OneWire and DallasTemperature setup OneWire oneWire(oneWirePin); DallasTemperature sensors(&oneWire); // Variables float tempC = 0; bool ledState = false; bool pin1State = false; bool pin2State = false; unsigned long lastTempUpdate = 0; const long tempUpdateInterval = 10000; // Button states int lastButtonTempState = HIGH; int lastButtonLedState = HIGH; unsigned long lastDebounceTime = 0; const long debounceDelay = 50; void setup() { Serial.begin(115200); // Initialize pins pinMode(ledPin, OUTPUT); pinMode(pin1, OUTPUT); pinMode(pin2, OUTPUT); pinMode(buttonTempPin, INPUT_PULLUP); pinMode(buttonLedPin, INPUT_PULLUP); digitalWrite(ledPin, LOW); digitalWrite(pin1, LOW); digitalWrite(pin2, LOW); // Initialize temperature sensor sensors.begin(); // Start both modes simultaneously initWiFi(); // Configure web server server.on("/", handleRoot); server.on("/temperature", handleTemperature); server.on("/led", handleLed); server.on("/pin1", handlePin1); server.on("/pin2", handlePin2); server.begin(); Serial.println("HTTP server started"); } void initWiFi() { // Start Access Point mode WiFi.softAP(apSSID, apPassword); Serial.println(); Serial.println("AP Mode Activated"); Serial.print("AP SSID: "); Serial.println(apSSID); Serial.print("AP Password: "); Serial.println(apPassword); Serial.print("AP IP address: "); Serial.println(WiFi.softAPIP()); // Simultaneously try to connect to router WiFi.begin(ssid, password); Serial.print("Connecting to WiFi network..."); // Don't wait for connection to avoid blocking // Connection will happen in background } void loop() { server.handleClient(); // Update temperature if (millis() - lastTempUpdate >= tempUpdateInterval) { updateTemperature(); } // Handle buttons handleButtons(); delay(10); } void updateTemperature() { sensors.requestTemperatures(); tempC = sensors.getTempCByIndex(0); lastTempUpdate = millis(); Serial.print("Temperature updated: "); Serial.print(tempC); Serial.println(" °C"); } void handleButtons() { // Read button states with debounce int currentButtonTempState = digitalRead(buttonTempPin); int currentButtonLedState = digitalRead(buttonLedPin); if ((millis() - lastDebounceTime) > debounceDelay) { // Temperature button handling if (currentButtonTempState != lastButtonTempState) { lastButtonTempState = currentButtonTempState; if (currentButtonTempState == LOW) { Serial.println("Temperature button pressed"); updateTemperature(); Serial.print("Current temperature: "); Serial.print(tempC); Serial.println(" °C"); } } // LED button handling if (currentButtonLedState != lastButtonLedState) { lastButtonLedState = currentButtonLedState; if (currentButtonLedState == LOW) { ledState = !ledState; digitalWrite(ledPin, ledState ? HIGH : LOW); Serial.print("LED turned "); Serial.println(ledState ? "ON" : "OFF"); } } } if (currentButtonTempState != lastButtonTempState || currentButtonLedState != lastButtonLedState) { lastDebounceTime = millis(); } } String getConnectionInfo() { String info; if (WiFi.status() == WL_CONNECTED) { info += "
"; info += "

Router Connection

"; info += "SSID: " + String(ssid) + "
"; info += "IP address: " + WiFi.localIP().toString() + "
"; info += "Signal strength: " + String(WiFi.RSSI()) + " dBm"; info += "
"; } else { info += "
"; info += "

Router Connection

"; info += "Status: Not connected"; info += "
"; } info += "
"; info += "

Direct Connection

"; info += "SSID: " + String(apSSID) + "
"; info += "Password: " + String(apPassword) + "
"; info += "IP address: " + WiFi.softAPIP().toString() + ""; info += "
"; return info; } void handleRoot() { String html = ""; html += "ESP32 Dual Mode Control Panel"; html += ""; html += ""; html += ""; html += "

ESP32 Dual Mode Control Panel

"; // Connection information html += getConnectionInfo(); html += "
Current temperature: " + String(tempC) + " °C
"; // Temperature button html += ""; // LED button html += "
LED: " + String(ledState ? "ON" : "OFF") + "
"; html += ""; // Pin1 button html += "
GPIO17: " + String(pin1State ? "ON" : "OFF") + "
"; html += ""; // Pin2 button html += "
GPIO18: " + String(pin2State ? "ON" : "OFF") + "
"; html += ""; html += ""; server.send(200, "text/html", html); } void handleTemperature() { updateTemperature(); server.sendHeader("Location", "/"); server.send(303); } void handleLed() { ledState = !ledState; digitalWrite(ledPin, ledState ? HIGH : LOW); server.sendHeader("Location", "/"); server.send(303); } void handlePin1() { pin1State = !pin1State; digitalWrite(pin1, pin1State ? HIGH : LOW); server.sendHeader("Location", "/"); server.send(303); } void handlePin2() { pin2State = !pin2State; digitalWrite(pin2, pin2State ? HIGH : LOW); server.sendHeader("Location", "/"); server.send(303); }