Official reference client implementation for Cumulocity SmartREST on u-blox C027.

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Vincent Wochnik

main.cpp

Committer:
vwochnik
Date:
2014-12-06
Revision:
68:0dc778a16d0d
Parent:
67:c360a2b2c948
Child:
71:063c45e99578

File content as of revision 68:0dc778a16d0d:

#include "mbed.h"
#include "rtos.h"
#include "MDM.h"
#include "GPS.h"
#include "DeviceInfo.h"
#include "DeviceMemory.h"
#include "MbedAgent.h"
#include "GPSTracker.h"

#include "DeviceConfiguration.h"

#include <stdio.h>

/**
 * SIM PIN. Null for no pin.
 */
#define SIM_PIN NULL

/**
 * SIM GPRS login data. Leave commented out for automatic setting.
 */
//#define SIM_APN ""
//#define SIM_USER ""
//#define SIM_PASS ""

int main()
{
    MDMParser::DevStatus devStatus;
    int res;
    uint8_t status = 0;

    MDMRtos<MDMSerial> mdm;
    GPSI2C gps;
    
    //mdm.setDebug(4);

    if (!mdm.init(SIM_PIN, &devStatus))
        status = 1;
    else if (!gps.init())
        status = 2;
    
    DeviceIO io(gps);

    switch (status) {
    case 1:
        io.lcdPrint("MODEM INIT FAILURE", "CHECK SIM");
        break;
    case 2:
        io.lcdPrint("GPS INIT FAILURE");
        break;
    }
    
    if (status != 0)
        goto error;
    
    io.lcdPrint("DEVICE INIT");
    
    io.lcdPrint("REGISTER NETWORK", "IMEI", devStatus.imei);

    if (!mdm.registerNet()) {
        io.lcdPrint("NETWORK REG ERROR");
        goto error;
    }

    io.lcdPrint("JOIN NETWORK");
#ifdef SIM_APN
    if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
#else
    if (mdm.join() == NOIP) {
#endif
        io.lcdPrint("NETWORK JOIN FAILURE");
        goto error;
    }
    
    {
        uint8_t tries;
        DeviceInfo deviceInfo(mdm, devStatus);
        DeviceMemory deviceMemory(mdm);
        
        if (io.resetButtonPressed()) {
            if (deviceMemory.resetPlatformCredentials())
                io.deviceFeedback().beepSuccess();
            else
                io.deviceFeedback().beepFailure();
            Thread::wait(1000);
            return;
        }

        MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
    
        io.lcdPrint("AGENT INIT");
        if (!agent.init()) {
            io.lcdPrint("AGENT INIT FAILURE");
            goto error;
        }
        
        tries = 3;
        do {
            io.lcdPrint("AGENT RUN");
            if (agent.run())
                break;
        } while (--tries > 0);

        if (tries == 0) {
            io.lcdPrint("AGENT RUN FAILURE");
            goto error;
        }
    }
    
    mdm.disconnect();
    return 0;

error:
    mdm.disconnect();
    return 1;
}