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

main.cpp

Committer:
tpettet3
Date:
2016-04-23
Revision:
13:d2b7aeed050d
Parent:
12:9de0e402ff17
Child:
14:85d3a42699af

File content as of revision 13:d2b7aeed050d:

// ESP8266 Static page WEB server to control Mbed

#include "mbed.h"
#include "DHT.h"
//#include "DS18B20.h"
#include "BH1750.h"

BH1750 lightSensor(p28, p27);

Serial pc(USBTX, USBRX);
Serial esp(p13, p14); // tx, rx
DigitalIn PIR(p30);
int PIR_sensor;
int SetMotion = 0;
//DS18B20 thermom(A0, DS18B20::RES_12_BIT);

// Standard Mbed LED definitions
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  Outlet1(p18);
DigitalOut  Outlet2(p17);
DigitalOut  Outlet3(p20);
DigitalOut  Outlet4(p19);
DigitalOut  reset(p29);

int setOut2=0; // if 0, turn off when temp >
                // if 1, turn on when temp >
int setOut3=0; // if 0, turn on when light <
                // if 1, Leave light off

DHT tsensor(p22, DHT11);

AnalogIn   I1(p15);
AnalogIn   Ain1(p16);

//PwmOut speaker(p21);
//AnalogIn   Ain1(p18);
//AnalogIn   Ain2(p19);

Timer t1;
Timer t2;

struct tm t;

int bufflen, DataRX, count, getcount, replycount, servreq, timeout;
int bufl, ipdLen, linkID, weberror, webcounter;
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];
char type[16];
char type1[16];
char channel[2];
char cmdbuff[32];
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];
int lighton=200;
char setlighton[20];
// manual set RTC values
int minute      =00;    // 0-59
int hour        =12;    // 2-23
int dayofmonth  =26;    // 1-31
int month       =8;     // 1-12
int year        =15;    // last 2 digits

int port        =80;  // set server port
int SERVtimeout =5;    // set server timeout in seconds in case link breaks.

// Serial Interrupt read ESP data
void callback()
{
    while (esp.readable()) {
        webbuff[count] = esp.getc();
        count++;
    }
    if(strlen(webbuff)>bufflen) {
        DataRX=1;
    }
}

