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:
- 5:280650ceb796
- Parent:
- 4:40dd020463ea
- Child:
- 6:66b21f6166bd
--- a/main.cpp Fri Aug 28 01:37:50 2015 +0000 +++ b/main.cpp Thu Mar 17 21:37:39 2016 +0000 @@ -1,6 +1,7 @@ // ESP8266 Static page WEB server to control Mbed #include "mbed.h" +#include "DHT.h" //#include "DS18B20.h" Serial pc(USBTX, USBRX); @@ -12,20 +13,26 @@ DigitalOut led1(LED1); // (PTB18) DigitalOut led2(LED2); // (PTB19) DigitalOut led3(LED3); // (PTD1) +DigitalOut led4(LED4); // Digital Out and In pins, can be configured to any suitable pin depending on Platform -DigitalOut Out1(p6); -DigitalOut Out2(p7); -DigitalOut Out3(p8); +DigitalOut Outlet1(p25); +DigitalOut Outlet2(p24); +DigitalOut Outlet3(p23); +DigitalOut Outlet4(p22); DigitalOut reset(p26); -DigitalIn In1(p9); -DigitalIn In2(p10); -DigitalIn In3(p11); +int setOut2=0; // if 0, turn off when temp > + // if 1, turn on when temp > + +DHT tsensor(p21, DHT11); -PwmOut speaker(p21); -AnalogIn Ain1(p18); -AnalogIn Ain2(p19); +AnalogIn I1(p20); +AnalogIn Ain1(p19); + +//PwmOut speaker(p21); +//AnalogIn Ain1(p18); +//AnalogIn Ain2(p19); Timer t1; Timer t2; @@ -34,11 +41,13 @@ int bufflen, DataRX, count, getcount, replycount, servreq, timeout; int bufl, ipdLen, linkID, weberror, webcounter; -float temperature, AdcIn, Ht; +float temperature, AdcIn, Ht, Tf, Tc, Tk, Hm, Current1; float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage char Vcc[10]; char Temp[10]; char temp[10]; +char Hum[10]; +char Cur1[10]; char webcount[8]; char lasthit[30]; char timebuf[30]; @@ -49,10 +58,15 @@ char replybuff[1024]; char webdata[1024]; // This may need to be bigger depending on WEB browser used char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added +int tmpval1; +int tmpval2; void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); void gettime(),gettemp(),getbattery(),setRTC(),beep(); +int tmpOn=70, tmpOff=70; +char setTmpOn[10]; +char setTmpOff[10]; // manual set RTC values int minute =00; // 0-59 int hour =12; // 2-23 @@ -80,12 +94,13 @@ int main() { reset=0; - pc.baud(115200); + pc.baud(9600); pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); wait(0.5); reset=1; - led1=1,led2=0,led3=0; + Outlet1=1, Outlet2=1, Outlet3=1, Outlet4=1; + led1=0,led2=0,led3=1; timeout=6000; getcount=500; getreply(); @@ -97,6 +112,26 @@ startserver(); while(1) { + if (setOut2 == 0){ // turn off when tmp > + if (Tf > tmpOff) { + Outlet2 = 1; + led2 = 0; + } + else { + Outlet2 = 0; + led2 = 1; + } + } + else { // turn on when tmp > + if (Tf > tmpOn) { + Outlet2 = 0; + led2 = 1; + } + else { + Outlet2 = 1; + led2 = 0; + } + } if(DataRX==1) { ReadWebData(); beep(); @@ -122,65 +157,69 @@ // WEB page data strcpy(webbuff, "<!DOCTYPE html>"); - strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>"); + strcat(webbuff, "<html><head><title>ESP8266 Mbed IoT Controlled Outlets</title></head>"); strcat(webbuff, "<body>"); strcat(webbuff, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>ESP8266 Mbed IoT Web Controller</h1>"); strcat(webbuff, "Hit Count - "); strcat(webbuff, webcount); - strcat(webbuff, "<br>Last Hit - "); - strcat(webbuff, lasthit); + strcat(webbuff, "<br>Temp - "); + strcat(webbuff, Temp); + strcat(webbuff, "<br>3.3v Supply Voltage:  "); //<input type=\"text\" size=4 value=\" + strcat(webbuff, Vcc); + strcat(webbuff, " </sup>V"); strcat(webbuff, "</div><br /><hr>"); strcat(webbuff, "<h3>Mbed RTC Time -  "); strcat(webbuff, timebuf); strcat(webbuff, "</h3>\r\n"); - strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:  <input type=\"text\" size=6 value=\""); - strcat(webbuff, Temp); - strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong>   Analog 2:  <input type=\"text\" size=4 value=\""); - strcat(webbuff, Vcc); - strcat(webbuff, "\"> </sup>V"); - if(led1==0) { - strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked> LED 1 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" > LED 1 on"); + strcat(webbuff, "<p><form method=\"POST\"><strong> Outlet 1 Current:  <input type=\"text\" size=6 value=\""); + strcat(webbuff, Cur1); + strcat(webbuff, "\"> </sup>A <form method=\"POST\"> <strong>   "); + if(Outlet1==1) { + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" checked> Outlet 1 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" > Outlet 1 on"); } else { - strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" > LED 1 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked> LED 1 on"); + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" > Outlet 1 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" checked> Outlet 1 on"); } - if(Out1==0) { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked> Digital Out 1 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" > Digital Out 1 on"); - } else { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" > Digital Out 1 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked> Digital Out 1 on"); - } - if(Out2==0) { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked> Digital Out 2 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" > Digital Out 2 on"); + if(setOut2==0) { + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet2\" value=\"0\" checked> Outlet 2 off when temp >  <input type=\"text\" name=\"tmpOff\" size=4 value=\""); + sprintf(setTmpOff, "%d", tmpOff); + strcat(webbuff,setTmpOff); + strcat(webbuff,"\"> </sup>"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet2\" value=\"1\" > Outlet 2 on when temp >  <input type=\"text\" name=\"tmpOn\" size=4 value=\""); + sprintf(setTmpOn, "%d", tmpOn); + strcat(webbuff,setTmpOn); + strcat(webbuff,"\"> </sup>"); } else { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" > Digital Out 2 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked> Digital Out 2 on"); + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet2\" value=\"0\" > Outlet 2 off when temp >  <input type=\"text\" name=\"tmpOff\" size=4 value=\""); + sprintf(setTmpOff, "%d", tmpOff); + strcat(webbuff,setTmpOff); + strcat(webbuff,"\"> </sup>"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet2\" value=\"1\" checked> Outlet 2 on when temp >  <input type=\"text\" name=\"tmpOn\" size=4 value=\""); + sprintf(setTmpOn, "%d", tmpOn); + strcat(webbuff,setTmpOn); + strcat(webbuff,"\"> </sup>"); } - if(Out3==0) { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked> Digital Out 3 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" > Digital Out 3 on"); - } else { - strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" > Digital Out 3 off"); - strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked> Digital Out 3 on"); - } - if(In1==0) { - strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1"); + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"1\" checked> Outlet 3 On"); + /*if(Outlet3==0) { + strcat(webbuff, "<p><input type=\"radio\" name=\"Outlet 3\" value=\"0\" checked> Outlet 3 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"Outlet 3\" value=\"1\" > Outlet 3 on"); } else { - strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1"); - } - if(In2==0) { - strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" > Digital In 2"); + strcat(webbuff, "<p><input type=\"radio\" name=\"Outlet 3\" value=\"0\" > Outlet 3 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"Outlet 3\" value=\"1\" checked> Outlet 3 on"); + }*/ + if(Outlet4==0) { + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" checked> Outlet 4 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet4\" value=\"1\" > Outlet 4 on"); } else { - strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked> Digital In 2"); + strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" > Outlet 4 off"); + strcat(webbuff, "<br><input type=\"radio\" name=\"outlet4\" value=\"1\" checked> Outlet 4 on"); } - if(In3==0) { - strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" > Digital In 3"); - } else { - strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked> Digital In 3"); - } +// if(I1==0) { +// strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"0\" > Digital In 1"); +// } else { +// strcat(webbuff, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked> Digital In 1"); +// } strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;"); strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);"); strcat(webbuff, "background-image:linear-gradient(to bottom, #3498db, #2980b9);"); @@ -189,12 +228,12 @@ strcat(webbuff, "background: #3cb0fd;"); strcat(webbuff, "background-image:-webkit-linear-gradient(top,#3cb0fd,#1a5f8a);"); strcat(webbuff, "background-image:linear-gradient(to bottom,#3cb0fd,#1a5f8a);"); - strcat(webbuff, "text-decoration:none;\"></form></span>"); - strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); - strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); - strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); - strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); - strcat(webbuff, "</ul>"); + strcat(webbuff, "text-decoration:none;\"></form></span></p>"); +// strcat(webbuff, "<p/><h2>How to use:</h2><ul>"); +// strcat(webbuff, "<li>Select the Radio buttons to control the digital out pins.</li>"); +// strcat(webbuff, "<li>Click 'Send-Refresh' to send.</li>"); +// strcat(webbuff, "<li>Use the 'Send-Refresh' button to refresh the data.</li>"); +// strcat(webbuff, "</ul>"); strcat(webbuff, "</body></html>"); // end of WEB page data bufl = strlen(webbuff); // get total page buffer length @@ -258,30 +297,90 @@ strcpy(webdata, webbuff + x); weberror=0; int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type); - if( strstr(webdata, "led1=1") != NULL ) { + if( strstr(webdata, "outlet1=1") != NULL ) { led1=1; + Outlet1=0; } + else pc.printf("outlet1 on not found\r\n"); + if( strstr(webdata, "outlet1=0") != NULL ) { + led1=0; + Outlet1=1; + } + else pc.printf("outlet1 off not found\r\n"); + + //pc.printf("CHECK TEMPON \r\n"); + + if( strstr(webdata, "tmpOn") != NULL ) { + //sprintf(setTmpOn,*(strstr(webdata, "tmpOn")+6)) + tmpval1 = (int)*(strstr(webdata, "tmpOn")+6); + if (tmpval1 >= 48 && tmpval1 <= 57) (tmpval1 = (tmpval1 - 48)*10); + tmpval2 = (int)*(strstr(webdata, "tmpOn")+7); + if (tmpval2 >= 48 && tmpval2 <= 57) tmpval1 += (tmpval2-48); + pc.printf("TMPON RECEIVED IS %d\r\n",tmpval1); + tmpOn=tmpval1; + //led1=1; + } + if( strstr(webdata, "tmpOff") != NULL ) { + tmpval1 = (int)*(strstr(webdata, "tmpOff")+7); + if (tmpval1 >= 48 && tmpval1 <= 57) (tmpval1 = (tmpval1 - 48)*10); + tmpval2 = (int)*(strstr(webdata, "tmpOff")+8); + if (tmpval2 >= 48 && tmpval2 <= 57) tmpval1 += (tmpval2-48); + pc.printf("TMPOFF RECEIVED IS %d\r\n",tmpval1); + tmpOff=tmpval1; + //led1=1; + } + else pc.printf("No tmpOn found\r\n"); + + if( strstr(webdata, "outlet2=1") != NULL ) { + //led2=1; + setOut2=1; + } + if( strstr(webdata, "outlet2=0") != NULL ) { + //led2=0; + setOut2=0; + } + + if( strstr(webdata, "outlet3=1") != NULL ) { + led3=1; + Outlet3=0; + } + else pc.printf("outlet3 on not found\r\n"); + if( strstr(webdata, "outlet3=0") != NULL ) { + led3=0; + Outlet3=0; + } + if( strstr(webdata, "outlet4=1") != NULL ) { + led4=1; + Outlet4=1; + } + else pc.printf("outlet4 on not found\r\n"); + if( strstr(webdata, "outlet4=0") != NULL ) { + led4=0; + Outlet4=0; + } + else pc.printf("outlet4 off not found\r\n"); + /* if( strstr(webdata, "led1=0") != NULL ) { led1=0; - } - if( strstr(webdata, "Out1=1") != NULL ) { - Out1=1; - } - if( strstr(webdata, "Out1=0") != NULL ) { - Out1=0; - } - if( strstr(webdata, "Out2=1") != NULL ) { - Out2=1; - } - if( strstr(webdata, "Out2=0") != NULL ) { - Out2=0; - } - if( strstr(webdata, "Out3=1") != NULL ) { - Out3=1; - } - if( strstr(webdata, "Out3=0") != NULL ) { - Out3=0; - } + }*/ +// if( strstr(webdata, "Out1=1") != NULL ) { +// Out1=1; +// } +// if( strstr(webdata, "Out1=0") != NULL ) { +// Out1=0; +// } +// if( strstr(webdata, "Out2=1") != NULL ) { +// Out2=1; +// } +// if( strstr(webdata, "Out2=0") != NULL ) { +// Out2=0; +// } +// if( strstr(webdata, "Out3=1") != NULL ) { +// Out3=1; +// } +// if( strstr(webdata, "Out3=0") != NULL ) { +// Out3=0; +// } sprintf(channel, "%d",linkID); if (strstr(webdata, "GET") != NULL) { servreq=1; @@ -350,7 +449,7 @@ //char* IP = replybuff + 5; sprintf(webdata,"%s", replybuff); pc.printf(webdata); - led2=1; + //led2=1; bufflen=200; count=0; pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n"); @@ -400,14 +499,30 @@ AdcIn=Ain1.read(); Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy sprintf(Vcc,"%2.3f",Ht); + + Current1 = I1.read(); + //Current1 = ((Current1/1023.0*5000)-2500)/66; + sprintf(Cur1,"%2.3f", Current1); } // Temperature example void gettemp() { - AdcIn=Ain2.read(); - Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy - sprintf(Temp,"%2.3f",Ht); + int err; + err = tsensor.readData(); + if (err == 0) { + Tc = tsensor.ReadTemperature(CELCIUS); + Tf = tsensor.ReadTemperature(FARENHEIT); + Tk = tsensor.ReadTemperature(KELVIN); + Hm = tsensor.ReadHumidity(); + } + else {printf("\nErr %i \n",err);} + sprintf(Temp, "%2.3f", Tf); + sprintf(Hum, "%2.3f", Hm); + pc.printf("Received temp is: %2.3f\r\n",Tf); +// AdcIn=Ain2.read(); +// Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy +// sprintf(Temp,"%2.3f",Ht); } // Get RTC time void gettime() @@ -418,10 +533,10 @@ void beep() { - speaker.period(1.0/2000); // 2000hz period - speaker = 0.5; //50% duty cycle - max volume - wait_ms(60); - speaker=0.0; // turn off audio +// speaker.period(1.0/2000); // 2000hz period +// speaker = 0.5; //50% duty cycle - max volume + wait_ms(10); +// speaker=0.0; // turn off audio } void setRTC()