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:
8:341d1b699cd3
Parent:
7:bdf957914029
Child:
9:69283f36da94
diff -r bdf957914029 -r 341d1b699cd3 main.cpp
--- a/main.cpp	Tue Apr 19 18:43:25 2016 +0000
+++ b/main.cpp	Tue Apr 19 20:16:40 2016 +0000
@@ -3,6 +3,9 @@
 #include "mbed.h"
 #include "DHT.h"
 //#include "DS18B20.h"
+#include "BH1750.h"
+
+BH1750 lightSensor(p28, p27);
 
 Serial pc(USBTX, USBRX);
 Serial esp(p13, p14); // tx, rx
@@ -24,6 +27,8 @@
 
 int setOut2=0; // if 0, turn off when temp >
                 // if 1, turn on when temp >
+int setOut3=0; // if 0, turn on when light <
+                // if 1, Leave light off
 
 DHT tsensor(p22, DHT11);
 
@@ -67,6 +72,8 @@
 int tmpOn=70, tmpOff=70;
 char setTmpOn[10];
 char setTmpOff[10];
+int lighton=200;
+char setlighton[20];
 // manual set RTC values
 int minute      =00;    // 0-59
 int hour        =12;    // 2-23
@@ -91,6 +98,7 @@
 
 int main()
 {
+    lightSensor.init();
     reset=0;
     pc.baud(9600);
 
@@ -130,6 +138,16 @@
                 led2 = 0;
             }
         }
+        if (setOut3 == 0) { //turn on when light <
+            if (lightSensor.readIntesity() < lighton) {
+                Outlet3 = 0;
+                led3 = 1;
+            }
+            else {
+                Outlet3 = 1;
+                led3 = 0;
+            }
+        }        
         if(DataRX==1) {
             ReadWebData();
             beep();
@@ -199,14 +217,20 @@
         strcat(webbuff,"\"> </sup>");
     }
     //strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"1\" checked>  Outlet 3  On");
-    if(Outlet3==1) {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" checked>  Outlet 3 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" >  Outlet 3 on");
-        pc.printf("outlet3 == 1\r\n");
+    if(setOut3==0) {
+        strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" checked>  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\" >  Outlet 3 is always OFF");
+        strcat(webbuff,"\"> </sup>");
     } else {
-        strcat(webbuff, "<p><input type=\"radio\" name=\"outlet3\" value=\"0\" >  Outlet 3 off");
-        strcat(webbuff, "<br><input type=\"radio\" name=\"outlet3\" value=\"1\" checked>  Outlet 3 on");
-        pc.printf("outlet3 == 0 \r\n");
+        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>");
     }
     if(Outlet4==1) {
         strcat(webbuff, "<p><input type=\"radio\" name=\"outlet4\" value=\"0\" checked>  Outlet 4 off");
@@ -337,23 +361,23 @@
         //else pc.printf("No tmpOn found\r\n");
         
         if( strstr(webdata, "outlet2=1") != NULL ) {
-            //led2=1;
+            led2=1;
             setOut2=1;
         }
         if( strstr(webdata, "outlet2=0") != NULL ) {
-            //led2=0;
+            led2=0;
             setOut2=0;
         }
         
         if( strstr(webdata, "outlet3=1") != NULL ) {
-            led3=1;
-            Outlet3=0;
+            led3=0;
+            setOut3=1;
             pc.printf("outlet3 set ON\r\n");
         }
         //else pc.printf("outlet3 on not found\r\n");
         if( strstr(webdata, "outlet3=0") != NULL ) {
             led3=0;
-            Outlet3=1;
+            setOut3=0;
             pc.printf("outlet3 set OFF\r\n");
         }
         if( strstr(webdata, "outlet4=1") != NULL ) {