hello been trying to get the sketch to work chat gpt gave me most of it,basically everything is working good except the gpio isnt turning on or off i am using the on board pin,the led is working under normal circumstances its not broken, could you guys look at my code and see what is wrong? many thanks
`#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
const char *ssid = "NETGEAR123";
const char *password = "password1";
const int led1Pin = 2; // Replace with the GPIO pin connected to LED 1
const int led2Pin = 4; // Replace with the GPIO pin connected to LED 2
bool led1State = false;
bool led2State = false;
ESP8266WebServer server(80);
void setup() {
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
digitalWrite(led1Pin, HIGH); // High is off, Low is on
digitalWrite(led2Pin, HIGH);
Serial.begin(115200);
WiFi.softAP(ssid, password);
// Print the IP address to the Serial Monitor
Serial.print("Access Point started. IP Address: ");
Serial.println(WiFi.softAPIP());
server.on("/", HTTP_GET, handleRoot);
server.on("/led1", HTTP_GET, handleLedState);
server.on("/led2", HTTP_GET, handleLedState);
server.begin();
}
void loop() {
server.handleClient();
// Add code here for any other tasks or sensors
}
void handleRoot() {
String html = "";
html += "body { font-family: 'Arial', sans-serif; margin: 0; padding: 0; background-color: #f4f4f4; }";
html += "header { background-color: #333; color: white; text-align: center; padding: 1em; }";
html += "main { display: flex; justify-content: center; align-items: center; height: 80vh; }";
html += ".control-panel { background-color: white; padding: 20px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); text-align: center; }";
html += "button { width: 150px; height: 75px; margin: 10px; font-size: 16px; cursor: pointer; border: none; border-radius: 5px; transition: background-color 0.3s ease; }";
html += "button:hover { background-color: #ddd; }";
html += ".led-indicator { width: 20px; height: 20px; display: inline-block; margin-left: 5px; border-radius: 50%; }";
html += ".led-on { background-color: red; }";
html += ".led-off { background-color: green; }";
html += "
esp led menu
html += "
Control Panel:
LED 1: Turn On";
html += "";
html += "Turn Off
html += "
LED 2: Turn On";
html += "";
html += "Turn Off
html += "";
server.send(200, "text/html", html);
}
void handleLedState() {
String ledNumberStr = server.arg("led");
String stateStr = server.arg("state");
Serial.println("Received LED request. LedNumber: " + ledNumberStr + ", State: " + stateStr);
if (ledNumberStr == "1") {
led1State = (stateStr == "on");
digitalWrite(led1Pin, led1State ? HIGH : LOW);
Serial.println("LED 1 State: " + String(led1State ? "ON" : "OFF"));
} else if (ledNumberStr == "2") {
led2State = (stateStr == "on");
digitalWrite(led2Pin, led2State ? HIGH : LOW);
Serial.println("LED 2 State: " + String(led2State ? "ON" : "OFF"));
}
server.send(200, "text/plain", "OK");
}`