wifi for mbed2
Dependencies: ESP8266Interface HTTPClient-SSL WebSocketClient mbed-rtos mbed
Revision 0:5d3ac3a545d0, committed 2015-12-08
- Comitter:
- jsmith352
- Date:
- Tue Dec 08 23:26:23 2015 +0000
- Commit message:
- WIFI for mbed2
Changed in this revision
diff -r 000000000000 -r 5d3ac3a545d0 ESP8266Interface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ESP8266Interface.lib Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/teams/ESP8266/code/ESP8266Interface/#1f4dd0e91837
diff -r 000000000000 -r 5d3ac3a545d0 HTTPClient-SSL.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPClient-SSL.lib Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/sarahmarshy/code/HTTPClient-SSL/#a18a06b000f3
diff -r 000000000000 -r 5d3ac3a545d0 SongPlayer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Tue Dec 08 23:26:23 2015 +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 000000000000 -r 5d3ac3a545d0 Speaker.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,23 @@ +#include "mbed.h" +// new class to play a note on Speaker based on PwmOut class +#ifndef _SPEAKER_H +#define _SPEAKER_H +class Speaker +{ +public: + Speaker(PinName pin) : _pin(pin) { +// _pin(pin) means pass pin to the Speaker Constructor + } +// class method to play a note based on PwmOut class + void PlayNote(float frequency, float duration, float volume) { + _pin.period(1.0/frequency); + _pin = volume/2.0; + wait(duration); + _pin = 0.0; + } + +private: + PwmOut _pin; +}; + +#endif // _SPEAKER_H \ No newline at end of file
diff -r 000000000000 -r 5d3ac3a545d0 WebSocketClient.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WebSocketClient.lib Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 000000000000 -r 5d3ac3a545d0 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,214 @@ +#include "mbed.h" +//#include "HTTPClient.h" +#include "ESP8266Interface.h" +#include "TCPSocketConnection.h" +//#include "Websocket.h" +#include <string> +#include <stdlib.h> +#include "SongPlayer.h" +#include "Speaker.h" +#include "rtos.h" + +// mbed LEDs +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); + +// door lock (led) +DigitalOut doorlock(p21); + +//IR sensor +AnalogIn IrSensor(p16); + +//speaker +SongPlayer mySpeaker(p22); +Speaker NotePlayer(p22); + +// serial PC communication +Serial pc(USBTX, USBRX); + +// wifi connection +ESP8266Interface wifi(p28,p27,p26,"GTother","GeorgeP@1927",115200); // TX,RX,Reset,SSID,Password,Baud + + +// ** STATE MACHINE ** // +volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4}; +Statetype state = Armed; +Statetype mbed0_state; + + +// ** GLOBAL VARIABLES ** // +volatile float IrVoltage = 0.0; +float note[18]= {1568.0,1396.9}; +float duration[18]= {0.48,0.24}; + +Semaphore Consul_Access(5); +Mutex mbed2_state_change; + +//thread prototypes +void IR_thread(void const *args); +void Speaker_thread(void const *args); +void Door_thread(void const *args); +//function prototypes +void WIFI_update_server(); +void reset_server(); + +int main() { + pc.baud(9600); + pc.printf("MBED 2 is Starting....\r\n"); + + Thread IRthread(IR_thread); + Thread Speakerthread(Speaker_thread); + Thread DoorUnlocker(Door_thread); + wait(3); + + //WIFI thread runs in main + char buffer[300]; + char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n"; + int ret,found,dummy; + wifi.init(); //Reset + wifi.connect(); //Use DHCP + pc.printf("Getting IP....\n\rIP Address is: %s\n\r", wifi.getIPAddress()); + wait(4); + pc.printf("try to connect to TCP\r\n"); + TCPSocketConnection sock; + wait(2); + + reset_server(); + + while(1){ + Thread::wait(1000); + sock.connect("dreamphysix.com", 80); + pc.printf("working so far..."); + sock.send_all(http_cmd, sizeof(http_cmd)-1); + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + if (ret <= 0) + break; + buffer[ret] = '\0'; + Consul_Access.wait(); + pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + Consul_Access.release(); + } + sock.close(); + string str(buffer); + found = str.find("Status="); + dummy = (buffer[found+7])-48; + mbed0_state = (Statetype)dummy; + mbed2_state_change.lock(); + if(state != mbed0_state) + state = mbed0_state; + mbed2_state_change.unlock(); + pc.printf("mbed0 state: %i\n\r",mbed0_state); + pc.printf("mbed2 state: %i\n\r",state); + } +} + +/////////////////// +// ** THREADS ** // +/////////////////// + +void IR_thread(void const *args) { + + Timer t; + t.start(); + + while(1) { + + if (state == Armed) { + IrVoltage=IrSensor.read(); + if (IrVoltage <= 0.1) { //if value just nois reset timer + t.reset(); + state = Armed; + } + if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone + mbed2_state_change.lock(); + state = Alarm_ON; + WIFI_update_server(); + mbed2_state_change.unlock(); + } + Thread::wait(1000); + } + else { + //nothing to do for this thread make space for others + Thread::wait(1000); + } + } +} + +void Speaker_thread(void const *args) { + while (1) { + if (state == Alarm_ON) { + mySpeaker.PlaySong(note,duration); + Thread::wait(1000); + } + } +} + +void Door_thread(void const *args){ + while(1){ + if(state == Cleared){ + doorlock = 1; + wait(5); + doorlock = 0; + } + else + Thread::wait(1000); + } +} + + +/////////////////////////////// +// ** FUNCTIONS ** // +/////////////////////////////// + +void WIFI_update_server(){ + char buffer[300]; + int ret; + + TCPSocketConnection sock; + sock.connect("dreamphysix.com", 80); + + + char tempStatus[2]; + snprintf(tempStatus, sizeof(tempStatus), "%i", state); + char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=2"; + strcat(http_cmd, "&status="); + strcat(http_cmd, tempStatus); + strcat(http_cmd, " HTTP/1.0\n\n"); + pc.printf("%s",http_cmd); + sock.send_all(http_cmd, sizeof(http_cmd)-1); + + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + if (ret <= 0) + break; + buffer[ret] = '\0'; + Consul_Access.wait(); + pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + Consul_Access.release(); + } + sock.close(); + snprintf(buffer, ret, "%c",buffer); +} + +void reset_server(){ + pc.printf("I was called"); + char buffer[300]; + int ret; + TCPSocketConnection sock; + sock.connect("dreamphysix.com", 80); + char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=0&status=0 HTTP/1.0\n\n"; + sock.send_all(http_cmd, sizeof(http_cmd)-1); + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + if (ret <= 0) + break; + buffer[ret] = '\0'; + Consul_Access.wait(); + pc.printf("Received %d chars from server ABSDDJAJDJAJSJD:\n%s\n", ret, buffer); + Consul_Access.release(); + } + sock.close(); +} \ No newline at end of file
diff -r 000000000000 -r 5d3ac3a545d0 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed-rtos/#c825593ece39
diff -r 000000000000 -r 5d3ac3a545d0 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Dec 08 23:26:23 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b \ No newline at end of file