I have two codes. One working just fine without wifi with FastLED on my ESP8266, and the same code with the addition of a WifiServer to get word inputs from my PC. The wifi code works just fine in Serial, and the FastLED code works just fine without Wifi. When I mix the two my LEDs do not behave properly. My suspicion is the D4 pin has something to do with Wifi and adds noise to my LED signal. I'm pretty sure this is possible since I used the same ESP8266 with Wled before which is the same thing I'm trying to do but with my script instead of flashing Wled.
My goal is to create lights like in stranger things. So the message is displayed by the LEDs in sequence of blinking the letters A-0, B-1, C-2, etc. The message is sent over Wifi or in the no wifi version just entered into the code.
What would cause the LEDs to not work when Wifi is enabled here?
Working Code without Wifi:
#include <FastLED.h>
bool displayingMsg = true;
// LED strip settings
#define LED_PIN 2 // D4 is GPIO2
#define NUM_LEDS 26
#define BRIGHTNESS 200
#define CHIPSET WS2811
CRGB leds[NUM_LEDS];
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(1000);
Serial.println("Starting");
// Setup LED strip
FastLED.addLeds<CHIPSET, LED_PIN, RGB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
Serial.println("LED setup complete.");
}
void loop() {
// put your main code here, to run repeatedly:
displayingMsg = true;
//Serial.println(message);
while (displayingMsg) {
displayMessage("Led test"); //led test replaces the message that would be sent by Wifi
}
delay(500000); // long delay since it would loop otherwise
}
void displayMessage(const char* message) {
Serial.print("the message ");
Serial.println(message);
for (int i = 0; message[i] != '\0'; i++) {
displayLetter(message[i]);
FastLED.show();
delay(1000);
FastLED.clear();
FastLED.show();
delay(1000);
}
displayingMsg = false;
FastLED.clear();
FastLED.show();
}
void displayLetter(char letter) {
Serial.print("Display Letter ");
Serial.println(letter);
int ledIndex = getLEDIndexForLetter(letter);
if (ledIndex != -1) {
leds[ledIndex] = CRGB::White;
Serial.println(leds[ledIndex].r);
}
}
int getLEDIndexForLetter(char letter) {
Serial.print("getting index ");
letter = toupper(letter);
if (letter < 'A' || letter > 'Z') {
return -1;
}
int n = letter - 'A';
Serial.println(n);
return n;
}
Same code with WifiServer added:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <FastLED.h>
// LED strip settings
#define LED_PIN 2 // D4 is GPIO2
#define NUM_LEDS 26
#define BRIGHTNESS 200
#define CHIPSET WS2811
CRGB leds[NUM_LEDS];
// Wi-Fi credentials
const char* ssid = "WiFi";
const char* password = "Password";
// Web server on port 80
ESP8266WebServer server(80);
// Global variable to store the last message entered
char lastMessage[256] = ""; // Allows for up to 255 characters + null terminator
bool displayingMsg = false; //tracking if message playing
// Function to handle the root page and display the input form
void handleRoot() {
String html = "<html><head><title>ESP8266 String Input</title></head><body>";
html += "<h1>Enter a Message</h1>";
html += "<form action='/setMessage' method='GET'>";
html += "Message: <input type='text' name='message' maxlength='255'>"; // Accept up to 255 characters
html += "<input type='submit' value='Submit'>";
html += "</form>";
// Show the last entered message
html += "<p>Last message entered: <strong>";
html += String(lastMessage);
html += "</strong></p>";
html += "</body></html>";
server.send(200, "text/html", html);
}
// Function to handle the /setMessage request
void handleSetMessage() {
if (server.hasArg("message")) {
String messageInput = server.arg("message");
messageInput.toCharArray(lastMessage, 256); // Convert the String to a char array and store it
displayingMsg = true;
}
// Redirect to the root after processing input to allow for new input
server.sendHeader("Location", "/"); // This redirects the user to the root page ("/")
server.send(302); // Send the 302 status code for redirection
}
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to WiFi! IP address: ");
Serial.println(WiFi.localIP());
// Set up web server routes
server.on("/", handleRoot); // Root page to display the form and last message
server.on("/setMessage", handleSetMessage); // Handle message submission
// Start the server
server.begin();
Serial.println("Web server started.");
// Setup LED strip
FastLED.addLeds<CHIPSET, LED_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness(BRIGHTNESS);
FastLED.clear();
FastLED.show();
Serial.println("LED setup complete.");
FastLED.clear();
FastLED.show();
}
void loop() {
// Handle client requests
server.handleClient();
delay(3000);
Serial.println("Inside Loop");
Serial.println(lastMessage);
if (displayingMsg) {
displayMessage(lastMessage);
}
}
void displayMessage(const char* message) {
Serial.print("the message ");
Serial.println(message);
for (int i = 0; message[i] != '\0'; i++) {
displayLetter(message[i]);
FastLED.show();
delay(1000);
FastLED.clear();
FastLED.show();
delay(1000);
}
displayingMsg = false;
FastLED.clear();
FastLED.show();
}
void displayLetter(char letter) {
Serial.print("Display Letter ");
Serial.println(letter);
int ledIndex = getLEDIndexForLetter(letter);
if (ledIndex != -1) {
leds[ledIndex] = CRGB::Blue;
}
}
int getLEDIndexForLetter(char letter) {
Serial.print("getting index ");
letter = toupper(letter);
if (letter < 'A' || letter > 'Z') {
return -1;
}
int n = letter - 'A';
Serial.println(n);
return n;
}