ethernet for mbed2
Dependencies: IoTsecuritySys mbed-rtos mbed
Revision 0:009a138526c5, committed 2015-12-08
- Comitter:
- jsmith352
- Date:
- Tue Dec 08 23:27:29 2015 +0000
- Commit message:
- ethernet for mbed2
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Tue Dec 08 23:27:29 2015 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/landes/code/IoTsecuritySys/#6e7256debbf6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SongPlayer.h Tue Dec 08 23:27:29 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Speaker.h Tue Dec 08 23:27:29 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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Dec 08 23:27:29 2015 +0000 @@ -0,0 +1,211 @@ +#include "mbed.h" +#include "rtos.h" +#include <string> +#include <stdlib.h> +#include "SongPlayer.h" +#include "Speaker.h" +#include "EthernetInterface.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); + +// ethernet connection +EthernetInterface eth; + + +// ** 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 Ethernet_update_server(); +void reset_server(); + + +int main() { + pc.baud(9600); + pc.printf("MBED 2 is Starting....**********************************************************************************************************************************\r\n"); + wait(3); + //pc.printf("Getting IP....\n\rIP Address is: %s\n\r", eth.getIPAddress()); + + Thread IRthread(IR_thread); + Thread Speakerthread(Speaker_thread); + Thread DoorUnlocker(Door_thread); + wait(3); + + //ethernet 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; + eth.init(); //Use DHCP + eth.connect(); + TCPSocketConnection sock; + + + reset_server(); + + while(1){ + Thread::wait(1000); + sock.connect("dreamphysix.com", 80); + 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; + Ethernet_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 Ethernet_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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Tue Dec 08 23:27:29 2015 +0000 @@ -0,0 +1,1 @@ +https://mbed.org/users/mbed_official/code/mbed-rtos/#c825593ece39
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Dec 08 23:27:29 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/165afa46840b \ No newline at end of file