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