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
Diff: main.cpp
- Revision:
- 1:71ed1afbf344
- Parent:
- 0:e2a155f50119
- Child:
- 2:d4c6bc0f2dc4
--- a/main.cpp Sat Feb 14 23:53:25 2015 +0000 +++ b/main.cpp Sun Feb 15 12:05:58 2015 +0000 @@ -24,8 +24,10 @@ DS18B20 thermom(PTB0, DS18B20::RES_12_BIT); -Timer t; Timer t1; +Timer t2; + +struct tm t; int buflen, DataRX, count, ended, servreq, timeout; int bufl, ipdLen, linkID, weberror; @@ -43,8 +45,15 @@ char buf[2048]; // Currently using 1407 characters, Increase this if more web page data added void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); -void gettime(),gettemp(),getbattery(); +void gettime(),gettemp(),getbattery(),setRTC(); +// manual set RTC values +int minute =58; // 0-59 +int hour =11; // 2-23 +int dayofmonth =15; // 1-31 +int month =2; // 1-12 +int year =15; // last 2 digits + // Serial Interrupt read ESP data void callback() { while (esp.readable()) {buf[count] = esp.getc();count++;} @@ -55,7 +64,7 @@ led1=1,led2=1,led3=1; pc.baud(115200); esp.baud(115200); // ESP8266 baudrate. - if (time(NULL) < 1420070400) {set_time(1420070400);} + if (time(NULL) < 1420070400) {setRTC();} startserver(); @@ -134,12 +143,12 @@ void sendcheck() { timeout=200; - weberror=1;t1.reset();t1.start(); - while(weberror==1 && t1.read() <10){ + weberror=1;t2.reset();t2.start(); + while(weberror==1 && t2.read() <10){ getreply(); if (strstr(buf, "SEND OK") != NULL) {weberror=0;} // wait for valid SEND OK } - t1.stop(); + t2.stop(); if(weberror==1){ pc.printf("\n\n\n ++++++++++ WEB Connect Error, restarting ... ++++++++++\n\n"); NVIC_SystemReset();} @@ -177,36 +186,45 @@ void startserver() { esp.attach(NULL); - pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); wait(1); + gettime(); + pc.printf("\n\n RTC time %s\r\n\n",timebuf); + pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); strcpy(snd,"AT+RST\r\n"); SendCMD(); - pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); - wait(3); - strcpy(snd, "AT+CIPMUX=1\r\n"); // set multiple connections. - SendCMD(); - wait_ms(50); - strcpy(snd, "AT+CIPSERVER=1,80\r\n"); // set to port 80, you can change this to what you want. - SendCMD(); - wait_ms(200); - pc.printf("\n Getting Server IP \r\n"); - strcpy(snd, "AT+CIFSR\r\n"); - timeout=500; - while(weberror==0){ - SendCMD();getreply(); - if (strstr(buf, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP + timeout=2000; + getreply(); + if (strstr(buf, "ready") != NULL) { + pc.printf("\n++++++++++ Starting Server ++++++++++\r\n"); + strcpy(snd, "AT+CIPMUX=1\r\n"); // set multiple connections. + SendCMD(); + wait_ms(50); + strcpy(snd, "AT+CIPSERVER=1,80\r\n"); // set to port 80, you can change this to what you want. + SendCMD(); + wait_ms(200); + pc.printf("\n Getting Server IP \r\n"); + strcpy(snd, "AT+CIFSR\r\n"); + timeout=500; + while(weberror==0){ + SendCMD();getreply(); + if (strstr(buf, "0.0.0.0") == NULL) {weberror=1;} // wait for valid IP + } + pc.printf("\n Enter WEB address in your browser \r\n\n"); + buf[strlen(buf) - 8] = '\0'; + char* IP = buf + 5; + sprintf(snd," http://%s:80", IP); + pc.printf(snd); + while (esp.readable()){esp.getc();} + buflen=200;count=0; + memset(buf, '\0', sizeof(buf)); + led2=0;wait(2);led2=1; + pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); + esp.attach(&callback); } - pc.printf("\n Enter WEB address in your browser \r\n\n"); - buf[strlen(buf) - 8] = '\0'; - char* IP = buf + 5; - sprintf(snd," http://%s:80", IP); - pc.printf(snd); - while (esp.readable()){esp.getc();} - buflen=200;count=0; - memset(buf, '\0', sizeof(buf)); - led2=0;wait(2);led2=1; - pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); - esp.attach(&callback); + else{ + pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n"); + while(1){} + } } // ESP Command data send void SendCMD() @@ -225,14 +243,14 @@ void getreply() { memset(buf, '\0', sizeof(buf)); - t.start(); + t1.start(); ended=0;count=0; while(!ended) { if(esp.readable()) { buf[count] = esp.getc();count++; } - if(t.read_ms() > timeout) { - ended = 1;t.stop();t.reset(); + if(t1.read_ms() > timeout) { + ended = 1;t1.stop();t1.reset(); } } } @@ -257,3 +275,13 @@ strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds)); } +void setRTC() +{ + t.tm_sec = (0); // 0-59 + t.tm_min = (minute); // 0-59 + t.tm_hour = (hour); // 0-23 + t.tm_mday = (dayofmonth); // 1-31 + t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format + t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year + set_time(mktime(&t)); // set RTC clock +}