Gpio pin not turning on

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");
}`

Did you (or ChatGPT) really mean:

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

or:

const int led1Pin = D2; // Replace with the D# pin connected to LED 1
const int led2Pin = D4; // Replace with the D# pin connected to LED 2

And please, please, please, enclose your code in a code block (``` on lines by themselves before and after)? It hurts to look at that unformatted mess.

have a read of how-to-get-the-best-out-of-this-forum
in particular

  1. what specific microcontroller are you using
  2. as it is the code is unreadable - format the code by selecting < CODE/ > and paste text where it says “type or paste code here”
  3. upload a schematic of the circuit

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

im sorry about that still getting the hang of the forum ive tried the physical and digital pin numbers no success but if i simply just digital write pin the pins to turn on it will,only when i try to control it via html webserver it does nada but the serial commands come back like it should,ive also tried connecting it to a wall outlet power source doesnt work either

As the next step please

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");}

i tried formatting this code please let me know if i didnt do it right im still getting used to this forum

You didn't get it right.

I copied it into the IDE, used Tools/Auto Format to tidy up its layout, then Edit/Copy for Forum and pasted it below. Try it yourself sometime

#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");
}

No I will not for the following reasons:

It is pretty easy to follow the tutorial how to post code as a code-section. You seem to have not read it.
You did not even explain in normal words what you want your code to do.
This means that your potential helpers would have to analyse the chatGPT-code
and then do a lot of assumptions where these assumptions about what the code shall do and are likely to be still wrong.

I'm pretty sure that you agree and will follow the way how to solve your problem mimimum 200 minutes faster.
This requires to invest 20 minutes of your precious time to read how to speedup solving your problems.

You should really read it.

Additionally you can read

best regards Stefan

fuck you then

like i said im new at this i thought you UK people are supposed to be chipper and "top of the morning to ya" , fuck you too helicopter enjoy your rancid teeth

@do_not_sleep will no longer be participating in the forum

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.