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:
Mon Feb 16 13:15:52 2015 +0000
Revision:
72:c5709ae7b193
Parent:
71:063c45e99578
Child:
73:313975bfec96
logging: incorporation of dedicated logging module.

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 {
xinlei 72:c5709ae7b193 28 setLevel(A_DEBUG);
Cumulocity 41:804f6a0bda26 29 MDMParser::DevStatus devStatus;
Cumulocity 46:f6976fd64387 30 int res;
vwochnik 52:8f1370084268 31 uint8_t status = 0;
Cumulocity 42:104746744af8 32
vwochnik 55:a0f7295ed6b6 33 MDMRtos<MDMSerial> mdm;
Cumulocity 47:89ae46d5c466 34 GPSI2C gps;
xinlei 71:063c45e99578 35 DeviceIO io(gps);
vwochnik 58:4cc0ae5a7058 36
vwochnik 68:0dc778a16d0d 37 //mdm.setDebug(4);
xinlei 71:063c45e99578 38 io.lcdPrint("Device Init");
vwochnik 52:8f1370084268 39 if (!mdm.init(SIM_PIN, &devStatus))
vwochnik 52:8f1370084268 40 status = 1;
vwochnik 52:8f1370084268 41 else if (!gps.init())
vwochnik 52:8f1370084268 42 status = 2;
Cumulocity 47:89ae46d5c466 43
vwochnik 52:8f1370084268 44 switch (status) {
vwochnik 52:8f1370084268 45 case 1:
xinlei 71:063c45e99578 46 io.lcdPrint("Modem Init Failure", "Check SIM");
vwochnik 52:8f1370084268 47 break;
vwochnik 52:8f1370084268 48 case 2:
xinlei 71:063c45e99578 49 io.lcdPrint("GPS Init Failure");
vwochnik 52:8f1370084268 50 break;
vwochnik 52:8f1370084268 51 }
vwochnik 52:8f1370084268 52
vwochnik 52:8f1370084268 53 if (status != 0)
vwochnik 52:8f1370084268 54 goto error;
vwochnik 52:8f1370084268 55
Cumulocity 46:f6976fd64387 56
xinlei 71:063c45e99578 57 io.lcdPrint("Register Network", "IMEI", devStatus.imei);
Cumulocity 42:104746744af8 58
Cumulocity 41:804f6a0bda26 59 if (!mdm.registerNet()) {
xinlei 71:063c45e99578 60 io.lcdPrint("Network Reg Error");
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 71:063c45e99578 70 io.lcdPrint("Network join failure");
vwochnik 52:8f1370084268 71 goto error;
vwochnik 52:8f1370084268 72 }
vwochnik 8:940c782eec5e 73
vwochnik 52:8f1370084268 74 {
vwochnik 52:8f1370084268 75 uint8_t tries;
vwochnik 52:8f1370084268 76 DeviceInfo deviceInfo(mdm, devStatus);
vwochnik 67:c360a2b2c948 77 DeviceMemory deviceMemory(mdm);
vwochnik 67:c360a2b2c948 78
vwochnik 67:c360a2b2c948 79 if (io.resetButtonPressed()) {
xinlei 71:063c45e99578 80 io.lcdPrint("Factory Reset");
xinlei 71:063c45e99578 81 if (deviceMemory.resetPlatformCredentials()) {
xinlei 71:063c45e99578 82 // io.deviceFeedback().beepSuccess();
xinlei 71:063c45e99578 83 io.lcdPrint("Reset Success");
xinlei 71:063c45e99578 84 }
xinlei 71:063c45e99578 85 else {
xinlei 71:063c45e99578 86 // io.deviceFeedback().beepFailure();
xinlei 71:063c45e99578 87 io.lcdPrint("Reset Failure");
xinlei 71:063c45e99578 88 }
xinlei 71:063c45e99578 89 // Thread::wait(1000);
vwochnik 67:c360a2b2c948 90 return;
vwochnik 67:c360a2b2c948 91 }
vwochnik 67:c360a2b2c948 92
vwochnik 67:c360a2b2c948 93 MbedAgent agent(io, mdm, deviceInfo, deviceMemory);
vwochnik 52:8f1370084268 94
xinlei 71:063c45e99578 95 io.lcdPrint("Agent Init");
vwochnik 52:8f1370084268 96 if (!agent.init()) {
xinlei 71:063c45e99578 97 io.lcdPrint("Agent Init Failure");
vwochnik 52:8f1370084268 98 goto error;
vwochnik 52:8f1370084268 99 }
vwochnik 52:8f1370084268 100
vwochnik 52:8f1370084268 101 tries = 3;
vwochnik 52:8f1370084268 102 do {
xinlei 71:063c45e99578 103 // io.lcdPrint("AGENT RUN");
xinlei 71:063c45e99578 104 puts("R");
vwochnik 52:8f1370084268 105 if (agent.run())
vwochnik 52:8f1370084268 106 break;
vwochnik 52:8f1370084268 107 } while (--tries > 0);
Cumulocity 41:804f6a0bda26 108
vwochnik 52:8f1370084268 109 if (tries == 0) {
xinlei 71:063c45e99578 110 io.lcdPrint("Agent Run Failure");
vwochnik 52:8f1370084268 111 goto error;
Cumulocity 41:804f6a0bda26 112 }
vwochnik 52:8f1370084268 113 }
vwochnik 32:56804dd00193 114
vwochnik 30:daa499571db7 115 mdm.disconnect();
vwochnik 8:940c782eec5e 116 return 0;
vwochnik 52:8f1370084268 117
vwochnik 52:8f1370084268 118 error:
vwochnik 52:8f1370084268 119 mdm.disconnect();
vwochnik 52:8f1370084268 120 return 1;
vwochnik 4:363b4cc49445 121 }