whatever

Dependencies:   C027 C027_Support M2XStreamClient PowerControl jsonlite mbed-rtos mbed

Fork of PONY_Ph0-uAXIS by Sean McBeath

main.cpp

Committer:
sgmcb
Date:
2015-12-15
Revision:
40:82b70b92169e
Parent:
39:c14aff678b25
Child:
41:f603d76dc6fe

File content as of revision 40:82b70b92169e:

#include "mbed.h"

//------------------------------------------------------------------------------------
/* This example was tested on C027-U20 and C027-G35 with the on board modem. 
   
   Additionally it was tested with a shield where the SARA-G350/U260/U270 RX/TX/PWRON 
   is connected to D0/D1/D4 and the GPS SCL/SDA is connected D15/D15. In this 
   configuration the following platforms were tested (it is likely that others 
   will work as well)
   - U-BLOX:    C027-G35, C027-U20, C027-C20 (for shield set define C027_FORCE_SHIELD)
   - NXP:       LPC1549v2, LPC4088qsb
   - Freescale: FRDM-KL05Z, FRDM-KL25Z, FRDM-KL46Z, FRDM-K64F
   - STM:       NUCLEO-F401RE, NUCLEO-F030R8
                mount resistors SB13/14 1k, SB62/63 0R
*/

// C027 Support Libraries
#include "GPS.h"
#include "MDM.h"

#include <jsonlite.h>
#include "M2XStreamClient.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 "54c6bbf11012f34830bd23cc091ca250"
#define DEVID "b9c57667ee0495dc0b3cddd890f8d2df"

//------------------------------------------------------------------------------------

#include "PONY_Loc.h"       // PONY Location Code
#include "H3LIS331DL.h"     // Accelerometer library




// PIN Config
DigitalIn jostle(P2_13);






// GLOBALS



Client client;
M2XStreamClient m2xClient(&client, M2XAPIKEY);
int M2X_response;   // For m2x message responses
I2C axis(P0_0, P0_1);

char statusBuf[145] = "";


unsigned int kReadingDelay = 3 * 60 * 1000;         // How many seconds to wait between reads
unsigned int kReadingDelayClimb = 15000;    // How many seconds to add to the wait period when sitting idle
unsigned int kReadingDelayMax = 5 * 60000;  // What's the maximum time between readings?


double kLaLoDiffMin = 0.01;
double kAltDiffMin  = 2.00;

unsigned int kIdleSpins = 0;





void parseM2XLocation2(const char* name, double latitude, double longitude, double elevation, const char* timestamp, int index, void* context) {
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\nparseM2XLocation2\r\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}

//#define CELLOCATE

