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:
tpettet3
Date:
Sat Apr 30 22:37:39 2016 +0000
Revision:
15:5a337e99f6f3
Parent:
14:85d3a42699af
Final/Demo code version

Who changed what in which revision?

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