This code creates a WiFi DoorLock

Dependencies:   4DGL-uLCD-SE ESP8266-WEB-Mbed-LPC1768-Controller Servo mbed

Fork of ESP8266-WEB-Mbed-LPC1768-Controller by jim hamblen

Revision:
5:3a405e2f4ad1
Parent:
4:40dd020463ea
--- a/main.cpp	Fri Aug 28 01:37:50 2015 +0000
+++ b/main.cpp	Fri Oct 23 10:55:38 2015 +0000
@@ -1,31 +1,17 @@
 // ESP8266 Static page WEB server to control Mbed
+// http://192.168.43.200
 
 #include "mbed.h"
-//#include "DS18B20.h"
+#include "Servo.h"
+#include "uLCD_4DGL.h"
 
 Serial pc(USBTX, USBRX);
-Serial esp(p28, p27); // tx, rx
-
-//DS18B20 thermom(A0, DS18B20::RES_12_BIT);
-
-// Standard Mbed LED definitions
-DigitalOut  led1(LED1);      // (PTB18)
-DigitalOut  led2(LED2);    // (PTB19)
-DigitalOut  led3(LED3);     // (PTD1)
+Serial esp(p9, p10); // tx, rx
+uLCD_4DGL lcd(p28, p27, p29);
 
-// Digital Out and In pins, can be configured to any suitable pin depending on Platform
-DigitalOut  Out1(p6);
-DigitalOut  Out2(p7);
-DigitalOut  Out3(p8);
-DigitalOut  reset(p26);
+Servo door_lock(p22);
+int locked = 0;
 
-DigitalIn  In1(p9);
-DigitalIn  In2(p10);
-DigitalIn  In3(p11);
-
-PwmOut speaker(p21);
-AnalogIn   Ain1(p18);
-AnalogIn   Ain2(p19);
 
 Timer t1;
 Timer t2;
@@ -51,7 +37,8 @@
 char webbuff[4096];     // Currently using 1986 characters, Increase this if more web page data added
 
 void SendCMD(),getreply(),ReadWebData(),startserver(),sendpage(),SendWEB(),sendcheck();
-void gettime(),gettemp(),getbattery(),setRTC(),beep();
+void gettime(),setRTC();
+void processLock();
 
 // manual set RTC values
 int minute      =00;    // 0-59
