LeeT WiFiLamp code and test
Dependencies: ESP8266_WebServer mbed
Fork of WiFiLamp by
main.cpp@11:3ab606a42227, 2014-12-28 (annotated)
- Committer:
- sschocke
- Date:
- Sun Dec 28 16:04:22 2014 +0000
- Revision:
- 11:3ab606a42227
- Parent:
- 9:319aeb6e0123
- Child:
- 12:fbf950985e1c
WebServer API Updates for multiple content types
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 | 11:3ab606a42227 | 21 | const char javascript[] = "function test() { alert('Hello'); }"; |
sschocke | 11:3ab606a42227 | 22 | const char css[] = "body { background: #DDDDDD; }"; |
sschocke | 11:3ab606a42227 | 23 | |
sschocke | 7:f15c81074400 | 24 | void rxint(void) { |
sschocke | 7:f15c81074400 | 25 | server.rxint(); |
sschocke | 0:d21e3e1c0a4b | 26 | } |
sschocke | 0:d21e3e1c0a4b | 27 | |
tomvdb | 4:4a502f72cbe3 | 28 | void setColor( uint8_t r, uint8_t g, uint8_t b ) |
tomvdb | 4:4a502f72cbe3 | 29 | { |
tomvdb | 4:4a502f72cbe3 | 30 | for ( int c = 0; c < LED_COUNT; c++ ) |
tomvdb | 4:4a502f72cbe3 | 31 | { |
tomvdb | 4:4a502f72cbe3 | 32 | colors[c].red = r; |
tomvdb | 4:4a502f72cbe3 | 33 | colors[c].green = g; |
tomvdb | 4:4a502f72cbe3 | 34 | colors[c].blue = b; |
tomvdb | 4:4a502f72cbe3 | 35 | } |
tomvdb | 4:4a502f72cbe3 | 36 | |
tomvdb | 4:4a502f72cbe3 | 37 | ledStrip.write(colors, LED_COUNT); |
tomvdb | 4:4a502f72cbe3 | 38 | } |
tomvdb | 4:4a502f72cbe3 | 39 | |
sschocke | 0:d21e3e1c0a4b | 40 | int main() { |
sschocke | 0:d21e3e1c0a4b | 41 | pc.printf("WiFi Lamp Test...\r\n"); |
tomvdb | 4:4a502f72cbe3 | 42 | |
tomvdb | 4:4a502f72cbe3 | 43 | setColor( 250, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 44 | |
sschocke | 0:d21e3e1c0a4b | 45 | wifiCHPD = 0; |
sschocke | 0:d21e3e1c0a4b | 46 | wifiReset = 0; |
sschocke | 0:d21e3e1c0a4b | 47 | wifiSerial.baud(9600); |
sschocke | 0:d21e3e1c0a4b | 48 | wifiSerial.attach(&rxint); |
sschocke | 7:f15c81074400 | 49 | #ifdef DEBUG_WIFI |
sschocke | 7:f15c81074400 | 50 | server.debugSerial = &pc; |
sschocke | 7:f15c81074400 | 51 | #endif |
sschocke | 0:d21e3e1c0a4b | 52 | wait_ms(1000); |
sschocke | 0:d21e3e1c0a4b | 53 | |
sschocke | 0:d21e3e1c0a4b | 54 | pc.printf("Powering WiFi...\r\n"); |
sschocke | 0:d21e3e1c0a4b | 55 | wifiCHPD = 1; |
sschocke | 0:d21e3e1c0a4b | 56 | wait_ms(250); |
sschocke | 0:d21e3e1c0a4b | 57 | pc.printf("Hardware Reset WiFi...\r\n"); |
sschocke | 0:d21e3e1c0a4b | 58 | wifiReset = 1; |
sschocke | 0:d21e3e1c0a4b | 59 | wifiOn = 1; |
sschocke | 0:d21e3e1c0a4b | 60 | |
sschocke | 7:f15c81074400 | 61 | pc.printf("Starting Web Server...\r\n"); |
sschocke | 7:f15c81074400 | 62 | server.Initialize(); |
sschocke | 0:d21e3e1c0a4b | 63 | pc.printf("Done\r\n"); |
sschocke | 0:d21e3e1c0a4b | 64 | |
tomvdb | 4:4a502f72cbe3 | 65 | setColor( 0, 250, 0); |
tomvdb | 4:4a502f72cbe3 | 66 | wait_ms(500); |
tomvdb | 4:4a502f72cbe3 | 67 | setColor( 0, 0, 0); |
tomvdb | 4:4a502f72cbe3 | 68 | |
sschocke | 0:d21e3e1c0a4b | 69 | while(true) { |
sschocke | 9:319aeb6e0123 | 70 | ESP8266_WebRequest* request = server.GetRequest(); |
sschocke | 9:319aeb6e0123 | 71 | if( request != NULL ) { |
sschocke | 9:319aeb6e0123 | 72 | pc.printf("HTTP %s %s\r\n", request->Method.c_str(), request->URI.c_str()); |
sschocke | 9:319aeb6e0123 | 73 | for( std::map<std::string,std::string>::iterator it = request->Parameters.begin(); it!=request->Parameters.end(); ++it ) { |
sschocke | 9:319aeb6e0123 | 74 | pc.printf("HTTP Parameter %s = %s\r\n", it->first.c_str(), it->second.c_str()); |
sschocke | 9:319aeb6e0123 | 75 | } |
sschocke | 7:f15c81074400 | 76 | std::string httpReply; |
sschocke | 9:319aeb6e0123 | 77 | if( request->URI == "/" ) { |
sschocke | 11:3ab606a42227 | 78 | httpReply = "<html><head>"; |
sschocke | 11:3ab606a42227 | 79 | httpReply += "<title>WiFi Lamp</title>"; |
sschocke | 11:3ab606a42227 | 80 | httpReply += "<link rel='stylesheet' type='text/css' href='wifilamp.css'>"; |
sschocke | 11:3ab606a42227 | 81 | httpReply += "<script type='text/javascript' src='wifilamp.js'></script>"; |
sschocke | 11:3ab606a42227 | 82 | httpReply += "</head><body>"; |
sschocke | 11:3ab606a42227 | 83 | httpReply += "<h1>The WiFi Lamp is alive(<a href='/red'>Red</a>)</h1>"; |
sschocke | 11:3ab606a42227 | 84 | httpReply += "</body></html>"; |
sschocke | 11:3ab606a42227 | 85 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
sschocke | 11:3ab606a42227 | 86 | } else if( request->URI == "/wifilamp.js" ) { |
sschocke | 11:3ab606a42227 | 87 | server.SendReply(request->LinkID, javascript, sizeof(javascript), mimeJavaScript); |
sschocke | 11:3ab606a42227 | 88 | } else if( request->URI == "/wifilamp.css" ) { |
sschocke | 11:3ab606a42227 | 89 | server.SendReply(request->LinkID, css, sizeof(css), mimeCSS); |
sschocke | 9:319aeb6e0123 | 90 | } else if ( request->URI == "/setcolor" ) { |
sschocke | 7:f15c81074400 | 91 | int r=0, g=0, b=0; |
sschocke | 9:319aeb6e0123 | 92 | |
sschocke | 9:319aeb6e0123 | 93 | if(request->Parameters.count("r") > 0) r=atoi(request->Parameters["r"].c_str()); |
sschocke | 9:319aeb6e0123 | 94 | if(request->Parameters.count("g") > 0) g=atoi(request->Parameters["g"].c_str()); |
sschocke | 9:319aeb6e0123 | 95 | if(request->Parameters.count("b") > 0) b=atoi(request->Parameters["b"].c_str()); |
sschocke | 7:f15c81074400 | 96 | |
sschocke | 7:f15c81074400 | 97 | pc.printf( "Set color to (%i, %i, %i)\r\n", r,g,b); |
sschocke | 0:d21e3e1c0a4b | 98 | |
sschocke | 7:f15c81074400 | 99 | setColor( r,g,b ); |
sschocke | 7:f15c81074400 | 100 | |
sschocke | 7:f15c81074400 | 101 | httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>"; |
sschocke | 11:3ab606a42227 | 102 | server.SendReply(request->LinkID, httpReply, mimeHTML); |
sschocke | 7:f15c81074400 | 103 | } else { |
sschocke | 11:3ab606a42227 | 104 | server.Send404Error(request->LinkID); |
sschocke | 0:d21e3e1c0a4b | 105 | } |
sschocke | 7:f15c81074400 | 106 | pc.printf("\r\nHTTP Reply Sent\r\n"); |
sschocke | 9:319aeb6e0123 | 107 | delete request; |
sschocke | 0:d21e3e1c0a4b | 108 | } |
sschocke | 1:f07afcffeb5a | 109 | |
sschocke | 0:d21e3e1c0a4b | 110 | wait_ms(10); |
sschocke | 0:d21e3e1c0a4b | 111 | } |
sschocke | 0:d21e3e1c0a4b | 112 | } |