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