Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
main.cpp@13:4045b6ac448d, 2014-02-16 (annotated)
- Committer:
- vwochnik
- Date:
- Sun Feb 16 17:44:15 2014 +0000
- Revision:
- 13:4045b6ac448d
- Parent:
- 12:beb64aa0da86
- Child:
- 15:0ccf0f530a05
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 6:642e7c233e83 | 1 | #include "common.h" |
vwochnik | 6:642e7c233e83 | 2 | #include "ATResultBuffer.h" |
vwochnik | 0:ed4d6fd405ea | 3 | |
vwochnik | 0:ed4d6fd405ea | 4 | C027 c027; |
vwochnik | 6:642e7c233e83 | 5 | ATCommandsInterface *cmdIface = NULL; |
vwochnik | 6:642e7c233e83 | 6 | char cIMEI[21] = ""; |
vwochnik | 6:642e7c233e83 | 7 | char cCellId[9] = ""; |
vwochnik | 7:f77afd49c35d | 8 | char cICCID[24] = ""; |
vwochnik | 6:642e7c233e83 | 9 | sigq_t sigQ = {0, 0}; |
vwochnik | 0:ed4d6fd405ea | 10 | |
vwochnik | 0:ed4d6fd405ea | 11 | int main() |
vwochnik | 0:ed4d6fd405ea | 12 | { |
vwochnik | 7:f77afd49c35d | 13 | int ret; |
vwochnik | 7:f77afd49c35d | 14 | |
vwochnik | 7:f77afd49c35d | 15 | puts("Started..."); |
vwochnik | 7:f77afd49c35d | 16 | c027.mdmWakeup(); |
vwochnik | 7:f77afd49c35d | 17 | c027.mdmReset(); |
vwochnik | 7:f77afd49c35d | 18 | c027.mdmPower(true); |
vwochnik | 7:f77afd49c35d | 19 | UbloxUSBGSMModem modem; |
vwochnik | 7:f77afd49c35d | 20 | cmdIface = modem.getATCommandsInterface(); |
vwochnik | 6:642e7c233e83 | 21 | |
vwochnik | 6:642e7c233e83 | 22 | puts("Connecting..."); |
vwochnik | 4:363b4cc49445 | 23 | if (modem.connect("public4.m2minternet.com")) { |
vwochnik | 4:363b4cc49445 | 24 | puts("GPRS connection failure."); |
vwochnik | 4:363b4cc49445 | 25 | return 2; |
vwochnik | 4:363b4cc49445 | 26 | } |
vwochnik | 5:dee05a7c70f9 | 27 | |
vwochnik | 8:940c782eec5e | 28 | // get before starting program as they're permanent |
vwochnik | 13:4045b6ac448d | 29 | if ((!strlen(imei())) || (!strlen(iccid()))) { |
vwochnik | 12:beb64aa0da86 | 30 | puts("Couldn't read IMEI or ICCID."); |
vwochnik | 12:beb64aa0da86 | 31 | return 2; |
vwochnik | 12:beb64aa0da86 | 32 | } |
vwochnik | 4:363b4cc49445 | 33 | |
vwochnik | 7:f77afd49c35d | 34 | puts("Starting program..."); |
vwochnik | 8:940c782eec5e | 35 | |
vwochnik | 8:940c782eec5e | 36 | // restart program upon failure |
vwochnik | 8:940c782eec5e | 37 | do { |
vwochnik | 8:940c782eec5e | 38 | ret = program(); |
vwochnik | 12:beb64aa0da86 | 39 | if (ret) |
vwochnik | 12:beb64aa0da86 | 40 | puts("Restarting program..."); |
vwochnik | 8:940c782eec5e | 41 | } while (ret); |
vwochnik | 4:363b4cc49445 | 42 | |
vwochnik | 4:363b4cc49445 | 43 | modem.disconnect(); |
vwochnik | 6:642e7c233e83 | 44 | c027.mdmPower(false); |
vwochnik | 6:642e7c233e83 | 45 | |
vwochnik | 8:940c782eec5e | 46 | return 0; |
vwochnik | 4:363b4cc49445 | 47 | } |
vwochnik | 4:363b4cc49445 | 48 | |
vwochnik | 6:642e7c233e83 | 49 | const char * imei() |
vwochnik | 4:363b4cc49445 | 50 | { |
vwochnik | 6:642e7c233e83 | 51 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 52 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 53 | |
vwochnik | 6:642e7c233e83 | 54 | if ((cmdIface == NULL) || (strlen(cIMEI))) |
vwochnik | 6:642e7c233e83 | 55 | return cIMEI; |
vwochnik | 4:363b4cc49445 | 56 | |
vwochnik | 6:642e7c233e83 | 57 | cmdIface->execute("AT+CGSN", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 58 | if (ATCommandsInterface::ATResult::AT_OK == result.result) |
vwochnik | 6:642e7c233e83 | 59 | strcpy(cIMEI, buffer.getLastLine()); |
vwochnik | 6:642e7c233e83 | 60 | else |
vwochnik | 6:642e7c233e83 | 61 | cIMEI[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 62 | |
vwochnik | 6:642e7c233e83 | 63 | return cIMEI; |
vwochnik | 0:ed4d6fd405ea | 64 | } |
vwochnik | 4:363b4cc49445 | 65 | |
vwochnik | 6:642e7c233e83 | 66 | const char * cellId() |
vwochnik | 4:363b4cc49445 | 67 | { |
vwochnik | 6:642e7c233e83 | 68 | int len; uint32_t cellId; |
vwochnik | 6:642e7c233e83 | 69 | |
vwochnik | 6:642e7c233e83 | 70 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 71 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 72 | |
vwochnik | 6:642e7c233e83 | 73 | cCellId[0] = '\0'; |
vwochnik | 4:363b4cc49445 | 74 | |
vwochnik | 6:642e7c233e83 | 75 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 76 | return cCellId; |
vwochnik | 4:363b4cc49445 | 77 | |
vwochnik | 6:642e7c233e83 | 78 | cmdIface->executeSimple("AT+CREG=2", &result); |
vwochnik | 6:642e7c233e83 | 79 | if (ATCommandsInterface::ATResult::AT_OK != result.result) |
vwochnik | 6:642e7c233e83 | 80 | return cCellId; |
vwochnik | 4:363b4cc49445 | 81 | |
vwochnik | 6:642e7c233e83 | 82 | cmdIface->execute("AT+CREG?", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 83 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 84 | return cCellId; |
vwochnik | 6:642e7c233e83 | 85 | sscanf(buffer.getLastLine(), "+CREG: 2,%*d,\"%*lx\",\"%lx\",%*d%n", &cellId, &len); |
vwochnik | 6:642e7c233e83 | 86 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 87 | return cCellId; |
vwochnik | 6:642e7c233e83 | 88 | snprintf(cCellId, sizeof(cCellId), "%lX", cellId); |
vwochnik | 4:363b4cc49445 | 89 | |
vwochnik | 6:642e7c233e83 | 90 | return cCellId; |
vwochnik | 4:363b4cc49445 | 91 | } |
vwochnik | 4:363b4cc49445 | 92 | |
vwochnik | 7:f77afd49c35d | 93 | const char * iccid() |
vwochnik | 7:f77afd49c35d | 94 | { |
vwochnik | 7:f77afd49c35d | 95 | int len; |
vwochnik | 7:f77afd49c35d | 96 | |
vwochnik | 7:f77afd49c35d | 97 | ATCommandsInterface::ATResult result; |
vwochnik | 7:f77afd49c35d | 98 | ATResultBuffer buffer; |
vwochnik | 7:f77afd49c35d | 99 | |
vwochnik | 7:f77afd49c35d | 100 | if ((cmdIface == NULL) || (strlen(cICCID))) |
vwochnik | 7:f77afd49c35d | 101 | return cICCID; |
vwochnik | 7:f77afd49c35d | 102 | |
vwochnik | 7:f77afd49c35d | 103 | cmdIface->execute("AT+CCID?", &buffer, &result); |
vwochnik | 7:f77afd49c35d | 104 | if (buffer.getLastLine() == NULL) |
vwochnik | 7:f77afd49c35d | 105 | return cCellId; |
vwochnik | 7:f77afd49c35d | 106 | sscanf(buffer.getLastLine(), "+CCID: %s%n", cICCID, &len); |
vwochnik | 7:f77afd49c35d | 107 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 7:f77afd49c35d | 108 | cICCID[0] = '\0'; |
vwochnik | 7:f77afd49c35d | 109 | |
vwochnik | 7:f77afd49c35d | 110 | return cICCID; |
vwochnik | 7:f77afd49c35d | 111 | } |
vwochnik | 7:f77afd49c35d | 112 | |
vwochnik | 6:642e7c233e83 | 113 | sigq_t * signalQuality() |
vwochnik | 4:363b4cc49445 | 114 | { |
vwochnik | 6:642e7c233e83 | 115 | int rssi = 0, ber = 0, len = 0; |
vwochnik | 4:363b4cc49445 | 116 | |
vwochnik | 6:642e7c233e83 | 117 | ATCommandsInterface::ATResult result; |
vwochnik | 6:642e7c233e83 | 118 | ATResultBuffer buffer; |
vwochnik | 6:642e7c233e83 | 119 | |
vwochnik | 6:642e7c233e83 | 120 | sigQ.rssi = 0; sigQ.ber = 0; |
vwochnik | 4:363b4cc49445 | 121 | |
vwochnik | 6:642e7c233e83 | 122 | if (cmdIface == NULL) |
vwochnik | 6:642e7c233e83 | 123 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 124 | |
vwochnik | 6:642e7c233e83 | 125 | cmdIface->execute("AT+CSQ", &buffer, &result); |
vwochnik | 6:642e7c233e83 | 126 | if (buffer.getLastLine() == NULL) |
vwochnik | 6:642e7c233e83 | 127 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 128 | |
vwochnik | 6:642e7c233e83 | 129 | sscanf(buffer.getLastLine(), "+CSQ: %d,%d%n", &rssi, &ber, &len); |
vwochnik | 6:642e7c233e83 | 130 | if (len != strlen(buffer.getLastLine())) |
vwochnik | 6:642e7c233e83 | 131 | return &sigQ; |
vwochnik | 6:642e7c233e83 | 132 | |
vwochnik | 6:642e7c233e83 | 133 | if (rssi <= 31) |
vwochnik | 6:642e7c233e83 | 134 | sigQ.rssi = -113 + (2 * rssi); |
vwochnik | 6:642e7c233e83 | 135 | else |
vwochnik | 6:642e7c233e83 | 136 | sigQ.rssi = 0; |
vwochnik | 4:363b4cc49445 | 137 | |
vwochnik | 6:642e7c233e83 | 138 | switch (ber) { |
vwochnik | 6:642e7c233e83 | 139 | case 0: sigQ.ber = 49; break; |
vwochnik | 6:642e7c233e83 | 140 | case 1: sigQ.ber = 43; break; |
vwochnik | 6:642e7c233e83 | 141 | case 2: sigQ.ber = 37; break; |
vwochnik | 6:642e7c233e83 | 142 | case 3: sigQ.ber = 31; break; |
vwochnik | 6:642e7c233e83 | 143 | case 4: sigQ.ber = 25; break; |
vwochnik | 6:642e7c233e83 | 144 | case 5: sigQ.ber = 19; break; |
vwochnik | 6:642e7c233e83 | 145 | case 6: sigQ.ber = 13; break; |
vwochnik | 6:642e7c233e83 | 146 | case 7: sigQ.ber = 7; break; |
vwochnik | 6:642e7c233e83 | 147 | default: sigQ.ber = 0; |
vwochnik | 4:363b4cc49445 | 148 | } |
vwochnik | 4:363b4cc49445 | 149 | |
vwochnik | 6:642e7c233e83 | 150 | return &sigQ; |
vwochnik | 4:363b4cc49445 | 151 | } |