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:
kbrahmbhatt6
Date:
Fri Apr 29 07:04:08 2016 +0000
Parent:
5:bc0296a5ad8a
Commit message:
1

Changed in this revision

ESP8266NodeMCUInterface.lib Show annotated file Show diff for this revision Revisions of this file
JPEGCamera.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.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 diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
xbee_lib.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ESP8266NodeMCUInterface.lib	Fri Apr 29 07:04:08 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/ESP8266/code/ESP8266NodeMCUInterface/#6031f70e3914
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/JPEGCamera.lib	Fri Apr 29 07:04:08 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/yamaguch/code/JPEGCamera/#27417b6a951d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Fri Apr 29 07:04:08 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- a/main.cpp	Fri Mar 18 19:10:19 2016 +0000
+++ b/main.cpp	Fri Apr 29 07:04:08 2016 +0000
@@ -1,10 +1,55 @@
 // ESP8266 Static page WEB server to control Mbed
 
 #include "mbed.h"
+#include "JPEGCamera.h"
+#include "xbee.h"
+#include "ESP8266Interface.h"
+#include "TCPSocketConnection.h"
+#include "SDFileSystem.h"
+ 
+SDFileSystem sd(p5, p6, p7, p8, "sd"); // the pinout on the mbed Cool Components workshop board
+//#include "rtos.h"
+
+//Print temperature from TMP36 analog temperature sensor
+///////////////////////////////////////////////////////////////////////////////
+//Setup a new class for TMP36 sensor
+class TMP36
+{
+public:
+TMP36(PinName pin);
+TMP36();
+operator float ();
+float read();
+private:
+//class sets up the AnalogIn pin
+AnalogIn _pin;
+};
+
+TMP36::TMP36(PinName pin) : _pin(pin)
+{
+// _pin(pin) means pass pin to the AnalogIn constructor
+}
+
+float TMP36::read()
+{
+//convert sensor reading to temperature in degrees C
+return ((_pin.read()*3.3)-0.500)*100.0;
+}
+//overload of float conversion (avoids needing to type .read() in equations)
+TMP36::operator float ()
+{
+//convert sensor reading to temperature in degrees C
+return ((_pin.read()*3.3)-0.500)*100.0;
+}
+///////////////////////////////////////////////////////////////////////////////
+
+
+
+
 
 Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
-
+//Serial esp(p28, p27); // tx, rx
+LocalFileSystem local("local"); //save images on mbed
 
 // Standard Mbed LED definitions
 DigitalOut  led1(LED1);
@@ -37,6 +82,7 @@
 void Rx_interrupt();
 void send_line();
 void read_line();
+void alarmPhoto(int);
 
 int DataRX;
 int update;
@@ -47,7 +93,7 @@
 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
 char timebuf[30];
 void SendCMD(),getreply(),ReadWebData(),startserver();
-void gettime(),setRTC(),gettemp(),getbattery();
+void gettime(),setRTC(),getTemperature();
 char rx_line[1024];
 int port        =80;  // set server port
 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
@@ -59,21 +105,107 @@
 int month       =8;     // 1-12
 int year        =15;    // last 2 digits
 
