ethernet for mbed2

Dependencies:   IoTsecuritySys mbed-rtos mbed

Committer:
jsmith352
Date:
Tue Dec 08 23:27:29 2015 +0000
Revision:
0:009a138526c5
ethernet for mbed2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jsmith352 0:009a138526c5 1 #include "mbed.h"
jsmith352 0:009a138526c5 2 #include "rtos.h"
jsmith352 0:009a138526c5 3 #include <string>
jsmith352 0:009a138526c5 4 #include <stdlib.h>
jsmith352 0:009a138526c5 5 #include "SongPlayer.h"
jsmith352 0:009a138526c5 6 #include "Speaker.h"
jsmith352 0:009a138526c5 7 #include "EthernetInterface.h"
jsmith352 0:009a138526c5 8
jsmith352 0:009a138526c5 9 // mbed LEDs
jsmith352 0:009a138526c5 10 DigitalOut led1(LED1);
jsmith352 0:009a138526c5 11 DigitalOut led2(LED2);
jsmith352 0:009a138526c5 12 DigitalOut led3(LED3);
jsmith352 0:009a138526c5 13 DigitalOut led4(LED4);
jsmith352 0:009a138526c5 14
jsmith352 0:009a138526c5 15 // door lock (led)
jsmith352 0:009a138526c5 16 DigitalOut doorlock(p21);
jsmith352 0:009a138526c5 17
jsmith352 0:009a138526c5 18 //IR sensor
jsmith352 0:009a138526c5 19 AnalogIn IrSensor(p16);
jsmith352 0:009a138526c5 20
jsmith352 0:009a138526c5 21 //speaker
jsmith352 0:009a138526c5 22 SongPlayer mySpeaker(p22);
jsmith352 0:009a138526c5 23 Speaker NotePlayer(p22);
jsmith352 0:009a138526c5 24
jsmith352 0:009a138526c5 25 // serial PC communication
jsmith352 0:009a138526c5 26 Serial pc(USBTX, USBRX);
jsmith352 0:009a138526c5 27
jsmith352 0:009a138526c5 28 // ethernet connection
jsmith352 0:009a138526c5 29 EthernetInterface eth;
jsmith352 0:009a138526c5 30
jsmith352 0:009a138526c5 31
jsmith352 0:009a138526c5 32 // ** STATE MACHINE ** //
jsmith352 0:009a138526c5 33 volatile enum Statetype { Armed = 0, IR_sensed = 1, Second_Step = 2, Cleared = 3, Alarm_ON = 4};
jsmith352 0:009a138526c5 34 Statetype state = Armed;
jsmith352 0:009a138526c5 35 Statetype mbed0_state;
jsmith352 0:009a138526c5 36
jsmith352 0:009a138526c5 37
jsmith352 0:009a138526c5 38 // ** GLOBAL VARIABLES ** //
jsmith352 0:009a138526c5 39 volatile float IrVoltage = 0.0;
jsmith352 0:009a138526c5 40 float note[18]= {1568.0,1396.9};
jsmith352 0:009a138526c5 41 float duration[18]= {0.48,0.24};
jsmith352 0:009a138526c5 42
jsmith352 0:009a138526c5 43 Semaphore Consul_Access(5);
jsmith352 0:009a138526c5 44 Mutex mbed2_state_change;
jsmith352 0:009a138526c5 45
jsmith352 0:009a138526c5 46 //thread prototypes
jsmith352 0:009a138526c5 47 void IR_thread(void const *args);
jsmith352 0:009a138526c5 48 void Speaker_thread(void const *args);
jsmith352 0:009a138526c5 49 void Door_thread(void const *args);
jsmith352 0:009a138526c5 50 //function prototypes
jsmith352 0:009a138526c5 51 void Ethernet_update_server();
jsmith352 0:009a138526c5 52 void reset_server();
jsmith352 0:009a138526c5 53
jsmith352 0:009a138526c5 54
jsmith352 0:009a138526c5 55 int main() {
jsmith352 0:009a138526c5 56 pc.baud(9600);
jsmith352 0:009a138526c5 57 pc.printf("MBED 2 is Starting....**********************************************************************************************************************************\r\n");
jsmith352 0:009a138526c5 58 wait(3);
jsmith352 0:009a138526c5 59 //pc.printf("Getting IP....\n\rIP Address is: %s\n\r", eth.getIPAddress());
jsmith352 0:009a138526c5 60
jsmith352 0:009a138526c5 61 Thread IRthread(IR_thread);
jsmith352 0:009a138526c5 62 Thread Speakerthread(Speaker_thread);
jsmith352 0:009a138526c5 63 Thread DoorUnlocker(Door_thread);
jsmith352 0:009a138526c5 64 wait(3);
jsmith352 0:009a138526c5 65
jsmith352 0:009a138526c5 66 //ethernet thread runs in main
jsmith352 0:009a138526c5 67 char buffer[300];
jsmith352 0:009a138526c5 68 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/readstatus.php?mbedID=0 HTTP/1.0\n\n";
jsmith352 0:009a138526c5 69 int ret,found,dummy;
jsmith352 0:009a138526c5 70 eth.init(); //Use DHCP
jsmith352 0:009a138526c5 71 eth.connect();
jsmith352 0:009a138526c5 72 TCPSocketConnection sock;
jsmith352 0:009a138526c5 73
jsmith352 0:009a138526c5 74
jsmith352 0:009a138526c5 75 reset_server();
jsmith352 0:009a138526c5 76
jsmith352 0:009a138526c5 77 while(1){
jsmith352 0:009a138526c5 78 Thread::wait(1000);
jsmith352 0:009a138526c5 79 sock.connect("dreamphysix.com", 80);
jsmith352 0:009a138526c5 80 sock.send_all(http_cmd, sizeof(http_cmd)-1);
jsmith352 0:009a138526c5 81 while (true) {
jsmith352 0:009a138526c5 82 ret = sock.receive(buffer, sizeof(buffer)-1);
jsmith352 0:009a138526c5 83 if (ret <= 0)
jsmith352 0:009a138526c5 84 break;
jsmith352 0:009a138526c5 85 buffer[ret] = '\0';
jsmith352 0:009a138526c5 86 Consul_Access.wait();
jsmith352 0:009a138526c5 87 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
jsmith352 0:009a138526c5 88 Consul_Access.release();
jsmith352 0:009a138526c5 89 }
jsmith352 0:009a138526c5 90 sock.close();
jsmith352 0:009a138526c5 91 string str(buffer);
jsmith352 0:009a138526c5 92 found = str.find("Status=");
jsmith352 0:009a138526c5 93 dummy = (buffer[found+7])-48;
jsmith352 0:009a138526c5 94 mbed0_state = (Statetype)dummy;
jsmith352 0:009a138526c5 95 mbed2_state_change.lock();
jsmith352 0:009a138526c5 96 if(state != mbed0_state)
jsmith352 0:009a138526c5 97 state = mbed0_state;
jsmith352 0:009a138526c5 98 mbed2_state_change.unlock();
jsmith352 0:009a138526c5 99 pc.printf("mbed0 state: %i\n\r",mbed0_state);
jsmith352 0:009a138526c5 100 pc.printf("mbed2 state: %i\n\r",state);
jsmith352 0:009a138526c5 101 }
jsmith352 0:009a138526c5 102
jsmith352 0:009a138526c5 103 }
jsmith352 0:009a138526c5 104
jsmith352 0:009a138526c5 105 ///////////////////
jsmith352 0:009a138526c5 106 // ** THREADS ** //
jsmith352 0:009a138526c5 107 ///////////////////
jsmith352 0:009a138526c5 108
jsmith352 0:009a138526c5 109 void IR_thread(void const *args) {
jsmith352 0:009a138526c5 110
jsmith352 0:009a138526c5 111 Timer t;
jsmith352 0:009a138526c5 112 t.start();
jsmith352 0:009a138526c5 113
jsmith352 0:009a138526c5 114 while(1) {
jsmith352 0:009a138526c5 115
jsmith352 0:009a138526c5 116 if (state == Armed) {
jsmith352 0:009a138526c5 117 IrVoltage=IrSensor.read();
jsmith352 0:009a138526c5 118 if (IrVoltage <= 0.1) { //if value just nois reset timer
jsmith352 0:009a138526c5 119 t.reset();
jsmith352 0:009a138526c5 120 state = Armed;
jsmith352 0:009a138526c5 121 }
jsmith352 0:009a138526c5 122 if (t.read() >= 5) { //wait 5 seconds to make sure that sense someone
jsmith352 0:009a138526c5 123 mbed2_state_change.lock();
jsmith352 0:009a138526c5 124 state = Alarm_ON;
jsmith352 0:009a138526c5 125 Ethernet_update_server();
jsmith352 0:009a138526c5 126 mbed2_state_change.unlock();
jsmith352 0:009a138526c5 127 }
jsmith352 0:009a138526c5 128 Thread::wait(1000);
jsmith352 0:009a138526c5 129 }
jsmith352 0:009a138526c5 130 else {
jsmith352 0:009a138526c5 131 //nothing to do for this thread make space for others
jsmith352 0:009a138526c5 132 Thread::wait(1000);
jsmith352 0:009a138526c5 133 }
jsmith352 0:009a138526c5 134 }
jsmith352 0:009a138526c5 135 }
jsmith352 0:009a138526c5 136
jsmith352 0:009a138526c5 137 void Speaker_thread(void const *args) {
jsmith352 0:009a138526c5 138 while (1) {
jsmith352 0:009a138526c5 139 if (state == Alarm_ON) {
jsmith352 0:009a138526c5 140 mySpeaker.PlaySong(note,duration);
jsmith352 0:009a138526c5 141 Thread::wait(1000);
jsmith352 0:009a138526c5 142 }
jsmith352 0:009a138526c5 143 }
jsmith352 0:009a138526c5 144 }
jsmith352 0:009a138526c5 145
jsmith352 0:009a138526c5 146 void Door_thread(void const *args){
jsmith352 0:009a138526c5 147 while(1){
jsmith352 0:009a138526c5 148 if(state == Cleared){
jsmith352 0:009a138526c5 149 doorlock = 1;
jsmith352 0:009a138526c5 150 wait(5);
jsmith352 0:009a138526c5 151 doorlock = 0;
jsmith352 0:009a138526c5 152 }
jsmith352 0:009a138526c5 153 else
jsmith352 0:009a138526c5 154 Thread::wait(1000);
jsmith352 0:009a138526c5 155 }
jsmith352 0:009a138526c5 156 }
jsmith352 0:009a138526c5 157
jsmith352 0:009a138526c5 158
jsmith352 0:009a138526c5 159 ///////////////////////////////
jsmith352 0:009a138526c5 160 // ** FUNCTIONS ** //
jsmith352 0:009a138526c5 161 ///////////////////////////////
jsmith352 0:009a138526c5 162
jsmith352 0:009a138526c5 163 void Ethernet_update_server(){
jsmith352 0:009a138526c5 164 char buffer[300];
jsmith352 0:009a138526c5 165 int ret;
jsmith352 0:009a138526c5 166
jsmith352 0:009a138526c5 167 TCPSocketConnection sock;
jsmith352 0:009a138526c5 168 sock.connect("dreamphysix.com", 80);
jsmith352 0:009a138526c5 169
jsmith352 0:009a138526c5 170
jsmith352 0:009a138526c5 171 char tempStatus[2];
jsmith352 0:009a138526c5 172 snprintf(tempStatus, sizeof(tempStatus), "%i", state);
jsmith352 0:009a138526c5 173 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=2";
jsmith352 0:009a138526c5 174 strcat(http_cmd, "&status=");
jsmith352 0:009a138526c5 175 strcat(http_cmd, tempStatus);
jsmith352 0:009a138526c5 176 strcat(http_cmd, " HTTP/1.0\n\n");
jsmith352 0:009a138526c5 177 pc.printf("%s",http_cmd);
jsmith352 0:009a138526c5 178 sock.send_all(http_cmd, sizeof(http_cmd)-1);
jsmith352 0:009a138526c5 179
jsmith352 0:009a138526c5 180 while (true) {
jsmith352 0:009a138526c5 181 ret = sock.receive(buffer, sizeof(buffer)-1);
jsmith352 0:009a138526c5 182 if (ret <= 0)
jsmith352 0:009a138526c5 183 break;
jsmith352 0:009a138526c5 184 buffer[ret] = '\0';
jsmith352 0:009a138526c5 185 Consul_Access.wait();
jsmith352 0:009a138526c5 186 pc.printf("Received %d chars from server:\n%s\n", ret, buffer);
jsmith352 0:009a138526c5 187 Consul_Access.release();
jsmith352 0:009a138526c5 188 }
jsmith352 0:009a138526c5 189 sock.close();
jsmith352 0:009a138526c5 190 snprintf(buffer, ret, "%c",buffer);
jsmith352 0:009a138526c5 191 }
jsmith352 0:009a138526c5 192
jsmith352 0:009a138526c5 193 void reset_server(){
jsmith352 0:009a138526c5 194 pc.printf("I was called");
jsmith352 0:009a138526c5 195 char buffer[300];
jsmith352 0:009a138526c5 196 int ret;
jsmith352 0:009a138526c5 197 TCPSocketConnection sock;
jsmith352 0:009a138526c5 198 sock.connect("dreamphysix.com", 80);
jsmith352 0:009a138526c5 199 char http_cmd[100] = "GET http://www.dreamphysix.com/alarm/updatestatus.php?mbedID=0&status=0 HTTP/1.0\n\n";
jsmith352 0:009a138526c5 200 sock.send_all(http_cmd, sizeof(http_cmd)-1);
jsmith352 0:009a138526c5 201 while (true) {
jsmith352 0:009a138526c5 202 ret = sock.receive(buffer, sizeof(buffer)-1);
jsmith352 0:009a138526c5 203 if (ret <= 0)
jsmith352 0:009a138526c5 204 break;
jsmith352 0:009a138526c5 205 buffer[ret] = '\0';
jsmith352 0:009a138526c5 206 Consul_Access.wait();
jsmith352 0:009a138526c5 207 pc.printf("Received %d chars from server ABSDDJAJDJAJSJD:\n%s\n", ret, buffer);
jsmith352 0:009a138526c5 208 Consul_Access.release();
jsmith352 0:009a138526c5 209 }
jsmith352 0:009a138526c5 210 sock.close();
jsmith352 0:009a138526c5 211 }