IOT Smart Plug using Huzzah ESP8266
Fork of huzzah_helloWorld by
Revision 1:75c100115242, committed 2016-03-21
- Comitter:
- fepie3
- Date:
- Mon Mar 21 15:27:38 2016 +0000
- Parent:
- 0:57cec3469a80
- Commit message:
- First Version
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 57cec3469a80 -r 75c100115242 main.cpp --- a/main.cpp Thu Oct 22 16:18:21 2015 +0000 +++ b/main.cpp Mon Mar 21 15:27:38 2016 +0000 @@ -3,21 +3,31 @@ Serial pc(USBTX, USBRX); Serial esp(p28, p27); // tx, rx DigitalOut reset(p26); +DigitalOut out1(p23); DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); DigitalOut led4(LED4); Timer t; - -int count,ended,timeout; -char buf[2024]; -char snd[1024]; +AnalogIn Ain2(p19); -char ssid[32] = "SwagInABag"; // enter WiFi router ssid inside the quotes -char pwd [32] = "yoloswag"; // enter WiFi router password inside the quotes +int count,ended,timeout,DataRX,bufflen,bufl,servreq,webcounter; +float AdcIn,Ht; +char temp[6]; +char webcount[8]; +char buf[2024]; +char snd[4096]; +char webbuff[4096]; +char webdata[1024]; -void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); +char ssid[32] = "DBkings"; // enter WiFi router ssid inside the quotes +char pwd [32] = "420errrdae"; // enter WiFi router password inside the quotes + +void SendCMD(),SendWEB(),getreply(),ESPconfig(),ESPsetbaudrate(),startserver(),ReadWebData(),callback(),gettemp(),refresh(); void dev_recv() { led1 = !led1; + out1=!out1; while(esp.readable()) { pc.putc(esp.getc()); } @@ -30,7 +40,20 @@ esp.putc(pc.getc()); } } - + +void callback() +{ + led3=!led3; + refresh(); + while (esp.readable()) { + snd[count] = esp.getc(); + count++; + } + if(strlen(snd)>bufflen) { + DataRX=1; + led3=0; + } +} int main() { @@ -41,21 +64,34 @@ reset=1; timeout=2; getreply(); + bufflen=200; esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time. //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** - ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** + //ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** + //wait(10); + startserver(); - pc.attach(&pc_recv, Serial::RxIrq); - esp.attach(&dev_recv, Serial::RxIrq); + // pc.attach(&pc_recv, Serial::RxIrq); + // esp.attach(&dev_recv, Serial::RxIrq); // continuosly get AP list and IP while(1) { - sleep(); + if(DataRX==1) { + ReadWebData(); + if (servreq == 1) { + startserver(); + } + esp.attach(&callback); + pc.printf(" -------------------------------------\n\n"); + pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata); + pc.printf(" -------------------------------------\n\n"); + servreq=0; + } } } @@ -164,11 +200,22 @@ pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); pc.printf(" It saves the SSID and password settings internally\r\n"); - wait(10); - + pc.printf("\r\nDONE"); + } - pc.printf("\n---------- Setting up http server ----------\r\n"); - strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); +void startserver() +{ + pc.printf("\n---------- Get IP's ----------\r\n"); + strcpy(snd, "print(wifi.sta.getip())\r\n"); + SendCMD(); + timeout=3; + getreply(); + pc.printf(buf); + gettemp(); + + wait(1); + pc.printf("\n---------- Setting up http server ----------\r\n"); + strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); SendCMD(); wait(1); strcpy(snd, "srv:listen(80,function(conn)\r\n"); @@ -179,42 +226,95 @@ wait(1); strcpy(snd, "print(payload)\r\n"); SendCMD(); - wait(1); + wait(5); strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); SendCMD(); - wait(1); + wait(1); strcpy(snd, "conn:send(\"<html>\")\r\n"); SendCMD(); - wait(1); + wait(1); + + strcpy(snd, "conn:send(\"<body>\")\r\n"); + SendCMD(); + wait(1); - strcpy(snd, "conn:send(\"<h1> Hi James, NodeMcu.</h1>\")\r\n"); + strcpy(snd, "conn:send(\"<h1> IOT Smart Socket</h1>\")\r\n"); SendCMD(); wait(1); - strcpy(snd, "conn:send(\"<h2> test</h2>\")\r\n"); + strcpy(snd, "conn:send(\"<h2> Turn Smart Socket On and Off</h2>\")\r\n"); + SendCMD(); + wait(1); + + strcpy(snd, "conn:send(\"<p><form><strong> Power:  <input type='text' size=6 value='\")\r\n"); + SendCMD(); + wait(1); + + sprintf(snd, "conn:send(\"%s'\")\r\n", temp); + SendCMD(); + wait(1); + + strcpy(snd, "conn:send(\"</sup>Watts\")\r\n"); + SendCMD(); + wait(1); + + if(led2==0) { + + strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0' checked> Socket off\")\r\n"); + SendCMD(); + wait(1); + + strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' > Socket on\")\r\n"); + SendCMD(); + wait(1); + } else { + strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0'> Socket off\")\r\n"); + SendCMD(); + wait(1); + + strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' checked> Socket on\")\r\n"); + SendCMD(); + wait(1); + } + + + strcpy(snd, "conn:send(\"</form><form></strong><p><input type='button' onClick='window.top.location.reload()' value='send-refresh' style='background: #3498db;'</form>\")\r\n"); + SendCMD(); + ReadWebData(); + wait(1); + + //strcpy(snd, "conn:send(\"</form>\")\r\n"); + //SendCMD(); + //wait(1); + + strcpy(snd, "conn:send(\"</body>\")\r\n"); SendCMD(); wait(1); strcpy(snd, "conn:send(\"</html>\")\r\n"); - SendCMD(); - wait(1); + SendCMD(); + wait(1); strcpy(snd, "end)\r\n"); - SendCMD(); - wait(1); + SendCMD(); + wait(1); strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); - SendCMD(); - wait(1); + SendCMD(); + wait(1); + strcpy(snd, "end)\r\n"); - SendCMD(); - wait(1); + SendCMD(); + wait(1); timeout=17; + getreply(); pc.printf(buf); - pc.printf("\r\nDONE"); + pc.printf("\r\nServer Started"); + //ReadWebData(); + esp.attach(&callback); } void SendCMD() @@ -240,5 +340,178 @@ } } } +void ReadWebData() +{ + wait_ms(200); + esp.attach(NULL); + DataRX=0; + memset(webdata, '\0', sizeof(webdata)); + int x = strcspn (snd,"+"); + if(x) { + strcpy(webdata, snd + x); + //int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); + if( strstr(webdata, "led2=1") != NULL ) { + led2=1; + } + if( strstr(webdata, "led2=0") != NULL ) { + led2=0; + } + /* if( strstr(webdata, "Out1=1") != NULL ) { + Out1=1; + } + if( strstr(webdata, "Out1=0") != NULL ) { + Out1=0; + } + if( strstr(webdata, "Out2=1") != NULL ) { + Out2=1; + } + if( strstr(webdata, "Out2=0") != NULL ) { + Out2=0; + } + if( strstr(webdata, "Out3=1") != NULL ) { + Out3=1; + } + if( strstr(webdata, "Out3=0") != NULL ) { + Out3=0; + } */ + //sprintf(channel, "%d",linkID); + if (strstr(webdata, "GET") != NULL) { + servreq=1; + } + if (strstr(webdata, "POST") != NULL) { + servreq=1; + } + webcounter++; + sprintf(webcount, "%d",webcounter); + } else { + memset(snd, '\0', sizeof(snd)); + esp.attach(&callback); + } +} +void SendWEB() +{ + int i=0; + if(esp.writeable()) { + while(webbuff[i]!='\0') { + esp.putc(webbuff[i]); + i++; + } + } +} +void gettemp() +{ + + AdcIn=Ain2.read(); + Ht = (AdcIn*120); // set the numeric to the exact MCU analog reference voltage for greater accuracy + sprintf(temp,"%2.3f",Ht); + //pc.printf(temp); +} +void refresh() +{ - \ No newline at end of file + out1=!out1; + if (out1==0){ + sprintf(temp,"%2.3d",0);} + else { + gettemp();} + wait(.2); + pc.printf("\n---------- Restarted ----------\r\n"); + strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); + SendCMD(); + wait(.2); + strcpy(snd, "srv:listen(80,function(conn)\r\n"); + SendCMD(); + wait(.2); + strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n"); + SendCMD(); + wait(.2); + strcpy(snd, "print(payload)\r\n"); + SendCMD(); + wait(.3); + + strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<html>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<body>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<h1> IOT Smart Socket</h1>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<h2> Turn Smart Socket On and Off</h2>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<p><form><strong> Power:  <input type='text' size=6 value='\")\r\n"); + SendCMD(); + wait(.2); + + sprintf(snd, "conn:send(\"%s'\")\r\n", temp); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"</sup>Watts\")\r\n"); + SendCMD(); + wait(.2); + + if(out1==0) { + + strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0' checked> Socket off\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' > Socket on\")\r\n"); + SendCMD(); + wait(.2); + } else { + strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='1'> Socket off\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='0' checked> Socket on\")\r\n"); + SendCMD(); + wait(.2); + } + + + strcpy(snd, "conn:send(\"</form><form></strong><p><input type='button' onClick='window.top.location.reload()' value='send-refresh' style='background: #3498db;'</form>\")\r\n"); + SendCMD(); + ReadWebData(); + wait(.2); + + + strcpy(snd, "conn:send(\"</body>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "conn:send(\"</html>\")\r\n"); + SendCMD(); + wait(.2); + + strcpy(snd, "end)\r\n"); + SendCMD(); + wait(.3); + + strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); + SendCMD(); + wait(.3); + + strcpy(snd, "end)\r\n"); + SendCMD(); + wait(.3); + timeout=17; + + getreply(); + pc.printf(buf); + pc.printf("\r\nRestarted"); + //ReadWebData(); + esp.attach(&callback); +} + \ No newline at end of file