+PwmOut Alarm(p21);
+JPEGCamera camera(p13, p14); //Camera
+TMP36 myTMP36(p15);
+DigitalOut P16(p16);
+DigitalOut P17(p17);
+DigitalOut P19(p19);
+DigitalOut P20(p20);
+float tempC;
+xbee xbee2(p9,p10,p11);
+char read_data[202];
+Serial esp(p28, p27); // tx, rx
+void Email();
+//ESP8266Interface esp1(p28, p27, p26, 9600, 3000);
+//EthernetInterface eth;
+
+////////
+//tempC = myTMP36;
+///printf(" T=%5.2F C \n\r", tempC);//for testing
+//if(tempC>45) {
+//add code
+//}
+///////
+
 int main()
 {
+    camera.setPictureSize(JPEGCamera::SIZE320x240);
     pc.baud(9600);
+    ////////////////////////////////xbee2.RecieveData(read_data,0); //ADD THIS BACK!!!
+//    led3 = 1;
+//    
+//    ///////////Emailing the user
+//    Email();
     esp.baud(9600);
     led1=1,led2=0,led3=0, led4=0;
     // Setup a serial interrupt function to receive data
     esp.attach(&Rx_interrupt, Serial::RxIrq);
     // Setup a serial interrupt function to transmit data
     esp.attach(&Tx_interrupt, Serial::TxIrq);
+    Alarm.period(1.0/100);
     if (time(NULL) < 1420070400) {
         setRTC();
     }
     startserver();
     DataRX=0;
     count=0;
+    xbee2.RecieveData(read_data,0);
+    led3 = 1;
+    Alarm = 0.5;
+    //Take photo
+    alarmPhoto(5); //will be saved as PICT005.jpg
+    //FILE *fp = fopen("/local/pict005.jpg", "w");  // Open "out.txt" on the local file system for writing
+//    if (fp != NULL) {
+//        pc.printf("File opens\n");
+//    }
+//    mkdir("/sd/mydir", 0777);
+//    FILE *fp1 = fopen("/sd/mydir/sdtest.jpg", "w");
+//    //fprintf(fp1, fp);
+//    fclose(fp1);
+    
+    // email the user
+    // Can turn-off the alarm
+    
+//    
+    ///////////Emailing the user
+    //Email();
+//    ESP8266Interface esp1(p28, p27, p26, 9600, 3000);
+//    esp1.init();
+//    esp1.connect("ssid", "password");
+//    printf("IP Address is %s\n", esp1.getIPAddress());
+//    TCPSocketConnection sock;
+//    //delete esp1;
+//
+//
+//    sock.connect("blahblahmbed.comlu.com", 80); //http://blahblahmbed.comlu.com/ "mbed.org"
+//
+//    char http_cmd[] = "GET http://blahblahmbed.comlu.com/index.php HTTP/1.0\n\n"; // /media/uploads/mbed_official/hello.txt
+//    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+//    char buffer[300];
+//    int ret;
+//    while (true) {
+//        ret = sock.receive(buffer, sizeof(buffer)-1);
+//        if (ret <= 0) break;
+//        buffer[ret] = '\0';
+//        printf("Received %d chars from server:\n%s\n", ret, buffer);
+//    }
+//    sock.close();
+//    esp1.disconnect();
+//    //////////
+    //if (time(NULL) < 1420070400) {
+//        setRTC();
+//    }
+//    startserver();
+    //eth.init(); //Use DHCP
+    //eth.connect();
+    //printf("IP Address is %s\n", eth.getIPAddress());
+    
+    //TCPSocketConnection sock;
+    //sock.connect("blahblahmbed.comlu.com", 80); //http://blahblahmbed.comlu.com/  "mbed.org"
+    
+    //char http_cmd[] = "GET http://blahblahmbed.comlu.com/index.php HTTP/1.0\n\n"; // /media/uploads/mbed_official/hello.txt
+    //sock.send_all(http_cmd, sizeof(http_cmd)-1);
     while(1) {
         if(DataRX==1) {
             ReadWebData();
@@ -83,11 +215,18 @@
         {
             // get new values
             gettime();
-            gettemp();
-            getbattery();
+            //gettemp();
+            //getbattery();
+            getTemperature();
+            //if (tempC > 27) {
+//                Alarm = 0.5;
+//            }
             count++;
             // send new values
-            sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
+            //sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
+            sprintf(cmdbuff, "count,time,tempC=%d,\"%s\",\"%f\"\r\n",count,timebuf,tempC);
+            //esp1.send(cmdbuff, 1024);
+            //esp1.recv(replybuff, sizeof(replybuff));
             SendCMD();
             getreply();
             update=0;   
@@ -95,6 +234,33 @@
     }
 }
 
+void Email() {
+    /////////Emailing the user
+    ESP8266Interface esp1(p28, p27, p26, 9600, 3000);
+    esp1.init();
+    esp1.connect("ssid", "password");
+    printf("IP Address is %s\n", esp1.getIPAddress());
+    TCPSocketConnection sock;
+    //delete esp1;
+
+
+    sock.connect("blahblahmbed.comlu.com", 80); //http://blahblahmbed.comlu.com/ "mbed.org"
+
+    char http_cmd[] = "GET http://blahblahmbed.comlu.com/index.php HTTP/1.0\n\n"; // /media/uploads/mbed_official/hello.txt
+    sock.send_all(http_cmd, sizeof(http_cmd)-1);
+    char buffer[300];
+    int ret;
+    while (true) {
+        ret = sock.receive(buffer, sizeof(buffer)-1);
+        if (ret <= 0) break;
+        buffer[ret] = '\0';
+        printf("Received %d chars from server:\n%s\n", ret, buffer);
+    }
+    sock.close();
+    esp1.disconnect();
+    //////////
+}
+
 // Reads and processes GET and POST web data
 void ReadWebData()
 {
@@ -119,6 +285,18 @@
     if( strstr(webdata, "check=led4v") != NULL ) {
         led4=!led4;
     }
+    if( strstr(webdata, "check=speakeron") != NULL ) {
+        if (Alarm == 0.5){Alarm=0.0;}
+        else Alarm=0.5;
+    }
+    if( strstr(webdata, "check=photo") != NULL ) {
+        alarmPhoto(count);
+        FILE *fp = fopen("/local/pict003.jpg", "w");  // Open "out.txt" on the local file system for writing
+        if (fp != NULL) {
+            pc.printf("File opens\n");
+        }
+        fclose(fp);
+    }
     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
         update=1;
     }
@@ -130,8 +308,8 @@
 void startserver()
 {
     gettime();
-    gettemp();
-    getbattery();
+    //gettemp();
+    //getbattery();
     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
     strcpy(cmdbuff,"node.restart()\r\n");
     SendCMD();
@@ -166,11 +344,14 @@
         getreply();
         wait(0.2);
        
-        //web page data
-        strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>ESP8266 Mbed IoT Web Controller</h1>')\r\n");
+        strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>IoT Alarm System</h1>')\r\n");
         SendCMD();
         getreply();
         wait(0.4);
+//        strcpy(cmdbuff,"conn:send('<body style=\"background-color:#FF0000;>')\r\n");
+//        SendCMD();
+//        getreply();
+//        wait(0.4);
         strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n");
         SendCMD();
         getreply();
@@ -179,10 +360,15 @@
         SendCMD();
         getreply();
         wait(0.4);
-        strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n");
+        //strcpy(cmdbuff,"conn:send('Analog 1: '..analog1..' V<br>Analog 2: '..analog2..' V<br><hr>')\r\n");
+//        SendCMD();
+//        getreply();
+//        wait(0.3);
+        strcpy(cmdbuff,"conn:send('Temperature: '..tempC..'')\r\n");
         SendCMD();
         getreply();
         wait(0.3);
+
         strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
         SendCMD();
         getreply();
@@ -191,6 +377,13 @@
         SendCMD();
         getreply();
         wait(0.3);
+        /////////////////////////////////////////////////////////////////////////////////////////////////////////
+//        strcpy(cmdbuff, "conn:send('<p><input type=\"image\" src=\"/PICT002.jpg\" alt=\"Submit\"> blah')\r\n");
+//        SendCMD();
+//        getreply();
+//        wait(0.3);
+        /////////////////////////////////////////////////////////////////////////////////////////////////////////
+        
         strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n");
         SendCMD();
         getreply();
@@ -203,6 +396,16 @@
         SendCMD();
         getreply();
         wait(0.3);
+        
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"speakeron\"> Alarm')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"photo\"> Take Photo')\r\n");
+        SendCMD();
+        getreply();
+        wait(0.3);
+        
         strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
         SendCMD();
         getreply();
@@ -382,18 +585,76 @@
     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
     set_time(mktime(&t));       // set RTC clock
 }
+
 // Analog in example
-void getbattery()
+void getTemperature()
 {
-    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);
+    tempC=myTMP36;
+    //Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
+    //tempC = 5.0;
+    
+    if (tempC > 30.0) {
+        //then alarm goes off (speaker)
+        //Message sent to webpage (maybe email or text)
+        
+        //Take input from webpage to turn it off    
+    }
+    
+    //sprintf(Vcc,"%2.3f",tempC);
 }
