My esp2866 is not reciving stuff correctly

Hello everyone, I am new to this type of stuff. I wanted to create a remote control for my project using the internet as testing. I made code in JS to send a variable to the ESP server to tell the built-in light to turn off or turn on. I am sure that my JS code is working correctly, but my ESP code, I am not sure if the light turns on and off either by random or extreme slowness, and when it does turn off, it does this for half a second. Can anyone help a newbie?

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>


AsyncWebServer server(80);
String idd = "XXXXX ";
String pass = "XXXX"; // this is for privacy reasons;
bool forward = false;
// the setup function runs once when you press reset or power the board
void setup()
{
  Serial.begin(9600);

  pinMode(LED_BUILTIN, OUTPUT);

  WiFi.begin(idd, pass);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.print("!!!");

  Serial.print(WiFi.localIP());
      // Initialize server and register routes
  server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {

    if (request->hasParam("forward")) 
    {
        String forwardParam = request->getParam("forward")->value();
        if (forwardParam == "true") {
            forward = true;
        } else if (forwardParam == "false") {
            forward = false;
        }
    }
  });

  server.begin();
  
}

// the loop function runs over and over again forever
void loop() 
{  
    
    digitalWrite(LED_BUILTIN, forward);
    
}

Well depending a bit on the ESP8266 board you are using, but most of them have a LED on GPIO 2 and it is 'active LOW' so when you say 'false' that would turn it on.
Secondly. digitalWrite() takes an int as an argument. LOW is defined as '0' and HIGH is defined as '1' but that could change.
Anyway it is probably a good idea to just do the digitalWrite when something has changed.

void loop() 
{  
    static bool oldForward = false;
    if (forward != oldForward) {
      // Serial.println("Variable change");   // for debugging
      if (forward)  digitalWrite(LED_BUILTIN, LOW);
      else digitalWrite(LED_BUILTIN, HIGH);
      oldForward = forward;
    }
}

Also how are you making the GET request ? There is no page sent back.

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