IOT Smart Plug using Huzzah ESP8266

Dependencies:   mbed

Fork of huzzah_helloWorld by ECE 4180 Team Who

Committer:
fepie3
Date:
Mon Mar 21 15:27:38 2016 +0000
Revision:
1:75c100115242
Parent:
0:57cec3469a80
First Version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhunter029 0:57cec3469a80 1 #include "mbed.h"
jhunter029 0:57cec3469a80 2
jhunter029 0:57cec3469a80 3 Serial pc(USBTX, USBRX);
jhunter029 0:57cec3469a80 4 Serial esp(p28, p27); // tx, rx
jhunter029 0:57cec3469a80 5 DigitalOut reset(p26);
fepie3 1:75c100115242 6 DigitalOut out1(p23);
jhunter029 0:57cec3469a80 7 DigitalOut led1(LED1);
fepie3 1:75c100115242 8 DigitalOut led2(LED2);
fepie3 1:75c100115242 9 DigitalOut led3(LED3);
jhunter029 0:57cec3469a80 10 DigitalOut led4(LED4);
jhunter029 0:57cec3469a80 11 Timer t;
fepie3 1:75c100115242 12 AnalogIn Ain2(p19);
jhunter029 0:57cec3469a80 13
fepie3 1:75c100115242 14 int count,ended,timeout,DataRX,bufflen,bufl,servreq,webcounter;
fepie3 1:75c100115242 15 float AdcIn,Ht;
fepie3 1:75c100115242 16 char temp[6];
fepie3 1:75c100115242 17 char webcount[8];
fepie3 1:75c100115242 18 char buf[2024];
fepie3 1:75c100115242 19 char snd[4096];
fepie3 1:75c100115242 20 char webbuff[4096];
fepie3 1:75c100115242 21 char webdata[1024];
jhunter029 0:57cec3469a80 22
fepie3 1:75c100115242 23 char ssid[32] = "DBkings"; // enter WiFi router ssid inside the quotes
fepie3 1:75c100115242 24 char pwd [32] = "420errrdae"; // enter WiFi router password inside the quotes
fepie3 1:75c100115242 25
fepie3 1:75c100115242 26 void SendCMD(),SendWEB(),getreply(),ESPconfig(),ESPsetbaudrate(),startserver(),ReadWebData(),callback(),gettemp(),refresh();
jhunter029 0:57cec3469a80 27 void dev_recv()
jhunter029 0:57cec3469a80 28 {
jhunter029 0:57cec3469a80 29 led1 = !led1;
fepie3 1:75c100115242 30 out1=!out1;
jhunter029 0:57cec3469a80 31 while(esp.readable()) {
jhunter029 0:57cec3469a80 32 pc.putc(esp.getc());
jhunter029 0:57cec3469a80 33 }
jhunter029 0:57cec3469a80 34 }
jhunter029 0:57cec3469a80 35
jhunter029 0:57cec3469a80 36 void pc_recv()
jhunter029 0:57cec3469a80 37 {
jhunter029 0:57cec3469a80 38 led4 = !led4;
jhunter029 0:57cec3469a80 39 while(pc.readable()) {
jhunter029 0:57cec3469a80 40 esp.putc(pc.getc());
jhunter029 0:57cec3469a80 41 }
jhunter029 0:57cec3469a80 42 }
fepie3 1:75c100115242 43
fepie3 1:75c100115242 44 void callback()
fepie3 1:75c100115242 45 {
fepie3 1:75c100115242 46 led3=!led3;
fepie3 1:75c100115242 47 refresh();
fepie3 1:75c100115242 48 while (esp.readable()) {
fepie3 1:75c100115242 49 snd[count] = esp.getc();
fepie3 1:75c100115242 50 count++;
fepie3 1:75c100115242 51 }
fepie3 1:75c100115242 52 if(strlen(snd)>bufflen) {
fepie3 1:75c100115242 53 DataRX=1;
fepie3 1:75c100115242 54 led3=0;
fepie3 1:75c100115242 55 }
fepie3 1:75c100115242 56 }
jhunter029 0:57cec3469a80 57
jhunter029 0:57cec3469a80 58 int main()
jhunter029 0:57cec3469a80 59 {
jhunter029 0:57cec3469a80 60 reset=0; //hardware reset for 8266
jhunter029 0:57cec3469a80 61 pc.baud(9600); // set what you want here depending on your terminal program speed
jhunter029 0:57cec3469a80 62 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
jhunter029 0:57cec3469a80 63 wait(0.5);
jhunter029 0:57cec3469a80 64 reset=1;
jhunter029 0:57cec3469a80 65 timeout=2;
jhunter029 0:57cec3469a80 66 getreply();
fepie3 1:75c100115242 67 bufflen=200;
jhunter029 0:57cec3469a80 68
jhunter029 0:57cec3469a80 69 esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time.
jhunter029 0:57cec3469a80 70
jhunter029 0:57cec3469a80 71 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ******************
jhunter029 0:57cec3469a80 72
fepie3 1:75c100115242 73 //ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
fepie3 1:75c100115242 74 //wait(10);
fepie3 1:75c100115242 75 startserver();
jhunter029 0:57cec3469a80 76
jhunter029 0:57cec3469a80 77
jhunter029 0:57cec3469a80 78
fepie3 1:75c100115242 79 // pc.attach(&pc_recv, Serial::RxIrq);
fepie3 1:75c100115242 80 // esp.attach(&dev_recv, Serial::RxIrq);
jhunter029 0:57cec3469a80 81
jhunter029 0:57cec3469a80 82 // continuosly get AP list and IP
jhunter029 0:57cec3469a80 83 while(1) {
fepie3 1:75c100115242 84 if(DataRX==1) {
fepie3 1:75c100115242 85 ReadWebData();
fepie3 1:75c100115242 86 if (servreq == 1) {
fepie3 1:75c100115242 87 startserver();
fepie3 1:75c100115242 88 }
fepie3 1:75c100115242 89 esp.attach(&callback);
fepie3 1:75c100115242 90 pc.printf(" -------------------------------------\n\n");
fepie3 1:75c100115242 91 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
fepie3 1:75c100115242 92 pc.printf(" -------------------------------------\n\n");
fepie3 1:75c100115242 93 servreq=0;
fepie3 1:75c100115242 94 }
jhunter029 0:57cec3469a80 95 }
jhunter029 0:57cec3469a80 96
jhunter029 0:57cec3469a80 97 }
jhunter029 0:57cec3469a80 98
jhunter029 0:57cec3469a80 99 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
jhunter029 0:57cec3469a80 100 void ESPsetbaudrate()
jhunter029 0:57cec3469a80 101 {
jhunter029 0:57cec3469a80 102 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
jhunter029 0:57cec3469a80 103 SendCMD();
jhunter029 0:57cec3469a80 104 }
jhunter029 0:57cec3469a80 105
jhunter029 0:57cec3469a80 106 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
jhunter029 0:57cec3469a80 107 void ESPconfig()
jhunter029 0:57cec3469a80 108 {
jhunter029 0:57cec3469a80 109
jhunter029 0:57cec3469a80 110 wait(5);
jhunter029 0:57cec3469a80 111 pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
jhunter029 0:57cec3469a80 112 strcpy(snd,".\r\n.\r\n");
jhunter029 0:57cec3469a80 113 SendCMD();
jhunter029 0:57cec3469a80 114 wait(1);
jhunter029 0:57cec3469a80 115 pc.printf("---------- Reset & get Firmware ----------\r\n");
jhunter029 0:57cec3469a80 116 strcpy(snd,"node.restart()\r\n");
jhunter029 0:57cec3469a80 117 SendCMD();
jhunter029 0:57cec3469a80 118 timeout=5;
jhunter029 0:57cec3469a80 119 getreply();
jhunter029 0:57cec3469a80 120 pc.printf(buf);
jhunter029 0:57cec3469a80 121
jhunter029 0:57cec3469a80 122 wait(2);
jhunter029 0:57cec3469a80 123
jhunter029 0:57cec3469a80 124 pc.printf("\n---------- Get Version ----------\r\n");
jhunter029 0:57cec3469a80 125 strcpy(snd,"print(node.info())\r\n");
jhunter029 0:57cec3469a80 126 SendCMD();
jhunter029 0:57cec3469a80 127 timeout=4;
jhunter029 0:57cec3469a80 128 getreply();
jhunter029 0:57cec3469a80 129 pc.printf(buf);
jhunter029 0:57cec3469a80 130
jhunter029 0:57cec3469a80 131 wait(3);
jhunter029 0:57cec3469a80 132
jhunter029 0:57cec3469a80 133 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
jhunter029 0:57cec3469a80 134 pc.printf("\n---------- Setting Mode ----------\r\n");
jhunter029 0:57cec3469a80 135 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
jhunter029 0:57cec3469a80 136 SendCMD();
jhunter029 0:57cec3469a80 137 timeout=4;
jhunter029 0:57cec3469a80 138 getreply();
jhunter029 0:57cec3469a80 139 pc.printf(buf);
jhunter029 0:57cec3469a80 140
jhunter029 0:57cec3469a80 141 wait(2);
jhunter029 0:57cec3469a80 142
jhunter029 0:57cec3469a80 143
jhunter029 0:57cec3469a80 144
jhunter029 0:57cec3469a80 145 pc.printf("\n---------- Listing Access Points ----------\r\n");
jhunter029 0:57cec3469a80 146 strcpy(snd, "function listap(t)\r\n");
jhunter029 0:57cec3469a80 147 SendCMD();
jhunter029 0:57cec3469a80 148 wait(1);
jhunter029 0:57cec3469a80 149 strcpy(snd, "for k,v in pairs(t) do\r\n");
jhunter029 0:57cec3469a80 150 SendCMD();
jhunter029 0:57cec3469a80 151 wait(1);
jhunter029 0:57cec3469a80 152 strcpy(snd, "print(k..\" : \"..v)\r\n");
jhunter029 0:57cec3469a80 153 SendCMD();
jhunter029 0:57cec3469a80 154 wait(1);
jhunter029 0:57cec3469a80 155 strcpy(snd, "end\r\n");
jhunter029 0:57cec3469a80 156 SendCMD();
jhunter029 0:57cec3469a80 157 wait(1);
jhunter029 0:57cec3469a80 158 strcpy(snd, "end\r\n");
jhunter029 0:57cec3469a80 159 SendCMD();
jhunter029 0:57cec3469a80 160 wait(1);
jhunter029 0:57cec3469a80 161 strcpy(snd, "wifi.sta.getap(listap)\r\n");
jhunter029 0:57cec3469a80 162 SendCMD();
jhunter029 0:57cec3469a80 163 wait(1);
jhunter029 0:57cec3469a80 164 timeout=15;
jhunter029 0:57cec3469a80 165 getreply();
jhunter029 0:57cec3469a80 166 pc.printf(buf);
jhunter029 0:57cec3469a80 167
jhunter029 0:57cec3469a80 168 wait(2);
jhunter029 0:57cec3469a80 169
jhunter029 0:57cec3469a80 170 pc.printf("\n---------- Connecting to AP ----------\r\n");
jhunter029 0:57cec3469a80 171 pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
jhunter029 0:57cec3469a80 172 strcpy(snd, "wifi.sta.config(\"");
jhunter029 0:57cec3469a80 173 strcat(snd, ssid);
jhunter029 0:57cec3469a80 174 strcat(snd, "\",\"");
jhunter029 0:57cec3469a80 175 strcat(snd, pwd);
jhunter029 0:57cec3469a80 176 strcat(snd, "\")\r\n");
jhunter029 0:57cec3469a80 177 SendCMD();
jhunter029 0:57cec3469a80 178 timeout=10;
jhunter029 0:57cec3469a80 179 getreply();
jhunter029 0:57cec3469a80 180 pc.printf(buf);
jhunter029 0:57cec3469a80 181
jhunter029 0:57cec3469a80 182 wait(5);
jhunter029 0:57cec3469a80 183
jhunter029 0:57cec3469a80 184 pc.printf("\n---------- Get IP's ----------\r\n");
jhunter029 0:57cec3469a80 185 strcpy(snd, "print(wifi.sta.getip())\r\n");
jhunter029 0:57cec3469a80 186 SendCMD();
jhunter029 0:57cec3469a80 187 timeout=3;
jhunter029 0:57cec3469a80 188 getreply();
jhunter029 0:57cec3469a80 189 pc.printf(buf);
jhunter029 0:57cec3469a80 190
jhunter029 0:57cec3469a80 191 wait(1);
jhunter029 0:57cec3469a80 192
jhunter029 0:57cec3469a80 193 pc.printf("\n---------- Get Connection Status ----------\r\n");
jhunter029 0:57cec3469a80 194 strcpy(snd, "print(wifi.sta.status())\r\n");
jhunter029 0:57cec3469a80 195 SendCMD();
jhunter029 0:57cec3469a80 196 timeout=5;
jhunter029 0:57cec3469a80 197 getreply();
jhunter029 0:57cec3469a80 198 pc.printf(buf);
jhunter029 0:57cec3469a80 199
jhunter029 0:57cec3469a80 200 pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n");
jhunter029 0:57cec3469a80 201 pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n");
jhunter029 0:57cec3469a80 202 pc.printf(" It saves the SSID and password settings internally\r\n");
fepie3 1:75c100115242 203 pc.printf("\r\nDONE");
fepie3 1:75c100115242 204 }
jhunter029 0:57cec3469a80 205
fepie3 1:75c100115242 206 void startserver()
fepie3 1:75c100115242 207 {
fepie3 1:75c100115242 208 pc.printf("\n---------- Get IP's ----------\r\n");
fepie3 1:75c100115242 209 strcpy(snd, "print(wifi.sta.getip())\r\n");
fepie3 1:75c100115242 210 SendCMD();
fepie3 1:75c100115242 211 timeout=3;
fepie3 1:75c100115242 212 getreply();
fepie3 1:75c100115242 213 pc.printf(buf);
fepie3 1:75c100115242 214 gettemp();
fepie3 1:75c100115242 215
fepie3 1:75c100115242 216 wait(1);
fepie3 1:75c100115242 217 pc.printf("\n---------- Setting up http server ----------\r\n");
fepie3 1:75c100115242 218 strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
jhunter029 0:57cec3469a80 219 SendCMD();
jhunter029 0:57cec3469a80 220 wait(1);
jhunter029 0:57cec3469a80 221 strcpy(snd, "srv:listen(80,function(conn)\r\n");
jhunter029 0:57cec3469a80 222 SendCMD();
jhunter029 0:57cec3469a80 223 wait(1);
jhunter029 0:57cec3469a80 224 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
jhunter029 0:57cec3469a80 225 SendCMD();
jhunter029 0:57cec3469a80 226 wait(1);
jhunter029 0:57cec3469a80 227 strcpy(snd, "print(payload)\r\n");
jhunter029 0:57cec3469a80 228 SendCMD();
fepie3 1:75c100115242 229 wait(5);
jhunter029 0:57cec3469a80 230
jhunter029 0:57cec3469a80 231 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
jhunter029 0:57cec3469a80 232 SendCMD();
fepie3 1:75c100115242 233 wait(1);
jhunter029 0:57cec3469a80 234
jhunter029 0:57cec3469a80 235 strcpy(snd, "conn:send(\"<html>\")\r\n");
jhunter029 0:57cec3469a80 236 SendCMD();
fepie3 1:75c100115242 237 wait(1);
fepie3 1:75c100115242 238
fepie3 1:75c100115242 239 strcpy(snd, "conn:send(\"<body>\")\r\n");
fepie3 1:75c100115242 240 SendCMD();
fepie3 1:75c100115242 241 wait(1);
jhunter029 0:57cec3469a80 242
fepie3 1:75c100115242 243 strcpy(snd, "conn:send(\"<h1> IOT Smart Socket</h1>\")\r\n");
jhunter029 0:57cec3469a80 244 SendCMD();
jhunter029 0:57cec3469a80 245 wait(1);
jhunter029 0:57cec3469a80 246
fepie3 1:75c100115242 247 strcpy(snd, "conn:send(\"<h2> Turn Smart Socket On and Off</h2>\")\r\n");
fepie3 1:75c100115242 248 SendCMD();
fepie3 1:75c100115242 249 wait(1);
fepie3 1:75c100115242 250
fepie3 1:75c100115242 251 strcpy(snd, "conn:send(\"<p><form><strong> Power:&nbsp&nbsp<input type='text' size=6 value='\")\r\n");
fepie3 1:75c100115242 252 SendCMD();
fepie3 1:75c100115242 253 wait(1);
fepie3 1:75c100115242 254
fepie3 1:75c100115242 255 sprintf(snd, "conn:send(\"%s'\")\r\n", temp);
fepie3 1:75c100115242 256 SendCMD();
fepie3 1:75c100115242 257 wait(1);
fepie3 1:75c100115242 258
fepie3 1:75c100115242 259 strcpy(snd, "conn:send(\"</sup>Watts\")\r\n");
fepie3 1:75c100115242 260 SendCMD();
fepie3 1:75c100115242 261 wait(1);
fepie3 1:75c100115242 262
fepie3 1:75c100115242 263 if(led2==0) {
fepie3 1:75c100115242 264
fepie3 1:75c100115242 265 strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0' checked> Socket off\")\r\n");
fepie3 1:75c100115242 266 SendCMD();
fepie3 1:75c100115242 267 wait(1);
fepie3 1:75c100115242 268
fepie3 1:75c100115242 269 strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' > Socket on\")\r\n");
fepie3 1:75c100115242 270 SendCMD();
fepie3 1:75c100115242 271 wait(1);
fepie3 1:75c100115242 272 } else {
fepie3 1:75c100115242 273 strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0'> Socket off\")\r\n");
fepie3 1:75c100115242 274 SendCMD();
fepie3 1:75c100115242 275 wait(1);
fepie3 1:75c100115242 276
fepie3 1:75c100115242 277 strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' checked> Socket on\")\r\n");
fepie3 1:75c100115242 278 SendCMD();
fepie3 1:75c100115242 279 wait(1);
fepie3 1:75c100115242 280 }
fepie3 1:75c100115242 281
fepie3 1:75c100115242 282
fepie3 1:75c100115242 283 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");
fepie3 1:75c100115242 284 SendCMD();
fepie3 1:75c100115242 285 ReadWebData();
fepie3 1:75c100115242 286 wait(1);
fepie3 1:75c100115242 287
fepie3 1:75c100115242 288 //strcpy(snd, "conn:send(\"</form>\")\r\n");
fepie3 1:75c100115242 289 //SendCMD();
fepie3 1:75c100115242 290 //wait(1);
fepie3 1:75c100115242 291
fepie3 1:75c100115242 292 strcpy(snd, "conn:send(\"</body>\")\r\n");
jhunter029 0:57cec3469a80 293 SendCMD();
jhunter029 0:57cec3469a80 294 wait(1);
jhunter029 0:57cec3469a80 295
jhunter029 0:57cec3469a80 296 strcpy(snd, "conn:send(\"</html>\")\r\n");
fepie3 1:75c100115242 297 SendCMD();
fepie3 1:75c100115242 298 wait(1);
jhunter029 0:57cec3469a80 299
jhunter029 0:57cec3469a80 300 strcpy(snd, "end)\r\n");
fepie3 1:75c100115242 301 SendCMD();
fepie3 1:75c100115242 302 wait(1);
jhunter029 0:57cec3469a80 303
jhunter029 0:57cec3469a80 304 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
fepie3 1:75c100115242 305 SendCMD();
fepie3 1:75c100115242 306 wait(1);
fepie3 1:75c100115242 307
jhunter029 0:57cec3469a80 308 strcpy(snd, "end)\r\n");
fepie3 1:75c100115242 309 SendCMD();
fepie3 1:75c100115242 310 wait(1);
jhunter029 0:57cec3469a80 311 timeout=17;
fepie3 1:75c100115242 312
jhunter029 0:57cec3469a80 313 getreply();
jhunter029 0:57cec3469a80 314 pc.printf(buf);
fepie3 1:75c100115242 315 pc.printf("\r\nServer Started");
fepie3 1:75c100115242 316 //ReadWebData();
fepie3 1:75c100115242 317 esp.attach(&callback);
jhunter029 0:57cec3469a80 318 }
jhunter029 0:57cec3469a80 319
jhunter029 0:57cec3469a80 320 void SendCMD()
jhunter029 0:57cec3469a80 321 {
jhunter029 0:57cec3469a80 322 esp.printf("%s", snd);
jhunter029 0:57cec3469a80 323 }
jhunter029 0:57cec3469a80 324
jhunter029 0:57cec3469a80 325 void getreply()
jhunter029 0:57cec3469a80 326 {
jhunter029 0:57cec3469a80 327 memset(buf, '\0', sizeof(buf));
jhunter029 0:57cec3469a80 328 t.start();
jhunter029 0:57cec3469a80 329 ended=0;
jhunter029 0:57cec3469a80 330 count=0;
jhunter029 0:57cec3469a80 331 while(!ended) {
jhunter029 0:57cec3469a80 332 if(esp.readable()) {
jhunter029 0:57cec3469a80 333 buf[count] = esp.getc();
jhunter029 0:57cec3469a80 334 count++;
jhunter029 0:57cec3469a80 335 }
jhunter029 0:57cec3469a80 336 if(t.read() > timeout) {
jhunter029 0:57cec3469a80 337 ended = 1;
jhunter029 0:57cec3469a80 338 t.stop();
jhunter029 0:57cec3469a80 339 t.reset();
jhunter029 0:57cec3469a80 340 }
jhunter029 0:57cec3469a80 341 }
jhunter029 0:57cec3469a80 342 }
fepie3 1:75c100115242 343 void ReadWebData()
fepie3 1:75c100115242 344 {
fepie3 1:75c100115242 345 wait_ms(200);
fepie3 1:75c100115242 346 esp.attach(NULL);
fepie3 1:75c100115242 347 DataRX=0;
fepie3 1:75c100115242 348 memset(webdata, '\0', sizeof(webdata));
fepie3 1:75c100115242 349 int x = strcspn (snd,"+");
fepie3 1:75c100115242 350 if(x) {
fepie3 1:75c100115242 351 strcpy(webdata, snd + x);
fepie3 1:75c100115242 352 //int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
fepie3 1:75c100115242 353 if( strstr(webdata, "led2=1") != NULL ) {
fepie3 1:75c100115242 354 led2=1;
fepie3 1:75c100115242 355 }
fepie3 1:75c100115242 356 if( strstr(webdata, "led2=0") != NULL ) {
fepie3 1:75c100115242 357 led2=0;
fepie3 1:75c100115242 358 }
fepie3 1:75c100115242 359 /* if( strstr(webdata, "Out1=1") != NULL ) {
fepie3 1:75c100115242 360 Out1=1;
fepie3 1:75c100115242 361 }
fepie3 1:75c100115242 362 if( strstr(webdata, "Out1=0") != NULL ) {
fepie3 1:75c100115242 363 Out1=0;
fepie3 1:75c100115242 364 }
fepie3 1:75c100115242 365 if( strstr(webdata, "Out2=1") != NULL ) {
fepie3 1:75c100115242 366 Out2=1;
fepie3 1:75c100115242 367 }
fepie3 1:75c100115242 368 if( strstr(webdata, "Out2=0") != NULL ) {
fepie3 1:75c100115242 369 Out2=0;
fepie3 1:75c100115242 370 }
fepie3 1:75c100115242 371 if( strstr(webdata, "Out3=1") != NULL ) {
fepie3 1:75c100115242 372 Out3=1;
fepie3 1:75c100115242 373 }
fepie3 1:75c100115242 374 if( strstr(webdata, "Out3=0") != NULL ) {
fepie3 1:75c100115242 375 Out3=0;
fepie3 1:75c100115242 376 } */
fepie3 1:75c100115242 377 //sprintf(channel, "%d",linkID);
fepie3 1:75c100115242 378 if (strstr(webdata, "GET") != NULL) {
fepie3 1:75c100115242 379 servreq=1;
fepie3 1:75c100115242 380 }
fepie3 1:75c100115242 381 if (strstr(webdata, "POST") != NULL) {
fepie3 1:75c100115242 382 servreq=1;
fepie3 1:75c100115242 383 }
fepie3 1:75c100115242 384 webcounter++;
fepie3 1:75c100115242 385 sprintf(webcount, "%d",webcounter);
fepie3 1:75c100115242 386 } else {
fepie3 1:75c100115242 387 memset(snd, '\0', sizeof(snd));
fepie3 1:75c100115242 388 esp.attach(&callback);
fepie3 1:75c100115242 389 }
fepie3 1:75c100115242 390 }
fepie3 1:75c100115242 391 void SendWEB()
fepie3 1:75c100115242 392 {
fepie3 1:75c100115242 393 int i=0;
fepie3 1:75c100115242 394 if(esp.writeable()) {
fepie3 1:75c100115242 395 while(webbuff[i]!='\0') {
fepie3 1:75c100115242 396 esp.putc(webbuff[i]);
fepie3 1:75c100115242 397 i++;
fepie3 1:75c100115242 398 }
fepie3 1:75c100115242 399 }
fepie3 1:75c100115242 400 }
fepie3 1:75c100115242 401 void gettemp()
fepie3 1:75c100115242 402 {
fepie3 1:75c100115242 403
fepie3 1:75c100115242 404 AdcIn=Ain2.read();
fepie3 1:75c100115242 405 Ht = (AdcIn*120); // set the numeric to the exact MCU analog reference voltage for greater accuracy
fepie3 1:75c100115242 406 sprintf(temp,"%2.3f",Ht);
fepie3 1:75c100115242 407 //pc.printf(temp);
fepie3 1:75c100115242 408 }
fepie3 1:75c100115242 409 void refresh()
fepie3 1:75c100115242 410 {
jhunter029 0:57cec3469a80 411
fepie3 1:75c100115242 412 out1=!out1;
fepie3 1:75c100115242 413 if (out1==0){
fepie3 1:75c100115242 414 sprintf(temp,"%2.3d",0);}
fepie3 1:75c100115242 415 else {
fepie3 1:75c100115242 416 gettemp();}
fepie3 1:75c100115242 417 wait(.2);
fepie3 1:75c100115242 418 pc.printf("\n---------- Restarted ----------\r\n");
fepie3 1:75c100115242 419 strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
fepie3 1:75c100115242 420 SendCMD();
fepie3 1:75c100115242 421 wait(.2);
fepie3 1:75c100115242 422 strcpy(snd, "srv:listen(80,function(conn)\r\n");
fepie3 1:75c100115242 423 SendCMD();
fepie3 1:75c100115242 424 wait(.2);
fepie3 1:75c100115242 425 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
fepie3 1:75c100115242 426 SendCMD();
fepie3 1:75c100115242 427 wait(.2);
fepie3 1:75c100115242 428 strcpy(snd, "print(payload)\r\n");
fepie3 1:75c100115242 429 SendCMD();
fepie3 1:75c100115242 430 wait(.3);
fepie3 1:75c100115242 431
fepie3 1:75c100115242 432 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
fepie3 1:75c100115242 433 SendCMD();
fepie3 1:75c100115242 434 wait(.2);
fepie3 1:75c100115242 435
fepie3 1:75c100115242 436 strcpy(snd, "conn:send(\"<html>\")\r\n");
fepie3 1:75c100115242 437 SendCMD();
fepie3 1:75c100115242 438 wait(.2);
fepie3 1:75c100115242 439
fepie3 1:75c100115242 440 strcpy(snd, "conn:send(\"<body>\")\r\n");
fepie3 1:75c100115242 441 SendCMD();
fepie3 1:75c100115242 442 wait(.2);
fepie3 1:75c100115242 443
fepie3 1:75c100115242 444 strcpy(snd, "conn:send(\"<h1> IOT Smart Socket</h1>\")\r\n");
fepie3 1:75c100115242 445 SendCMD();
fepie3 1:75c100115242 446 wait(.2);
fepie3 1:75c100115242 447
fepie3 1:75c100115242 448 strcpy(snd, "conn:send(\"<h2> Turn Smart Socket On and Off</h2>\")\r\n");
fepie3 1:75c100115242 449 SendCMD();
fepie3 1:75c100115242 450 wait(.2);
fepie3 1:75c100115242 451
fepie3 1:75c100115242 452 strcpy(snd, "conn:send(\"<p><form><strong> Power:&nbsp&nbsp<input type='text' size=6 value='\")\r\n");
fepie3 1:75c100115242 453 SendCMD();
fepie3 1:75c100115242 454 wait(.2);
fepie3 1:75c100115242 455
fepie3 1:75c100115242 456 sprintf(snd, "conn:send(\"%s'\")\r\n", temp);
fepie3 1:75c100115242 457 SendCMD();
fepie3 1:75c100115242 458 wait(.2);
fepie3 1:75c100115242 459
fepie3 1:75c100115242 460 strcpy(snd, "conn:send(\"</sup>Watts\")\r\n");
fepie3 1:75c100115242 461 SendCMD();
fepie3 1:75c100115242 462 wait(.2);
fepie3 1:75c100115242 463
fepie3 1:75c100115242 464 if(out1==0) {
fepie3 1:75c100115242 465
fepie3 1:75c100115242 466 strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='0' checked> Socket off\")\r\n");
fepie3 1:75c100115242 467 SendCMD();
fepie3 1:75c100115242 468 wait(.2);
fepie3 1:75c100115242 469
fepie3 1:75c100115242 470 strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='1' > Socket on\")\r\n");
fepie3 1:75c100115242 471 SendCMD();
fepie3 1:75c100115242 472 wait(.2);
fepie3 1:75c100115242 473 } else {
fepie3 1:75c100115242 474 strcpy(snd, "conn:send(\"<p><input type='radio' name='led2' value='1'> Socket off\")\r\n");
fepie3 1:75c100115242 475 SendCMD();
fepie3 1:75c100115242 476 wait(.2);
fepie3 1:75c100115242 477
fepie3 1:75c100115242 478 strcpy(snd, "conn:send(\"<input type='radio' name='led2' value='0' checked> Socket on\")\r\n");
fepie3 1:75c100115242 479 SendCMD();
fepie3 1:75c100115242 480 wait(.2);
fepie3 1:75c100115242 481 }
fepie3 1:75c100115242 482
fepie3 1:75c100115242 483
fepie3 1:75c100115242 484 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");
fepie3 1:75c100115242 485 SendCMD();
fepie3 1:75c100115242 486 ReadWebData();
fepie3 1:75c100115242 487 wait(.2);
fepie3 1:75c100115242 488
fepie3 1:75c100115242 489
fepie3 1:75c100115242 490 strcpy(snd, "conn:send(\"</body>\")\r\n");
fepie3 1:75c100115242 491 SendCMD();
fepie3 1:75c100115242 492 wait(.2);
fepie3 1:75c100115242 493
fepie3 1:75c100115242 494 strcpy(snd, "conn:send(\"</html>\")\r\n");
fepie3 1:75c100115242 495 SendCMD();
fepie3 1:75c100115242 496 wait(.2);
fepie3 1:75c100115242 497
fepie3 1:75c100115242 498 strcpy(snd, "end)\r\n");
fepie3 1:75c100115242 499 SendCMD();
fepie3 1:75c100115242 500 wait(.3);
fepie3 1:75c100115242 501
fepie3 1:75c100115242 502 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
fepie3 1:75c100115242 503 SendCMD();
fepie3 1:75c100115242 504 wait(.3);
fepie3 1:75c100115242 505
fepie3 1:75c100115242 506 strcpy(snd, "end)\r\n");
fepie3 1:75c100115242 507 SendCMD();
fepie3 1:75c100115242 508 wait(.3);
fepie3 1:75c100115242 509 timeout=17;
fepie3 1:75c100115242 510
fepie3 1:75c100115242 511 getreply();
fepie3 1:75c100115242 512 pc.printf(buf);
fepie3 1:75c100115242 513 pc.printf("\r\nRestarted");
fepie3 1:75c100115242 514 //ReadWebData();
fepie3 1:75c100115242 515 esp.attach(&callback);
fepie3 1:75c100115242 516 }
fepie3 1:75c100115242 517