//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
int main(void)
{
    
    printf("\r\n\n\n\n-----\r\nI'm alive - u9\r\n\n");
    
    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, PONYlast;
    MDMSerial mdm;
    MDMParser::DevStatus devStatus = {};
    MDMParser::NetStatus netStatus = {};    
    bool mdmOk = mdm.init(SIMPIN, &devStatus);
    //mdm.setDebug(4);
    
    
    
    // Acceleromter object
    char ax[2];
    
    //axis.write(0x3A, 
    //axis.read(0x3A, 
    
    //moveData kegMotion;
    
    
    // Open modem connection
    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD))
        return -1;    
    
    
        
    // Ready the GPS
    readyGPS(&GPSone, 5);
    
    
    
    
    moveData kegMotion;
    
    status_t axisStatus;
    uint8_t axisID;


    // Cell location call variables
    const int sensorMask = 2;  // 1 = GNSS, 2 = CellLocate, 3 = Hybrid: GNSS + CellLocate       
    const int timeout = 55; // in seconds
    const int targetAccuracy = 1; // meters

    // Cell location data
    MDMParser::CellLocData ponyLoc, thisLoc;
    

    printf("Configure deep scan mode\r\n");
    int locConf = mdm.cellLocConfigSensor(1);   // Set deep scan mode
    
    // Loop driving variables
    bool locLock;
    int cellLocDelay = 2 * 1000;
    int readLoopMax = 10;
    int loopIter = 0;
    int locAccLower = 50;   // Immediately accept locations with uncertainty lower than this
    int locAccUpper = 5000; // Don't accept locations with uncertainty greater than this
    
    
    
    // Location reporting loop
    while(true) {
        printf("\r\n");
        
        locLock = false;
        loopIter++;
        
        //printf("Cell location Request; deep scan=%i, mask=%i, accuracy=%i\r\n", locConf, sensorMask, targetAccuracy);
        mdm.cellLocRequest(sensorMask, timeout, targetAccuracy);
        
        // Have location data
        if ( mdm.cellLocGet(&thisLoc) ){           
            printf("CellLocate position rec'd with uncertainty= %d\r\n", thisLoc.uncertainty );           
            
            
            if(thisLoc.uncertainty < ponyLoc.uncertainty) {
                printf("  NEW-latitude: %0.5f, longitude: %0.5f, altitute: %d, uncertainty: %d\r\n", thisLoc.latitude, thisLoc.longitude, thisLoc.altitude, thisLoc.uncertainty);
                ponyLoc.latitude = thisLoc.latitude;
                ponyLoc.longitude = thisLoc.longitude;
                ponyLoc.altitude = thisLoc.altitude;
                ponyLoc.uncertainty = thisLoc.uncertainty;
            }
            
            
            // Check location data
            if( ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccLower ) || ( 0 < ponyLoc.uncertainty && ponyLoc.uncertainty < locAccUpper && loopIter > readLoopMax ) ) {
                
                printf("\r\nTRANSMIT LOCATION DATA:\r\nla=%0.5f, lo=%0.5f, alt=%d\r\n", ponyLoc.latitude, ponyLoc.longitude, ponyLoc.altitude);
                M2X_response = m2xClient.updateLocation(DEVID, "pony-spot", (double) ponyLoc.latitude, (double) ponyLoc.longitude, (double) ponyLoc.altitude);
                
                if(M2X_response == 202)
                    printf("Location POST successful\r\n");
                
                
                // Report uncertainty
                M2X_response = m2xClient.updateStreamValue(DEVID, "locacc", ponyLoc.uncertainty);
                
                if(M2X_response == 202)
                    printf("Location Confidence (%d) POST successful\r\n", ponyLoc.uncertainty);
                
                locLock = true;
            }             
        }
        
        // No location data
        else {
            printf("cellLocGet failed.\r\n");
            //
        }

        // Wait only if we've received a good location reading
        if(locLock) {
            printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
            ponyLoc.uncertainty = locAccUpper + 1;
            continue;
        }
        else if (loopIter > readLoopMax) {
            sprintf(statusBuf, "CellLoc location not found after %i loops.\r\n", loopIter);
            printf(statusBuf);
            M2X_response = m2xClient.updateStreamValue(DEVID, "status", statusBuf);
            if(M2X_response == 202)
                printf("Status POST successful\r\n");            
            
            ponyLoc.uncertainty = locAccUpper + 1;
            printf("Begin %i second delay\r\n",kReadingDelay/1000); loopIter = 0; delay(kReadingDelay);
            continue;
        }
        else {
            delay(cellLocDelay);    // Delay between CellLoc requests when not getting good data
        }
    }
    
    
    
    
    /*
    
            if( true ) { //(abs(PONYspot.lat - PONYlast.lat) > kLaLoDiffMin) || (abs(PONYspot.lon - PONYlast.lon) > kLaLoDiffMin) || (abs(PONYspot.alt - PONYlast.alt) > kAltDiffMin) ) {
                
                //printf("New location found!\r\n");
                M2X_response = m2xClient.updateLocation(DEVID, "Igor-spot", PONYspot.lat, PONYspot.lon, PONYspot.alt);
                

                    
                PONYlast = PONYspot;
            
                kIdleSpins = 0;
            }
        }
        else {
            
            
            
        }
        */


    //mdm.disconnect();
    //mdm.powerOff();
    
}