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
Revision 15:5a337e99f6f3, committed 2016-04-30
- Comitter:
- tpettet3
- Date:
- Sat Apr 30 22:37:39 2016 +0000
- Parent:
- 14:85d3a42699af
- Commit message:
- Final/Demo code version
Changed in this revision
SongPlayer.h | Show annotated file Show diff for this revision Revisions of this file |
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 85d3a42699af -r 5a337e99f6f3 SongPlayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Sat Apr 30 22:37:39 2016 +0000 @@ -0,0 +1,41 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +class SongPlayer +{ +public: + SongPlayer(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the constructor + } +// class method to play a note based on PwmOut class + void PlaySong(float frequency[], float duration[], float volume=1.0) { + vol = volume; + notecount = 0; + _pin.period(1.0/frequency[notecount]); + _pin = volume/2.0; + noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); + // setup timer to interrupt for next note to play + frequencyptr = frequency; + durationptr = duration; + //returns after first note starts to play + } + void nextnote(); +private: + Timeout noteduration; + PwmOut _pin; + int notecount; + float vol; + float * frequencyptr; + float * durationptr; +}; +//Interrupt Routine to play next note +void SongPlayer::nextnote() +{ + _pin = 0.0; + notecount++; //setup next note in song + if (durationptr[notecount]!=0.0) { + _pin.period(1.0/frequencyptr[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); + _pin = vol/2.0; + } else + _pin = 0.0; //turn off on last note +} \ No newline at end of file
diff -r 85d3a42699af -r 5a337e99f6f3 main.cpp --- 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;