present
Dependencies: DS3231 RHT03 TextLCD mbed
Fork of HomeStatus by
Diff: main.cpp
- Revision:
- 2:dad081eaee81
- Parent:
- 1:ae97d85a47dd
- Child:
- 3:19accdf325fe
diff -r ae97d85a47dd -r dad081eaee81 main.cpp --- a/main.cpp Sat May 23 04:58:25 2015 +0000 +++ b/main.cpp Tue May 26 15:46:01 2015 +0000 @@ -7,6 +7,40 @@ DS3231 rtc(D14,D15); RHT03 rht(D13); DigitalOut p1(D3),p2(D4),p3(D5),p4(D6),p5(D7),p6(D9),p7(D10),p8(D11); +Serial esp(D8, D2); // tx, rx +Serial pc(USBTX, USBRX); // tx, rx + + +int bufflen, DataRX, count, getcount, replycount, servreq, timeout; +int bufl, ipdLen, linkID, weberror, webcounter; +float Temp,Hum; +int dw,d,M,y,h,m,s; +char webcount[8]; +char lasthit[30]; +char timebuf[30]; +char type[16]; +char type1[16]; +char channel[2]; +char cmdbuff[32]; +char replybuff[512]; +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 +Timer t1; +Timer t2; + +void sendpage(); +void ReadWebData(); +void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck(); + +// Serial Interrupt read ESP data +void callback() { + while (esp.readable()) {webbuff[count] = esp.getc();count++;} + if(strlen(webbuff)>bufflen){DataRX=1;} +} + + + + int main() { @@ -15,8 +49,6 @@ lcd.setCursor(TextLCD::CurOff_BlkOff); //rtc.setTime(20,58,00); //rtc.setDate(5,22,5,2015); - float Temp,Hum; - int dw,d,M,y,h,m,s; while(1) { rtc.readDateTime(&dw,&d,&M,&y,&h,&m,&s); @@ -50,9 +82,203 @@ lcd.setAddress(0,1); lcd.printf("Port7:%3s Port8:%3s",(p7==1)?"On":"Off",(p8==1)?"On":"Off"); wait(3); + + if(DataRX==1){ + ReadWebData(); + 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; + } } return 0; } + +void sendpage() +{ + char t1[1000]; + +// WEB page data + strcpy(webbuff, "<!DOCTYPE html>"); + strcat(webbuff, "<html><head><title>Home Status</title></head>"); + strcat(webbuff, "<body>"); + strcat(webbuff, "<p>"); + sprintf(t1,"%02d/%02d/%4d %02d:%02d:%02d",d,M,y,h,m,s); + strcat(webbuff,t1); + strcat(webbuff, "<p>"); + strcat(webbuff,"<p>Temperature : "); + sprintf(t1,"%.1f",Temp); + strcat(webbuff,t1); + strcat(webbuff, " C<p>"); + strcat(webbuff,"<p>Humidity : "); + sprintf(t1,"%.1f",Hum); + strcat(webbuff,t1); + strcat(webbuff, " %<p>"); + strcat(webbuff, "<table>"); + strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 1 :</td><td>"); + if(p1==1) + { + strcat(webbuff, "<form action=\"Light1:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light1:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + } + strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p2==1) + { + strcat(webbuff, "<form action=\"Light2:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light2:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + } + strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p3==1) + { + strcat(webbuff, "<form action=\"Light3:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light3:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + }strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p4==1) + { + strcat(webbuff, "<form action=\"Light4:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light4:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + }strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p5==1) + { + strcat(webbuff, "<form action=\"Light5:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light5:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + }strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p6==1) + { + strcat(webbuff, "<form action=\"Light6:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light6:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + }strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p7==1) + { + strcat(webbuff, "<form action=\"Light7:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light7:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + }strcat(webbuff, "<tr>"); + strcat(webbuff, "<td>Light 2 :</td><td>"); + if(p8==1) + { + strcat(webbuff, "<form action=\"Light8:OFF\"><input type=\"submit\" value=\"ON\" /></form></td></tr>"); + }else{ + strcat(webbuff, "<form action=\"Light8:ON\"><input type=\"submit\" value=\"OFF\" /></form></td></tr>"); + } + + + + + strcat(webbuff, "</table></p></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(); +} +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, "Light1:OFF") != NULL ) {p1=0;} + if( strstr(webdata, "Light1:ON") != NULL ) {p1=1;} + if( strstr(webdata, "Light2:OFF") != NULL ) {p2=0;} + if( strstr(webdata, "Light2:ON") != NULL ) {p2=1;} + if( strstr(webdata, "Light3:OFF") != NULL ) {p3=0;} + if( strstr(webdata, "Light3:ON") != NULL ) {p3=1;} + if( strstr(webdata, "Light4:OFF") != NULL ) {p4=0;} + if( strstr(webdata, "Light4:ON") != NULL ) {p4=1;} + if( strstr(webdata, "Light5:OFF") != NULL ) {p5=0;} + if( strstr(webdata, "Light5:ON") != NULL ) {p5=1;} + if( strstr(webdata, "Light6:OFF") != NULL ) {p6=0;} + if( strstr(webdata, "Light6:ON") != NULL ) {p6=1;} + if( strstr(webdata, "Light7:OFF") != NULL ) {p7=0;} + if( strstr(webdata, "Light7:ON") != NULL ) {p7=1;} + if( strstr(webdata, "Light8:OFF") != NULL ) {p8=0;} + if( strstr(webdata, "Light8:ON") != NULL ) {p8=1;} + + 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; + } +} +// ESP Command data send +void SendCMD() +{ + esp.printf("%s", cmdbuff); +} +// Get Cammand 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(); +} +// Large WEB buffer data send +void SendWEB() +{ + int i=0; + if(esp.writeable()) { + while(webbuff[i]!='\0') {esp.putc(webbuff[i]);i++;} + } +} +// 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(); + sprintf(cmdbuff,"AT+CIPSERVER=1,%d\r\n", 80); + timeout=500;getcount=10; + SendCMD();getreply(); + } + else{ + sprintf(cmdbuff, "AT+CIPCLOSE=%s\r\n",channel); // close current connection + SendCMD();} + t2.reset(); +} \ No newline at end of file