Skip to content

Instantly share code, notes, and snippets.

@davidsword
Last active August 19, 2020 14:42
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 davidsword/e88ecd2d7586a51fe739d651efbc92f9 to your computer and use it in GitHub Desktop.
Save davidsword/e88ecd2d7586a51fe739d651efbc92f9 to your computer and use it in GitHub Desktop.
ESP8266 NodeMCU RGB LED Busy Server
/* ESP8266 NodeMCU RGB LED Busy Server
*
* Set an RGB LED to current status via web request
*
* 1. Flash this file to a ESP-12E
* 2. Open Serial to get the IP address of board
* 3. Optionally set a dedicated DHCP reservation at the router level to ensure same IP address is used
* 4. Connect common anode or common cathode RGB LED (see pins below)
* 5. Make `GET` request to `http://<IP>/<status>`
*
* Statuses:
*
* - 🔴 `/busy`
* - 🟢 `/available`
* - 🟡 `/away`
* - ⚫️ `/offline`
*
* @see https://davidsword.ca/esp8266-busy-server/
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// @see https://github.com/BretStateham/RGBLED
// must install this library in Arduino IDE
#include <RGBLED.h>
uint8_t red_led = 16; // D0
uint8_t green_led = 5; // D1
uint8_t blue_led = 4; // D2
ESP8266WebServer server;
RGBLED rgbLed( red_led, green_led, blue_led, COMMON_CATHODE ); // or `COMMON_ANODE` if using common anode RGB LED instead
char* ssid = ""; // <<< enter your WIFI network name
char* password = ""; // <<< enter your WIFI password
void setup()
{
Serial.begin(115200);
WiFi.begin(ssid,password);
while(WiFi.status()!=WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
digitalWrite(BUILTIN_LED, LOW);
rgbLed.writeRGB(255,255,255);
server.on("/",[](){server.send(200,"text/plain","see readme");});
// @TODO can output current state on /status requests
// server.on("/status",[](){server.send(200,"text/plain", "(r,g,b)=(" + String(rgbLed.redValue) + "," + String(rgbLed.greenValue) + "," + String(rgbLed.blueValue) + ")" });
server.on("/busy",setBusy);
server.on("/available",setAvailable);
server.on("/away",setAway);
server.on("/offline",setOffline);
server.on("/off",setOffline);
server.begin();
}
void loop()
{
server.handleClient();
}
void setBusy()
{
rgbLed.writeRGB(50,0,0); // red. not using the full `255` in an attempt to reduce brightness.
server.send(204,"");
}
void setAvailable()
{
rgbLed.writeRGB(0,25,0); // green
server.send(204,"");
}
void setAway()
{
rgbLed.writeRGB(100,25,0); // yellow. mix wasn't 50/50 red/green as expected. this might be different for each make of LED.
server.send(204,"");
}
void setOffline()
{
rgbLed.turnOff();
server.send(204,"");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment