WiFi RGB Lamp Web Server

Dependencies:   mbed ESP8266_WebServer

RGB WiFi Lamp

Firmware

This is the official firmware repository for the BinarySpace RGB WiFi Lamp project. This firmware is still in alpha stage, and subject to change.

Planned changes include:

  • Configure the WiFi Lamp to connect onto your SSID
  • Variety of operating modes like
    • Fixed colour operation
    • Rainbow gradient
    • Time-based colour changing
    • API-based colour changing

Connecting to the WiFi lamp

To connect to the WiFi lamp web server, scan for an open WiFi network using your cellphone, tablet or laptop that begins with the letters ESP_xxxxxx. This is the automatically created SSID of the ESP8266 WiFi module used in the lamp. Your WiFi client needs to be configured to use DHCP.

Once connected, simply point your browser at http://192.168.4.1 and you should see the rudementary web interface allowing you to switch the WiFi lamp on in the red colour or off.

A second option is to enter the following URL
http://192.168.4.1/setcolor?r=x&g=x&b=x
where x is a number between 0 and 255 for the intensity of (r)ed, (g)reen and (b)lue respectively. Any of the r,g,b parts not specified will automatically default to 0

Supported Platforms

  • ST Nucleo F103RB
  • ST Nucleo F302R8
  • ST Nucleo L152RE
  • ST Nucleo F401RE

Unsupported Platforms

  • ST Nucleo F030R8 (unsupported due to insufficient registers for PololuLed library)

How to update your firmware

One of the best things about the ST Nucleo series is that they enumerate as a USB Mass Storage device when plugged in. Updating the firmware is as simple as compiling it using mbed compiler(free registration required to use) for your selected platform, plugging in your Nucleo and copying the .bin file created by the compiler to the USB drive enumerated by the Nucleo. That's it!

Code is fully Open Source

Please feel free to fork this repository and to submit pull requests if you make any cool additions/changes.

If you are developing changes to the firmware and monitoring via serial console for debugging purposes, note than you can simply comment out the #define DEBUG_WIFI line at the top of the main.cpp file to make the output much less verbose. This effectively disables debugging of the WebServer library code and echoing of communications between the Nucleo and the ESP. It also makes the web server noticeably faster, as it doesn't have to output a lot of serial data before handling requests.

LED Strip colour inconsistency

If you are experiencing problems with the LED's not all changing colour, or perhaps flickering or incorrect colour, there could be 2 potential problems we have identified.

  • Power Supply problems - If the power supply is not providing enough power, or not clean enough power, you may experience flickering or random colour changes. Ensure that your power supply can provide enough power (1A @ 5V recommended). If this does not solve your problem, soldering a capacitor over the power supply lines(5V, GND) may help to clean out any noise from the power supply. (100uF minimum)
  • Depending on cable lengths and connectors, noise on the data line may also be a problem. Try soldering a 100Ω - 500Ω resistor in line on the Din pin of the LED strip

Firmware update for the ESP8266 Module

We suggest you upgrade the firmware on the ESP8266 module to the latest official AT firmware from Espressif. Click Here for a detailed upgrade quide.

Revision:
26:ea5e0ff46492
Parent:
25:841fc9daffa5
Child:
27:88093aa94e24
--- a/main.cpp	Tue Jan 06 18:44:43 2015 +0000
+++ b/main.cpp	Wed Jan 07 21:36:45 2015 +0000
@@ -52,6 +52,34 @@
     ledStrip.write(colors, LED_COUNT);
 }
 
