whatever
Dependencies: C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed
Fork of PONY_Ph0-uAXIS by
Diff: main.cpp
- Revision:
- 38:d86e744166b6
- Parent:
- 37:71ab32b61dfb
- Child:
- 39:c14aff678b25
--- a/main.cpp Wed Dec 02 01:57:56 2015 +0000 +++ b/main.cpp Wed Dec 09 00:55:23 2015 +0000 @@ -14,89 +14,13 @@ mount resistors SB13/14 1k, SB62/63 0R */ - +// C027 Support Libraries #include "GPS.h" #include "MDM.h" - -//------------------------------------------------------------------------------------ -// Cellular modem/SIM parameters -#define SIMPIN "1111" //!SIMPIN is 1111 by default for AT&T SIMs. -#define APN "m2m.com.attz" // Defined AT&T M2M APN -#define USERNAME NULL //! Set the user name for your APN, or NULL if not needed (which, apparently, it isn't) -#define PASSWORD NULL //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't) -//------------------------------------------------------------------------------------ -// AT&T M2X Kekys - -#define M2XAPIKEY "295d76119ddac541c4e8c51bc3a45497" -#define UBLOX1DEVID "8066dc4be6828638af1a62521f61f6e3" - -//------------------------------------------------------------------------------------ - -//#define CELLOCATE - -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -//------------------------------------------------------------------------------------ -int main(void) -{ - - printf("\r\n\n\n\n-----\r\nI'm alive!\r\n\n"); - - int ret; - char buf[2048] = ""; - - // Create the GPS object - GPSI2C gps; - +#include <jsonlite.h> +#include "M2XStreamClient.h" - //----- MODEM UP!!! - printf("Waiting 15s for boot or what-not\r\n"); - wait(15); - - printf("Open modem object mdm\r\n"); - - MDMSerial mdm; - mdm.setDebug(4); // enable this for debugging issues - - - // Create status objects - MDMParser::DevStatus devStatus = {}; - MDMParser::NetStatus netStatus = {}; - - bool mdmOk = mdm.init(SIMPIN, &devStatus); - - // How we doing so far? - printf("\r\n-- mdm.init status dump\r\n"); - mdm.dumpDevStatus(&devStatus); - - if (mdmOk) { - // wait until we are connected (SGM: Not sure what this means, since this isn't a loop... - mdmOk = mdm.registerNet(&netStatus); - mdm.dumpNetStatus(&netStatus); - - } - - printf("-- mdm.registerNet status dump\r\n"); - - // Open a data connecction - if(mdmOk) { - - MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); - printf("-- mdm.join dump\r\n"); - - - // If we secure an IP address, keep going! - - if (ip != NOIP) { - - printf("\n\n\rWe got's an IP address!\r\n"); - mdm.dumpIp(ip); - - } - } - - /* @@ -106,218 +30,115 @@ - // Create the modem object - MDMSerial mdm; - mdm.setDebug(4); // enable this for debugging issues +//------------------------------------------------------------------------------------ +// Cellular modem/SIM parameters +#define SIMPIN "1111" //!SIMPIN is 1111 by default for AT&T SIMs. +#define APN "m2m.com.attz" // Defined AT&T M2M APN +#define USERNAME NULL //! Set the user name for your APN, or NULL if not needed (which, apparently, it isn't) +#define PASSWORD NULL //! Set the password for your APN, or NULL if not needed (which, apparently, it isn't) + + + +//------------------------------------------------------------------------------------ +// AT&T M2X Kekys + +#define M2XAPIKEY "295d76119ddac541c4e8c51bc3a45497" +#define DEVID "8066dc4be6828638af1a62521f61f6e3" + +//------------------------------------------------------------------------------------ + +#include "PONY_Loc.h" // PONY Location Code + + + + +// GLOBALS + + + +Client client; +M2XStreamClient m2xClient(&client, M2XAPIKEY); + + + +unsigned int kReadingDelay = 30000; + +double kLaLoDiffMin = 0.01; +double kAltDiffMin = 2.00; + + +//#define CELLOCATE + +//------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------ +//------------------------------------------------------------------------------------ +int main(void) +{ + + printf("\r\n\n\n\n-----\r\nI'm alive - u8!\r\n\n"); - // initialize the modem - MDMParser::DevStatus devStatus = {}; - MDMParser::NetStatus netStatus = {}; - bool mdmOk = mdm.init(SIMPIN, &devStatus); - mdm.dumpDevStatus(&devStatus); + int ret = 0; + int len = 0; + char buf[2048] = ""; + + // Create the GPS object + GPSI2C gpsInst; + + // Struct for our GPS sentence info + struct GPSbundle GPSone; + GPSone.bGPS = gpsInst; + GPSone.bRet = ret; + GPSone.bLen = len; + GPSone.bBuf = &buf[0]; + GPSone.bBufLen = sizeof(buf); + + // Struct for item location + //double timestamp = 0, lat = 0, lon = 0, alt = 0, vel = 0; + struct GPSloc PONYspot; + MDMSerial mdm; + + // Open modem connection + if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) + return -1; + + //Timer tmr; + //tmr.reset(); + //tmr.start(); + + + // Ready the GPS + readyGPS(&GPSone, 5); + + + + while(true) { + + // Get coordinates + if ( fetchLocBundle(&GPSone, &PONYspot, 30) == 7 ) + printf("Filled bundle!\r\n"); + + printf("Lat=%f, Long=%f, Elev=%f, Time=%f\r\n",PONYspot.lat, PONYspot.lon, PONYspot.alt, PONYspot.tmstmp); + + //tmr.reset(); + //tmr.start(); + int response; + + response = m2xClient.updateLocation(DEVID, "Igor-spot", PONYspot.lat, PONYspot.lon, PONYspot.alt); + + if(response == 202) + printf("POST success\r\n"); + + + delay(kReadingDelay); + } + + mdm.disconnect(); + mdm.powerOff(); - if (mdmOk) { - - // wait until we are connected - mdmOk = mdm.registerNet(&netStatus); - mdm.dumpNetStatus(&netStatus); - } + + - if (mdmOk) - { - // http://www.geckobeach.com/cellular/secrets/gsmcodes.php - // http://de.wikipedia.org/wiki/USSD-Codes - const char* ussd = "*130#"; // You may get answer "UNKNOWN APPLICATION" - printf("Ussd Send Command %s\r\n", ussd); - ret = mdm.ussdCommand(ussd, buf); - if (ret > 0) - printf("Ussd Got Answer: \"%*s\"\r\n", ret, buf); - - // join the internet connection - MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); - if (ip != NOIP) - { - mdm.dumpIp(ip); - printf("Make a Http Post Request\r\n"); - int socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - if (mdm.socketConnect(socket, "mbed.org", 80)) - { - const char http[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; - mdm.socketSend(socket, http, sizeof(http)-1); - - ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); - if (ret > 0) - printf("Socket Recv \"%*s\"\r\n", ret, buf); - mdm.socketClose(socket); - } - mdm.socketFree(socket); - } - - int port = 7; - const char* host = "echo.u-blox.com"; - MDMParser::IP ip = mdm.gethostbyname(host); - char data[] = "\r\nxxx Socket Hello World\r\n" -#ifdef LARGE_DATA - "00 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "01 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "02 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "03 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "04 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "05 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "06 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "07 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "08 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "09 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "10 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "11 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "12 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "13 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "14 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - - "15 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "16 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "17 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "18 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" - "19 0123456789 0123456789 0123456789 0123456789 0123456789 \r\n" -#endif - "End\r\n"; - - printf("Testing TCP sockets with ECHO server\r\n"); - socket = mdm.socketSocket(MDMParser::IPPROTO_TCP); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - if (mdm.socketConnect(socket, host, port)) { - memcpy(data, "\r\nTCP", 5); - ret = mdm.socketSend(socket, data, sizeof(data)-1); - if (ret == sizeof(data)-1) { - printf("Socket Send %d \"%s\"\r\n", ret, data); - } - ret = mdm.socketRecv(socket, buf, sizeof(buf)-1); - if (ret >= 0) { - printf("Socket Recv %d \"%.*s\"\r\n", ret, ret, buf); - } - mdm.socketClose(socket); - } - mdm.socketFree(socket); - } - - printf("Testing UDP sockets with ECHO server\r\n"); - socket = mdm.socketSocket(MDMParser::IPPROTO_UDP, port); - if (socket >= 0) - { - mdm.socketSetBlocking(socket, 10000); - memcpy(data, "\r\nUDP", 5); - ret = mdm.socketSendTo(socket, ip, port, data, sizeof(data)-1); - if (ret == sizeof(data)-1) { - printf("Socket SendTo %s:%d " IPSTR " %d \"%s\"\r\n", host, port, IPNUM(ip), ret, data); - } - ret = mdm.socketRecvFrom(socket, &ip, &port, buf, sizeof(buf)-1); - if (ret >= 0) { - printf("Socket RecvFrom " IPSTR ":%d %d \"%.*s\" \r\n", IPNUM(ip),port, ret, ret,buf); - } - mdm.socketFree(socket); - } - - // disconnect - mdm.disconnect(); - } - } - printf("SMS and GPS Loop\r\n"); - char link[128] = ""; - unsigned int i = 0xFFFFFFFF; - const int wait = 100; - bool abort = false; -#ifdef CELLOCATE - const int sensorMask = 3; // Hybrid: GNSS + CellLocate - const int timeoutMargin = 5; // seconds - const int submitPeriod = 60; // 1 minutes in seconds - const int targetAccuracy = 1; // meters - unsigned int j = submitPeriod * 1000/wait; - bool cellLocWait = false; - MDMParser::CellLocData loc; - //Token can be released from u-blox site, when you got one replace "TOKEN" below - if (!mdm.cellLocSrvHttp("TOKEN")) - mdm.cellLocSrvUdp(); - mdm.cellLocConfigSensor(1); // Deep scan mode - //mdm.cellUnsolIndication(1); -#endif - //DigitalOut led(LED1); - while (!abort) { - // led = !led; -#ifndef CELLOCATE - while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) - { - int len = LENGTH(ret); - //printf("NMEA: %.*s\r\n", len-2, msg); - if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) - { - - // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS - if ((buf[0] == '$') || buf[1] == 'G') { - #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) - if (_CHECK_TALKER("GLL")) { - double la = 0, lo = 0; - char ch; - - - if (gps.getNmeaAngle(1,buf,len,la) && - gps.getNmeaAngle(3,buf,len,lo) && - gps.getNmeaItem(6,buf,len,ch) && ch == 'A') - { - printf("GPS Location: %.5f %.5f\r\n", la, lo); - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", la, lo); - } - } else if (_CHECK_TALKER("GGA") || _CHECK_TALKER("GNS") ) { - double a = 0; - if (gps.getNmeaItem(9,buf,len,a)) // altitude msl [m] - printf("GPS Altitude: %.1f\r\n", a); - } else if (_CHECK_TALKER("VTG")) { - double s = 0; - if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] - printf("GPS Speed: %.1f\r\n", s); - } - } - } - } -#endif - - -#ifdef CELLOCATE - if (mdmOk && (j++ == submitPeriod * 1000/wait)) { - j=0; - printf("CellLocate Request\r\n"); - mdm.cellLocRequest(sensorMask, submitPeriod-timeoutMargin, targetAccuracy); - cellLocWait = true; - } - if (cellLocWait && mdm.cellLocGet(&loc)){ - cellLocWait = false; - printf("CellLocate position received, sensor_used: %d, \r\n", loc.sensorUsed ); - printf(" latitude: %0.5f, longitude: %0.5f, altitute: %d\r\n", loc.latitue, loc.longitude, loc.altitutude); - if (loc.sensorUsed == 1) - printf(" uncertainty: %d, speed: %d, direction: %d, vertical_acc: %d, satellite used: %d \r\n", loc.uncertainty,loc.speed,loc.direction,loc.verticalAcc,loc.svUsed); - if (loc.sensorUsed == 1 || loc.sensorUsed == 2) - sprintf(link, "I am here!\n" - "https://maps.google.com/?q=%.5f,%.5f", loc.latitue, loc.longitude); - } - if (cellLocWait && (j%100 == 0 )) - printf("Waiting for CellLocate...\r\n"); -#endif - - - } - gps.powerOff(); - mdm.powerOff(); - return 0; - - */ }