IoT Door

Dependencies:   mbed

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

Revision:
6:c31fd3a33ea2
Parent:
5:bc0296a5ad8a
Child:
7:eee53c450d3d
--- a/main.cpp	Fri Mar 18 19:10:19 2016 +0000
+++ b/main.cpp	Thu Mar 09 23:17:18 2017 +0000
@@ -3,27 +3,28 @@
 #include "mbed.h"
 
 Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
-
-
-// Standard Mbed LED definitions
-DigitalOut  led1(LED1);
-DigitalOut  led2(LED2);
-DigitalOut  led3(LED3);
-DigitalOut  led4(LED4);
-
-// some test values to show on web page
-AnalogIn   Ain1(p18);
-AnalogIn   Ain2(p19);
-
+Serial esp(p9, p10); // tx, rx
+DigitalOut led1(LED1) ;
+DigitalOut led2(LED2) ;
+DigitalOut led3(LED3) ;
+DigitalOut led4(LED4) ;
+DigitalOut door(p23); //Solenoid For Door. 1 = Open
+AnalogIn ir_sensor(p19); //Input from IR Sensor
+PwmOut speaker(p22); //Speaker Driving Signal
+DigitalOut speaker_on(p21); //Turn Speaker and Amp off when not in use, 1=ON
+volatile int alarm = 0;
+volatile int armed = 0;
+int rangecount = 0;
+int unlock = 0;
+Timer tim;
+Timer t_unlock;
+Timer t_alarm;
+Ticker door_sense;
 /*
 char ssid[32] = "hsd";     // enter WiFi router ssid inside the quotes
 char pwd [32] = "austin123"; // enter WiFi router password inside the quotes
 */
-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];
+
 
 // things for sending/receiving data over serial
 volatile int tx_in=0;
@@ -47,7 +48,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();
 char rx_line[1024];
 int port        =80;  // set server port
 int SERVtimeout =5;    // set server timeout in seconds in case link breaks.
@@ -59,11 +60,34 @@
 int month       =8;     // 1-12
 int year        =15;    // last 2 digits
 
+void sensorcheck(){
+    led2 = alarm;
+    if(armed){
+            if((ir_sensor.read() > 0.5) && (rangecount < 15)){
+                if(rangecount == 0) tim.start();
+                rangecount++;
+            }
+            else if(rangecount == 15 && tim.read() > 6 && alarm==0){
+                 alarm = 1;
+                 
+                 t_alarm.start();
+            }
+            else if(ir_sensor.read() < 0.5 && alarm == 0){
+                tim.reset();
+                rangecount = 0;
+                led3 =!led3;
+            }
+        }
+        else alarm = 0;
+}
+    
+
 int main()
 {
+    
     pc.baud(9600);
     esp.baud(9600);
-    led1=1,led2=0,led3=0, led4=0;
+    led1 = 1;
     // Setup a serial interrupt function to receive data
     esp.attach(&Rx_interrupt, Serial::RxIrq);
     // Setup a serial interrupt function to transmit data
@@ -74,7 +98,20 @@
     startserver();
     DataRX=0;
     count=0;
+    led2=1;
+    door_sense.attach(&sensorcheck, 1.0);
     while(1) {
+        led4 =! led4;
+        if(unlock){
+            t_unlock.start();
+            door = 1;
+            if(t_unlock.read() > 3){
+                door = 0;
+                t_unlock.reset();
+                t_unlock.stop();
+                unlock = 0;
+            }
+        }
         if(DataRX==1) {
             ReadWebData();
             esp.attach(&Rx_interrupt, Serial::RxIrq);
@@ -83,11 +120,8 @@
         {
             // get new values
             gettime();
-            gettemp();
-            getbattery();
-            count++;
             // send new values
-            sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
+            sprintf(cmdbuff, "rangecount,time,alarm=%d,\"%s\",\"%d\"\r\n",rangecount,timebuf,alarm);
             SendCMD();
             getreply();
             update=0;   
@@ -108,16 +142,13 @@
     rx_out = 0;
     // check web data for form information
     if( strstr(webdata, "check=led1v") != NULL ) {
-        led1=!led1;
+        armed =! armed;
     }
     if( strstr(webdata, "check=led2v") != NULL ) {
-        led2=!led2;
+        unlock=!unlock;
     }
     if( strstr(webdata, "check=led3v") != NULL ) {
-        led3=!led3;
-    }
-    if( strstr(webdata, "check=led4v") != NULL ) {
-        led4=!led4;
+        alarm = 0;
     }
     if( strstr(webdata, "POST") != NULL ) { // set update flag if POST request
         update=1;
@@ -130,8 +161,6 @@
 void startserver()
 {
     gettime();
-    gettemp();
-    getbattery();
     pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
     strcpy(cmdbuff,"node.restart()\r\n");
     SendCMD();
@@ -139,13 +168,13 @@
     getreply();
     
     pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
-
+/*
     // initial values
     sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
     SendCMD();
     getreply();
     wait(0.5);
-
+*/
     //create server
     sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
     SendCMD();
@@ -167,19 +196,19 @@
         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 Door Control</h1>')\r\n");
         SendCMD();
         getreply();
         wait(0.4);
-        strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n");
+        strcpy(cmdbuff,"conn:send('Hit count: '..rangecount..'')\r\n");
         SendCMD();
         getreply();
         wait(0.2);
-        strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
+        strcpy(cmdbuff,"conn:send('<br>Sens Count: '..time..'<br><hr>')\r\n");
         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('Alarm Status: '..alarm..' V<br><hr>')\r\n");
         SendCMD();
         getreply();
         wait(0.3);
@@ -187,22 +216,18 @@
         SendCMD();
         getreply();
         wait(0.3);
-        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> flip LED1')\r\n");
-        SendCMD();
-        getreply();
-        wait(0.3);
-        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> flip LED2')\r\n");
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led1v\"> Arm/Disarm (LED1)')\r\n");
         SendCMD();
         getreply();
         wait(0.3);
-        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led3v\"> flip LED3')\r\n");
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led2v\"> Unlock (for 3s)')\r\n");
         SendCMD();
         getreply();
         wait(0.3);
-        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"led4v\"> flip LED4')\r\n");
+        strcpy(cmdbuff, "conn:send('<p><input type=\"checkbox\" name=\"check\" value=\"alarmv\"> Alarm on/off')\r\n");
         SendCMD();
         getreply();
-        wait(0.3);
+        wait(0.3);        
         strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
         SendCMD();
         getreply();
@@ -382,18 +407,3 @@
     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()
-{
-    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