@@ -66,26 +53,26 @@
 // Serial Interrupt read ESP data
 void callback()
 {
-    led3=1;
     while (esp.readable()) {
         webbuff[count] = esp.getc();
         count++;
     }
     if(strlen(webbuff)>bufflen) {
         DataRX=1;
-        led3=0;
     }
 }
 
 int main()
 {
-    reset=0;
     pc.baud(115200);
+    lcd.baudrate(3000000);
+    lcd.background_color(0);
+    lcd.cls();
+    
+    lcd.printf("Initializing...");
 
     pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r");
     wait(0.5);
-    reset=1;
-    led1=1,led2=0,led3=0;
     timeout=6000;
     getcount=500;
     getreply();
@@ -93,13 +80,13 @@
     if (time(NULL) < 1420070400) {
         setRTC();
     }
-    beep();
     startserver();
+    lcd.cls();
+    lcd.printf("Finished Setup: Door is unlocked");
 
     while(1) {
         if(DataRX==1) {
             ReadWebData();
-            beep();
             if (servreq == 1 && weberror == 0) {
                 sendpage();
             }
@@ -116,15 +103,13 @@
 // Static WEB page
 void sendpage()
 {
-    gettemp();
-    getbattery();
     gettime();
 
 // WEB page data
     strcpy(webbuff, "<!DOCTYPE html>");
-    strcat(webbuff, "<html><head><title>ESP8266 Mbed LPC1768</title></head>");
+    strcat(webbuff, "<html><head><title>Internet-enabled Door Lock</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, "<div style=\"text-align:center; background-color:#F4F4F4; color:#00AEDB;\"><h1>Door Lock Web Controller</h1>");
     strcat(webbuff, "Hit Count - ");
     strcat(webbuff, webcount);
     strcat(webbuff, "<br>Last Hit - ");
@@ -133,53 +118,13 @@
     strcat(webbuff, "<h3>Mbed RTC Time -&nbsp&nbsp");
     strcat(webbuff, timebuf);
     strcat(webbuff, "</h3>\r\n");
-    strcat(webbuff, "<p><form method=\"POST\"><strong> Analog 1:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
-    strcat(webbuff, Temp);
-    strcat(webbuff, "\"> </sup>V <form method=\"POST\"> <strong> &nbsp&nbspAnalog 2:&nbsp&nbsp<input type=\"text\" size=4 value=\"");
-    strcat(webbuff, Vcc);
-    strcat(webbuff, "\"> </sup>V");
-    if(led1==0) {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" checked>  LED 1 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" >  LED 1 on");
-    } else {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"led1\" value=\"0\" >  LED 1 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"led1\" value=\"1\" checked>  LED 1 on");
-    }
-    if(Out1==0) {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" checked>  Digital Out 1  off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" >  Digital Out 1 on");
-    } else {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out1\" value=\"0\" >  Digital Out 1 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out1\" value=\"1\" checked>  Digital Out 1 on");
-    }
-    if(Out2==0) {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" checked>  Digital Out 2 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" >  Digital Out 2 on");
+    strcat(webbuff, "<p><form method=\"POST\">");
+    if(locked==0) {
+        strcat(webbuff, "<p><input type=\"radio\" name=\"locked\" value=\"0\" checked>  Door Unlocked");
+        strcat(webbuff, "<br><input type=\"radio\" name=\"locked\" value=\"1\" >  Door Locked");
     } else {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out2\" value=\"0\" >  Digital Out 2 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out2\" value=\"1\" checked>  Digital Out 2 on");
-    }
-    if(Out3==0) {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" checked>  Digital Out 3 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" >  Digital Out 3 on");
-    } else {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"Out3\" value=\"0\" >  Digital Out 3 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"Out3\" value=\"1\" checked>  Digital Out 3 on");
-    }
-    if(In1==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");
-    }
-    if(In2==0) {
-        strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"0\" >  Digital In 2");
-    } else {
-        strcat(webbuff, "<br><input type=\"radio\" name=\"In2\" value=\"1\" checked>  Digital In 2");
-    }
-    if(In3==0) {
-        strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"0\" >  Digital In 3");
-    } else {
-        strcat(webbuff, "<br><input type=\"radio\" name=\"In3\" value=\"1\" checked>  Digital In 3");
+        strcat(webbuff, "<p><input type=\"radio\" name=\"locked\" value=\"0\" >  Door Unlocked");
+        strcat(webbuff, "<br><input type=\"radio\" name=\"locked\" value=\"1\" checked>  Door Locked");
     }
     strcat(webbuff, "</strong><p><input type=\"submit\" value=\"send-refresh\" style=\"background: #3498db;");
     strcat(webbuff, "background-image:-webkit-linear-gradient(top, #3498db, #2980b9);");
@@ -258,29 +203,11 @@
         strcpy(webdata, webbuff + x);
         weberror=0;
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
-        if( strstr(webdata, "led1=1") != NULL ) {
-            led1=1;
-        }
-        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, "locked=0") != NULL ) {
+            locked = 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;
+        if( strstr(webdata, "locked=1") != NULL ) {
+            locked = 1;
         }
         sprintf(channel, "%d",linkID);
         if (strstr(webdata, "GET") != NULL) {
@@ -296,11 +223,12 @@
         esp.attach(&callback);
         weberror=1;
     }
+    
+    processLock();
 }
 // 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");
@@ -350,7 +278,6 @@
         //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");
@@ -361,7 +288,6 @@
     }
     t2.reset();
     t2.start();
-    beep();
 }
 // ESP Command data send
 void SendCMD()
@@ -394,21 +320,7 @@
     }
     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);
-}
-// 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);
-}
+
 // Get RTC time
 void gettime()
 {
@@ -416,14 +328,6 @@
     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(60);
-    speaker=0.0; // turn off audio
-}
-
 void setRTC()
 {
     t.tm_sec = (0);             // 0-59
@@ -434,3 +338,15 @@
     t.tm_year = ((year)+100);   // year since 1900,  current DCF year + 100 + 1900 = correct year
     set_time(mktime(&t));       // set RTC clock
 }
+
+void processLock()
+{
+    lcd.cls();
+    if(locked == 1) {
+        door_lock.write(0.0);
+        lcd.printf("Door is Locked");
+    } else {
+        door_lock.write(0.5);
+        lcd.printf("Door is unlocked");
+    }
+}
\ No newline at end of file