IoT Alarm System

Dependencies:   ESP8266NodeMCUInterface JPEGCamera SDFileSystem IoT_Security xbee_lib

Fork of HUZZAHESP8266-web-control-LPC1768 by Austin Dong

Files at this revision

API Documentation at this revision

Comitter:
star297
Date:
Sat Feb 14 23:53:25 2015 +0000
Child:
1:71ed1afbf344
Commit message:
ESP8266 Wi-Fi Mbed controller v1.0

Changed in this revision

DS18B20.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DS18B20.lib	Sat Feb 14 23:53:25 2015 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/richardlane/code/DS18B20_1wire/#00972ed59ba3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Feb 14 23:53:25 2015 +0000
@@ -0,0 +1,259 @@
+// ESP8266 Static page WEB server to control Mbed
+
+#include "mbed.h"
+#include "DS18B20.h"
+
+Serial pc(USBTX, USBRX);
+Serial esp(PTE0, PTE1); // tx, rx
+
+// Standard Mbed LED definitions
+DigitalOut  led1(LED_RED);      // (PTB18)
+DigitalOut  led2(LED_GREEN);    // (PTB19)
+DigitalOut  led3(LED_BLUE);     // (PTD1)
+
+// Digital Out and In pins, can be configured to any suitable pin depending on Platform
+DigitalOut  Out1(PTC7);
+DigitalOut  Out2(PTC0);
+DigitalOut  Out3(PTC3);
+
+DigitalIn  In1(PTC12);
+DigitalIn  In2(PTC13);
+DigitalIn  In3(PTC16);
+
+AnalogIn   BATin(PTC1);
+
+DS18B20 thermom(PTB0, DS18B20::RES_12_BIT);
+
+Timer t;
+Timer t1;
+
+int buflen, DataRX, count, ended, servreq, timeout;
+int bufl, ipdLen, linkID, weberror;
+float temperature, AdcIn, Ht; 
+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 timebuf[30];
+char type[16];
+char type1[16];
+char channel[2];
+char snd[256];
+char webdata[1024]; // This may need to be bigger depending on WEB browser used
+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();
+
+// Serial Interrupt read ESP data
+void callback() {    
+    while (esp.readable()) {buf[count] = esp.getc();count++;}
+    if(strlen(buf)>buflen){DataRX=1;}  
+}
+
+int main() {
+    led1=1,led2=1,led3=1;
+    pc.baud(115200);
+    esp.baud(115200);   // ESP8266 baudrate.
+    if (time(NULL) < 1420070400) {set_time(1420070400);}    
+    
+    startserver();
+      
+    while(1){        
+        
+        if(DataRX==1){
+            ReadWebData();
+            if (servreq == 1 && weberror == 0){sendpage();}    
+            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");
+            esp.attach(&callback);
+            servreq=0;               
+        }                               
+    }
+}    
+// Static WEB page    
+void sendpage()
+{          
+    gettemp();getbattery();gettime();
+// WEB page data    
+    memset(buf, '\0', sizeof(buf));
+    strcpy(buf, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n");
+    strcat(buf, "<html><head><title>ESP8266 Mbed</title></head>\r\n");
+    strcat(buf, "<body>\r\n");
+    strcat(buf, "<h1>ESP8266 Mbed Web Controller</h1>\r\n");                        
+    strcat(buf, "<br /><hr>\r\n");
+    strcat(buf, "<h3>Mbed RTC Time -  ");             
+    strcat(buf, timebuf);
+    strcat(buf, "</h3>\r\n");
+    strcat(buf, "<p><form method=\"POST\"><strong> Temperature:  <input type=\"text\" size=4 value=\"");
+    strcat(buf, Temp);
+    strcat(buf, "\"> <sup>O</sup>C <form method=\"POST\"> <strong> Battery:  <input type=\"text\" size=4 value=\"");
+    strcat(buf, Vcc);
+    strcat(buf, "\"> </sup>V");    
+    if(led1==1){strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked>  Red LED off");
+                strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" >  Red LED on");} 
+        else{strcat(buf, "<p><input type=\"radio\" name=\"led1\" value=\"0\" >  Red LED off");
+                strcat(buf, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked>  Red LED on");}   
+    if(Out1==0){strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked>  Digital Out 1  off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" >  Digital Out 1 on");}
+        else{strcat(buf, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" >  Digital Out 1 off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked>  Digital Out 1 on");}   
+    if(Out2==0){strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked>  Digital Out 2 off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" >  Digital Out 2 on");}
+        else{strcat(buf, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" >  Digital Out 2 off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked>  Digital Out 2 on");}           
+    if(Out3==0){strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked>  Digital Out 3 off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" >  Digital Out 3 on");}
+        else{strcat(buf, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" >  Digital Out 3 off");
+                strcat(buf, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked>  Digital Out 3 on");}    
+    if(In1==0){strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"0\" >  Digital In 1");}
+        else{strcat(buf, "<p><input type=\"radio\" name=\"In1\" value=\"1\" checked>  Digital In 1");}   
+    if(In2==0){strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"0\" >  Digital In 2");}
+        else{strcat(buf, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked>  Digital In 2");}           
+    if(In3==0){strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"0\" >  Digital In 3");}
+        else{strcat(buf, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked>  Digital In 3");}           
+    strcat(buf, "</strong><p><input type=\"submit\"></form></span>");
+    strcat(buf, "</body>\r\n</html>\r\n");   
+    strcat(buf, "<p/><h2>How to use:</h2><ul>");
+    strcat(buf, "<li>Select the Radio buttons to control the digital out pins.</li>");
+    strcat(buf, "<li>Click 'Submit Query' to send.</li>");      
+    strcat(buf, "<li>Use the 'Submit Query' button to refresh the page.</li>"); 
+    strcat(buf, "</ul>");
+// end of WEB page data       
+    bufl = strlen(buf); // get total page buffer length            
+    sprintf(snd,"AT+CIPSEND=%s,%d\r\n", channel, bufl); // send IPD link channel and buffer character length.
+    SendCMD();                                   
+    wait_ms(100);                        
+    SendWEB();  // send web page
+    sendcheck();              
+}
+
+//  wait for ESP "SEND OK" reply, then close IP to load web page, if lock out, system fully restarts
+void sendcheck()
+{
+    timeout=200;
+    weberror=1;t1.reset();t1.start();
+    while(weberror==1 && t1.read() <10){
+        getreply();
+        if (strstr(buf, "SEND OK") != NULL) {weberror=0;}  // wait for valid SEND OK
+        }
+    t1.stop();
+    if(weberror==1){
+        pc.printf("\n\n\n ++++++++++ WEB Connect Error, restarting ...   ++++++++++\n\n");
+        NVIC_SystemReset();}     
+    strcpy(snd, "AT+CIPCLOSE=0\r\n");
+    SendCMD();    
+}    
+
+// Reads and processes GET and POST web data 
+void ReadWebData()
+ {
+    wait_ms(200);
+    led3=0;    
+    count=0;DataRX=0;weberror=0;    
+    esp.attach(NULL);    
+    int x = strcspn (buf,"+");
+    if(x){
+        strcpy(webdata, buf + x);weberror=0;                                   
+        int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);    
+        if( strstr(webdata, "led1=1") != NULL ) {led1=0;}
+        if( strstr(webdata, "led1=0") != NULL ) {led1=1;}
+        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;}
+        }
+            else {strcpy(webdata, buf);weberror=1;}
+    led3=1;
+}
+// Starts and restarts webserver if errors detected.
+void startserver()
+{
+    esp.attach(NULL);
+    pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
+    wait(1);
+    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
+        }
+    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);  
+}    
+// ESP Command data send
+void SendCMD()
+{
+    esp.printf("%s", snd);      
+}    
+// Large WEB buffer data send
+void SendWEB()
+{    
+    int i=0;
+    if(esp.writeable()) {
+        while(buf[i]!='\0') {esp.putc(buf[i]);i++;}
+        }     
+} 
+// Get Cammand and ESP status replies
+void getreply()
+{    
+    memset(buf, '\0', sizeof(buf));
+    t.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();
+            }
+        }   
+}
+// Analog in example
+void getbattery()
+{
+    AdcIn=BATin.read(); 
+    Ht = (AdcIn*3.328*(R1+R2)/R2); // set the numeric to the exact MCU analog reference voltage for greater accuracy
+    sprintf(Vcc,"%2.3f",Ht);    
+}
+// Temperature example
+void gettemp()
+{
+    temperature=thermom.GetTemperature();   // comment this out if no sensor connected
+    //temperature = 21.357;                 // include for dummy value
+    sprintf(Temp,"%3.3f",temperature);             
+}
+// Get RTC time        
+void gettime()
+{
+    time_t seconds = time(NULL);
+    strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));               
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Feb 14 23:53:25 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e188a91d3eaa
\ No newline at end of file