Hello, I'm brand new here so apologies if I'm doing something wrong. I am trying to get a web server setup to control an Adafruit pixie for a christmas tree star light. I get the server and button online and talking but I don't know how to get the "void light();" to run on the button press. Any help would be appreciated.
#include <ESP8266WiFi.h>
const char* ssid = "xxxx";
const char* password = "xxxx";
WiFiServer server(80);
String header;
String outputLightState = "off";
const int lightLED = 2;
unsigned long currentTime = millis();
unsigned long previousTime = 0;
const long timeoutTime = 2000;
#include "Adafruit_Pixie.h"
#define pixieSerial Serial1
#define NUMPIXELS 1 // Number of Pixies in the strip
Adafruit_Pixie strip = Adafruit_Pixie(NUMPIXELS, &Serial1);
void setup() {
Serial.begin(115200);
pinMode(lightLED, OUTPUT);
digitalWrite(lightLED, LOW);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
Serial1.begin(115200); // <- Alt. if using hardware serial port
strip.setBrightness(150); // Adjust as necessary to avoid blinding
void light(); {
int i;
Serial.println("THIS");
for(i=0; i< NUMPIXELS; i++)
strip.setPixelColor(i, 255, 147, 41);
strip.show();
delay(300);
}
}
void loop() {
WiFiClient client = server.available(); // Listen for incoming clients
if (client) { // If a new client connects,
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
currentTime = millis();
previousTime = currentTime;
while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
currentTime = millis();
if (client.available()) { // if there's bytes to read from the client,
char c = client.read(); // read a byte, then
Serial.write(c); // print it out the serial monitor
header += c;
if (c == '\n') { // if the byte is a newline character
// if the current line is blank, you got two newline characters in a row.
// that's the end of the client HTTP request, so send a response:
if (currentLine.length() == 0) {
// HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
// and a content-type so the client knows what's coming, then a blank line:
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println("Connection: close");
client.println();
// turns the GPIOs on and off
if (header.indexOf("GET /2/on") >= 0) {
Serial.println("Light LED is on");
outputLightState = "on";
digitalWrite(lightLED, HIGH);
void light();
} else if (header.indexOf("GET /2/off") >= 0) {
Serial.println("Light LED is off");
outputLightState = "off";
digitalWrite(lightLED, LOW);
}
// Display the HTML web page
client.println("<!DOCTYPE html><html>");
client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
client.println("<link rel=\"icon\" href=\"data:,\">");
// CSS to style the on/off buttons
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".buttonRed { background-color: #ff0000; border: none; color: white; padding: 16px 40px; border-radius: 60%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".buttonOff { background-color: #77878A; border: none; color: white; padding: 16px 40px; border-radius: 70%;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}</style></head>");
// Web Page Heading
client.println("<body><h1>My LED Control Server</h1>");
// Display current state, and ON/OFF buttons for GPIO 2 Red LED
client.println("<p>Light LED is " + outputLightState + "</p>");
// If the outputRedState is off, it displays the OFF button
if (outputLightState=="off") {
client.println("<p><a href=\"/2/on\"><button class=\"button buttonOff\">OFF</button></a></p>");
} else {
client.println("<p><a href=\"/2/off\"><button class=\"button buttonRed\">ON</button></a></p>");
}
client.println("</body></html>");
// The HTTP response ends with another blank line
client.println();
// Break out of the while loop
break;
} else { // if you got a newline, then clear currentLine
currentLine = "";
}
} else if (c != '\r') { // if you got anything else but a carriage return character,
currentLine += c; // add it to the end of the currentLine
}
}
}
// Clear the header variable
header = "";
// Close the connection
client.stop();
Serial.println("Client disconnected.");
Serial.println("");
}
void light();
}