Esp8266 webserver button control to run code

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() {


  pinMode(lightLED, OUTPUT);

  digitalWrite(lightLED, LOW);

  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
// Print local IP address and start web server
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");

  Serial1.begin(115200);  // <- Alt. if using hardware serial port

  strip.setBrightness(150);  // Adjust as necessary to avoid blinding

void light(); {
  int i;
  for(i=0; i< NUMPIXELS; i++)
    strip.setPixelColor(i, 255, 147, 41);;
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 =; // 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("Connection: close");

// 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>");

// The HTTP response ends with another blank line
// Break out of the while loop
} 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
Serial.println("Client disconnected.");
void light();

Have a look at the example "Button" sketch.

In the IDE >File >Examples > 02. Digital >Button

I'm having a difficult time seeing how that's the least bit relevant to the question of responding to the click of a "button" on a web page.

@ kidkill, there are lots of tutorials / videos of various sophistication levels on the web. Suggest you do a Google search and look over a few.

I am surprised if it compiles.
Use CTRL-T to properly format the code and the problem becomes very obvious.

You are defining the function light() inside the setup function.

The light function runs 1 time at startup, then doesn't run anymore. Where does this function belong?

But, it shouldn't even compile.
If it executes in setup(), it's because the compiler thinks this is setup code.
But, what you posted should not compile without errors.

Go to "file" then "preferences" and turn on all warnings:

In my opinion this should be the default setting. Fix warnings and you will be a better programmer for it.

It doesn't really matter where you define functions, except that it can't be inside another function. The easiest place to put it is at the end of the sketch.

Thanks for the help. I don't know if this makes any difference but it runs once when it connects to the wifi.

See my reply #2.

So i've moved the light function out of the setup function, and it works! Thanks again for the help. Now, the light comes on but only stays on for the 300 delay, how can I get it to stay on?

Post your current code. Please read the instructions for posting at the top of each forum and edit your post to correctly display the code. The forum editor doesn't keep the sketch formatting without code tags.

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