![](/media/cache/group/u-blox_logo_trans.png.50x50_q85.png)
portable version of the cumulocity demo
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@24:8f9b678a7932, 2014-04-15 (annotated)
- Committer:
- vwochnik
- Date:
- Tue Apr 15 16:59:56 2014 +0000
- Revision:
- 24:8f9b678a7932
- Parent:
- 23:0a48eebaaba8
- Child:
- 27:bfd402593acc
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 6:642e7c233e83 | 1 | #include "common.h" |
vwochnik | 23:0a48eebaaba8 | 2 | #include "apndb.h" |
vwochnik | 15:0ccf0f530a05 | 3 | #include "io.h" |
vwochnik | 6:642e7c233e83 | 4 | #include "ATResultBuffer.h" |
vwochnik | 0:ed4d6fd405ea | 5 | |
vwochnik | 0:ed4d6fd405ea | 6 | C027 c027; |
vwochnik | 6:642e7c233e83 | 7 | ATCommandsInterface *cmdIface = NULL; |
vwochnik | 6:642e7c233e83 | 8 | char cIMEI[21] = ""; |
vwochnik | 23:0a48eebaaba8 | 9 | char cIMSI[16] = ""; |
vwochnik | 6:642e7c233e83 | 10 | char cCellId[9] = ""; |
vwochnik | 7:f77afd49c35d | 11 | char cICCID[24] = ""; |
vwochnik | 6:642e7c233e83 | 12 | sigq_t sigQ = {0, 0}; |
vwochnik | 0:ed4d6fd405ea | 13 | |
vwochnik | 0:ed4d6fd405ea | 14 | int main() |
vwochnik | 0:ed4d6fd405ea | 15 | { |
vwochnik | 24:8f9b678a7932 | 16 | int ret; size_t c; |
vwochnik | 7:f77afd49c35d | 17 | |
vwochnik | 7:f77afd49c35d | 18 | puts("Started..."); |
vwochnik | 15:0ccf0f530a05 | 19 | io_init(); |
vwochnik | 15:0ccf0f530a05 | 20 | puts("IO initialized."); |
vwochnik | 15:0ccf0f530a05 | 21 | |
vwochnik | 7:f77afd49c35d | 22 | c027.mdmWakeup(); |
vwochnik | 7:f77afd49c35d | 23 | c027.mdmReset(); |
vwochnik | 7:f77afd49c35d | 24 | c027.mdmPower(true); |
vwochnik | 7:f77afd49c35d | 25 | UbloxUSBGSMModem modem; |
vwochnik | 7:f77afd49c35d | 26 | cmdIface = modem.getATCommandsInterface(); |
vwochnik | 6:642e7c233e83 | 27 | |
vwochnik | 24:8f9b678a7932 | 28 | /*puts("Searching for login..."); |
vwochnik | 24:8f9b678a7932 | 29 | // get imsi number |
vwochnik | 24:8f9b678a7932 | 30 | printf("IMSI: %s\n", imsi()); |
vwochnik | 24:8f9b678a7932 | 31 | apndb_t *apn = apndb_get(imsi()); |
vwochnik | 24:8f9b678a7932 | 32 | if (apn == NULL) |
vwochnik | 24:8f9b678a7932 | 33 | puts("No APN found."); |
vwochnik | 24:8f9b678a7932 | 34 | else |
vwochnik | 24:8f9b678a7932 | 35 | printf("APN: %s\n", apn->apn); |
vwochnik | 24:8f9b678a7932 | 36 | */ |
vwochnik | 24:8f9b678a7932 | 37 | |
vwochnik | 24:8f9b678a7932 | 38 | if (modem.connect("public4.m2minternet.com")) { |
vwochnik | 24:8f9b678a7932 | 39 | puts("GPRS connection failure."); |
vwochnik | 24:8f9b678a7932 | 40 | return 2; |
vwochnik | 4:363b4cc49445 | 41 | } |
vwochnik | 24:8f9b678a7932 | 42 | |
vwochnik | 24:8f9b678a7932 | 43 | /*printf("IMSI: %s\n", imsi()); |
vwochnik | 24:8f9b678a7932 | 44 | apn = apndb_get(imsi()); |
vwochnik | 24:8f9b678a7932 | 45 | if (apn == NULL) |
vwochnik | 24:8f9b678a7932 | 46 | puts("No APN found."); |
vwochnik | 24:8f9b678a7932 | 47 | else |
vwochnik | 24:8f9b678a7932 | 48 | printf("APN: %s\n", apn->apn); |
vwochnik | 24:8f9b678a7932 | 49 | */ |
vwochnik | 24:8f9b678a7932 | 50 | |
vwochnik | 8:940c782eec5e | 51 | // get before starting program as they're permanent |
vwochnik | 13:4045b6ac448d | 52 | if ((!strlen(imei())) || (!strlen(iccid()))) { |
vwochnik | 12:beb64aa0da86 | 53 | puts("Couldn't read IMEI or ICCID."); |
vwochnik | 12:beb64aa0da86 | 54 | return 2; |
vwochnik | 12:beb64aa0da86 | 55 | } |
vwochnik | 23:0a48eebaaba8 | 56 | |
vwochnik | 23:0a48eebaaba8 | 57 | // Shown on display |
vwochnik | 23:0a48eebaaba8 | 58 | signalQuality(); |
vwochnik | 4:363b4cc49445 | 59 | |
vwochnik | 7:f77afd49c35d | 60 | puts("Starting program..."); |
vwochnik | 8:940c782eec5e | 61 | |
vwochnik | 8:940c782eec5e | 62 | // restart program upon failure |
vwochnik | 8:940c782eec5e | 63 | do { |
vwochnik | 8:940c782eec5e | 64 | ret = program(); |
vwochnik | 12:beb64aa0da86 | 65 | if (ret) |
vwochnik | 12:beb64aa0da86 | 66 | puts("Restarting program..."); |
vwochnik | 8:940c782eec5e | 67 | } while (ret); |
vwochnik | 4:363b4cc49445 | 68 | |
vwochnik | 4:363b4cc49445 | 69 | modem.disconnect(); |
vwochnik | 6:642e7c233e83 | 70 | c027.mdmPower(false); |
vwochnik | 6:642e7c233e83 | 71 | |
vwochnik | 8:940c782eec5e | 72 | return 0; |
vwochnik | 4:363b4cc49445 | 73 | } |
vwochnik | 4:363b4cc49445 | 74 | |
vwochnik | 6:642e7c233e83 | 75 | const char * imei() |
vwochnik | 4:363b4cc49445 | 76 | { |
vwochnik | 6:642e7c233e83 | 77 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 78 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 79 | |
vwochnik | 6:642e7c233e83 | 80 | if ((cmdIface == NULL) || (strlen(cIMEI))) |
vwochnik | 6:642e7c233e83 | 81 | return cIMEI; |
vwochnik | 4:363b4cc49445 | 82 | |
vwochnik | 6:642e7c233e83 | 83 | cmdIface->execute("AT+CGSN", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 84 | if (ATCommandsInterface::ATResult::AT_OK == result.result) |
vwochnik | 6:642e7c233e83 | 85 | strcpy(cIMEI, buffer.getLastLine()); |
vwochnik | 6:642e7c233e83 | 86 | else |
vwochnik | 6:642e7c233e83 | 87 | cIMEI[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 88 | |
vwochnik | 6:642e7c233e83 | 89 | return cIMEI; |
vwochnik | 0:ed4d6fd405ea | 90 | } |
vwochnik | 4:363b4cc49445 | 91 | |
vwochnik | 23:0a48eebaaba8 | 92 | const char * imsi() |
vwochnik | 23:0a48eebaaba8 | 93 | { |
vwochnik | 23:0a48eebaaba8 | 94 | ATCommandsInterface::ATResult result; |
vwochnik | 23:0a48eebaaba8 | 95 | ATResultBuffer buffer; |
vwochnik | 23:0a48eebaaba8 | 96 | |
vwochnik | 23:0a48eebaaba8 | 97 | if ((cmdIface == NULL) || (strlen(cIMSI))) |
vwochnik | 23:0a48eebaaba8 | 98 | return cIMSI; |
vwochnik | 23:0a48eebaaba8 | 99 | |
vwochnik | 23:0a48eebaaba8 | 100 | cmdIface->execute("AT+CIMI", &buffer, &result); |
vwochnik | 23:0a48eebaaba8 | 101 | if (ATCommandsInterface::ATResult::AT_OK == result.result) |
vwochnik | 23:0a48eebaaba8 | 102 | strcpy(cIMSI, buffer.getLastLine()); |
vwochnik | 23:0a48eebaaba8 | 103 | else |
vwochnik | 23:0a48eebaaba8 | 104 | cIMSI[0] = '\0'; |
vwochnik | 23:0a48eebaaba8 | 105 | |
vwochnik | 23:0a48eebaaba8 | 106 | return cIMSI; |
vwochnik | 23:0a48eebaaba8 | 107 | } |
vwochnik | 23:0a48eebaaba8 | 108 | |
vwochnik | 6:642e7c233e83 | 109 | const char * cellId() |
vwochnik | 4:363b4cc49445 | 110 | { |
vwochnik | 6:642e7c233e83 | 111 | int len; uint32_t cellId; |
vwochnik | 6:642e7c233e83 | 112 | |
vwochnik | 6:642e7c233e83 | 113 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 114 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 115 | |
vwochnik | 6:642e7c233e83 | 116 | cCellId[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 117 | |
vwochnik | 6:642e7c233e83 | 118 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 119 | return cCellId; |
vwochnik | 4:363b4cc49445 | 120 | |
vwochnik | 6:642e7c233e83 | 121 | cmdIface->executeSimple("AT+CREG=2", &result); |
vwochnik | 6:642e7c233e83 | 122 | if (ATCommandsInterface::ATResult::AT_OK != result.result) |
vwochnik | 6:642e7c233e83 | 123 | return cCellId; |
vwochnik | 4:363b4cc49445 | 124 | |
vwochnik | 6:642e7c233e83 | 125 | cmdIface->execute("AT+CREG?", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 126 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 127 | return cCellId; |
vwochnik | 6:642e7c233e83 | 128 | sscanf(buffer.getLastLine(), "+CREG: 2,%*d,\"%*lx\",\"%lx\",%*d%n", &cellId, &len); |
vwochnik | 6:642e7c233e83 | 129 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 130 | return cCellId; |
vwochnik | 6:642e7c233e83 | 131 | snprintf(cCellId, sizeof(cCellId), "%lX", cellId); |
vwochnik | 4:363b4cc49445 | 132 | |
vwochnik | 6:642e7c233e83 | 133 | return cCellId; |
vwochnik | 4:363b4cc49445 | 134 | } |
vwochnik | 4:363b4cc49445 | 135 | |
vwochnik | 7:f77afd49c35d | 136 | const char * iccid() |
vwochnik | 7:f77afd49c35d | 137 | { |
vwochnik | 7:f77afd49c35d | 138 | int len; |
vwochnik | 7:f77afd49c35d | 139 | |
vwochnik | 7:f77afd49c35d | 140 | ATCommandsInterface::ATResult result; |
vwochnik | 7:f77afd49c35d | 141 | ATResultBuffer buffer; |
vwochnik | 7:f77afd49c35d | 142 | |
vwochnik | 7:f77afd49c35d | 143 | if ((cmdIface == NULL) || (strlen(cICCID))) |
vwochnik | 7:f77afd49c35d | 144 | return cICCID; |
vwochnik | 7:f77afd49c35d | 145 | |
vwochnik | 7:f77afd49c35d | 146 | cmdIface->execute("AT+CCID?", &buffer, &result); |
vwochnik | 7:f77afd49c35d | 147 | if (buffer.getLastLine() == NULL) |
vwochnik | 7:f77afd49c35d | 148 | return cCellId; |
vwochnik | 7:f77afd49c35d | 149 | sscanf(buffer.getLastLine(), "+CCID: %s%n", cICCID, &len); |
vwochnik | 7:f77afd49c35d | 150 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 7:f77afd49c35d | 151 | cICCID[0] = '\0'; |
vwochnik | 7:f77afd49c35d | 152 | |
vwochnik | 7:f77afd49c35d | 153 | return cICCID; |
vwochnik | 7:f77afd49c35d | 154 | } |
vwochnik | 7:f77afd49c35d | 155 | |
vwochnik | 6:642e7c233e83 | 156 | sigq_t * signalQuality() |
vwochnik | 4:363b4cc49445 | 157 | { |
vwochnik | 6:642e7c233e83 | 158 | int rssi = 0, ber = 0, len = 0; |
vwochnik | 4:363b4cc49445 | 159 | |
vwochnik | 6:642e7c233e83 | 160 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 161 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 162 | |
vwochnik | 6:642e7c233e83 | 163 | sigQ.rssi = 0; sigQ.ber = 0; |
vwochnik | 20:ef9cc1b42e9d | 164 | |
vwochnik | 20:ef9cc1b42e9d | 165 | lcd_signal(0, 0); |
vwochnik | 4:363b4cc49445 | 166 | |
vwochnik | 6:642e7c233e83 | 167 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 168 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 169 | |
vwochnik | 6:642e7c233e83 | 170 | cmdIface->execute("AT+CSQ", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 171 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 172 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 173 | |
vwochnik | 6:642e7c233e83 | 174 | sscanf(buffer.getLastLine(), "+CSQ: %d,%d%n", &rssi, &ber, &len); |
vwochnik | 6:642e7c233e83 | 175 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 176 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 177 | |
vwochnik | 6:642e7c233e83 | 178 | if (rssi <= 31) |
vwochnik | 6:642e7c233e83 | 179 | sigQ.rssi = -113 + (2 * rssi); |
vwochnik | 6:642e7c233e83 | 180 | else |
vwochnik | 6:642e7c233e83 | 181 | sigQ.rssi = 0; |
vwochnik | 4:363b4cc49445 | 182 | |
vwochnik | 6:642e7c233e83 | 183 | switch (ber) { |
vwochnik | 6:642e7c233e83 | 184 | case 0: sigQ.ber = 49; break; |
vwochnik | 6:642e7c233e83 | 185 | case 1: sigQ.ber = 43; break; |
vwochnik | 6:642e7c233e83 | 186 | case 2: sigQ.ber = 37; break; |
vwochnik | 6:642e7c233e83 | 187 | case 3: sigQ.ber = 31; break; |
vwochnik | 6:642e7c233e83 | 188 | case 4: sigQ.ber = 25; break; |
vwochnik | 6:642e7c233e83 | 189 | case 5: sigQ.ber = 19; break; |
vwochnik | 6:642e7c233e83 | 190 | case 6: sigQ.ber = 13; break; |
vwochnik | 6:642e7c233e83 | 191 | case 7: sigQ.ber = 7; break; |
vwochnik | 6:642e7c233e83 | 192 | default: sigQ.ber = 0; |
vwochnik | 4:363b4cc49445 | 193 | } |
vwochnik | 20:ef9cc1b42e9d | 194 | |
vwochnik | 20:ef9cc1b42e9d | 195 | lcd_signal(sigQ.rssi, sigQ.ber); |
vwochnik | 4:363b4cc49445 | 196 | |
vwochnik | 6:642e7c233e83 | 197 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 198 | } |