Hello. I have been tinkering with a project for the past 3 or 4 weeks and am currently hitting a brick wall and wondering if someone can point me in the right direction or tell me that I'm doing it completely wrong .
My end goal of this project is to have an ESP8266 respond to an HTTP request which will run some effects on 5meters of RGB LED strip using the Adafruit NeoPixel library (or similar library).
Each different HTTP request/URL should trigger a different effect. And the "effect" will repeat over and over until a different HTTP request/URL is triggered externally (I have a system for that already).
My sketch currently is just using a breadboard and a couple of LEDs as a proof of concept before moving on to the full Adafruit library and connecting the LED strip.
The code below successfully connects to WiFi, get's an IP address, and runs the HTTP server.
I am able to call the URLs listed to switch the on board LED on and off successfully. And I am able to use the URL examples of /BlinkSlow and /BlinkFast to run the blink program, but it only ones the program once and doesn't repeat (as expected since there is no loop).
While running these programs/functions they block any subsequent requests over HTTP etc as they are using the "delay" command which is tying up the CPU etc. This means that a new URL cannot be triggered until the program gets to the end. Also the end goal is that the program will repeat so will need to be interrupted.
I have studied the Blink without Delay code and I have a program/function setup on /BlinkLoopA however this crashes and reboots the ESP every time the URL is triggered.
Could someone please advise where I might be going wrong with this or if this is the wrong approach?
Eventually I would like to have no more the 10 URLs on the setup, and on calling each URL from the browser it'll run (and repeat) the given effect over and over until a different URL is called to start a different effect.
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#ifndef STASSID
#define STASSID "mywifi"
#define STAPSK "mywifipass"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
const int led = 13;
// The root website page that is displayed when browsing to the IP of the ESP over the local network
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "The web server is running\r\n");
digitalWrite(led, 0);
}
//Display error if URL isn't found
void handleNotFound() {
digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++) {
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
digitalWrite(led, 0);
}
//BlinkLoopA test without delay code
int ledStateA = LOW;
unsigned long previousMillisA = 0;
const long intervalA = 500;
const unsigned int LED_PINA = 5;
void setup(void) {
pinMode(LED_PINA, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print connection details
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
// Switch the Onboard LED On
server.on("/led-on", []() {
server.send(200, "text/plain", "LED is ON");
digitalWrite(LED_BUILTIN, LOW); // Turn the LED on
Serial.print("LED is ON ");
});
// Switch the onboard LED Off
server.on("/led-off", []() {
server.send(200, "text/plain", "LED is OFF");
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is OFF ");
});
// Actions for various URLs that will be called by HTTP and trigger the "Programs" listed below.
server.on("/BlinkSlow", HTTP_GET, handleBlinkSlow);
server.on("/BlinkFast", HTTP_GET, handleBlinkFast);
server.on("/BlinkLoopA", HTTP_GET, handleBlinkLoopA);
server.on("/ProgramB", HTTP_GET, handleProgramB);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
MDNS.update();
}
// An example of a program using "delay" that will blink the onboard LED every 5 seconds for 3 times and then stop.
void handleBlinkSlow() {
Serial.println("...................");
Serial.println("BlinkSlow started");
server.send(200, "text/plain", "BlinkSlow Starting");
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK ON\n");
delay(5000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK OFF\n");
delay(5000);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK ON\n");
delay(5000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK OFF\n");
delay(5000);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK ON\n");
delay(5000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK OFF\n");
delay(5000);
};
// An example of a program using "delay" that will blink the onboard LED every 1 seconds for 3 times and then stop.
void handleBlinkFast() {
Serial.println("...................");
Serial.println("BlinkFast started");
server.send(200, "text/plain", "BlinkFast Starting");
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK FAST ON\n");
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK FAST OFF\n");
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK FAST ON\n");
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK FAST OFF\n");
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
Serial.print("LED is BLINK FAST ON\n");
delay(1000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("LED is BLINK FAST OFF\n");
delay(1000);
};
// An exmaple of a BlinkLoop without using Delay - this program currently causes the ESP to crash
void handleBlinkLoopA() {
Serial.println("handleBlinkLoopA started");
server.send(200, "text/plain", "handleBlinkLoopA started");
for(;;) {
unsigned long currentMillisA = millis();
if (currentMillisA - previousMillisA >= intervalA) {
previousMillisA = currentMillisA;
if (ledStateA == LOW) {
ledStateA = HIGH; // Note that this switches the LED *off*
} else {
ledStateA = LOW; // Note that this switches the LED *on*
}
digitalWrite(LED_PINA, ledStateA);
}
}
}
//An placeholder for future "programs".
void handleProgramB() {
// Repeat some code here without blocking other URLs to be called (which would stop this "program").
}