int main()
{
    lightSensor.init();
    reset=0;
    pc.baud(9600);

    pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r");
    wait(0.5);
    reset=1;
    Outlet1=1, Outlet2=1, Outlet3=1, Outlet4=1;
    led1=0,led2=0,led3=1,led4=1;
    timeout=6000;
    getcount=500;
    getreply();
    esp.baud(115200);   // ESP8266 baudrate. Maximum on KLxx' is 115200, 230400 works on K20 and K22F
    if (time(NULL) < 1420070400) {
        setRTC();
    }
    beep();
    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 (setOut3 == 0) { //turn on when light <
            if (lightSensor.readIntesity() < lighton) {
                Outlet3 = 1;
                led3 = 1;
            }
            else {
                Outlet3 = 0;
                led3 = 0;
            }
        }        
        
        if( SetMotion == 1)
        {
            PIR_sensor = PIR;
            if(PIR_sensor == 1)
            {
                Outlet1 = 0;
                led1 = 1;
            }
            else
            {
                Outlet1 = 1;
                led1 = 0;} 
            
            }
        if (SetMotion ==0)
        {Outlet1 = 1;
        led1 = 0;}
        if(DataRX==1) {
            ReadWebData();
            beep();
            if (servreq == 1 && weberror == 0) {
                sendpage();
            }
            esp.attach(&callback);
            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);
            pc.printf("\n\n  HTTP Packet: \n\n%s\n", webdata);
            pc.printf("  Web Characters sent : %d\n\n", bufl);
            pc.printf("  -------------------------------------\n\n");
            strcpy(lasthit, timebuf);
            servreq=0;
        }
    }
}
// Static WEB page
void sendpage()
{
    gettemp();
    getbattery();
    gettime();

// WEB page data
    strcpy(webbuff, "<!DOCTYPE html>");
    strcat(webbuff, "<html><head><title>Wi-Fi Controlled Smart Wall Outlet</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>Temp - ");
    strcat(webbuff, Temp);
    strcat(webbuff, "<br>3.3v Supply Voltage:&nbsp&nbsp"); //<input type=\"text\" size=4 value=\"
    strcat(webbuff, Vcc);
    strcat(webbuff, " </sup>V");
    strcat(webbuff, "</div><br /><hr>");
    //strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");
    //strcat(webbuff, timebuf);
    strcat(webbuff, "</h3>\r\n");
    //strcat(webbuff, "<p><form method=\"POST\"><strong> Outlet 1 Current:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
    //strcat(webbuff, Cur1);
    strcat(webbuff, "\ </sup> <form method=\"POST\"> <strong> &nbsp&nbsp");
    if(SetMotion==0) {
        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 is motion controlled");
    } else {
        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 is motion controlled");
    }
    if(setOut2==0) {
        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=\"");
        sprintf(setTmpOff, "%d", tmpOff);
        strcat(webbuff,setTmpOff);
        strcat(webbuff,"\"> </sup>");
        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=\"");
        sprintf(setTmpOn, "%d", tmpOn);
        strcat(webbuff,setTmpOn);
        strcat(webbuff,"\"> </sup>");
    } else {
        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=\"");
        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 temperature >&nbsp&nbsp<input type=\"text\" name=\"tmpOn\" size=4 value=\"");
        sprintf(setTmpOn, "%d", tmpOn);
        strcat(webbuff,setTmpOn);
        strcat(webbuff,"\"> </sup>");
    }
    //strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"1\" checked>  Outlet 3  On");
    if(setOut3==0) {
        strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" checked>  Outlet 3 on in low light");
        //sprintf(setlighton, "%d", lighton);
        //strcat(webbuff,setlighton);
        strcat(webbuff,"\ </sup>");
        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" >  Outlet 3 is always off");
        strcat(webbuff,"\ </sup>");
    } else {
        strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" >  Outlet 3 on in low light");
        //sprintf(setlighton, "%d", lighton);
        //strcat(webbuff,setlighton);
        strcat(webbuff,"\ </sup>");
        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" checked>  Outlet 3 is always off");
        strcat(webbuff,"\ </sup>");
    }
    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");
        pc.printf("outlet4 == 1\r\n");
    } else {
        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");
        pc.printf("outlet4 == 0 \r\n");
    }
//    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);");
    strcat(webbuff, "-webkit-border-radius:12;border-radius: 12px;font-family: Arial;color:#ffffff;font-size:20px;padding:");
    strcat(webbuff, "10px 20px 10px 20px; border:solid #103c57 3px;text-decoration: none;");
    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></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
    sprintf(cmdbuff,"AT+CIPSEND=%d,%d\r\n", linkID, bufl); // send IPD link channel and buffer character length.
    timeout=200;
    getcount=7;
    SendCMD();
    getreply();
    SendWEB();  // send web page
    memset(webbuff, '\0', sizeof(webbuff));
    sendcheck();
    pc.printf(webdata);
}

//  wait for ESP "SEND OK" reply, then close IP to load web page
void sendcheck()
{
    weberror=1;
    timeout=500;
    getcount=24;
    t2.reset();
    t2.start();
    while(weberror==1 && t2.read() <5) {
        getreply();
        if (strstr(replybuff, "SEND OK") != NULL) {
            weberror=0;   // wait for valid SEND OK
        }
    }
    if(weberror==1) { // restart connection
        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");
        timeout=500;
        getcount=10;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
        timeout=500;
        getcount=10;
        SendCMD();
        getreply();
        pc.printf(replybuff);
    } else {
        sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection
        SendCMD();
        getreply();
        pc.printf(replybuff);
    }
    t2.reset();
}

