Skip to content

Instantly share code, notes, and snippets.

@brennancheung
Created February 6, 2019 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brennancheung/959449e0e423f5dfffd51db86b86558c to your computer and use it in GitHub Desktop.
Save brennancheung/959449e0e423f5dfffd51db86b86558c to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "password";
WiFiServer server(80);
String header;
int gpio_pins [] = { D0, D1, D2 };
int pin_states [] = { HIGH, HIGH, HIGH };
WiFiClient client;
void setPin(int pin, int state) {
Serial.print("Setting pin ");
Serial.print(pin);
Serial.print(" to ");
Serial.print(state == HIGH ? "off" : "on");
Serial.println("");
digitalWrite(gpio_pins[pin], state);
pin_states[pin] = state;
}
void pinControl(int pin) {
int state = pin_states[pin];
String colors [] = { "red", "green", "blue" };
String stateStr = state == HIGH ? "ON" : "OFF";
String toggleStr = state == HIGH ? "off" : "on";
String str = String("") +
"<p>" + colors[pin] + "</p>" +
"<p><a href=\"/" + pin + "/" + toggleStr + "\"><button class=\"button\">" + stateStr + "</button></a></p>";
if (client) { client.println(str); }
}
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address:");
Serial.println(WiFi.localIP());
server.begin();
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
setPin(0, HIGH);
setPin(1, HIGH);
setPin(2, HIGH);
}
// the loop function runs over and over again forever
void loop(){
client = server.available(); // Listen for incoming clients
if (!client) { return; }
Serial.println("New Client."); // print a message out in the serial port
String currentLine = ""; // make a String to hold incoming data from the client
while (client.connected()) { // loop while the client's connected
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 /0/on") >= 0) { setPin(0, HIGH); }
if (header.indexOf("GET /0/off") >= 0) { setPin(0, LOW); }
if (header.indexOf("GET /1/on") >= 0) { setPin(1, HIGH); }
if (header.indexOf("GET /1/off") >= 0) { setPin(1, LOW); }
if (header.indexOf("GET /2/on") >= 0) { setPin(2, HIGH); }
if (header.indexOf("GET /2/off") >= 0) { setPin(2, 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
// Feel free to change the background-color and font-size attributes to fit your preferences
client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
client.println(".button2 {background-color: #77878A;}</style></head>");
// Web Page Heading
client.println("<body><h1>RGB LED Strip Control</h1>");
pinControl(0);
pinControl(1);
pinControl(2);
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("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment