Spidey Wall is the name for a physical wall lit up by multiple addressable LED strips. This program is an LPC1768 web server to control the wall from a browser.
Dependencies: EthernetInterfacePlusHostname RdWebServer mbed-rtos mbed
This project is part of a Light-Wall using addressable LED strips (WS2801). I have published a few posts on my blog about the construction of the wall and building a game to play on it (PacMan). I have also had a guest post from a friend who has set his children the task of producing some interesting animations. The original post is http://robdobson.com/2015/07/spidey-wall/
So far, however, I hadn't fully connected the physical (and electronic) wall with the web-browser creations to drive it. This project is hopefully the final link. A fast and reliable web server using REST commands to drive the 1686 LEDs in the Spidey Wall from code running in a browser (say on an iPad while you are playing a game).
The approach taken here results in the ability to control the RGB values of all 1686 LEDs at a rate of 20 frames per second.
A blog post describing the whole thing is here:
http://robdobson.com/2015/08/a-reliable-mbed-webserver/
main.cpp@1:362331cec9b7, 2015-08-20 (annotated)
- Committer:
- Bobty
- Date:
- Thu Aug 20 07:41:02 2015 +0000
- Revision:
- 1:362331cec9b7
- Parent:
- 0:887096209439
- Child:
- 2:99eb4c6e9ea4
Implemented a command protocol using HTTP POST
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobty | 1:362331cec9b7 | 1 | // |
Bobty | 1:362331cec9b7 | 2 | // Spidey Wall WebServer |
Bobty | 1:362331cec9b7 | 3 | // |
Bobty | 1:362331cec9b7 | 4 | // Rob Dobson 2015 |
Bobty | 0:887096209439 | 5 | |
Bobty | 0:887096209439 | 6 | #include "mbed.h" |
Bobty | 0:887096209439 | 7 | #include "EthernetInterface.h" |
Bobty | 1:362331cec9b7 | 8 | #include "RdWebServer.h" |
Bobty | 1:362331cec9b7 | 9 | #include "DrawingManager.h" |
Bobty | 0:887096209439 | 10 | #include <string.h> |
Bobty | 1:362331cec9b7 | 11 | |
Bobty | 1:362331cec9b7 | 12 | // Web port |
Bobty | 1:362331cec9b7 | 13 | const int WEBPORT = 80; // Port for web server |
Bobty | 0:887096209439 | 14 | |
Bobty | 1:362331cec9b7 | 15 | // Debugging and status |
Bobty | 1:362331cec9b7 | 16 | RawSerial pc(USBTX, USBRX); |
Bobty | 1:362331cec9b7 | 17 | DigitalOut led1(LED1); // ticking (flashes) |
Bobty | 1:362331cec9b7 | 18 | DigitalOut led2(LED2); // |
Bobty | 1:362331cec9b7 | 19 | DigitalOut led3(LED3); // |
Bobty | 1:362331cec9b7 | 20 | DigitalOut led4(LED4); // web server status |
Bobty | 0:887096209439 | 21 | |
Bobty | 1:362331cec9b7 | 22 | // Drawing Manager |
Bobty | 1:362331cec9b7 | 23 | DrawingManager drawingManager(1686, 780); |
Bobty | 0:887096209439 | 24 | |
Bobty | 1:362331cec9b7 | 25 | //int ledsCount = 904; |
Bobty | 1:362331cec9b7 | 26 | //int ledSplitPoint = 448; |
Bobty | 1:362331cec9b7 | 27 | //ledstrip* pLedStrip = NULL; |
Bobty | 1:362331cec9b7 | 28 | // |
Bobty | 1:362331cec9b7 | 29 | //char* indexHtmName = "index.htm"; |
Bobty | 1:362331cec9b7 | 30 | // |
Bobty | 1:362331cec9b7 | 31 | //const int TICK_MS = 100; |
Bobty | 1:362331cec9b7 | 32 | //LedCmdHandler* pLedCmdHandler = NULL; |
Bobty | 1:362331cec9b7 | 33 | //int blinkCtr = 0; |
Bobty | 1:362331cec9b7 | 34 | // |
Bobty | 1:362331cec9b7 | 35 | //void ledTickfunc() |
Bobty | 1:362331cec9b7 | 36 | //{ |
Bobty | 1:362331cec9b7 | 37 | // if(webServer.isListening()) |
Bobty | 1:362331cec9b7 | 38 | // { |
Bobty | 1:362331cec9b7 | 39 | // blinkCtr++; |
Bobty | 1:362331cec9b7 | 40 | // if (blinkCtr > 1) |
Bobty | 1:362331cec9b7 | 41 | // { |
Bobty | 1:362331cec9b7 | 42 | // led1 = !led1; |
Bobty | 1:362331cec9b7 | 43 | // blinkCtr = 0; |
Bobty | 1:362331cec9b7 | 44 | // } |
Bobty | 1:362331cec9b7 | 45 | // } |
Bobty | 1:362331cec9b7 | 46 | // else |
Bobty | 1:362331cec9b7 | 47 | // { |
Bobty | 1:362331cec9b7 | 48 | // led1 = false; |
Bobty | 1:362331cec9b7 | 49 | // } |
Bobty | 1:362331cec9b7 | 50 | // |
Bobty | 1:362331cec9b7 | 51 | // if (pLedCmdHandler != NULL) |
Bobty | 1:362331cec9b7 | 52 | // pLedCmdHandler->NextGen(); |
Bobty | 1:362331cec9b7 | 53 | //} |
Bobty | 1:362331cec9b7 | 54 | // |
Bobty | 1:362331cec9b7 | 55 | //void handleCmd_ledControl(char* cmdStr, char* argStr) |
Bobty | 1:362331cec9b7 | 56 | //{ |
Bobty | 1:362331cec9b7 | 57 | // printf("LEDS COMMAND %s %s\r\n", cmdStr, argStr); |
Bobty | 1:362331cec9b7 | 58 | // if (argStr == NULL) |
Bobty | 1:362331cec9b7 | 59 | // return; |
Bobty | 1:362331cec9b7 | 60 | // pLedCmdHandler->DoCommand(argStr); |
Bobty | 1:362331cec9b7 | 61 | // |
Bobty | 1:362331cec9b7 | 62 | // // Store last command |
Bobty | 1:362331cec9b7 | 63 | // LocalFileSystem local("local"); |
Bobty | 1:362331cec9b7 | 64 | // FILE* fp = fopen("/local/lastcmd.inf", "w"); |
Bobty | 1:362331cec9b7 | 65 | // if (fp != NULL) |
Bobty | 1:362331cec9b7 | 66 | // { |
Bobty | 1:362331cec9b7 | 67 | // fwrite(argStr, 1, strlen(argStr)+1, fp); |
Bobty | 1:362331cec9b7 | 68 | // fclose(fp); |
Bobty | 1:362331cec9b7 | 69 | // } |
Bobty | 1:362331cec9b7 | 70 | //} |
Bobty | 1:362331cec9b7 | 71 | // |
Bobty | 1:362331cec9b7 | 72 | //void reRunLastCommand() |
Bobty | 1:362331cec9b7 | 73 | //{ |
Bobty | 1:362331cec9b7 | 74 | // // Store last command |
Bobty | 1:362331cec9b7 | 75 | // LocalFileSystem local("local"); |
Bobty | 1:362331cec9b7 | 76 | // FILE* fp = fopen("/local/lastcmd.inf", "r"); |
Bobty | 1:362331cec9b7 | 77 | // if (fp != NULL) |
Bobty | 1:362331cec9b7 | 78 | // { |
Bobty | 1:362331cec9b7 | 79 | // char buf[501]; |
Bobty | 1:362331cec9b7 | 80 | // buf[sizeof(buf)-1] = 0; |
Bobty | 1:362331cec9b7 | 81 | // int nread = fread(buf, 1, sizeof(buf), fp); |
Bobty | 1:362331cec9b7 | 82 | // fclose(fp); |
Bobty | 1:362331cec9b7 | 83 | // if (nread > 0 && nread <= sizeof(buf)) |
Bobty | 1:362331cec9b7 | 84 | // { |
Bobty | 1:362331cec9b7 | 85 | // buf[nread] = 0; |
Bobty | 1:362331cec9b7 | 86 | // pLedCmdHandler->DoCommand(buf); |
Bobty | 1:362331cec9b7 | 87 | // } |
Bobty | 1:362331cec9b7 | 88 | // } |
Bobty | 1:362331cec9b7 | 89 | //} |
Bobty | 1:362331cec9b7 | 90 | // |
Bobty | 1:362331cec9b7 | 91 | //#include "colourconverters.h" |
Bobty | 1:362331cec9b7 | 92 | // |
Bobty | 1:362331cec9b7 | 93 | //void BodgeSmooth(ledstrip* pLedStrip) |
Bobty | 1:362331cec9b7 | 94 | //{ |
Bobty | 1:362331cec9b7 | 95 | // pLedStrip->Clear(); |
Bobty | 1:362331cec9b7 | 96 | // pLedStrip->ShowLeds(); |
Bobty | 1:362331cec9b7 | 97 | // |
Bobty | 1:362331cec9b7 | 98 | // |
Bobty | 1:362331cec9b7 | 99 | // RgbColor startRGB(50,0,0); |
Bobty | 1:362331cec9b7 | 100 | // HsvColor curHsv = RgbToHsv(startRGB); |
Bobty | 1:362331cec9b7 | 101 | // while(1) |
Bobty | 1:362331cec9b7 | 102 | // for (int k = 0; k < 1000; k++) |
Bobty | 1:362331cec9b7 | 103 | // for (int j = 0; j < 255; j++) |
Bobty | 1:362331cec9b7 | 104 | // { |
Bobty | 1:362331cec9b7 | 105 | // pLedStrip->Clear(); |
Bobty | 1:362331cec9b7 | 106 | // RgbColor colrVal = HsvToRgb(curHsv); |
Bobty | 1:362331cec9b7 | 107 | // pLedStrip->Fill(0,pLedStrip->GetNumLeds(),colrVal.r, colrVal.g, colrVal.b); |
Bobty | 1:362331cec9b7 | 108 | // pLedStrip->ShowLeds(); |
Bobty | 1:362331cec9b7 | 109 | // wait_ms(250); |
Bobty | 1:362331cec9b7 | 110 | // curHsv.h++; |
Bobty | 1:362331cec9b7 | 111 | // } |
Bobty | 1:362331cec9b7 | 112 | //} |
Bobty | 1:362331cec9b7 | 113 | // |
Bobty | 1:362331cec9b7 | 114 | //void setLightsConfig() |
Bobty | 1:362331cec9b7 | 115 | //{ |
Bobty | 1:362331cec9b7 | 116 | // printf("Rob LightWall - Configured for "); |
Bobty | 1:362331cec9b7 | 117 | // // Check for a config file on the local file system |
Bobty | 1:362331cec9b7 | 118 | // strcpy(nameOfLights, "Spidey"); |
Bobty | 1:362331cec9b7 | 119 | // LocalFileSystem local("local"); |
Bobty | 1:362331cec9b7 | 120 | // FILE* fp = fopen("/local/spidey.cnf", "r"); |
Bobty | 1:362331cec9b7 | 121 | // if (fp != NULL) |
Bobty | 1:362331cec9b7 | 122 | // { |
Bobty | 1:362331cec9b7 | 123 | // char buf[201]; |
Bobty | 1:362331cec9b7 | 124 | // buf[sizeof(buf)-1] = 0; |
Bobty | 1:362331cec9b7 | 125 | // int nread = fread(buf, 1, sizeof(buf), fp); |
Bobty | 1:362331cec9b7 | 126 | // if (nread > 0 && nread <= sizeof(buf)) |
Bobty | 1:362331cec9b7 | 127 | // { |
Bobty | 1:362331cec9b7 | 128 | // buf[nread] = 0; |
Bobty | 1:362331cec9b7 | 129 | // sscanf(buf, "%s %d %d", nameOfLights, &ledsCount, &ledSplitPoint); |
Bobty | 1:362331cec9b7 | 130 | // } |
Bobty | 1:362331cec9b7 | 131 | // fclose(fp); |
Bobty | 1:362331cec9b7 | 132 | // printf("%s (%d LEDs, Split at %d)", nameOfLights, ledsCount, ledSplitPoint); |
Bobty | 1:362331cec9b7 | 133 | // printf("\n\r"); |
Bobty | 1:362331cec9b7 | 134 | // } |
Bobty | 1:362331cec9b7 | 135 | // |
Bobty | 1:362331cec9b7 | 136 | // // Leds setup |
Bobty | 1:362331cec9b7 | 137 | // pLedStrip = new ledstrip(ledsCount, ledSplitPoint); |
Bobty | 1:362331cec9b7 | 138 | // wait_ms(100); |
Bobty | 1:362331cec9b7 | 139 | // pLedStrip->Clear(); |
Bobty | 1:362331cec9b7 | 140 | // pLedStrip->ShowLeds(); |
Bobty | 1:362331cec9b7 | 141 | // |
Bobty | 1:362331cec9b7 | 142 | // // Cmd handler |
Bobty | 1:362331cec9b7 | 143 | // pLedCmdHandler = new LedCmdHandler(pLedStrip); |
Bobty | 1:362331cec9b7 | 144 | //} |
Bobty | 1:362331cec9b7 | 145 | // |
Bobty | 1:362331cec9b7 | 146 | //int main (void) |
Bobty | 1:362331cec9b7 | 147 | //{ |
Bobty | 1:362331cec9b7 | 148 | // pc.baud(115200); |
Bobty | 1:362331cec9b7 | 149 | // |
Bobty | 1:362331cec9b7 | 150 | // setLightsConfig(); |
Bobty | 1:362331cec9b7 | 151 | // |
Bobty | 1:362331cec9b7 | 152 | // BodgeSmooth(pLedStrip); |
Bobty | 1:362331cec9b7 | 153 | // |
Bobty | 1:362331cec9b7 | 154 | // ledTick.attach(&ledTickfunc,TICK_MS / 1000.0); |
Bobty | 1:362331cec9b7 | 155 | // |
Bobty | 1:362331cec9b7 | 156 | //// reRunLastCommand(); |
Bobty | 1:362331cec9b7 | 157 | // |
Bobty | 1:362331cec9b7 | 158 | // // setup ethernet interface |
Bobty | 1:362331cec9b7 | 159 | // eth.init(); //Use DHCP |
Bobty | 1:362331cec9b7 | 160 | // eth.connect(); |
Bobty | 1:362331cec9b7 | 161 | // printf("IP Address is %s\n\r", eth.getIPAddress()); |
Bobty | 1:362331cec9b7 | 162 | // |
Bobty | 1:362331cec9b7 | 163 | // webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, indexHtmName, true); |
Bobty | 1:362331cec9b7 | 164 | // webServer.addCommand("cmd", RdWebServerCmdDef::CMD_CALLBACK, &handleCmd_ledControl); |
Bobty | 1:362331cec9b7 | 165 | // webServer.init(PORT, &led2); |
Bobty | 1:362331cec9b7 | 166 | // webServer.run(); |
Bobty | 1:362331cec9b7 | 167 | //} |
Bobty | 0:887096209439 | 168 | |
Bobty | 1:362331cec9b7 | 169 | // Handle a command |
Bobty | 1:362331cec9b7 | 170 | char* lightwallCmd(int method, char* cmdStr, char* argStr, char* msgBuf) |
Bobty | 0:887096209439 | 171 | { |
Bobty | 1:362331cec9b7 | 172 | int cmdLen = RdWebServer::getPayloadLengthFromMsg(msgBuf); |
Bobty | 1:362331cec9b7 | 173 | unsigned char* cmdBuf = RdWebServer::getPayloadDataFromMsg(msgBuf); |
Bobty | 1:362331cec9b7 | 174 | pc.printf("Command Payload Len %d\r\n", cmdLen); |
Bobty | 1:362331cec9b7 | 175 | if (cmdLen == 256) |
Bobty | 0:887096209439 | 176 | { |
Bobty | 1:362331cec9b7 | 177 | for (int i = 0; i < cmdLen; i++) |
Bobty | 1:362331cec9b7 | 178 | if (cmdBuf[i] != i) |
Bobty | 1:362331cec9b7 | 179 | { |
Bobty | 1:362331cec9b7 | 180 | printf("Binary content err at %d\r\n", i); |
Bobty | 1:362331cec9b7 | 181 | break; |
Bobty | 1:362331cec9b7 | 182 | } |
Bobty | 0:887096209439 | 183 | } |
Bobty | 1:362331cec9b7 | 184 | char* respStr = ""; |
Bobty | 1:362331cec9b7 | 185 | respStr = drawingManager.start(cmdBuf, cmdLen); |
Bobty | 1:362331cec9b7 | 186 | return respStr; |
Bobty | 0:887096209439 | 187 | } |
Bobty | 0:887096209439 | 188 | |
Bobty | 1:362331cec9b7 | 189 | // Create, configure and run the web server |
Bobty | 1:362331cec9b7 | 190 | void http_thread(void const* arg) |
Bobty | 0:887096209439 | 191 | { |
Bobty | 1:362331cec9b7 | 192 | pc.printf("Starting web server\r\n"); |
Bobty | 1:362331cec9b7 | 193 | char* baseWebFolder = "/sd/"; // should be /sd/ for SDcard files - not used for local file system |
Bobty | 1:362331cec9b7 | 194 | RdWebServer webServer; |
Bobty | 1:362331cec9b7 | 195 | webServer.addCommand("", RdWebServerCmdDef::CMD_LOCALFILE, NULL, "index.htm", true); |
Bobty | 1:362331cec9b7 | 196 | webServer.addCommand("favicon.ico", RdWebServerCmdDef::CMD_LOCALFILE, NULL, NULL, true); |
Bobty | 1:362331cec9b7 | 197 | webServer.addCommand("cmd", RdWebServerCmdDef::CMD_CALLBACK, &lightwallCmd); |
Bobty | 1:362331cec9b7 | 198 | webServer.init(WEBPORT, &led4, baseWebFolder); |
Bobty | 1:362331cec9b7 | 199 | webServer.run(); |
Bobty | 0:887096209439 | 200 | } |
Bobty | 0:887096209439 | 201 | |
Bobty | 1:362331cec9b7 | 202 | int main() |
Bobty | 0:887096209439 | 203 | { |
Bobty | 1:362331cec9b7 | 204 | // Init |
Bobty | 1:362331cec9b7 | 205 | pc.baud(115200); |
Bobty | 1:362331cec9b7 | 206 | pc.printf("Light Wall - Rob Dobson 2015\r\n"); |
Bobty | 1:362331cec9b7 | 207 | |
Bobty | 1:362331cec9b7 | 208 | // Setup ethernet interface |
Bobty | 1:362331cec9b7 | 209 | char macAddr[6]; |
Bobty | 1:362331cec9b7 | 210 | mbed_mac_address(macAddr); |
Bobty | 1:362331cec9b7 | 211 | pc.printf("Ethernet MAC address: %02x:%02x:%02x:%02x:%02x:%02x\r\n", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]); |
Bobty | 1:362331cec9b7 | 212 | pc.printf("Connecting to ethernet ...\r\n"); |
Bobty | 1:362331cec9b7 | 213 | EthernetInterface::init(); |
Bobty | 1:362331cec9b7 | 214 | EthernetInterface::connect(); |
Bobty | 1:362331cec9b7 | 215 | pc.printf("IP Address: %s\r\n", EthernetInterface::getIPAddress()); |
Bobty | 1:362331cec9b7 | 216 | |
Bobty | 1:362331cec9b7 | 217 | // Web Server |
Bobty | 1:362331cec9b7 | 218 | Thread httpServer(&http_thread, NULL, osPriorityNormal, (DEFAULT_STACK_SIZE * 4)); |
Bobty | 0:887096209439 | 219 | |
Bobty | 1:362331cec9b7 | 220 | // Home |
Bobty | 1:362331cec9b7 | 221 | drawingManager.init(); |
Bobty | 0:887096209439 | 222 | |
Bobty | 1:362331cec9b7 | 223 | while(true) |
Bobty | 1:362331cec9b7 | 224 | { |
Bobty | 1:362331cec9b7 | 225 | // led1=!led1; |
Bobty | 1:362331cec9b7 | 226 | // Service drawing manager |
Bobty | 1:362331cec9b7 | 227 | drawingManager.service(); |
Bobty | 1:362331cec9b7 | 228 | // Thread::wait(500); |
Bobty | 1:362331cec9b7 | 229 | } |
Bobty | 0:887096209439 | 230 | } |