// Reads and processes GET and POST web data
void ReadWebData()
{
    wait_ms(200);
    esp.attach(NULL);
    count=0;
    DataRX=0;
    weberror=0;
    memset(webdata, '\0', sizeof(webdata));
    int x = strcspn (webbuff,"+");
    if(x) {
        strcpy(webdata, webbuff + x);
        weberror=0;
        int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
        if( strstr(webdata, "outlet1=1") != NULL ) {
            //led1=1;
            //Outlet1=0;
            //pc.printf("outlet1 set ON\r\n");
            SetMotion = 1;
        }
        //else pc.printf("outlet1 on not found\r\n");
        if( strstr(webdata, "outlet1=0") != NULL ) {
            //led1=0;
            //Outlet1=1;
            SetMotion = 0;
            pc.printf("outlet1 set OFF\r\n");
        }
        //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=0;
            setOut3=1;
            pc.printf("outlet3 set ON\r\n");
        }
        //else pc.printf("outlet3 on not found\r\n");
        if( strstr(webdata, "outlet3=0") != NULL ) {
            led3=0;
            setOut3=0;
            pc.printf("outlet3 set OFF\r\n");
        }
        if( strstr(webdata, "outlet4=1") != NULL ) {
            led4=1;
            Outlet4=1;
            pc.printf("outlet4 set ON\r\n");
        }
        //else pc.printf("outlet4 on not found\r\n");
        if( strstr(webdata, "outlet4=0") != NULL ) {
            led4=0;
            Outlet4=0;
            pc.printf("outlet4 set OFF\r\n");
        }
        //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;
//        }
        sprintf(channel, "%d",linkID);
        if (strstr(webdata, "GET") != NULL) {
            servreq=1;
        }
        if (strstr(webdata, "POST") != NULL) {
            servreq=1;
        }
        webcounter++;
        sprintf(webcount, "%d",webcounter);
    } else {
        memset(webbuff, '\0', sizeof(webbuff));
        esp.attach(&callback);
        weberror=1;
    }
}
// Starts and restarts webserver if errors detected.
void startserver()
{
    gettemp();
    gettime();
    pc.printf("\n\n RTC time   %s\r\n\n",timebuf);
    pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
    strcpy(cmdbuff,"AT+RST\r\n");
    timeout=8000;
    getcount=1000;
    SendCMD();
    getreply();
    pc.printf(replybuff);
    pc.printf("%d",count);
    if (strstr(replybuff, "OK") != NULL) {
        pc.printf("\n++++++++++ Starting Server ++++++++++\r\n");
        strcpy(cmdbuff, "AT+CIPMUX=1\r\n");  // set multiple connections.
        timeout=500;
        getcount=20;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", port);
        timeout=500;
        getcount=20;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        wait(1);
        sprintf(cmdbuff,"AT+CIPSTO=%d\r\n",SERVtimeout);
        timeout=500;
        getcount=50;
        SendCMD();
        getreply();
        pc.printf(replybuff);
        wait(5);
        pc.printf("\n Getting Server IP \r\n");
        strcpy(cmdbuff, "AT+CIFSR\r\n");
        timeout=2500;
        getcount=200;
        while(weberror==0) {
            SendCMD();
            getreply();
            if (strstr(replybuff, "0.0.0.0") == NULL) {
                weberror=1;   // wait for valid IP
            }
        }
        pc.printf("\n Enter WEB address (IP) found below in your browser \r\n\n");
        pc.printf("\n The MAC address is also shown below,if it is needed \r\n\n");
        replybuff[strlen(replybuff)-1] = '\0';
        //char* IP = replybuff + 5;
        sprintf(webdata,"%s", replybuff);
        pc.printf(webdata);
        //led2=1;
        bufflen=200;
        count=0;
        pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
        esp.attach(&callback);
    } else {
        pc.printf("\n++++++++++ ESP8266 error, check power/connections ++++++++++\r\n");
        while(1) {}
    }
    t2.reset();
    t2.start();
    beep();
}
// ESP Command data send
void SendCMD()
{
    esp.printf("%s", cmdbuff);
}
// Large WEB buffer data send
void SendWEB()
{
    int i=0;
    if(esp.writeable()) {
        while(webbuff[i]!='\0') {
            esp.putc(webbuff[i]);
            i++;
        }
    }
}
// Get Command and ESP status replies
void getreply()
{
    memset(replybuff, '\0', sizeof(replybuff));
    t1.reset();
    t1.start();
    replycount=0;
    while(t1.read_ms()< timeout && replycount < getcount) {
        if(esp.readable()) {
            replybuff[replycount] = esp.getc();
            replycount++;
        }
    }
    t1.stop();
}
// Analog in example
void getbattery()
{
    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()
{
 
    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()
{
    time_t seconds = time(NULL);
    strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
}

void beep()
{
//    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()
{
    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
}