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

Committer:
xinlei
Date:
Tue Mar 03 14:10:09 2015 +0000
Revision:
77:f6717e4eccc4
Parent:
76:b07effe83fb8
Child:
80:757c3ff7b92b
Working device push, DNS caching, change logging level when running and more.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 72:c5709ae7b193 1 #include <stdio.h>
xinlei 72:c5709ae7b193 2
Cumulocity 41:804f6a0bda26 3 #include "mbed.h"
Cumulocity 41:804f6a0bda26 4 #include "rtos.h"
Cumulocity 41:804f6a0bda26 5 #include "MDM.h"
Cumulocity 47:89ae46d5c466 6 #include "GPS.h"
Cumulocity 41:804f6a0bda26 7 #include "DeviceInfo.h"
vwochnik 67:c360a2b2c948 8 #include "DeviceMemory.h"
Cumulocity 41:804f6a0bda26 9 #include "MbedAgent.h"
Cumulocity 47:89ae46d5c466 10 #include "GPSTracker.h"
vwochnik 68:0dc778a16d0d 11 #include "DeviceConfiguration.h"
xinlei 72:c5709ae7b193 12 #include "logging.h"
vwochnik 67:c360a2b2c948 13
vwochnik 52:8f1370084268 14 /**
vwochnik 52:8f1370084268 15 * SIM PIN. Null for no pin.
vwochnik 52:8f1370084268 16 */
vwochnik 52:8f1370084268 17 #define SIM_PIN NULL
vwochnik 52:8f1370084268 18
vwochnik 52:8f1370084268 19 /**
vwochnik 52:8f1370084268 20 * SIM GPRS login data. Leave commented out for automatic setting.
vwochnik 52:8f1370084268 21 */
vwochnik 52:8f1370084268 22 //#define SIM_APN ""
vwochnik 52:8f1370084268 23 //#define SIM_USER ""
vwochnik 52:8f1370084268 24 //#define SIM_PASS ""
vwochnik 52:8f1370084268 25
vwochnik 0:ed4d6fd405ea 26 int main()
vwochnik 0:ed4d6fd405ea 27 {
Cumulocity 41:804f6a0bda26 28 MDMParser::DevStatus devStatus;
Cumulocity 46:f6976fd64387 29 int res;
vwochnik 52:8f1370084268 30 uint8_t status = 0;
Cumulocity 42:104746744af8 31
vwochnik 55:a0f7295ed6b6 32 MDMRtos<MDMSerial> mdm;
Cumulocity 47:89ae46d5c466 33 GPSI2C gps;
xinlei 77:f6717e4eccc4 34 DeviceIO io(gps);
vwochnik 58:4cc0ae5a7058 35
xinlei 77:f6717e4eccc4 36 DigitalIn joystickUp(A2);
xinlei 77:f6717e4eccc4 37 DigitalIn joystickDown(A3);
xinlei 77:f6717e4eccc4 38 if (joystickUp) {
xinlei 77:f6717e4eccc4 39 setLevel(A_DEBUG);
xinlei 77:f6717e4eccc4 40 mdm.setDebug(3);
xinlei 77:f6717e4eccc4 41 printf("Enable debug mode.\r\n");
xinlei 77:f6717e4eccc4 42 } else {
xinlei 77:f6717e4eccc4 43 setLevel(A_INFO);
xinlei 77:f6717e4eccc4 44 mdm.setDebug(1);
xinlei 77:f6717e4eccc4 45 }
xinlei 77:f6717e4eccc4 46
xinlei 71:063c45e99578 47 io.lcdPrint("Device Init");
xinlei 76:b07effe83fb8 48 if (!mdm.init(SIM_PIN, &devStatus)) {
vwochnik 52:8f1370084268 49 status = 1;
xinlei 76:b07effe83fb8 50 io.lcdPrint("Modem Init Failure", "No SIM card found", "Or SIM has PIN code");
xinlei 76:b07effe83fb8 51 } else if (!gps.init()) {
vwochnik 52:8f1370084268 52 status = 2;
xinlei 71:063c45e99578 53 io.lcdPrint("GPS Init Failure");
xinlei 76:b07effe83fb8 54 }
vwochnik 52:8f1370084268 55 if (status != 0)
vwochnik 52:8f1370084268 56 goto error;
vwochnik 52:8f1370084268 57
xinlei 71:063c45e99578 58 io.lcdPrint("Register Network", "IMEI", devStatus.imei);
Cumulocity 41:804f6a0bda26 59 if (!mdm.registerNet()) {
xinlei 77:f6717e4eccc4 60 io.lcdPrint("No Network Coverage");
vwochnik 52:8f1370084268 61 goto error;
vwochnik 28:2004400abeec 62 }
vwochnik 28:2004400abeec 63
xinlei 71:063c45e99578 64 io.lcdPrint("Join Network");
vwochnik 52:8f1370084268 65 #ifdef SIM_APN
vwochnik 52:8f1370084268 66 if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) {
vwochnik 55:a0f7295ed6b6 67 #else
vwochnik 55:a0f7295ed6b6 68 if (mdm.join() == NOIP) {
vwochnik 55:a0f7295ed6b6 69 #endif
xinlei 77:f6717e4eccc4 70 io.lcdPrint("Network join failure", "Wrong APN settting,", "username and password");
vwochnik 52:8f1370084268 71 goto error;
vwochnik 52:8f1370084268 72 }
vwochnik 8:940c782eec5e 73
vwochnik 52:8f1370084268 74 {
vwochnik 52:8f1370084268 75 DeviceInfo deviceInfo(mdm, devStatus);
vwochnik 67:c360a2b2c948 76 DeviceMemory deviceMemory(mdm);
vwochnik 67:c360a2b2c948 77
vwochnik 67:c360a2b2c948 78 if (io.resetButtonPressed()) {
xinlei 71:063c45e99578 79 io.lcdPrint("Factory Reset");
xinlei 71:063c45e99578 80 if (deviceMemory.resetPlatformCredentials()) {
xinlei 71:063c45e99578 81 // io.deviceFeedback().beepSuccess();
xinlei 71:063c45e99578 82 io.lcdPrint("Reset Success");
xinlei 71:063c45e99578 83 }
xinlei 71:063c45e99578 84 else {
xinlei 71:063c45e99578 85 // io.deviceFeedback().beepFailure();
xinlei 71:063c45e99578 86 io.lcdPrint("Reset Failure");
xinlei 71:063c45e99578 87 }
xinlei 76:b07effe83fb8 88 Thread::wait(1000);
xinlei 76:b07effe83fb8 89 return 0;
vwochnik 67:c360a2b2c948 90 }
vwochnik 67:c360a2b2c948 91
vwochnik 67:c360a2b2c948 92 MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
vwochnik 52:8f1370084268 93
xinlei 71:063c45e99578 94 io.lcdPrint("Agent Init");
vwochnik 52:8f1370084268 95 if (!agent.init()) {
xinlei 77:f6717e4eccc4 96 io.lcdPrint("Agent Init Failure", "Debug via serial port");
vwochnik 52:8f1370084268 97 goto error;
vwochnik 52:8f1370084268 98 }
vwochnik 52:8f1370084268 99
xinlei 76:b07effe83fb8 100 uint8_t tries = 3;
vwochnik 52:8f1370084268 101 do {
xinlei 73:313975bfec96 102 io.lcdPrint("Agent Run");
vwochnik 52:8f1370084268 103 if (agent.run())
vwochnik 52:8f1370084268 104 break;
vwochnik 52:8f1370084268 105 } while (--tries > 0);
Cumulocity 41:804f6a0bda26 106
vwochnik 52:8f1370084268 107 if (tries == 0) {
xinlei 77:f6717e4eccc4 108 io.lcdPrint("Integration/Config Failure");
vwochnik 52:8f1370084268 109 goto error;
Cumulocity 41:804f6a0bda26 110 }
vwochnik 52:8f1370084268 111 }
vwochnik 32:56804dd00193 112
vwochnik 30:daa499571db7 113 mdm.disconnect();
xinlei 76:b07effe83fb8 114 mdm.powerOff();
vwochnik 8:940c782eec5e 115 return 0;
vwochnik 52:8f1370084268 116
vwochnik 52:8f1370084268 117 error:
vwochnik 52:8f1370084268 118 mdm.disconnect();
xinlei 76:b07effe83fb8 119 mdm.powerOff();
vwochnik 52:8f1370084268 120 return 1;
vwochnik 4:363b4cc49445 121 }