Demo code for a Wi-Fi controlled wall outlet that monitors light intensity, temperature, and motion and can control which plugs are supplied with power based off of user preference.

Dependencies:   BH1750 DHT11_Lib mbed

Fork of ECE4180_Lab4_ESP8266_IoT_Outlets by Deron Mai

Revision:
9:69283f36da94
Parent:
8:341d1b699cd3
Child:
10:1913b24b4afb
Child:
11:74be5786e4b0
diff -r 341d1b699cd3 -r 69283f36da94 main.cpp
--- a/main.cpp	Tue Apr 19 20:16:40 2016 +0000
+++ b/main.cpp	Sat Apr 23 20:14:53 2016 +0000
@@ -9,7 +9,9 @@
 
 Serial pc(USBTX, USBRX);
 Serial esp(p13, p14); // tx, rx
-
+DigitalIn PIR(p30);
+int PIR_sensor;
+int SetMotion = 0;
 //DS18B20 thermom(A0, DS18B20::RES_12_BIT);
 
 // Standard Mbed LED definitions
@@ -148,6 +150,24 @@
                 led3 = 0;
             }
         }        
+        
+        if( SetMotion == 1)
+        {
+            PIR_sensor = PIR;
+            if(PIR_sensor == 1)
+            {
+                Outlet1 = 0;
+                led1 = 1;
+            }
+            else
+            {
+                Outlet1 = 1;
+                led1 = 0;} 
+            
+            }
+        if (SetMotion ==0)
+        {Outlet1 = 1;
+        led1 = 0;}
         if(DataRX==1) {
             ReadWebData();
             beep();
@@ -190,12 +210,12 @@
     strcat(webbuff, "<p><form method=\"POST\"><strong> Outlet 1 Current:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
     strcat(webbuff, Cur1);
     strcat(webbuff, "\"> </sup>A <form method=\"POST\"> <strong> &nbsp&nbsp");
-    if(Outlet1==1) {
+    if(SetMotion==0) {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" checked>  Outlet 1 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" >  Outlet 1 on");
+        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" >  Outlet 1 is motion controlled");
     } else {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet1\" value=\"0\" >  Outlet 1 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" checked>  Outlet 1 on");
+        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet1\" value=\"1\" checked>  Outlet 1 is motion controlled");
     }
     if(setOut2==0) {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet2\" value=\"0\" checked>  Outlet 2 off when temp >&nbsp&nbsp<input type=\"text\" name=\"tmpOff\" size=4 value=\"");
@@ -223,14 +243,14 @@
         strcat(webbuff,setlighton);
         strcat(webbuff,"\"> </sup>");
         strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" >  Outlet 3 is always OFF");
-        strcat(webbuff,"\"> </sup>");
+        strcat(webbuff,"\ </sup>");
     } else {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" >  Outlet 3 ON when LUX <&nbsp&nbsp<input type=\"text\" name=\"lighton\" size=4 value=\"");
         sprintf(setlighton, "%d", lighton);
         strcat(webbuff,setlighton);
         strcat(webbuff,"\"> </sup>");
         strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" checked>  Outlet 3 is always OFF");
-        strcat(webbuff,"\"> </sup>");
+        strcat(webbuff,"\ </sup>");
     }
     if(Outlet4==1) {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" checked>  Outlet 4 off");
@@ -325,14 +345,16 @@
         weberror=0;
         int numMatched = sscanf(webdata,"+IPD,%d,%d:%s", &linkID, &ipdLen, type);
         if( strstr(webdata, "outlet1=1") != NULL ) {
-            led1=1;
-            Outlet1=0;
-            pc.printf("outlet1 set ON\r\n");
+            //led1=1;
+            //Outlet1=0;
+            //pc.printf("outlet1 set ON\r\n");
+            SetMotion = 1;
         }
         //else pc.printf("outlet1 on not found\r\n");
         if( strstr(webdata, "outlet1=0") != NULL ) {
-            led1=0;
-            Outlet1=1;
+            //led1=0;
+            //Outlet1=1;
+            SetMotion = 0;
             pc.printf("outlet1 set OFF\r\n");
         }
         //else pc.printf("outlet1 off not found\r\n");