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-10-23
Revision:
55:a0f7295ed6b6
Parent:
52:8f1370084268
Child:
58:4cc0ae5a7058

File content as of revision 55:a0f7295ed6b6:

#include "mbed.h"
#include "rtos.h"
#include "MDM.h"
#include "GPS.h"
#include "DeviceInfo.h"
#include "MbedAgent.h"
#include "GPSTracker.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;

    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");
    
    /*if (io.resetButtonPressed()) {
        puts("Resetting program.");
        res = mdm.delFile("001_CREDENTIALS");
        if (res < 0) {
            puts("Credential reset failed.");
            io.lcdPrint("CREDENTIAL RESET", "FAILURE", "PLEASE RESTART DEVICE");
        } else {
            puts("Credential reset successful.");
            io.lcdPrint("CREDENTIAL RESET", "SUCCESS", "PLEASE RESTART DEVICE");
        }
        return 0;
    }*/

    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);
        MbedAgent agent(io, mdm, deviceInfo);
    
        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;
}