Charles Austin / Mbed 2 deprecated gps

Dependencies:   mbed PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 
00004 //hardware declarations
00005 Serial pc(USBTX, USBRX);
00006 Serial esp(p13, p14);
00007 Serial gps(p28, p27);
00008 BusOut status(LED4, LED3, LED2, LED1);
00009 PinDetect interrupt(p21);
00010 
00011 //global variables
00012 unsigned long tripid;
00013 int button_signal;
00014 int on_trip;
00015 
00016 //golbals for Serial communication
00017 char snd[1500];
00018 char buf[1024];
00019 Timer t;
00020 int timeout = 1;
00021 int ended, count2;
00022 
00023 //functions
00024 void button_press(void);
00025 void start_trip(void);
00026 void setup(void);
00027 void get_reply(void);
00028 
00029 void button_press(void) {
00030     button_signal = 1;   
00031 }
00032 
00033 void start_trip(void) {
00034     if (on_trip) {
00035         sprintf(snd, "http.get(\"http://ece4180.sousys.com/home/endtrip/%d/\", nil, function(code, data) print(data) end)\r\n", tripid);
00036         on_trip = 0;
00037     } else {
00038         ++tripid;
00039         strcpy(snd, "http.get(\"http://ece4180.sousys.com/home/startnewtrip/100/\", nil, function(code, data) print(data) end)\r\n");
00040         on_trip = 1;
00041     }    
00042     esp.printf("%s", snd);
00043     timeout = 1;
00044     get_reply();
00045     char* response = strchr(buf + 1, '>') + 2; //get pointer to data after 2nd '>' char
00046     tripid = atoi(response);                   //format is '>', then ' ', then response
00047     pc.printf("%lu", tripid);                 
00048     button_signal = 0;
00049     printf("on_trip: %d\r\n", on_trip);
00050 }
00051 
00052 void setup(void) {
00053     status = 14;
00054     timeout = 1;
00055     strcpy(snd, "node.restart()\r\n");
00056     esp.printf("%s", snd);
00057     get_reply();
00058     wait(.1);
00059     strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
00060     esp.printf("%s", snd);
00061     timeout=2;
00062     get_reply();
00063     wait(.1);
00064     strcpy(snd, "station_cfg={}\r\n"); 
00065     esp.printf("%s", snd);
00066     get_reply();
00067     wait(.1);   
00068     sprintf(snd, "station_cfg.ssid=\"%s\"\r\n", "iPhone");
00069     esp.printf("%s", snd);
00070     get_reply();
00071     wait(.1);
00072     sprintf(snd, "station_cfg.pwd=\"%s\"\r\n", "ece4180wifi"); 
00073     timeout = 3;
00074     esp.printf("%s", snd);
00075     get_reply();
00076     wait(.1);   
00077     sprintf(snd, "wifi.sta.config(station_cfg)\r\n");   
00078     esp.printf("%s", snd);
00079     get_reply();
00080     wait(.1);
00081     strcpy(snd, "wifi.sta.connect()\r\n");
00082     esp.printf("%s", snd);
00083     timeout = 5;
00084     get_reply();
00085     wait(2); 
00086     
00087     strcpy(snd, "print(wifi.sta.getip())\r\n");
00088     esp.printf("%s", snd);
00089     timeout=5;
00090     get_reply();
00091     wait(.1); 
00092     
00093     strcpy(snd, "print(wifi.sta.status())\r\n");
00094     esp.printf("%s", snd);
00095     get_reply();
00096     wait(.1);
00097 }
00098 
00099 void get_reply()
00100 {
00101     memset(buf, '\0', sizeof(buf));
00102     t.start();
00103     ended=0;
00104     count2=0;
00105     while(!ended) {
00106         if(esp.readable()) {
00107             buf[count2++] = esp.getc();
00108         }
00109         if(t.read() > timeout) {
00110             ended = 1;
00111             t.stop();
00112             t.reset();
00113         }
00114     }
00115     pc.printf("%s", buf);
00116 }
00117 
00118 int main() {
00119     pc.baud(115200);
00120     esp.baud(115200);
00121     //setup();
00122     
00123     interrupt.mode(PullUp);
00124     wait(.1);
00125     interrupt.attach_deasserted(&button_press);
00126     interrupt.setSampleFrequency();
00127     char* response;
00128     do {
00129         status = 4;
00130         strcpy(snd, "http.get(\"http://checkip.amazonaws.com\", nil, function(code, data) print(data) end)\r\n");
00131         esp.printf("%s", snd);
00132         timeout = 1;
00133         get_reply();
00134         wait(.1);
00135         response = strchr(buf + 1, '>') + 2;        //get pointer to data after 2nd '>' char
00136         pc.printf("%s", response);                  //format is '>', then ' ', then response
00137     } while (!(response[0] >= '0' && response[0] <= '9'));
00138     while (!gps.readable()) {status = 12;}
00139     pc.printf("ready\r\n");
00140     status = 15;
00141     char cDataBuffer[500];
00142     char c;
00143     on_trip = 0;
00144     
00145     while(1) {
00146         if (button_signal) {
00147             start_trip();
00148         }
00149         if(on_trip && gps.readable()) {
00150             if(gps.getc() == '$'); {
00151                 for(int i=0; i<sizeof(cDataBuffer); i++) {
00152                     c = gps.getc();
00153                     if( c == '\r' ) {
00154                         char ns, ew, status;
00155                         int date;
00156                         float latitude, longitude, timefix, speed;
00157                         
00158                         // Geographic position, Latitude and Longitude
00159                         if(strncmp(cDataBuffer,"$GPRMC", 6) == 0) {
00160                             sscanf(cDataBuffer, "$GPRMC,%f,%c,%f,%c,%f,%c,%f,,%d", &timefix, &status, &latitude, &ns, &longitude, &ew, &speed, &date);
00161                             //convert to decimal degrees
00162                             int latdeg = ((int) latitude) / 100;
00163                             float latmin = latitude - (latdeg * 100);
00164                             latitude = latdeg + (latmin / 60);
00165                             int londeg = ((int) longitude) / 100;
00166                             float lonmin = longitude - (londeg * 100);
00167                             longitude = londeg + (lonmin / 60);
00168                             pc.printf("tripid: %lu\r\nlatitude: %f\r\nlongitude: %f\r\n", tripid, latitude, longitude);
00169                             if (latitude > 1 && longitude > 1) {
00170                                 status = 10;
00171                                 if (ns == 'S') latitude *= -1;
00172                                 if (ew == 'W') longitude *= -1;    
00173                                 sprintf(snd, "http.get(\"http://ece4180.sousys.com/realtime/uploadposition/%d/%f/%f\", nil, function(code, data) print(data) end)\r\n", tripid, latitude, longitude);
00174                                 pc.printf("%s", snd);
00175                                 esp.printf("%s", snd);
00176                                 timeout = 1;
00177                                 get_reply();
00178                             } else {
00179                                 status = 7;   
00180                             }
00181                             wait(.5);
00182                         }
00183                         i = sizeof(cDataBuffer);
00184                     } else {
00185                         cDataBuffer[i] = c;
00186                     }                 
00187                 }
00188             }
00189         }
00190     }
00191 }