ECE-4180 project: GPS based home automation
Dependencies: EthernetInterface mbed-rtos mbed oJSON
main.cpp@0:4492c0468687, 2014-04-28 (annotated)
- Committer:
- ajayke
- Date:
- Mon Apr 28 17:54:23 2014 +0000
- Revision:
- 0:4492c0468687
Code for GPS based home automation-networking mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ajayke | 0:4492c0468687 | 1 | #include "mbed.h" |
ajayke | 0:4492c0468687 | 2 | #include "EthernetInterface.h" |
ajayke | 0:4492c0468687 | 3 | #include <string> |
ajayke | 0:4492c0468687 | 4 | #include <sstream> |
ajayke | 0:4492c0468687 | 5 | #include "oJSON.h" |
ajayke | 0:4492c0468687 | 6 | #include "rtos.h" |
ajayke | 0:4492c0468687 | 7 | |
ajayke | 0:4492c0468687 | 8 | float light_thresh = 0.1; |
ajayke | 0:4492c0468687 | 9 | |
ajayke | 0:4492c0468687 | 10 | |
ajayke | 0:4492c0468687 | 11 | DigitalOut myled(LED1); |
ajayke | 0:4492c0468687 | 12 | DigitalOut cs(p8); |
ajayke | 0:4492c0468687 | 13 | DigitalOut P11(p11); // Device 1 control, obtained from database server |
ajayke | 0:4492c0468687 | 14 | DigitalOut P12(p12); // Device 2 control, obtained from database server |
ajayke | 0:4492c0468687 | 15 | DigitalOut P13(p13); // Device 3 control, obtained from database server |
ajayke | 0:4492c0468687 | 16 | DigitalOut P14(p14); // Device 4 control, obtained from database server |
ajayke | 0:4492c0468687 | 17 | DigitalOut P15(p15); // Manual / Automatic mode ( 0 / 1 ) , obtained from database server |
ajayke | 0:4492c0468687 | 18 | DigitalOut P16(p16); // Security feature ( off / on - 0 / 1 ) , obtained from database server |
ajayke | 0:4492c0468687 | 19 | DigitalOut P17(p17); // Near / Far (The user is near / far from home based on GPS location, obtained from database server, near / far - 0 / 1 ) |
ajayke | 0:4492c0468687 | 20 | DigitalIn P18(p18); // Used for data transfer synchronization between 2 mbeds |
ajayke | 0:4492c0468687 | 21 | DigitalOut P19(p19); // Used for data transfer synchronization between 2 mbeds |
ajayke | 0:4492c0468687 | 22 | DigitalOut led1(LED1); // blinks when transferring data to the other mbed |
ajayke | 0:4492c0468687 | 23 | DigitalOut led2(LED2); |
ajayke | 0:4492c0468687 | 24 | DigitalOut led3(LED3); |
ajayke | 0:4492c0468687 | 25 | DigitalOut led4(LED4); |
ajayke | 0:4492c0468687 | 26 | Mutex lock_mutex; |
ajayke | 0:4492c0468687 | 27 | extern "C" void mbed_reset(); |
ajayke | 0:4492c0468687 | 28 | int x; |
ajayke | 0:4492c0468687 | 29 | EthernetInterface eth; |
ajayke | 0:4492c0468687 | 30 | int global_var=0; |
ajayke | 0:4492c0468687 | 31 | Serial pc(USBTX, USBRX); |
ajayke | 0:4492c0468687 | 32 | void replaceAll(std::string& str, const std::string& from, const std::string& to) { |
ajayke | 0:4492c0468687 | 33 | size_t start_pos = 0; |
ajayke | 0:4492c0468687 | 34 | while((start_pos = str.find(from, start_pos)) != std::string::npos) { |
ajayke | 0:4492c0468687 | 35 | |
ajayke | 0:4492c0468687 | 36 | size_t end_pos = start_pos + from.length(); |
ajayke | 0:4492c0468687 | 37 | str.replace(start_pos, from.length(), to); |
ajayke | 0:4492c0468687 | 38 | start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx' |
ajayke | 0:4492c0468687 | 39 | } |
ajayke | 0:4492c0468687 | 40 | } |
ajayke | 0:4492c0468687 | 41 | |
ajayke | 0:4492c0468687 | 42 | // Thread for connecting to the database server, accessing the data (control signals) and then sending it to the other mbed |
ajayke | 0:4492c0468687 | 43 | void thread_server_ping(void const *args) |
ajayke | 0:4492c0468687 | 44 | { |
ajayke | 0:4492c0468687 | 45 | string lat_1; |
ajayke | 0:4492c0468687 | 46 | string lon_1; |
ajayke | 0:4492c0468687 | 47 | stringstream st; |
ajayke | 0:4492c0468687 | 48 | string ss; |
ajayke | 0:4492c0468687 | 49 | pc.printf("Connecting...\n"); |
ajayke | 0:4492c0468687 | 50 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
ajayke | 0:4492c0468687 | 51 | lock_mutex.lock(); |
ajayke | 0:4492c0468687 | 52 | TCPSocketConnection sock; |
ajayke | 0:4492c0468687 | 53 | x = sock.connect("dev.m.gatech.edu", 80); |
ajayke | 0:4492c0468687 | 54 | lock_mutex.unlock(); |
ajayke | 0:4492c0468687 | 55 | if(x!=0) |
ajayke | 0:4492c0468687 | 56 | mbed_reset(); |
ajayke | 0:4492c0468687 | 57 | pc.printf("Return Value: %d\n", x); |
ajayke | 0:4492c0468687 | 58 | pc.printf("\n\n\n Accessing Database Server \n\n"); |
ajayke | 0:4492c0468687 | 59 | char http_cmd[] = "GET /d/rsrs3/w/SilentGardian/c/api/gpsLocation/ HTTP/1.0\n\n"; |
ajayke | 0:4492c0468687 | 60 | lock_mutex.lock(); |
ajayke | 0:4492c0468687 | 61 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
ajayke | 0:4492c0468687 | 62 | lock_mutex.unlock(); |
ajayke | 0:4492c0468687 | 63 | char *buffer; |
ajayke | 0:4492c0468687 | 64 | char *buffer2; |
ajayke | 0:4492c0468687 | 65 | buffer = (char *)malloc (sizeof(char)*3000); |
ajayke | 0:4492c0468687 | 66 | buffer2 = (char *)malloc (sizeof(char)*3000); |
ajayke | 0:4492c0468687 | 67 | |
ajayke | 0:4492c0468687 | 68 | string str; |
ajayke | 0:4492c0468687 | 69 | int ret; |
ajayke | 0:4492c0468687 | 70 | pc.printf("Waiting to receive data..\n"); |
ajayke | 0:4492c0468687 | 71 | lock_mutex.lock(); |
ajayke | 0:4492c0468687 | 72 | sock.set_blocking(false,7500); |
ajayke | 0:4492c0468687 | 73 | ret = sock.receive(buffer, (sizeof(char)*3000)-1); |
ajayke | 0:4492c0468687 | 74 | lock_mutex.unlock(); |
ajayke | 0:4492c0468687 | 75 | st << buffer; |
ajayke | 0:4492c0468687 | 76 | st >> ss; |
ajayke | 0:4492c0468687 | 77 | |
ajayke | 0:4492c0468687 | 78 | |
ajayke | 0:4492c0468687 | 79 | |
ajayke | 0:4492c0468687 | 80 | if (ret <= 0) |
ajayke | 0:4492c0468687 | 81 | { |
ajayke | 0:4492c0468687 | 82 | // pc.printf("Ret <=0, break here \n"); |
ajayke | 0:4492c0468687 | 83 | pc.printf("... \n"); |
ajayke | 0:4492c0468687 | 84 | //break; |
ajayke | 0:4492c0468687 | 85 | mbed_reset(); |
ajayke | 0:4492c0468687 | 86 | terminate(); |
ajayke | 0:4492c0468687 | 87 | } |
ajayke | 0:4492c0468687 | 88 | buffer[ret] = '\0'; |
ajayke | 0:4492c0468687 | 89 | int cnt=0; |
ajayke | 0:4492c0468687 | 90 | char tmp; |
ajayke | 0:4492c0468687 | 91 | while (tmp!='[') |
ajayke | 0:4492c0468687 | 92 | { |
ajayke | 0:4492c0468687 | 93 | cnt++; |
ajayke | 0:4492c0468687 | 94 | tmp=buffer[cnt]; |
ajayke | 0:4492c0468687 | 95 | |
ajayke | 0:4492c0468687 | 96 | } |
ajayke | 0:4492c0468687 | 97 | int i=cnt; |
ajayke | 0:4492c0468687 | 98 | for(i=cnt;i<ret;i++) |
ajayke | 0:4492c0468687 | 99 | { |
ajayke | 0:4492c0468687 | 100 | buffer2[i-cnt]=buffer[i]; |
ajayke | 0:4492c0468687 | 101 | if(buffer[i]==']') |
ajayke | 0:4492c0468687 | 102 | break; |
ajayke | 0:4492c0468687 | 103 | } |
ajayke | 0:4492c0468687 | 104 | buffer2[i-cnt+1]=buffer[ret]; |
ajayke | 0:4492c0468687 | 105 | pc.printf("Received %d chars from server:\n", ret); |
ajayke | 0:4492c0468687 | 106 | str=string(buffer2); |
ajayke | 0:4492c0468687 | 107 | string str2 = ""; |
ajayke | 0:4492c0468687 | 108 | const char *cstr=str.c_str(); |
ajayke | 0:4492c0468687 | 109 | for(int i=0;i<str.size();i++) |
ajayke | 0:4492c0468687 | 110 | { |
ajayke | 0:4492c0468687 | 111 | char a=cstr[i]; |
ajayke | 0:4492c0468687 | 112 | if(a!='_') |
ajayke | 0:4492c0468687 | 113 | { |
ajayke | 0:4492c0468687 | 114 | str2= str2 + a; |
ajayke | 0:4492c0468687 | 115 | } |
ajayke | 0:4492c0468687 | 116 | } |
ajayke | 0:4492c0468687 | 117 | JSONArray *j= new JSONArray(str2); |
ajayke | 0:4492c0468687 | 118 | JSONArray j1= j->getArray(0); |
ajayke | 0:4492c0468687 | 119 | string s1=j1.getString(1); |
ajayke | 0:4492c0468687 | 120 | |
ajayke | 0:4492c0468687 | 121 | int ind=s1.find(":\"", 0); |
ajayke | 0:4492c0468687 | 122 | ind = ind + 2; |
ajayke | 0:4492c0468687 | 123 | string s11=s1.substr(ind,s1.length()); |
ajayke | 0:4492c0468687 | 124 | string s2=j1.getString(2); |
ajayke | 0:4492c0468687 | 125 | string s0=j1.getString(0); |
ajayke | 0:4492c0468687 | 126 | |
ajayke | 0:4492c0468687 | 127 | int ind2=s2.find(":\"", 0); |
ajayke | 0:4492c0468687 | 128 | ind2 = ind2 + 2; |
ajayke | 0:4492c0468687 | 129 | string s22=s2.substr(ind,s2.length()); |
ajayke | 0:4492c0468687 | 130 | int control_sig=atoi(s11.c_str()); |
ajayke | 0:4492c0468687 | 131 | int d1to4=atoi(s22.c_str()); |
ajayke | 0:4492c0468687 | 132 | pc.printf("JSON parsing : d1to d4 values:%d, \n control_sig value:%d\n",d1to4,control_sig); |
ajayke | 0:4492c0468687 | 133 | P15=1; |
ajayke | 0:4492c0468687 | 134 | P11=(int)((d1to4>>3) & 1); |
ajayke | 0:4492c0468687 | 135 | P12=(int)((d1to4>>2) & 1); |
ajayke | 0:4492c0468687 | 136 | P13=(int)((d1to4>>1) & 1); |
ajayke | 0:4492c0468687 | 137 | P14=(int)((d1to4>>0) & 1); |
ajayke | 0:4492c0468687 | 138 | P15=(int)((control_sig>>2) & 1); |
ajayke | 0:4492c0468687 | 139 | P16=(int)((control_sig>>1) & 1); |
ajayke | 0:4492c0468687 | 140 | P17=(int)((control_sig>>0) & 1); |
ajayke | 0:4492c0468687 | 141 | |
ajayke | 0:4492c0468687 | 142 | P19=0; |
ajayke | 0:4492c0468687 | 143 | int P111=(int)((d1to4>>3) & 1); // Device 1 control, obtained from database server |
ajayke | 0:4492c0468687 | 144 | int P112=(int)((d1to4>>2) & 1); // Device 2 control, obtained from database server |
ajayke | 0:4492c0468687 | 145 | int P113=(int)((d1to4>>1) & 1); // Device 3 control, obtained from database server |
ajayke | 0:4492c0468687 | 146 | int P114=(int)((d1to4>>0) & 1); // Device 4 control, obtained from database server |
ajayke | 0:4492c0468687 | 147 | int P115=(int)((control_sig>>2) & 1); // Manual / Automatic mode ( 0 / 1 ) , obtained from database server |
ajayke | 0:4492c0468687 | 148 | int P116=(int)((control_sig>>1) & 1); // Security feature ( off / on - 0 / 1 ) , obtained from database server |
ajayke | 0:4492c0468687 | 149 | int P117=(int)((control_sig>>0) & 1); // Near / Far (The user is near / far from home based on GPS location, obtained from database server, near / far - 0 / 1 ) |
ajayke | 0:4492c0468687 | 150 | |
ajayke | 0:4492c0468687 | 151 | pc.printf("P11: %d, P12: %d, P13: %d, P14:%d, P15:%d, P16:%d, P17:%d, P19: %d \n",(int)P11,(int)P12,(int)P13,(int)P14,(int)P15,(int)P16,(int)P17,(int)P19); |
ajayke | 0:4492c0468687 | 152 | |
ajayke | 0:4492c0468687 | 153 | while(P18==1) |
ajayke | 0:4492c0468687 | 154 | { |
ajayke | 0:4492c0468687 | 155 | |
ajayke | 0:4492c0468687 | 156 | } |
ajayke | 0:4492c0468687 | 157 | P19=1; |
ajayke | 0:4492c0468687 | 158 | myled = 1; |
ajayke | 0:4492c0468687 | 159 | wait(0.2); |
ajayke | 0:4492c0468687 | 160 | myled = 0; |
ajayke | 0:4492c0468687 | 161 | wait(0.2); |
ajayke | 0:4492c0468687 | 162 | sock.close(); |
ajayke | 0:4492c0468687 | 163 | |
ajayke | 0:4492c0468687 | 164 | delete buffer; |
ajayke | 0:4492c0468687 | 165 | delete buffer2; |
ajayke | 0:4492c0468687 | 166 | mbed_reset(); |
ajayke | 0:4492c0468687 | 167 | terminate(); |
ajayke | 0:4492c0468687 | 168 | while(1) { |
ajayke | 0:4492c0468687 | 169 | |
ajayke | 0:4492c0468687 | 170 | } |
ajayke | 0:4492c0468687 | 171 | } |
ajayke | 0:4492c0468687 | 172 | |
ajayke | 0:4492c0468687 | 173 | int main() { |
ajayke | 0:4492c0468687 | 174 | P19=1; |
ajayke | 0:4492c0468687 | 175 | P18.mode(PullUp); |
ajayke | 0:4492c0468687 | 176 | eth.init(); //Use DHCP |
ajayke | 0:4492c0468687 | 177 | eth.connect(); |
ajayke | 0:4492c0468687 | 178 | TCPSocketConnection sock; |
ajayke | 0:4492c0468687 | 179 | |
ajayke | 0:4492c0468687 | 180 | pc.printf("IP Address is %s\n", eth.getIPAddress()); |
ajayke | 0:4492c0468687 | 181 | lock_mutex.lock(); |
ajayke | 0:4492c0468687 | 182 | x = sock.connect("dev.m.gatech.edu", 80); // Connect to the database server |
ajayke | 0:4492c0468687 | 183 | lock_mutex.unlock(); |
ajayke | 0:4492c0468687 | 184 | Thread *ping_server=new Thread(thread_server_ping); //Start thread which fetches data from database server |
ajayke | 0:4492c0468687 | 185 | |
ajayke | 0:4492c0468687 | 186 | global_var=1; |
ajayke | 0:4492c0468687 | 187 | while(1){ |
ajayke | 0:4492c0468687 | 188 | Thread::wait(30000); |
ajayke | 0:4492c0468687 | 189 | ping_server->terminate(); |
ajayke | 0:4492c0468687 | 190 | lock_mutex.unlock(); |
ajayke | 0:4492c0468687 | 191 | pc.printf("New thread started \n"); |
ajayke | 0:4492c0468687 | 192 | ping_server=new Thread(thread_server_ping); |
ajayke | 0:4492c0468687 | 193 | } |
ajayke | 0:4492c0468687 | 194 | } |