Demo code for a Wi-Fi controlled wall outlet that monitors light intensity, temperature, and motion and can control which plugs are supplied with power based off of user preference.

Dependencies:   BH1750 DHT11_Lib mbed

Fork of ECE4180_Lab4_ESP8266_IoT_Outlets by Deron Mai

Committer:
deronmai
Date:
Thu Mar 17 21:37:39 2016 +0000
Revision:
5:280650ceb796
Parent:
4:40dd020463ea
Child:
6:66b21f6166bd
init commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:e2a155f50119 1 // ESP8266 Static page WEB server to control Mbed
star297 0:e2a155f50119 2
star297 0:e2a155f50119 3 #include "mbed.h"
deronmai 5:280650ceb796 4 #include "DHT.h"
4180_1 4:40dd020463ea 5 //#include "DS18B20.h"
star297 0:e2a155f50119 6
star297 0:e2a155f50119 7 Serial pc(USBTX, USBRX);
4180_1 4:40dd020463ea 8 Serial esp(p28, p27); // tx, rx
star297 0:e2a155f50119 9
4180_1 4:40dd020463ea 10 //DS18B20 thermom(A0, DS18B20::RES_12_BIT);
star297 2:d4c6bc0f2dc4 11
star297 0:e2a155f50119 12 // Standard Mbed LED definitions
4180_1 4:40dd020463ea 13 DigitalOut led1(LED1); // (PTB18)
4180_1 4:40dd020463ea 14 DigitalOut led2(LED2); // (PTB19)
4180_1 4:40dd020463ea 15 DigitalOut led3(LED3); // (PTD1)
deronmai 5:280650ceb796 16 DigitalOut led4(LED4);
star297 0:e2a155f50119 17
star297 0:e2a155f50119 18 // Digital Out and In pins, can be configured to any suitable pin depending on Platform
deronmai 5:280650ceb796 19 DigitalOut Outlet1(p25);
deronmai 5:280650ceb796 20 DigitalOut Outlet2(p24);
deronmai 5:280650ceb796 21 DigitalOut Outlet3(p23);
deronmai 5:280650ceb796 22 DigitalOut Outlet4(p22);
4180_1 4:40dd020463ea 23 DigitalOut reset(p26);
star297 0:e2a155f50119 24
deronmai 5:280650ceb796 25 int setOut2=0; // if 0, turn off when temp >
deronmai 5:280650ceb796 26 // if 1, turn on when temp >
deronmai 5:280650ceb796 27
deronmai 5:280650ceb796 28 DHT tsensor(p21, DHT11);
star297 0:e2a155f50119 29
deronmai 5:280650ceb796 30 AnalogIn I1(p20);
deronmai 5:280650ceb796 31 AnalogIn Ain1(p19);
deronmai 5:280650ceb796 32
deronmai 5:280650ceb796 33 //PwmOut speaker(p21);
deronmai 5:280650ceb796 34 //AnalogIn Ain1(p18);
deronmai 5:280650ceb796 35 //AnalogIn Ain2(p19);
star297 0:e2a155f50119 36
star297 0:e2a155f50119 37 Timer t1;
star297 1:71ed1afbf344 38 Timer t2;
star297 1:71ed1afbf344 39
star297 1:71ed1afbf344 40 struct tm t;
star297 0:e2a155f50119 41
star297 3:f7febfa77784 42 int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
star297 2:d4c6bc0f2dc4 43 int bufl, ipdLen, linkID, weberror, webcounter;
deronmai 5:280650ceb796 44 float temperature, AdcIn, Ht, Tf, Tc, Tk, Hm, Current1;
star297 0:e2a155f50119 45 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
star297 0:e2a155f50119 46 char Vcc[10];
star297 0:e2a155f50119 47 char Temp[10];
star297 0:e2a155f50119 48 char temp[10];
deronmai 5:280650ceb796 49 char Hum[10];
deronmai 5:280650ceb796 50 char Cur1[10];
star297 2:d4c6bc0f2dc4 51 char webcount[8];
star297 3:f7febfa77784 52 char lasthit[30];
star297 0:e2a155f50119 53 char timebuf[30];
star297 0:e2a155f50119 54 char type[16];
star297 0:e2a155f50119 55 char type1[16];
star297 0:e2a155f50119 56 char channel[2];
star297 3:f7febfa77784 57 char cmdbuff[32];
4180_1 4:40dd020463ea 58 char replybuff[1024];
star297 0:e2a155f50119 59 char webdata[1024]; // This may need to be bigger depending on WEB browser used
star297 3:f7febfa77784 60 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
deronmai 5:280650ceb796 61 int tmpval1;
deronmai 5:280650ceb796 62 int tmpval2;
star297 0:e2a155f50119 63
star297 0:e2a155f50119 64 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
star297 2:d4c6bc0f2dc4 65 void gettime(),gettemp(),getbattery(),setRTC(),beep();
star297 0:e2a155f50119 66
deronmai 5:280650ceb796 67 int tmpOn=70, tmpOff=70;
deronmai 5:280650ceb796 68 char setTmpOn[10];
deronmai 5:280650ceb796 69 char setTmpOff[10];
star297 1:71ed1afbf344 70 // manual set RTC values
4180_1 4:40dd020463ea 71 int minute =00; // 0-59
4180_1 4:40dd020463ea 72 int hour =12; // 2-23
4180_1 4:40dd020463ea 73 int dayofmonth =26; // 1-31
4180_1 4:40dd020463ea 74 int month =8; // 1-12
star297 1:71ed1afbf344 75 int year =15; // last 2 digits
star297 2:d4c6bc0f2dc4 76
4180_1 4:40dd020463ea 77 int port =80; // set server port
4180_1 4:40dd020463ea 78 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
4180_1 4:40dd020463ea 79
star297 0:e2a155f50119 80 // Serial Interrupt read ESP data
4180_1 4:40dd020463ea 81 void callback()
4180_1 4:40dd020463ea 82 {
4180_1 4:40dd020463ea 83 led3=1;
4180_1 4:40dd020463ea 84 while (esp.readable()) {
4180_1 4:40dd020463ea 85 webbuff[count] = esp.getc();
4180_1 4:40dd020463ea 86 count++;
4180_1 4:40dd020463ea 87 }
4180_1 4:40dd020463ea 88 if(strlen(webbuff)>bufflen) {
4180_1 4:40dd020463ea 89 DataRX=1;
4180_1 4:40dd020463ea 90 led3=0;
4180_1 4:40dd020463ea 91 }
star297 0:e2a155f50119 92 }
star297 0:e2a155f50119 93
4180_1 4:40dd020463ea 94 int main()
4180_1 4:40dd020463ea 95 {
4180_1 4:40dd020463ea 96 reset=0;
deronmai 5:280650ceb796 97 pc.baud(9600);
4180_1 4:40dd020463ea 98
4180_1 4:40dd020463ea 99 pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r");
4180_1 4:40dd020463ea 100 wait(0.5);
4180_1 4:40dd020463ea 101 reset=1;
deronmai 5:280650ceb796 102 Outlet1=1, Outlet2=1, Outlet3=1, Outlet4=1;
deronmai 5:280650ceb796 103 led1=0,led2=0,led3=1;
4180_1 4:40dd020463ea 104 timeout=6000;
4180_1 4:40dd020463ea 105 getcount=500;
4180_1 4:40dd020463ea 106 getreply();
star297 3:f7febfa77784 107 esp.baud(115200); // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
4180_1 4:40dd020463ea 108 if (time(NULL) < 1420070400) {
4180_1 4:40dd020463ea 109 setRTC();
4180_1 4:40dd020463ea 110 }
star297 2:d4c6bc0f2dc4 111 beep();
star297 0:e2a155f50119 112 startserver();
4180_1 4:40dd020463ea 113
4180_1 4:40dd020463ea 114 while(1) {
deronmai 5:280650ceb796 115 if (setOut2 == 0){ // turn off when tmp >
deronmai 5:280650ceb796 116 if (Tf > tmpOff) {
deronmai 5:280650ceb796 117 Outlet2 = 1;
deronmai 5:280650ceb796 118 led2 = 0;
deronmai 5:280650ceb796 119 }
deronmai 5:280650ceb796 120 else {
deronmai 5:280650ceb796 121 Outlet2 = 0;
deronmai 5:280650ceb796 122 led2 = 1;
deronmai 5:280650ceb796 123 }
deronmai 5:280650ceb796 124 }
deronmai 5:280650ceb796 125 else { // turn on when tmp >
deronmai 5:280650ceb796 126 if (Tf > tmpOn) {
deronmai 5:280650ceb796 127 Outlet2 = 0;
deronmai 5:280650ceb796 128 led2 = 1;
deronmai 5:280650ceb796 129 }
deronmai 5:280650ceb796 130 else {
deronmai 5:280650ceb796 131 Outlet2 = 1;
deronmai 5:280650ceb796 132 led2 = 0;
deronmai 5:280650ceb796 133 }
deronmai 5:280650ceb796 134 }
4180_1 4:40dd020463ea 135 if(DataRX==1) {
star297 0:e2a155f50119 136 ReadWebData();
star297 2:d4c6bc0f2dc4 137 beep();
4180_1 4:40dd020463ea 138 if (servreq == 1 && weberror == 0) {
4180_1 4:40dd020463ea 139 sendpage();
4180_1 4:40dd020463ea 140 }
4180_1 4:40dd020463ea 141 esp.attach(&callback);
4180_1 4:40dd020463ea 142 pc.printf(" IPD Data:\r\n\n Link ID = %d,\r\n IPD Header Length = %d \r\n IPD Type = %s\r\n", linkID, ipdLen, type);
4180_1 4:40dd020463ea 143 pc.printf("\n\n HTTP Packet: \n\n%s\n", webdata);
star297 0:e2a155f50119 144 pc.printf(" Web Characters sent : %d\n\n", bufl);
star297 0:e2a155f50119 145 pc.printf(" -------------------------------------\n\n");
star297 2:d4c6bc0f2dc4 146 strcpy(lasthit, timebuf);
4180_1 4:40dd020463ea 147 servreq=0;
4180_1 4:40dd020463ea 148 }
star297 0:e2a155f50119 149 }
4180_1 4:40dd020463ea 150 }
4180_1 4:40dd020463ea 151 // Static WEB page
star297 0:e2a155f50119 152 void sendpage()
4180_1 4:40dd020463ea 153 {
star297 3:f7febfa77784 154 gettemp();
4180_1 4:40dd020463ea 155 getbattery();
4180_1 4:40dd020463ea 156 gettime();
4180_1 4:40dd020463ea 157
4180_1 4:40dd020463ea 158 // WEB page data
star297 3:f7febfa77784 159 strcpy(webbuff, "<!DOCTYPE html>");
deronmai 5:280650ceb796 160 strcat(webbuff, "<html><head><title>ESP8266 Mbed IoT Controlled Outlets</title></head>");
4180_1 4:40dd020463ea 161 strcat(webbuff, "<body>");
4180_1 4:40dd020463ea 162 strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web Controller</h1>");
star297 3:f7febfa77784 163 strcat(webbuff, "Hit Count - ");
star297 3:f7febfa77784 164 strcat(webbuff, webcount);
deronmai 5:280650ceb796 165 strcat(webbuff, "<br>Temp - ");
deronmai 5:280650ceb796 166 strcat(webbuff, Temp);
deronmai 5:280650ceb796 167 strcat(webbuff, "<br>3.3v Supply Voltage:&nbsp&nbsp"); //<input type=\"text\" size=4 value=\"
deronmai 5:280650ceb796 168 strcat(webbuff, Vcc);
deronmai 5:280650ceb796 169 strcat(webbuff, " </sup>V");
star297 3:f7febfa77784 170 strcat(webbuff, "</div><br /><hr>");
4180_1 4:40dd020463ea 171 strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");
star297 3:f7febfa77784 172 strcat(webbuff, timebuf);
star297 3:f7febfa77784 173 strcat(webbuff, "</h3>\r\n");
deronmai 5:280650ceb796 174 strcat(webbuff, "<p><form method=\"POST\"><strong> Outlet 1 Current:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
deronmai 5:280650ceb796 175 strcat(webbuff, Cur1);
deronmai 5:280650ceb796 176 strcat(webbuff, "\"> </sup>A <form method=\"POST\"> <strong> &nbsp&nbsp");
deronmai 5:280650ceb796 177 if(Outlet1==1) {
deronmai 5:280650ceb796 178 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" checked> Outlet 1 off");
deronmai 5:280650ceb796 179 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" > Outlet 1 on");
4180_1 4:40dd020463ea 180 } else {
deronmai 5:280650ceb796 181 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" > Outlet 1 off");
deronmai 5:280650ceb796 182 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" checked> Outlet 1 on");
4180_1 4:40dd020463ea 183 }
deronmai 5:280650ceb796 184 if(setOut2==0) {
deronmai 5:280650ceb796 185 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet2\" value=\"0\" checked> Outlet 2 off when temp >&nbsp&nbsp<input type=\"text\" name=\"tmpOff\" size=4 value=\"");
deronmai 5:280650ceb796 186 sprintf(setTmpOff, "%d", tmpOff);
deronmai 5:280650ceb796 187 strcat(webbuff,setTmpOff);
deronmai 5:280650ceb796 188 strcat(webbuff,"\"> </sup>");
deronmai 5:280650ceb796 189 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet2\" value=\"1\" > Outlet 2 on when temp >&nbsp&nbsp<input type=\"text\" name=\"tmpOn\" size=4 value=\"");
deronmai 5:280650ceb796 190 sprintf(setTmpOn, "%d", tmpOn);
deronmai 5:280650ceb796 191 strcat(webbuff,setTmpOn);
deronmai 5:280650ceb796 192 strcat(webbuff,"\"> </sup>");
4180_1 4:40dd020463ea 193 } else {
deronmai 5:280650ceb796 194 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet2\" value=\"0\" > Outlet 2 off when temp >&nbsp&nbsp<input type=\"text\" name=\"tmpOff\" size=4 value=\"");
deronmai 5:280650ceb796 195 sprintf(setTmpOff, "%d", tmpOff);
deronmai 5:280650ceb796 196 strcat(webbuff,setTmpOff);
deronmai 5:280650ceb796 197 strcat(webbuff,"\"> </sup>");
deronmai 5:280650ceb796 198 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet2\" value=\"1\" checked> Outlet 2 on when temp >&nbsp&nbsp<input type=\"text\" name=\"tmpOn\" size=4 value=\"");
deronmai 5:280650ceb796 199 sprintf(setTmpOn, "%d", tmpOn);
deronmai 5:280650ceb796 200 strcat(webbuff,setTmpOn);
deronmai 5:280650ceb796 201 strcat(webbuff,"\"> </sup>");
4180_1 4:40dd020463ea 202 }
deronmai 5:280650ceb796 203 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"1\" checked> Outlet 3 On");
deronmai 5:280650ceb796 204 /*if(Outlet3==0) {
deronmai 5:280650ceb796 205 strcat(webbuff, "<p><input type=\"radio\" name=\"Outlet 3\" value=\"0\" checked> Outlet 3 off");
deronmai 5:280650ceb796 206 strcat(webbuff, "<br><input type=\"radio\" name=\"Outlet 3\" value=\"1\" > Outlet 3 on");
4180_1 4:40dd020463ea 207 } else {
deronmai 5:280650ceb796 208 strcat(webbuff, "<p><input type=\"radio\" name=\"Outlet 3\" value=\"0\" > Outlet 3 off");
deronmai 5:280650ceb796 209 strcat(webbuff, "<br><input type=\"radio\" name=\"Outlet 3\" value=\"1\" checked> Outlet 3 on");
deronmai 5:280650ceb796 210 }*/
deronmai 5:280650ceb796 211 if(Outlet4==0) {
deronmai 5:280650ceb796 212 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" checked> Outlet 4 off");
deronmai 5:280650ceb796 213 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet4\" value=\"1\" > Outlet 4 on");
4180_1 4:40dd020463ea 214 } else {
deronmai 5:280650ceb796 215 strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" > Outlet 4 off");
deronmai 5:280650ceb796 216 strcat(webbuff, "<br><input type=\"radio\" name=\"outlet4\" value=\"1\" checked> Outlet 4 on");
4180_1 4:40dd020463ea 217 }
deronmai 5:280650ceb796 218 // if(I1==0) {
deronmai 5:280650ceb796 219 // strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1");
deronmai 5:280650ceb796 220 // } else {
deronmai 5:280650ceb796 221 // strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1");
deronmai 5:280650ceb796 222 // }
4180_1 4:40dd020463ea 223 strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
4180_1 4:40dd020463ea 224 strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
star297 3:f7febfa77784 225 strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);");
star297 3:f7febfa77784 226 strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
4180_1 4:40dd020463ea 227 strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
star297 3:f7febfa77784 228 strcat(webbuff, "background: #3cb0fd;");
4180_1 4:40dd020463ea 229 strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);");
star297 3:f7febfa77784 230 strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);");
deronmai 5:280650ceb796 231 strcat(webbuff, "text-decoration:none;\"></form></span></p>");
deronmai 5:280650ceb796 232 // strcat(webbuff, "<p/><h2>How to use:</h2><ul>");
deronmai 5:280650ceb796 233 // strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>");
deronmai 5:280650ceb796 234 // strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>");
deronmai 5:280650ceb796 235 // strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>");
deronmai 5:280650ceb796 236 // strcat(webbuff, "</ul>");
4180_1 4:40dd020463ea 237 strcat(webbuff, "</body></html>");
4180_1 4:40dd020463ea 238 // end of WEB page data
4180_1 4:40dd020463ea 239 bufl = strlen(webbuff); // get total page buffer length
star297 3:f7febfa77784 240 sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
4180_1 4:40dd020463ea 241 timeout=200;
4180_1 4:40dd020463ea 242 getcount=7;
star297 3:f7febfa77784 243 SendCMD();
4180_1 4:40dd020463ea 244 getreply();
star297 0:e2a155f50119 245 SendWEB(); // send web page
star297 3:f7febfa77784 246 memset(webbuff, '\0', sizeof(webbuff));
4180_1 4:40dd020463ea 247 sendcheck();
star297 0:e2a155f50119 248 }
star297 0:e2a155f50119 249
star297 3:f7febfa77784 250 // wait for ESP "SEND OK" reply, then close IP to load web page
star297 0:e2a155f50119 251 void sendcheck()
star297 0:e2a155f50119 252 {
4180_1 4:40dd020463ea 253 weberror=1;
4180_1 4:40dd020463ea 254 timeout=500;
4180_1 4:40dd020463ea 255 getcount=24;
4180_1 4:40dd020463ea 256 t2.reset();
4180_1 4:40dd020463ea 257 t2.start();
4180_1 4:40dd020463ea 258 while(weberror==1 && t2.read() <5) {
star297 0:e2a155f50119 259 getreply();
4180_1 4:40dd020463ea 260 if (strstr(replybuff, "SEND OK") != NULL) {
4180_1 4:40dd020463ea 261 weberror=0; // wait for valid SEND OK
star297 0:e2a155f50119 262 }
4180_1 4:40dd020463ea 263 }
4180_1 4:40dd020463ea 264 if(weberror==1) { // restart connection
4180_1 4:40dd020463ea 265 strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
4180_1 4:40dd020463ea 266 timeout=500;
4180_1 4:40dd020463ea 267 getcount=10;
4180_1 4:40dd020463ea 268 SendCMD();
4180_1 4:40dd020463ea 269 getreply();
4180_1 4:40dd020463ea 270 pc.printf(replybuff);
star297 3:f7febfa77784 271 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
4180_1 4:40dd020463ea 272 timeout=500;
4180_1 4:40dd020463ea 273 getcount=10;
4180_1 4:40dd020463ea 274 SendCMD();
4180_1 4:40dd020463ea 275 getreply();
4180_1 4:40dd020463ea 276 pc.printf(replybuff);
4180_1 4:40dd020463ea 277 } else {
4180_1 4:40dd020463ea 278 sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
4180_1 4:40dd020463ea 279 SendCMD();
4180_1 4:40dd020463ea 280 getreply();
4180_1 4:40dd020463ea 281 pc.printf(replybuff);
4180_1 4:40dd020463ea 282 }
4180_1 4:40dd020463ea 283 t2.reset();
4180_1 4:40dd020463ea 284 }
star297 0:e2a155f50119 285
4180_1 4:40dd020463ea 286 // Reads and processes GET and POST web data
star297 0:e2a155f50119 287 void ReadWebData()
4180_1 4:40dd020463ea 288 {
4180_1 4:40dd020463ea 289 wait_ms(200);
star297 3:f7febfa77784 290 esp.attach(NULL);
4180_1 4:40dd020463ea 291 count=0;
4180_1 4:40dd020463ea 292 DataRX=0;
4180_1 4:40dd020463ea 293 weberror=0;
4180_1 4:40dd020463ea 294 memset(webdata, '\0', sizeof(webdata));
star297 3:f7febfa77784 295 int x = strcspn (webbuff,"+");
4180_1 4:40dd020463ea 296 if(x) {
4180_1 4:40dd020463ea 297 strcpy(webdata, webbuff + x);
4180_1 4:40dd020463ea 298 weberror=0;
4180_1 4:40dd020463ea 299 int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
deronmai 5:280650ceb796 300 if( strstr(webdata, "outlet1=1") != NULL ) {
4180_1 4:40dd020463ea 301 led1=1;
deronmai 5:280650ceb796 302 Outlet1=0;
4180_1 4:40dd020463ea 303 }
deronmai 5:280650ceb796 304 else pc.printf("outlet1 on not found\r\n");
deronmai 5:280650ceb796 305 if( strstr(webdata, "outlet1=0") != NULL ) {
deronmai 5:280650ceb796 306 led1=0;
deronmai 5:280650ceb796 307 Outlet1=1;
deronmai 5:280650ceb796 308 }
deronmai 5:280650ceb796 309 else pc.printf("outlet1 off not found\r\n");
deronmai 5:280650ceb796 310
deronmai 5:280650ceb796 311 //pc.printf("CHECK TEMPON \r\n");
deronmai 5:280650ceb796 312
deronmai 5:280650ceb796 313 if( strstr(webdata, "tmpOn") != NULL ) {
deronmai 5:280650ceb796 314 //sprintf(setTmpOn,*(strstr(webdata, "tmpOn")+6))
deronmai 5:280650ceb796 315 tmpval1 = (int)*(strstr(webdata, "tmpOn")+6);
deronmai 5:280650ceb796 316 if (tmpval1 >= 48 && tmpval1 <= 57) (tmpval1 = (tmpval1 - 48)*10);
deronmai 5:280650ceb796 317 tmpval2 = (int)*(strstr(webdata, "tmpOn")+7);
deronmai 5:280650ceb796 318 if (tmpval2 >= 48 && tmpval2 <= 57) tmpval1 += (tmpval2-48);
deronmai 5:280650ceb796 319 pc.printf("TMPON RECEIVED IS %d\r\n",tmpval1);
deronmai 5:280650ceb796 320 tmpOn=tmpval1;
deronmai 5:280650ceb796 321 //led1=1;
deronmai 5:280650ceb796 322 }
deronmai 5:280650ceb796 323 if( strstr(webdata, "tmpOff") != NULL ) {
deronmai 5:280650ceb796 324 tmpval1 = (int)*(strstr(webdata, "tmpOff")+7);
deronmai 5:280650ceb796 325 if (tmpval1 >= 48 && tmpval1 <= 57) (tmpval1 = (tmpval1 - 48)*10);
deronmai 5:280650ceb796 326 tmpval2 = (int)*(strstr(webdata, "tmpOff")+8);
deronmai 5:280650ceb796 327 if (tmpval2 >= 48 && tmpval2 <= 57) tmpval1 += (tmpval2-48);
deronmai 5:280650ceb796 328 pc.printf("TMPOFF RECEIVED IS %d\r\n",tmpval1);
deronmai 5:280650ceb796 329 tmpOff=tmpval1;
deronmai 5:280650ceb796 330 //led1=1;
deronmai 5:280650ceb796 331 }
deronmai 5:280650ceb796 332 else pc.printf("No tmpOn found\r\n");
deronmai 5:280650ceb796 333
deronmai 5:280650ceb796 334 if( strstr(webdata, "outlet2=1") != NULL ) {
deronmai 5:280650ceb796 335 //led2=1;
deronmai 5:280650ceb796 336 setOut2=1;
deronmai 5:280650ceb796 337 }
deronmai 5:280650ceb796 338 if( strstr(webdata, "outlet2=0") != NULL ) {
deronmai 5:280650ceb796 339 //led2=0;
deronmai 5:280650ceb796 340 setOut2=0;
deronmai 5:280650ceb796 341 }
deronmai 5:280650ceb796 342
deronmai 5:280650ceb796 343 if( strstr(webdata, "outlet3=1") != NULL ) {
deronmai 5:280650ceb796 344 led3=1;
deronmai 5:280650ceb796 345 Outlet3=0;
deronmai 5:280650ceb796 346 }
deronmai 5:280650ceb796 347 else pc.printf("outlet3 on not found\r\n");
deronmai 5:280650ceb796 348 if( strstr(webdata, "outlet3=0") != NULL ) {
deronmai 5:280650ceb796 349 led3=0;
deronmai 5:280650ceb796 350 Outlet3=0;
deronmai 5:280650ceb796 351 }
deronmai 5:280650ceb796 352 if( strstr(webdata, "outlet4=1") != NULL ) {
deronmai 5:280650ceb796 353 led4=1;
deronmai 5:280650ceb796 354 Outlet4=1;
deronmai 5:280650ceb796 355 }
deronmai 5:280650ceb796 356 else pc.printf("outlet4 on not found\r\n");
deronmai 5:280650ceb796 357 if( strstr(webdata, "outlet4=0") != NULL ) {
deronmai 5:280650ceb796 358 led4=0;
deronmai 5:280650ceb796 359 Outlet4=0;
deronmai 5:280650ceb796 360 }
deronmai 5:280650ceb796 361 else pc.printf("outlet4 off not found\r\n");
deronmai 5:280650ceb796 362 /*
4180_1 4:40dd020463ea 363 if( strstr(webdata, "led1=0") != NULL ) {
4180_1 4:40dd020463ea 364 led1=0;
deronmai 5:280650ceb796 365 }*/
deronmai 5:280650ceb796 366 // if( strstr(webdata, "Out1=1") != NULL ) {
deronmai 5:280650ceb796 367 // Out1=1;
deronmai 5:280650ceb796 368 // }
deronmai 5:280650ceb796 369 // if( strstr(webdata, "Out1=0") != NULL ) {
deronmai 5:280650ceb796 370 // Out1=0;
deronmai 5:280650ceb796 371 // }
deronmai 5:280650ceb796 372 // if( strstr(webdata, "Out2=1") != NULL ) {
deronmai 5:280650ceb796 373 // Out2=1;
deronmai 5:280650ceb796 374 // }
deronmai 5:280650ceb796 375 // if( strstr(webdata, "Out2=0") != NULL ) {
deronmai 5:280650ceb796 376 // Out2=0;
deronmai 5:280650ceb796 377 // }
deronmai 5:280650ceb796 378 // if( strstr(webdata, "Out3=1") != NULL ) {
deronmai 5:280650ceb796 379 // Out3=1;
deronmai 5:280650ceb796 380 // }
deronmai 5:280650ceb796 381 // if( strstr(webdata, "Out3=0") != NULL ) {
deronmai 5:280650ceb796 382 // Out3=0;
deronmai 5:280650ceb796 383 // }
4180_1 4:40dd020463ea 384 sprintf(channel, "%d",linkID);
4180_1 4:40dd020463ea 385 if (strstr(webdata, "GET") != NULL) {
4180_1 4:40dd020463ea 386 servreq=1;
4180_1 4:40dd020463ea 387 }
4180_1 4:40dd020463ea 388 if (strstr(webdata, "POST") != NULL) {
4180_1 4:40dd020463ea 389 servreq=1;
4180_1 4:40dd020463ea 390 }
star297 2:d4c6bc0f2dc4 391 webcounter++;
star297 2:d4c6bc0f2dc4 392 sprintf(webcount, "%d",webcounter);
4180_1 4:40dd020463ea 393 } else {
4180_1 4:40dd020463ea 394 memset(webbuff, '\0', sizeof(webbuff));
4180_1 4:40dd020463ea 395 esp.attach(&callback);
4180_1 4:40dd020463ea 396 weberror=1;
4180_1 4:40dd020463ea 397 }
star297 0:e2a155f50119 398 }
star297 0:e2a155f50119 399 // Starts and restarts webserver if errors detected.
star297 0:e2a155f50119 400 void startserver()
star297 0:e2a155f50119 401 {
4180_1 4:40dd020463ea 402 gettemp();
4180_1 4:40dd020463ea 403 gettime();
star297 1:71ed1afbf344 404 pc.printf("\n\n RTC time %s\r\n\n",timebuf);
4180_1 4:40dd020463ea 405 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
star297 3:f7febfa77784 406 strcpy(cmdbuff,"AT+RST\r\n");
4180_1 4:40dd020463ea 407 timeout=8000;
4180_1 4:40dd020463ea 408 getcount=1000;
star297 0:e2a155f50119 409 SendCMD();
star297 1:71ed1afbf344 410 getreply();
star297 3:f7febfa77784 411 pc.printf(replybuff);
star297 3:f7febfa77784 412 pc.printf("%d",count);
star297 3:f7febfa77784 413 if (strstr(replybuff, "OK") != NULL) {
star297 1:71ed1afbf344 414 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
4180_1 4:40dd020463ea 415 strcpy(cmdbuff, "AT+CIPMUX=1\r\n"); // set multiple connections.
4180_1 4:40dd020463ea 416 timeout=500;
4180_1 4:40dd020463ea 417 getcount=20;
star297 1:71ed1afbf344 418 SendCMD();
star297 2:d4c6bc0f2dc4 419 getreply();
4180_1 4:40dd020463ea 420 pc.printf(replybuff);
star297 3:f7febfa77784 421 sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
4180_1 4:40dd020463ea 422 timeout=500;
4180_1 4:40dd020463ea 423 getcount=20;
star297 1:71ed1afbf344 424 SendCMD();
star297 3:f7febfa77784 425 getreply();
4180_1 4:40dd020463ea 426 pc.printf(replybuff);
4180_1 4:40dd020463ea 427 wait(1);
star297 3:f7febfa77784 428 sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
4180_1 4:40dd020463ea 429 timeout=500;
4180_1 4:40dd020463ea 430 getcount=50;
star297 3:f7febfa77784 431 SendCMD();
star297 2:d4c6bc0f2dc4 432 getreply();
4180_1 4:40dd020463ea 433 pc.printf(replybuff);
4180_1 4:40dd020463ea 434 wait(5);
4180_1 4:40dd020463ea 435 pc.printf("\n Getting Server IP \r\n");
star297 3:f7febfa77784 436 strcpy(cmdbuff, "AT+CIFSR\r\n");
4180_1 4:40dd020463ea 437 timeout=2500;
4180_1 4:40dd020463ea 438 getcount=200;
4180_1 4:40dd020463ea 439 while(weberror==0) {
4180_1 4:40dd020463ea 440 SendCMD();
4180_1 4:40dd020463ea 441 getreply();
4180_1 4:40dd020463ea 442 if (strstr(replybuff, "0.0.0.0") == NULL) {
4180_1 4:40dd020463ea 443 weberror=1; // wait for valid IP
star297 1:71ed1afbf344 444 }
4180_1 4:40dd020463ea 445 }
4180_1 4:40dd020463ea 446 pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
4180_1 4:40dd020463ea 447 pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
4180_1 4:40dd020463ea 448 replybuff[strlen(replybuff)-1] = '\0';
4180_1 4:40dd020463ea 449 //char* IP = replybuff + 5;
4180_1 4:40dd020463ea 450 sprintf(webdata,"%s", replybuff);
4180_1 4:40dd020463ea 451 pc.printf(webdata);
deronmai 5:280650ceb796 452 //led2=1;
4180_1 4:40dd020463ea 453 bufflen=200;
4180_1 4:40dd020463ea 454 count=0;
star297 1:71ed1afbf344 455 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
star297 1:71ed1afbf344 456 esp.attach(&callback);
4180_1 4:40dd020463ea 457 } else {
4180_1 4:40dd020463ea 458 pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
4180_1 4:40dd020463ea 459 while(1) {}
4180_1 4:40dd020463ea 460 }
4180_1 4:40dd020463ea 461 t2.reset();
4180_1 4:40dd020463ea 462 t2.start();
4180_1 4:40dd020463ea 463 beep();
4180_1 4:40dd020463ea 464 }
star297 0:e2a155f50119 465 // ESP Command data send
star297 0:e2a155f50119 466 void SendCMD()
star297 0:e2a155f50119 467 {
4180_1 4:40dd020463ea 468 esp.printf("%s", cmdbuff);
4180_1 4:40dd020463ea 469 }
star297 0:e2a155f50119 470 // Large WEB buffer data send
star297 0:e2a155f50119 471 void SendWEB()
4180_1 4:40dd020463ea 472 {
star297 0:e2a155f50119 473 int i=0;
star297 0:e2a155f50119 474 if(esp.writeable()) {
4180_1 4:40dd020463ea 475 while(webbuff[i]!='\0') {
4180_1 4:40dd020463ea 476 esp.putc(webbuff[i]);
4180_1 4:40dd020463ea 477 i++;
4180_1 4:40dd020463ea 478 }
4180_1 4:40dd020463ea 479 }
4180_1 4:40dd020463ea 480 }
4180_1 4:40dd020463ea 481 // Get Command and ESP status replies
star297 0:e2a155f50119 482 void getreply()
4180_1 4:40dd020463ea 483 {
star297 3:f7febfa77784 484 memset(replybuff, '\0', sizeof(replybuff));
4180_1 4:40dd020463ea 485 t1.reset();
4180_1 4:40dd020463ea 486 t1.start();
4180_1 4:40dd020463ea 487 replycount=0;
star297 3:f7febfa77784 488 while(t1.read_ms()< timeout && replycount < getcount) {
star297 0:e2a155f50119 489 if(esp.readable()) {
4180_1 4:40dd020463ea 490 replybuff[replycount] = esp.getc();
4180_1 4:40dd020463ea 491 replycount++;
star297 2:d4c6bc0f2dc4 492 }
4180_1 4:40dd020463ea 493 }
4180_1 4:40dd020463ea 494 t1.stop();
star297 0:e2a155f50119 495 }
star297 0:e2a155f50119 496 // Analog in example
star297 0:e2a155f50119 497 void getbattery()
star297 0:e2a155f50119 498 {
4180_1 4:40dd020463ea 499 AdcIn=Ain1.read();
4180_1 4:40dd020463ea 500 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
4180_1 4:40dd020463ea 501 sprintf(Vcc,"%2.3f",Ht);
deronmai 5:280650ceb796 502
deronmai 5:280650ceb796 503 Current1 = I1.read();
deronmai 5:280650ceb796 504 //Current1 = ((Current1/1023.0*5000)-2500)/66;
deronmai 5:280650ceb796 505 sprintf(Cur1,"%2.3f", Current1);
star297 0:e2a155f50119 506 }
star297 0:e2a155f50119 507 // Temperature example
star297 0:e2a155f50119 508 void gettemp()
4180_1 4:40dd020463ea 509 {
4180_1 4:40dd020463ea 510
deronmai 5:280650ceb796 511 int err;
deronmai 5:280650ceb796 512 err = tsensor.readData();
deronmai 5:280650ceb796 513 if (err == 0) {
deronmai 5:280650ceb796 514 Tc = tsensor.ReadTemperature(CELCIUS);
deronmai 5:280650ceb796 515 Tf = tsensor.ReadTemperature(FARENHEIT);
deronmai 5:280650ceb796 516 Tk = tsensor.ReadTemperature(KELVIN);
deronmai 5:280650ceb796 517 Hm = tsensor.ReadHumidity();
deronmai 5:280650ceb796 518 }
deronmai 5:280650ceb796 519 else {printf("\nErr %i \n",err);}
deronmai 5:280650ceb796 520 sprintf(Temp, "%2.3f", Tf);
deronmai 5:280650ceb796 521 sprintf(Hum, "%2.3f", Hm);
deronmai 5:280650ceb796 522 pc.printf("Received temp is: %2.3f\r\n",Tf);
deronmai 5:280650ceb796 523 // AdcIn=Ain2.read();
deronmai 5:280650ceb796 524 // Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
deronmai 5:280650ceb796 525 // sprintf(Temp,"%2.3f",Ht);
star297 0:e2a155f50119 526 }
4180_1 4:40dd020463ea 527 // Get RTC time
star297 0:e2a155f50119 528 void gettime()
star297 0:e2a155f50119 529 {
star297 0:e2a155f50119 530 time_t seconds = time(NULL);
4180_1 4:40dd020463ea 531 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
star297 0:e2a155f50119 532 }
star297 0:e2a155f50119 533
star297 2:d4c6bc0f2dc4 534 void beep()
4180_1 4:40dd020463ea 535 {
deronmai 5:280650ceb796 536 // speaker.period(1.0/2000); // 2000hz period
deronmai 5:280650ceb796 537 // speaker = 0.5; //50% duty cycle - max volume
deronmai 5:280650ceb796 538 wait_ms(10);
deronmai 5:280650ceb796 539 // speaker=0.0; // turn off audio
star297 2:d4c6bc0f2dc4 540 }
star297 2:d4c6bc0f2dc4 541
star297 1:71ed1afbf344 542 void setRTC()
star297 1:71ed1afbf344 543 {
4180_1 4:40dd020463ea 544 t.tm_sec = (0); // 0-59
4180_1 4:40dd020463ea 545 t.tm_min = (minute); // 0-59
4180_1 4:40dd020463ea 546 t.tm_hour = (hour); // 0-23
4180_1 4:40dd020463ea 547 t.tm_mday = (dayofmonth); // 1-31
4180_1 4:40dd020463ea 548 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
4180_1 4:40dd020463ea 549 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
4180_1 4:40dd020463ea 550 set_time(mktime(&t)); // set RTC clock
4180_1 4:40dd020463ea 551 }