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