-// Temperature example
-void gettemp()
+void alarmPhoto(int i)
 {
- 
-    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);
-}
\ No newline at end of file
+    if (camera.isReady()) {
+    char filename[32];
+    sprintf(filename, "/local/pict%03d.jpg", i);
+    //string a = string(filename);
+    pc.printf("Picture: %s ", filename);
+        if (camera.takePicture(filename)) 
+        {
+            while (camera.isProcessing()) 
+            {
+                camera.processPicture();
+            }
+        led1 = 1; //show successful picture was taken 
+        wait(2.0);
+        led1 = 0;
+        } 
+        else 
+         {
+             pc.printf("take picture failed\n");
+             led3 = 1; //show picture take failed
+             wait(2.0);
+             led3 = 0;
+         }
+        } 
+    else
+         {
+            pc.printf("camera is not ready\n");
+            led4 = 1; //show camera is not ready
+            wait(2.0);
+            led4 = 0;
+          }  
+     led2 = 1; //show end of sequence
+     wait(2.0);
+     led2 = 0;
+     pc.printf("Photo taken\n");
+     //return filename;
+}
+
+
+
+// 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);
+//}
+//// 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);
+//}
\ No newline at end of file
--- a/mbed.bld	Fri Mar 18 19:10:19 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/8ed44a420e5c
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Fri Apr 29 07:04:08 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/kbrahmbhatt6/code/IoT_Security/#2f388b030837
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee_lib.lib	Fri Apr 29 07:04:08 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/tristanjph/code/xbee_lib/#6455a079bdb3