Dependencies: C12832 FreescaleIAP MMA8451Q mbed
main.cpp@0:526edf64eda3, 2018-04-09 (annotated)
- Committer:
- vishnu001
- Date:
- Mon Apr 09 05:33:19 2018 +0000
- Revision:
- 0:526edf64eda3
final accelerometer data to thingspeak using esp8266
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vishnu001 | 0:526edf64eda3 | 1 | #pragma once |
vishnu001 | 0:526edf64eda3 | 2 | #include "mbed.h" |
vishnu001 | 0:526edf64eda3 | 3 | //https://developer.mbed.org/users/4180_1/notebook/using-the-esp8266-with-the-mbed-lpc1768/ |
vishnu001 | 0:526edf64eda3 | 4 | //modified to run on KL25Z using serial port at PTA2,PTA1 (tx,rx) and PTD4 as RST pin |
vishnu001 | 0:526edf64eda3 | 5 | #include "MMA8451Q.h" |
vishnu001 | 0:526edf64eda3 | 6 | #include "C12832.h" |
vishnu001 | 0:526edf64eda3 | 7 | #include "FreescaleIAP.h" |
vishnu001 | 0:526edf64eda3 | 8 | |
vishnu001 | 0:526edf64eda3 | 9 | PinName const SDA = PTE25; |
vishnu001 | 0:526edf64eda3 | 10 | PinName const SCL = PTE24; |
vishnu001 | 0:526edf64eda3 | 11 | |
vishnu001 | 0:526edf64eda3 | 12 | #define IP "api.thingspeak.com" |
vishnu001 | 0:526edf64eda3 | 13 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
vishnu001 | 0:526edf64eda3 | 14 | char cDataBuffer[100]; |
vishnu001 | 0:526edf64eda3 | 15 | double latitude, longitude; |
vishnu001 | 0:526edf64eda3 | 16 | double lat_ch,long_ch; |
vishnu001 | 0:526edf64eda3 | 17 | //int i = 0; |
vishnu001 | 0:526edf64eda3 | 18 | |
vishnu001 | 0:526edf64eda3 | 19 | |
vishnu001 | 0:526edf64eda3 | 20 | void Init(); |
vishnu001 | 0:526edf64eda3 | 21 | void parse(char *cmd, int n); |
vishnu001 | 0:526edf64eda3 | 22 | void check(); |
vishnu001 | 0:526edf64eda3 | 23 | |
vishnu001 | 0:526edf64eda3 | 24 | Serial pc(USBTX,USBRX); |
vishnu001 | 0:526edf64eda3 | 25 | Serial gps(PTD3, PTD2);//tx,rx |
vishnu001 | 0:526edf64eda3 | 26 | Serial esp(PTC4,PTC3); // tx, rx |
vishnu001 | 0:526edf64eda3 | 27 | DigitalOut reset(PTD4); |
vishnu001 | 0:526edf64eda3 | 28 | DigitalOut alert(PTD5); |
vishnu001 | 0:526edf64eda3 | 29 | //C12832 lcd(D11, D13, D12, D7, D10); |
vishnu001 | 0:526edf64eda3 | 30 | |
vishnu001 | 0:526edf64eda3 | 31 | |
vishnu001 | 0:526edf64eda3 | 32 | Timer timer1; |
vishnu001 | 0:526edf64eda3 | 33 | //Timer time; |
vishnu001 | 0:526edf64eda3 | 34 | |
vishnu001 | 0:526edf64eda3 | 35 | int count,ended,timeout; |
vishnu001 | 0:526edf64eda3 | 36 | char buf='0'; |
vishnu001 | 0:526edf64eda3 | 37 | char snd[255]; |
vishnu001 | 0:526edf64eda3 | 38 | |
vishnu001 | 0:526edf64eda3 | 39 | char ssid[32] = "praveena"; // enter WiFi router ssid inside the quotes |
vishnu001 | 0:526edf64eda3 | 40 | char pwd [32] = "12345678"; // enter WiFi router password inside the quotes |
vishnu001 | 0:526edf64eda3 | 41 | int t; |
vishnu001 | 0:526edf64eda3 | 42 | void record(void); |
vishnu001 | 0:526edf64eda3 | 43 | //void check(void); |
vishnu001 | 0:526edf64eda3 | 44 | |
vishnu001 | 0:526edf64eda3 | 45 | void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(); |
vishnu001 | 0:526edf64eda3 | 46 | int main(void) { |
vishnu001 | 0:526edf64eda3 | 47 | int flag=0; |
vishnu001 | 0:526edf64eda3 | 48 | pc.baud(115200); |
vishnu001 | 0:526edf64eda3 | 49 | esp.baud(115200); // change this to the new ESP8266 baudrate if it is changed at any time. |
vishnu001 | 0:526edf64eda3 | 50 | gps.baud(9600); |
vishnu001 | 0:526edf64eda3 | 51 | int over = 1; |
vishnu001 | 0:526edf64eda3 | 52 | char c; |
vishnu001 | 0:526edf64eda3 | 53 | MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS); |
vishnu001 | 0:526edf64eda3 | 54 | //lcd.cls(); |
vishnu001 | 0:526edf64eda3 | 55 | timer1.start(); |
vishnu001 | 0:526edf64eda3 | 56 | reset=0; //hardware reset for 8266 |
vishnu001 | 0:526edf64eda3 | 57 | // set what you want here depending on your terminal program speed |
vishnu001 | 0:526edf64eda3 | 58 | pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); |
vishnu001 | 0:526edf64eda3 | 59 | wait(0.5); |
vishnu001 | 0:526edf64eda3 | 60 | reset=1; |
vishnu001 | 0:526edf64eda3 | 61 | timeout=2; |
vishnu001 | 0:526edf64eda3 | 62 | getreply(); |
vishnu001 | 0:526edf64eda3 | 63 | |
vishnu001 | 0:526edf64eda3 | 64 | |
vishnu001 | 0:526edf64eda3 | 65 | |
vishnu001 | 0:526edf64eda3 | 66 | //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** |
vishnu001 | 0:526edf64eda3 | 67 | |
vishnu001 | 0:526edf64eda3 | 68 | ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** |
vishnu001 | 0:526edf64eda3 | 69 | |
vishnu001 | 0:526edf64eda3 | 70 | |
vishnu001 | 0:526edf64eda3 | 71 | |
vishnu001 | 0:526edf64eda3 | 72 | |
vishnu001 | 0:526edf64eda3 | 73 | while (true) |
vishnu001 | 0:526edf64eda3 | 74 | { |
vishnu001 | 0:526edf64eda3 | 75 | over = 1; |
vishnu001 | 0:526edf64eda3 | 76 | //lcd.locate(0,3); |
vishnu001 | 0:526edf64eda3 | 77 | while(over){ |
vishnu001 | 0:526edf64eda3 | 78 | if(gps.readable()) |
vishnu001 | 0:526edf64eda3 | 79 | { |
vishnu001 | 0:526edf64eda3 | 80 | pc.printf("hi\n\r"); |
vishnu001 | 0:526edf64eda3 | 81 | if(gps.getc() == '$'); // wait a $ |
vishnu001 | 0:526edf64eda3 | 82 | { |
vishnu001 | 0:526edf64eda3 | 83 | for(int i=0; i<sizeof(cDataBuffer); i++) |
vishnu001 | 0:526edf64eda3 | 84 | { |
vishnu001 | 0:526edf64eda3 | 85 | c = gps.getc(); |
vishnu001 | 0:526edf64eda3 | 86 | if( c == '\r' ) |
vishnu001 | 0:526edf64eda3 | 87 | { |
vishnu001 | 0:526edf64eda3 | 88 | pc.printf("hi\n\r"); |
vishnu001 | 0:526edf64eda3 | 89 | parse(cDataBuffer, i); |
vishnu001 | 0:526edf64eda3 | 90 | i = sizeof(cDataBuffer); |
vishnu001 | 0:526edf64eda3 | 91 | over = 0; |
vishnu001 | 0:526edf64eda3 | 92 | } |
vishnu001 | 0:526edf64eda3 | 93 | else |
vishnu001 | 0:526edf64eda3 | 94 | { |
vishnu001 | 0:526edf64eda3 | 95 | cDataBuffer[i] = c; |
vishnu001 | 0:526edf64eda3 | 96 | } |
vishnu001 | 0:526edf64eda3 | 97 | } |
vishnu001 | 0:526edf64eda3 | 98 | } |
vishnu001 | 0:526edf64eda3 | 99 | } |
vishnu001 | 0:526edf64eda3 | 100 | } |
vishnu001 | 0:526edf64eda3 | 101 | pc.printf("GPS: latitude: %.6f :: longitude: %.6f\n\r", latitude/100.0 , longitude/100.0); |
vishnu001 | 0:526edf64eda3 | 102 | check(); |
vishnu001 | 0:526edf64eda3 | 103 | float z; |
vishnu001 | 0:526edf64eda3 | 104 | |
vishnu001 | 0:526edf64eda3 | 105 | z = (acc.getAccZ())*10; |
vishnu001 | 0:526edf64eda3 | 106 | wait(0.1); |
vishnu001 | 0:526edf64eda3 | 107 | //check(); |
vishnu001 | 0:526edf64eda3 | 108 | if(z<=8.0 || z>= 10.5) |
vishnu001 | 0:526edf64eda3 | 109 | { flag++; |
vishnu001 | 0:526edf64eda3 | 110 | if(flag == 1){ |
vishnu001 | 0:526edf64eda3 | 111 | t= timer1.read(); |
vishnu001 | 0:526edf64eda3 | 112 | } |
vishnu001 | 0:526edf64eda3 | 113 | if( (timer1.read() - t) >= 2.5 && (timer1.read() - t) < 4.5 && flag>2){ |
vishnu001 | 0:526edf64eda3 | 114 | //lcd.locate(0,15); |
vishnu001 | 0:526edf64eda3 | 115 | //int p=t; |
vishnu001 | 0:526edf64eda3 | 116 | pc.printf("bump detected at latitude: %.6f :: longitude: %.6f\n\r", latitude/100.0 , longitude/100.0); |
vishnu001 | 0:526edf64eda3 | 117 | lat_ch=latitude; |
vishnu001 | 0:526edf64eda3 | 118 | long_ch=longitude; |
vishnu001 | 0:526edf64eda3 | 119 | flag =0; |
vishnu001 | 0:526edf64eda3 | 120 | wait(5); |
vishnu001 | 0:526edf64eda3 | 121 | record(); |
vishnu001 | 0:526edf64eda3 | 122 | //lcd.cls(); |
vishnu001 | 0:526edf64eda3 | 123 | } |
vishnu001 | 0:526edf64eda3 | 124 | else if( (timer1.read() - t) >= 5.0 ){ |
vishnu001 | 0:526edf64eda3 | 125 | flag =0; |
vishnu001 | 0:526edf64eda3 | 126 | } |
vishnu001 | 0:526edf64eda3 | 127 | } |
vishnu001 | 0:526edf64eda3 | 128 | |
vishnu001 | 0:526edf64eda3 | 129 | //lcd.locate(0,15); |
vishnu001 | 0:526edf64eda3 | 130 | pc.printf("good to go\n\r"); |
vishnu001 | 0:526edf64eda3 | 131 | //wait(0.5); |
vishnu001 | 0:526edf64eda3 | 132 | |
vishnu001 | 0:526edf64eda3 | 133 | } |
vishnu001 | 0:526edf64eda3 | 134 | } |
vishnu001 | 0:526edf64eda3 | 135 | void record() |
vishnu001 | 0:526edf64eda3 | 136 | { |
vishnu001 | 0:526edf64eda3 | 137 | |
vishnu001 | 0:526edf64eda3 | 138 | |
vishnu001 | 0:526edf64eda3 | 139 | |
vishnu001 | 0:526edf64eda3 | 140 | // continuosly get AP list and IP |
vishnu001 | 0:526edf64eda3 | 141 | pc.printf("\n---------- Listing Acces Points ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 142 | |
vishnu001 | 0:526edf64eda3 | 143 | |
vishnu001 | 0:526edf64eda3 | 144 | strcpy(snd,"AT+CIPMODE=0\r\n");//Setting WiFi into MultiChannel mode |
vishnu001 | 0:526edf64eda3 | 145 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 146 | getreply(); |
vishnu001 | 0:526edf64eda3 | 147 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 148 | wait(0.5); |
vishnu001 | 0:526edf64eda3 | 149 | /* |
vishnu001 | 0:526edf64eda3 | 150 | strcpy(snd,"AT+CIPMUX=1");//Setting WiFi into MultiChannel mode |
vishnu001 | 0:526edf64eda3 | 151 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 152 | getreply(); |
vishnu001 | 0:526edf64eda3 | 153 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 154 | wait(0.5); |
vishnu001 | 0:526edf64eda3 | 155 | */ |
vishnu001 | 0:526edf64eda3 | 156 | //pc.printf("%s\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 157 | strcpy(snd,"AT+CIPSTART=4,\"TCP\",\"api.thingspeak.com\",80\r\n"); |
vishnu001 | 0:526edf64eda3 | 158 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 159 | timeout=5; |
vishnu001 | 0:526edf64eda3 | 160 | getreply(); |
vishnu001 | 0:526edf64eda3 | 161 | wait(1); |
vishnu001 | 0:526edf64eda3 | 162 | strcpy(snd,"AT+CIPSEND=4,68\r\n"); |
vishnu001 | 0:526edf64eda3 | 163 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 164 | timeout=1; |
vishnu001 | 0:526edf64eda3 | 165 | getreply(); |
vishnu001 | 0:526edf64eda3 | 166 | wait(1); |
vishnu001 | 0:526edf64eda3 | 167 | sprintf(snd,"GET /update?key=DLTFSJQRE1WQTXF3&field1=%.6f&field2=%.6f\r\n",latitude/100.0,longitude/100.0); |
vishnu001 | 0:526edf64eda3 | 168 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 169 | timeout=1; |
vishnu001 | 0:526edf64eda3 | 170 | getreply(); |
vishnu001 | 0:526edf64eda3 | 171 | wait(1); |
vishnu001 | 0:526edf64eda3 | 172 | pc.printf("length = %d\n\r",strlen(snd)); |
vishnu001 | 0:526edf64eda3 | 173 | getreply(); |
vishnu001 | 0:526edf64eda3 | 174 | pc.printf(" buf = ##start %s ..##end\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 175 | strcpy(snd,"AT+CIPCLOSE=4\r\n"); |
vishnu001 | 0:526edf64eda3 | 176 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 177 | timeout=1; |
vishnu001 | 0:526edf64eda3 | 178 | getreply(); |
vishnu001 | 0:526edf64eda3 | 179 | wait(1); |
vishnu001 | 0:526edf64eda3 | 180 | //wait(1); |
vishnu001 | 0:526edf64eda3 | 181 | |
vishnu001 | 0:526edf64eda3 | 182 | } |
vishnu001 | 0:526edf64eda3 | 183 | |
vishnu001 | 0:526edf64eda3 | 184 | // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed |
vishnu001 | 0:526edf64eda3 | 185 | void ESPsetbaudrate() |
vishnu001 | 0:526edf64eda3 | 186 | { |
vishnu001 | 0:526edf64eda3 | 187 | strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate |
vishnu001 | 0:526edf64eda3 | 188 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 189 | } |
vishnu001 | 0:526edf64eda3 | 190 | |
vishnu001 | 0:526edf64eda3 | 191 | // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ |
vishnu001 | 0:526edf64eda3 | 192 | void ESPconfig() |
vishnu001 | 0:526edf64eda3 | 193 | { |
vishnu001 | 0:526edf64eda3 | 194 | wait(5); |
vishnu001 | 0:526edf64eda3 | 195 | strcpy(snd,"AT\r\n"); |
vishnu001 | 0:526edf64eda3 | 196 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 197 | |
vishnu001 | 0:526edf64eda3 | 198 | getreply(); |
vishnu001 | 0:526edf64eda3 | 199 | wait(1); |
vishnu001 | 0:526edf64eda3 | 200 | pc.printf("%s\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 201 | strcpy(snd,"AT\r\n"); |
vishnu001 | 0:526edf64eda3 | 202 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 203 | getreply(); |
vishnu001 | 0:526edf64eda3 | 204 | wait(1); |
vishnu001 | 0:526edf64eda3 | 205 | pc.printf("%s\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 206 | strcpy(snd,"AT\r\n"); |
vishnu001 | 0:526edf64eda3 | 207 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 208 | timeout=1; |
vishnu001 | 0:526edf64eda3 | 209 | getreply(); |
vishnu001 | 0:526edf64eda3 | 210 | wait(1); |
vishnu001 | 0:526edf64eda3 | 211 | pc.printf("%s\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 212 | pc.printf("\f---------- Starting ESP Config ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 213 | |
vishnu001 | 0:526edf64eda3 | 214 | pc.printf("---------- Reset & get Firmware ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 215 | strcpy(snd,"AT+RST\r\n"); |
vishnu001 | 0:526edf64eda3 | 216 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 217 | timeout=5; |
vishnu001 | 0:526edf64eda3 | 218 | getreply(); |
vishnu001 | 0:526edf64eda3 | 219 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 220 | |
vishnu001 | 0:526edf64eda3 | 221 | wait(2); |
vishnu001 | 0:526edf64eda3 | 222 | |
vishnu001 | 0:526edf64eda3 | 223 | pc.printf("\n---------- Get Version ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 224 | strcpy(snd,"AT+GMR\r\n"); |
vishnu001 | 0:526edf64eda3 | 225 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 226 | timeout=4; |
vishnu001 | 0:526edf64eda3 | 227 | getreply(); |
vishnu001 | 0:526edf64eda3 | 228 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 229 | |
vishnu001 | 0:526edf64eda3 | 230 | wait(3); |
vishnu001 | 0:526edf64eda3 | 231 | |
vishnu001 | 0:526edf64eda3 | 232 | // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) |
vishnu001 | 0:526edf64eda3 | 233 | pc.printf("\n---------- Setting Mode ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 234 | strcpy(snd, "AT+CWMODE=1\r\n"); |
vishnu001 | 0:526edf64eda3 | 235 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 236 | timeout=4; |
vishnu001 | 0:526edf64eda3 | 237 | getreply(); |
vishnu001 | 0:526edf64eda3 | 238 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 239 | |
vishnu001 | 0:526edf64eda3 | 240 | wait(2); |
vishnu001 | 0:526edf64eda3 | 241 | |
vishnu001 | 0:526edf64eda3 | 242 | // set CIPMUX to 0=Single,1=Multi |
vishnu001 | 0:526edf64eda3 | 243 | pc.printf("\n---------- Setting Connection Mode ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 244 | strcpy(snd, "AT+CIPMUX=1\r\n"); |
vishnu001 | 0:526edf64eda3 | 245 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 246 | timeout=4; |
vishnu001 | 0:526edf64eda3 | 247 | getreply(); |
vishnu001 | 0:526edf64eda3 | 248 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 249 | |
vishnu001 | 0:526edf64eda3 | 250 | wait(2); |
vishnu001 | 0:526edf64eda3 | 251 | /* |
vishnu001 | 0:526edf64eda3 | 252 | pc.printf("\n---------- Listing Access Points ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 253 | strcpy(snd, "AT+CWLAP\r\n"); |
vishnu001 | 0:526edf64eda3 | 254 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 255 | timeout=15; |
vishnu001 | 0:526edf64eda3 | 256 | getreply(); |
vishnu001 | 0:526edf64eda3 | 257 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 258 | |
vishnu001 | 0:526edf64eda3 | 259 | wait(2); |
vishnu001 | 0:526edf64eda3 | 260 | */ |
vishnu001 | 0:526edf64eda3 | 261 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 262 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
vishnu001 | 0:526edf64eda3 | 263 | strcpy(snd, "AT+CWJAP=\""); |
vishnu001 | 0:526edf64eda3 | 264 | strcat(snd, ssid); |
vishnu001 | 0:526edf64eda3 | 265 | strcat(snd, "\",\""); |
vishnu001 | 0:526edf64eda3 | 266 | strcat(snd, pwd); |
vishnu001 | 0:526edf64eda3 | 267 | strcat(snd, "\"\r\n"); |
vishnu001 | 0:526edf64eda3 | 268 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 269 | timeout=10; |
vishnu001 | 0:526edf64eda3 | 270 | getreply(); |
vishnu001 | 0:526edf64eda3 | 271 | pc.printf(" buf = ##start %s ..##end\n",buf); |
vishnu001 | 0:526edf64eda3 | 272 | |
vishnu001 | 0:526edf64eda3 | 273 | wait(5); |
vishnu001 | 0:526edf64eda3 | 274 | |
vishnu001 | 0:526edf64eda3 | 275 | |
vishnu001 | 0:526edf64eda3 | 276 | pc.printf("\n---------- Get IP's ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 277 | strcpy(snd, "AT+CIFSR\r\n"); |
vishnu001 | 0:526edf64eda3 | 278 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 279 | timeout=3; |
vishnu001 | 0:526edf64eda3 | 280 | getreply(); |
vishnu001 | 0:526edf64eda3 | 281 | pc.printf(" buf = ##start %s ..##end\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 282 | |
vishnu001 | 0:526edf64eda3 | 283 | wait(1); |
vishnu001 | 0:526edf64eda3 | 284 | |
vishnu001 | 0:526edf64eda3 | 285 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
vishnu001 | 0:526edf64eda3 | 286 | strcpy(snd, "AT+CIPSTATUS\r\n"); |
vishnu001 | 0:526edf64eda3 | 287 | SendCMD(); |
vishnu001 | 0:526edf64eda3 | 288 | timeout=2; |
vishnu001 | 0:526edf64eda3 | 289 | getreply(); |
vishnu001 | 0:526edf64eda3 | 290 | pc.printf(" buf = ##start %s ..##end\n\r",buf); |
vishnu001 | 0:526edf64eda3 | 291 | /* |
vishnu001 | 0:526edf64eda3 | 292 | |
vishnu001 | 0:526edf64eda3 | 293 | pc.printf("\n\n\n If you get a valid (non zero) IP, ESP8266 has been set up.\r\n"); |
vishnu001 | 0:526edf64eda3 | 294 | pc.printf(" Run this if you want to reconfig the ESP8266 at any time.\r\n"); |
vishnu001 | 0:526edf64eda3 | 295 | pc.printf(" It saves the SSID and password settings internally\r\n"); |
vishnu001 | 0:526edf64eda3 | 296 | wait(10);*/ |
vishnu001 | 0:526edf64eda3 | 297 | } |
vishnu001 | 0:526edf64eda3 | 298 | |
vishnu001 | 0:526edf64eda3 | 299 | void SendCMD() |
vishnu001 | 0:526edf64eda3 | 300 | { |
vishnu001 | 0:526edf64eda3 | 301 | esp.printf("%s", snd); |
vishnu001 | 0:526edf64eda3 | 302 | } |
vishnu001 | 0:526edf64eda3 | 303 | |
vishnu001 | 0:526edf64eda3 | 304 | void getreply() |
vishnu001 | 0:526edf64eda3 | 305 | { |
vishnu001 | 0:526edf64eda3 | 306 | //memset(buf, '\0', sizeof(buf)); // this zeros the array |
vishnu001 | 0:526edf64eda3 | 307 | timer1.stop(); |
vishnu001 | 0:526edf64eda3 | 308 | timer1.reset(); |
vishnu001 | 0:526edf64eda3 | 309 | timer1.start(); // start a timer |
vishnu001 | 0:526edf64eda3 | 310 | ended=0; |
vishnu001 | 0:526edf64eda3 | 311 | //count=0; |
vishnu001 | 0:526edf64eda3 | 312 | while(!ended) { |
vishnu001 | 0:526edf64eda3 | 313 | //pc.putc('c'); |
vishnu001 | 0:526edf64eda3 | 314 | while(esp.readable()) { |
vishnu001 | 0:526edf64eda3 | 315 | buf= esp.getc(); |
vishnu001 | 0:526edf64eda3 | 316 | pc.putc(buf); |
vishnu001 | 0:526edf64eda3 | 317 | //count++; |
vishnu001 | 0:526edf64eda3 | 318 | } |
vishnu001 | 0:526edf64eda3 | 319 | if(timer1.read() > timeout) { |
vishnu001 | 0:526edf64eda3 | 320 | //pc.putc('$'); |
vishnu001 | 0:526edf64eda3 | 321 | ended = 1; |
vishnu001 | 0:526edf64eda3 | 322 | //t.stop(); |
vishnu001 | 0:526edf64eda3 | 323 | // t.reset(); |
vishnu001 | 0:526edf64eda3 | 324 | } |
vishnu001 | 0:526edf64eda3 | 325 | } |
vishnu001 | 0:526edf64eda3 | 326 | } |
vishnu001 | 0:526edf64eda3 | 327 | |
vishnu001 | 0:526edf64eda3 | 328 | inline void parse(char *cmd, int n) |
vishnu001 | 0:526edf64eda3 | 329 | { |
vishnu001 | 0:526edf64eda3 | 330 | |
vishnu001 | 0:526edf64eda3 | 331 | char ns, ew, tf, status; |
vishnu001 | 0:526edf64eda3 | 332 | int fq, nst, fix, date; // fix quality, Number of satellites being tracked, 3D fix |
vishnu001 | 0:526edf64eda3 | 333 | float timefix, speed, altitude; |
vishnu001 | 0:526edf64eda3 | 334 | |
vishnu001 | 0:526edf64eda3 | 335 | |
vishnu001 | 0:526edf64eda3 | 336 | // Global Positioning System Fix Data |
vishnu001 | 0:526edf64eda3 | 337 | if(strncmp(cmd,"$GPGGA", 6) == 0) |
vishnu001 | 0:526edf64eda3 | 338 | { |
vishnu001 | 0:526edf64eda3 | 339 | sscanf(cmd, "$GPGGA,%f,%lf,%c,%lf,%c,%d,%d,%*f,%f", &timefix, &latitude, &ns, &longitude, &ew, &fq, &nst, &altitude); |
vishnu001 | 0:526edf64eda3 | 340 | //pc.printf("GPGGA Fix taken at: %f, Latitude: %f %c, Longitude: %f %c, Fix quality: %d, Number of sat: %d, Altitude: %f M\n", timefix, latitude, ns, longitude, ew, fq, nst, altitude); |
vishnu001 | 0:526edf64eda3 | 341 | } |
vishnu001 | 0:526edf64eda3 | 342 | |
vishnu001 | 0:526edf64eda3 | 343 | // Satellite status |
vishnu001 | 0:526edf64eda3 | 344 | if(strncmp(cmd,"$GPGSA", 6) == 0) |
vishnu001 | 0:526edf64eda3 | 345 | { |
vishnu001 | 0:526edf64eda3 | 346 | sscanf(cmd, "$GPGSA,%c,%d,%d", &tf, &fix, &nst); |
vishnu001 | 0:526edf64eda3 | 347 | //pc.printf("GPGSA Type fix: %c, 3D fix: %d, number of sat: %d\r\n", tf, fix, nst); |
vishnu001 | 0:526edf64eda3 | 348 | } |
vishnu001 | 0:526edf64eda3 | 349 | |
vishnu001 | 0:526edf64eda3 | 350 | // Geographic position, Latitude and Longitude |
vishnu001 | 0:526edf64eda3 | 351 | if(strncmp(cmd,"$GPGLL", 6) == 0) |
vishnu001 | 0:526edf64eda3 | 352 | { |
vishnu001 | 0:526edf64eda3 | 353 | sscanf(cmd, "$GPGLL,%lf,%c,%lf,%c,%f", &latitude, &ns, &longitude, &ew, &timefix); |
vishnu001 | 0:526edf64eda3 | 354 | //pc.printf("GPGLL Latitude: %f %c, Longitude: %f %c, Fix taken at: %f\n\r", latitude/100.0, ns, longitude/100.0, ew, timefix); |
vishnu001 | 0:526edf64eda3 | 355 | } |
vishnu001 | 0:526edf64eda3 | 356 | |
vishnu001 | 0:526edf64eda3 | 357 | // Geographic position, Latitude and Longitude |
vishnu001 | 0:526edf64eda3 | 358 | if(strncmp(cmd,"$GPRMC", 6) == 0) |
vishnu001 | 0:526edf64eda3 | 359 | { |
vishnu001 | 0:526edf64eda3 | 360 | sscanf(cmd, "$GPRMC,%f,%c,%lf,%c,%lf,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date); |
vishnu001 | 0:526edf64eda3 | 361 | //pc.printf("GPRMC Fix taken at: %f, Status: %c, Latitude: %f %c, Longitude: %f %c, Speed: %f, Date: %d\n", timefix, status, latitude, ns, longitude, ew, speed, date); |
vishnu001 | 0:526edf64eda3 | 362 | } |
vishnu001 | 0:526edf64eda3 | 363 | } |
vishnu001 | 0:526edf64eda3 | 364 | void check(){ |
vishnu001 | 0:526edf64eda3 | 365 | if(latitude!=0){ |
vishnu001 | 0:526edf64eda3 | 366 | if(abs(latitude*1000 - lat_ch*1000) <= 20 && abs(longitude*1000 - long_ch*1000) <=20 ){ |
vishnu001 | 0:526edf64eda3 | 367 | alert = 1; |
vishnu001 | 0:526edf64eda3 | 368 | wait(2); |
vishnu001 | 0:526edf64eda3 | 369 | alert = 0; |
vishnu001 | 0:526edf64eda3 | 370 | lat_ch = 0; |
vishnu001 | 0:526edf64eda3 | 371 | long_ch = 0; |
vishnu001 | 0:526edf64eda3 | 372 | } |
vishnu001 | 0:526edf64eda3 | 373 | } |
vishnu001 | 0:526edf64eda3 | 374 | } |