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
Diff: main.cpp
- Revision:
- 15:5a337e99f6f3
- Parent:
- 14:85d3a42699af
--- a/main.cpp Tue Apr 26 02:19:52 2016 +0000 +++ b/main.cpp Sat Apr 30 22:37:39 2016 +0000 @@ -1,34 +1,37 @@ -// ESP8266 Static page WEB server to control Mbed - #include "mbed.h" #include "DHT.h" -//#include "DS18B20.h" #include "BH1750.h" +#include "SongPlayer.h" +SongPlayer mySpeaker(p21); +float note[1] = {1500}; +float duration[1] = {0.25}; BH1750 lightSensor(p28, p27); 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 -DigitalOut led1(LED1); // (PTB18) -DigitalOut led2(LED2); // (PTB19) -DigitalOut led3(LED3); // (PTD1) +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); DigitalOut led4(LED4); -// Digital Out and In pins, can be configured to any suitable pin depending on Platform +//Setting up the control pins for the outlets DigitalOut Outlet1(p18); DigitalOut Outlet2(p17); DigitalOut Outlet3(p20); DigitalOut Outlet4(p19); DigitalOut reset(p29); -int setOut2=0; // if 0, turn off when temp > - // if 1, turn on when temp > +int setOut2=0; //Variable used to set the outlet based on temperature, only ever 0 or 1 + int setOut3=0; // if 0, turn on when light < // if 1, Leave light off @@ -37,9 +40,6 @@ AnalogIn I1(p15); AnalogIn Ain1(p16); -//PwmOut speaker(p21); -//AnalogIn Ain1(p18); -//AnalogIn Ain2(p19); Timer t1; Timer t2; @@ -107,8 +107,8 @@ pc.printf("\f\n\r------------ ESP8266 Hardware Reset --------------\n\r"); wait(0.5); reset=1; - Outlet1=1, Outlet2=1, Outlet3=1, Outlet4=1; - led1=0,led2=0,led3=1,led4=1; + Outlet1=1, Outlet2=1, Outlet3=1, Outlet4=1; // initialize all outlets to 1 + led1=0,led2=0,led3=1,led4=1; // set leds to mirror outlets timeout=6000; getcount=500; getreply(); @@ -124,8 +124,10 @@ while(I1 < 0.2) { led1=1;led2=1;led3=1;led4=1; + mySpeaker.PlaySong(note,duration); wait(0.25); led1=0;led2=0;led3=0;led4=0; + mySpeaker.PlaySong(note,duration); wait(0.25); } if (setOut2 == 0){ // turn off when tmp > @@ -465,8 +467,8 @@ { gettemp(); gettime(); - pc.printf("\n\n RTC time %s\r\n\n",timebuf); - pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); + //pc.printf("\n\n RTC time %s\r\n\n",timebuf); + //pc.printf("++++++++++ Resetting ESP ++++++++++\r\n"); strcpy(cmdbuff,"AT+RST\r\n"); timeout=8000; getcount=1000;