Hello,
I’m trying to make Blinking a LED with a web server on my Adafruit Feather HUZZAH ESP8266 with JavaScript when I push a button on my web interface. The problem is when I push the button the LED won’t blink (at least just a little, will be on for a couple ms). I’m using the ESPAsyncWebServer library and this my code to make the LED blinked:
Relevant C++ code within the setup function (I have nothing within my loop function):
server.on("/blue", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(led, LOW);
delay(1000);
request->send(200);
Serial.println(digitalRead(led));
digitalWrite(led, HIGH);
delay(1000);
Serial.println(digitalRead(led));
request->send(200);
});
JavaScript code:
function blueButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "blue", true);
xhttp.send();
}
It’s like the delays of the C++ code don’t do anything.
Thanks,
Does the serial output indicate 0
and 1
after 1 second?
Yes it indicates 0 and 1 but I don’t think after 1 second because I tried to put 5 second and it doesn’t change anything, it is like the delays are ignored. In fact, the LED “blink”, not with a delay of 1 second and with a very low brightness.
Can you show the complete sketch please (without WiFi credentials)?
C++ code:
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFSEditor.h>
#include <FS.h>
const char *ssid = "wifissd";
const char *password = "password";
const int led = 0;
AsyncWebServer server(80);
void setup() {
//##################################
// Serial
//##################################
Serial.begin(115200);
while(!Serial){}
Serial.println("\n");
//##################################
// GPIO
//##################################
pinMode(led, OUTPUT);
digitalWrite(led, LOW);
//##################################
// SPIFFS
//##################################
if(!SPIFFS.begin())
{
Serial.println("SPIFFS Error...");
return;
}
//##################################
// WIFI
//##################################
WiFi.begin(ssid, password);
Serial.print("Connection in progress...");
while(WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(100);
}
Serial.println("\n");
Serial.println("Connection established!");
Serial.print("IP Adresse: ");
Serial.println(WiFi.localIP());
//##################################
// Server
//##################################
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/index.html", "text/html");
});
server.on("/w3.css", HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/w3.css", "text/css");
});
server.on("/script.js", HTTP_GET, [](AsyncWebServerRequest *request)
{
request->send(SPIFFS, "/script.js", "text/javascript");
});
server.on("/blue", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(led, LOW);
delay(1000);
request->send(200);
Serial.println(digitalRead(led));
digitalWrite(led, HIGH);
delay(1000);
Serial.println(digitalRead(led));
request->send(200);
});
server.on("/red", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(led, LOW);
request->send(200);
});
server.on("/green", HTTP_GET, [](AsyncWebServerRequest *request)
{
digitalWrite(led, HIGH);
request->send(200);
});
server.begin();
Serial.println("Server actif!");
}
void loop() {
}
JavaScript code:
function blueButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "blue", true);
xhttp.send();
}
function greenButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "green", true);
xhttp.send();
}
function redButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "red", true);
xhttp.send();
}
function whiteButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "white", true);
xhttp.send();
}
function restartButton() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "restart", true);
xhttp.send();
}
Thanks