LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
main.cpp@16:f2f2da9ef9ab, 2014-12-28 (annotated)
- Committer:
- leet
- Date:
- Sun Dec 28 19:20:09 2014 +0000
- Revision:
- 16:f2f2da9ef9ab
- Parent:
- 12:fbf950985e1c
- Child:
- 19:7fdccfd5b50b
Add alias for setcolour/setcolor; Add to frontpage, basic set colour form;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sschocke | 0:d21e3e1c0a4b | 1 | #include "mbed.h" |
sschocke | 1:f07afcffeb5a | 2 | #include "PololuLedStrip.h" |
sschocke | 7:f15c81074400 | 3 | #include "ESP8266_WebServer.h" |
sschocke | 0:d21e3e1c0a4b | 4 | #include <string> |
sschocke | 0:d21e3e1c0a4b | 5 | #define DEBUG_WIFI |
sschocke | 0:d21e3e1c0a4b | 6 | |
sschocke | 8:f819de1946a7 | 7 | PololuLedStrip ledStrip(D11); |
sschocke | 1:f07afcffeb5a | 8 | |
sschocke | 8:f819de1946a7 | 9 | #define LED_COUNT 8 |
sschocke | 1:f07afcffeb5a | 10 | rgb_color colors[LED_COUNT]; |
sschocke | 1:f07afcffeb5a | 11 | |
sschocke | 0:d21e3e1c0a4b | 12 | DigitalOut wifiCHPD(D4); |
sschocke | 0:d21e3e1c0a4b | 13 | DigitalOut wifiReset(D9); |
sschocke | 0:d21e3e1c0a4b | 14 | |
sschocke | 0:d21e3e1c0a4b | 15 | int wifiOn = 0; |
sschocke | 0:d21e3e1c0a4b | 16 | |
sschocke | 0:d21e3e1c0a4b | 17 | Serial wifiSerial(D8,D2); |
sschocke | 0:d21e3e1c0a4b | 18 | Serial pc(USBTX,USBRX); |
sschocke | 7:f15c81074400 | 19 | ESP8266_WebServer server(&wifiSerial); |
sschocke | 0:d21e3e1c0a4b | 20 | |
sschocke | 7:f15c81074400 | 21 | void rxint(void) { |
sschocke | 7:f15c81074400 | 22 | server.rxint(); |
sschocke | 0:d21e3e1c0a4b | 23 | } |
sschocke | 0:d21e3e1c0a4b | 24 | |
tomvdb | 4:4a502f72cbe3 | 25 | void setColor( uint8_t r, uint8_t g, uint8_t b ) |
tomvdb | 4:4a502f72cbe3 | 26 | { |
tomvdb | 4:4a502f72cbe3 | 27 | for ( int c = 0; c < LED_COUNT; c++ ) |
tomvdb | 4:4a502f72cbe3 | 28 | { |
tomvdb | 4:4a502f72cbe3 | 29 | colors[c].red = r; |
tomvdb | 4:4a502f72cbe3 | 30 | colors[c].green = g; |
tomvdb | 4:4a502f72cbe3 | 31 | colors[c].blue = b; |
tomvdb | 4:4a502f72cbe3 | 32 | } |
tomvdb | 4:4a502f72cbe3 | 33 | |
tomvdb | 4:4a502f72cbe3 | 34 | ledStrip.write(colors, LED_COUNT); |
tomvdb | 4:4a502f72cbe3 | 35 | } |
tomvdb | 4:4a502f72cbe3 | 36 | |
sschocke | 0:d21e3e1c0a4b | 37 | int main() { |
leet | 10:f48a9f923271 | 38 | |
leet | 16:f2f2da9ef9ab | 39 | pc.printf("WiFi Lamp - v0.02 ...\r\n"); |
leet | 10:f48a9f923271 | 40 | |
tomvdb | 4:4a502f72cbe3 | 41 | setColor( 250, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 42 | |
sschocke | 0:d21e3e1c0a4b | 43 | wifiCHPD = 0; |
sschocke | 0:d21e3e1c0a4b | 44 | wifiReset = 0; |
sschocke | 0:d21e3e1c0a4b | 45 | wifiSerial.baud(9600); |
sschocke | 0:d21e3e1c0a4b | 46 | wifiSerial.attach(&rxint); |
sschocke | 7:f15c81074400 | 47 | #ifdef DEBUG_WIFI |
sschocke | 7:f15c81074400 | 48 | server.debugSerial = &pc; |
sschocke | 7:f15c81074400 | 49 | #endif |
sschocke | 0:d21e3e1c0a4b | 50 | wait_ms(1000); |
sschocke | 0:d21e3e1c0a4b | 51 | |
sschocke | 0:d21e3e1c0a4b | 52 | pc.printf("Powering WiFi...\r\n"); |
sschocke | 0:d21e3e1c0a4b | 53 | wifiCHPD = 1; |
sschocke | 0:d21e3e1c0a4b | 54 | wait_ms(250); |
sschocke | 0:d21e3e1c0a4b | 55 | pc.printf("Hardware Reset WiFi...\r\n"); |
sschocke | 0:d21e3e1c0a4b | 56 | wifiReset = 1; |
sschocke | 0:d21e3e1c0a4b | 57 | wifiOn = 1; |
sschocke | 0:d21e3e1c0a4b | 58 | |
sschocke | 7:f15c81074400 | 59 | pc.printf("Starting Web Server...\r\n"); |
sschocke | 7:f15c81074400 | 60 | server.Initialize(); |
sschocke | 0:d21e3e1c0a4b | 61 | pc.printf("Done\r\n"); |
sschocke | 0:d21e3e1c0a4b | 62 | |
tomvdb | 4:4a502f72cbe3 | 63 | setColor( 0, 250, 0); |
tomvdb | 4:4a502f72cbe3 | 64 | wait_ms(500); |
tomvdb | 4:4a502f72cbe3 | 65 | setColor( 0, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 66 | |
sschocke | 0:d21e3e1c0a4b | 67 | while(true) { |
sschocke | 9:319aeb6e0123 | 68 | ESP8266_WebRequest* request = server.GetRequest(); |
sschocke | 9:319aeb6e0123 | 69 | if( request != NULL ) { |
sschocke | 9:319aeb6e0123 | 70 | pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str()); |
sschocke | 9:319aeb6e0123 | 71 | for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) { |
sschocke | 9:319aeb6e0123 | 72 | pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str()); |
sschocke | 9:319aeb6e0123 | 73 | } |
sschocke | 7:f15c81074400 | 74 | std::string httpReply; |
sschocke | 9:319aeb6e0123 | 75 | if( request->URI == "/" ) { |
leet | 16:f2f2da9ef9ab | 76 | httpReply = "<html><title>WiFi Lamp</title> \ |
leet | 16:f2f2da9ef9ab | 77 | <body><h1>The WiFi Lamp v0.02 is alive ...</h1>Quick colour links:<br> \ |
leet | 16:f2f2da9ef9ab | 78 | <a href='/red'>Red</a><br><a href='/green'>Green</a><br><a href='/blue'>Blue</a><br><a href='/white'>White</a><br> \ |
leet | 16:f2f2da9ef9ab | 79 | <form id='setcolour' action='setcolour' > \ |
leet | 16:f2f2da9ef9ab | 80 | <p> \ |
leet | 16:f2f2da9ef9ab | 81 | Red: <input type='text' name='r' value='0' /> \ |
leet | 16:f2f2da9ef9ab | 82 | </p> \ |
leet | 16:f2f2da9ef9ab | 83 | <p> \ |
leet | 16:f2f2da9ef9ab | 84 | Green: <input type='text' name='g' value='0' /> \ |
leet | 16:f2f2da9ef9ab | 85 | </p> \ |
leet | 16:f2f2da9ef9ab | 86 | <p> \ |
leet | 16:f2f2da9ef9ab | 87 | Blue: <input type='text' name='b' value='0' /> \ |
leet | 16:f2f2da9ef9ab | 88 | </p> \ |
leet | 16:f2f2da9ef9ab | 89 | <p> \ |
leet | 16:f2f2da9ef9ab | 90 | <input type='submit' /> \ |
leet | 16:f2f2da9ef9ab | 91 | </p> \ |
leet | 16:f2f2da9ef9ab | 92 | </form> \ |
leet | 16:f2f2da9ef9ab | 93 | </body></html>"; |
sschocke | 7:f15c81074400 | 94 | setColor(0,0,0); |
sschocke | 12:fbf950985e1c | 95 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
sschocke | 9:319aeb6e0123 | 96 | } else if( request->URI == "/red" ) { |
leet | 10:f48a9f923271 | 97 | httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Red - To Turn <a href='/'>Off</a></h1></body></html>"; |
sschocke | 7:f15c81074400 | 98 | setColor(100, 0, 0); |
sschocke | 11:3ab606a42227 | 99 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
leet | 10:f48a9f923271 | 100 | } else if( request->URI == "/green" ) { |
leet | 10:f48a9f923271 | 101 | httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Green - To Turn <a href='/'>Off</a></h1></body></html>"; |
leet | 10:f48a9f923271 | 102 | setColor(0, 100, 0); |
sschocke | 12:fbf950985e1c | 103 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
leet | 10:f48a9f923271 | 104 | } else if( request->URI == "/blue" ) { |
leet | 10:f48a9f923271 | 105 | httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now Blue - To Turn <a href='/'>Off</a></h1></body></html>"; |
leet | 10:f48a9f923271 | 106 | setColor(0, 0, 100); |
sschocke | 12:fbf950985e1c | 107 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
leet | 10:f48a9f923271 | 108 | } else if( request->URI == "/white" ) { |
leet | 10:f48a9f923271 | 109 | httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is now White - To Turn <a href='/'>Off</a></h1></body></html>"; |
leet | 10:f48a9f923271 | 110 | setColor(100, 100, 100); |
sschocke | 12:fbf950985e1c | 111 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
leet | 16:f2f2da9ef9ab | 112 | } else if( request->URI == "/setcolour" || request->URI == "/setcolor" ) { |
sschocke | 7:f15c81074400 | 113 | int r=0, g=0, b=0; |
sschocke | 9:319aeb6e0123 | 114 | |
sschocke | 9:319aeb6e0123 | 115 | if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str()); |
sschocke | 9:319aeb6e0123 | 116 | if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str()); |
sschocke | 9:319aeb6e0123 | 117 | if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str()); |
sschocke | 7:f15c81074400 | 118 | |
leet | 16:f2f2da9ef9ab | 119 | pc.printf( "Set colour to (%i, %i, %i)\r\n", r,g,b); |
sschocke | 0:d21e3e1c0a4b | 120 | |
sschocke | 7:f15c81074400 | 121 | setColor( r,g,b ); |
sschocke | 7:f15c81074400 | 122 | |
sschocke | 7:f15c81074400 | 123 | httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>"; |
sschocke | 11:3ab606a42227 | 124 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
sschocke | 7:f15c81074400 | 125 | } else { |
sschocke | 11:3ab606a42227 | 126 | server.Send404Error(request->LinkID); |
sschocke | 0:d21e3e1c0a4b | 127 | } |
sschocke | 7:f15c81074400 | 128 | pc.printf("\r\nHTTP Reply Sent\r\n"); |
sschocke | 9:319aeb6e0123 | 129 | delete request; |
sschocke | 0:d21e3e1c0a4b | 130 | } |
sschocke | 1:f07afcffeb5a | 131 | |
sschocke | 0:d21e3e1c0a4b | 132 | wait_ms(10); |
sschocke | 0:d21e3e1c0a4b | 133 | } |
sschocke | 0:d21e3e1c0a4b | 134 | } |