+void setDiscoColor( )
+{
+    for ( int c = 0; c < LED_COUNT; c++ )
+    {
+        colors[c].red = rand()%255;
+        colors[c].green = rand()%255;
+        colors[c].blue = rand()%255;
+    }
+    
+    ledStrip.write(colors, LED_COUNT);
+}
+
+Timer DiscoTimer;
+#define DiscoDelay 100
+
+int lampMode = 0;
+
+unsigned long heapSize()
+{
+    char   stackVariable;
+    void   *heap;
+    unsigned long result;
+    heap  = malloc(4);
+    result  = (uint8_t*)&stackVariable - (uint8_t*)heap;
+    free(heap);
+    return result;
+}
+
 void sendConfigJSONReply(int linkID, const char* result) {
     sprintf(temp, jsonConfigReply, opMode, stationIP.c_str(), ssid.c_str(), result);
     server.SendReply(linkID, temp, mimeJSON, 0);
@@ -63,7 +91,7 @@
     pc.attach(&pcrxint);
 #endif
     
-    pc.printf("WiFi Lamp - %s...\r\n", VERSION);    
+    pc.printf("\r\nWiFi Lamp - %s...\r\n", VERSION);    
     
     setColor( 25, 0, 0);
     
@@ -73,6 +101,7 @@
     wifiSerial.attach(&rxint);
 #ifdef DEBUG_WIFI
     server.debugSerial = &pc;
+    pc.printf("Debug WiFi Enabled!\r\n");
 #endif
     wait_ms(1000);
     
@@ -84,7 +113,6 @@
     server.ResetModule();
     wifiOn = 1;
     
-    
     std::string fwVer = server.GetFirmwareVersion();
     pc.printf("ESP Firmware Version: %s\r\n", fwVer.c_str());
     
@@ -112,6 +140,9 @@
     wait_ms(500);
     setColor( 0, 0, 0);
     
+    pc.printf("Heap Memory %d", heapSize());
+    DiscoTimer.start();
+                    
     std::string httpReply;
     while(true) {
         ESP8266_WebRequest* request = server.GetRequest();
@@ -120,7 +151,7 @@
             for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) {
                 pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str());
             }
-            if( request->URI == "/" ) {
+            if( request->URI == "/" || request->URI == "/index.html" ) {
                 httpReply = htmlHead;
                 httpReply += "<table><tr><td align='right'><a href='config'><img src='config.gif' /></a></td></tr>";
                 httpReply += "<tr><td align='center'>";
@@ -185,11 +216,21 @@
             } else if( request->URI == "/wifilamp.css" ) {
                 server.SendReply(request->LinkID, css, strlen(css), mimeCSS);
             } else if( request->URI == "/colormap.gif" ) {
-                server.SendReply(request->LinkID, (char*)colormap, sizeof(colormap), mimeGIF);
+                server.SendReply(request->LinkID, (char*)colorMap, sizeof(colorMap), mimeGIF);
             } else if( request->URI == "/config.gif" ) {
                 server.SendReply(request->LinkID, (char*)configIcon, sizeof(configIcon), mimeGIF);
+           } else if( request->URI == "/robtos.txt" ) {
+                server.SendReply(request->LinkID, robotsTxt, strlen(robotsTxt), mimeText);
+            } else if( request->URI == "/favicon.ico" ) {
+                server.SendReply(request->LinkID, (char*)favIcon, sizeof(favIcon), "image/x-icon");
+            } else if( request->URI == "/disco" ) {
+                lampMode = 2; // All colours
+                DiscoTimer.reset();
+                server.SendReply(request->LinkID, "OK", mimeText, 0);
             } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) {
                 int r=0, g=0, b=0;
+
+                lampMode = 1; // All LED 1 colour                
                 
                 if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str());
                 if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str());
@@ -206,5 +247,20 @@
             pc.printf("\r\nHTTP Reply Sent\r\n");
             delete request;
         }
+        if( lampMode == 2 && DiscoTimer.read_ms() > DiscoDelay ) {
+            setDiscoColor();
+            // Delay random colour change, else it just looks white
+            DiscoTimer.reset();
+        }
+/*
+        if( lampMode == 3 ) {
+            // strobe
+        }
+
+        if( lampMode == 4 ) {
+            // fade
+        }
+
+*/
     }
 }
\ No newline at end of file