Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: aconno_I2C Lis2dh12 WatchdogTimer
modem.cpp@12:8345612bf867, 2018-12-16 (annotated)
- Committer:
- pathfindr
- Date:
- Sun Dec 16 01:06:14 2018 +0000
- Revision:
- 12:8345612bf867
- Parent:
- 7:e9a19750700d
- Child:
- 13:29f67f256709
4
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| pathfindr | 7:e9a19750700d | 1 | #include "modem.h" |
| pathfindr | 7:e9a19750700d | 2 | |
| pathfindr | 12:8345612bf867 | 3 | static char ATinBuffer[180]; |
| pathfindr | 12:8345612bf867 | 4 | |
| pathfindr | 7:e9a19750700d | 5 | Modem::Modem(PinName tx, PinName rx, PinName cts, PinName rts, PinName pwrkey, PinName vreg_en): _uart(tx,rx,115200), _pwrkey(pwrkey), _vreg_en(vreg_en) |
| pathfindr | 7:e9a19750700d | 6 | { |
| pathfindr | 7:e9a19750700d | 7 | requireSoftReset = true; //TODO: this can be removed when uart sleep issue resolved |
| pathfindr | 7:e9a19750700d | 8 | } |
| pathfindr | 7:e9a19750700d | 9 | |
| pathfindr | 7:e9a19750700d | 10 | void Modem::flushSerial(void) |
| pathfindr | 7:e9a19750700d | 11 | { |
| pathfindr | 7:e9a19750700d | 12 | char char1 = 0; |
| pathfindr | 7:e9a19750700d | 13 | while (_uart.readable()) { |
| pathfindr | 7:e9a19750700d | 14 | char1 = _uart.getc(); |
| pathfindr | 7:e9a19750700d | 15 | } |
| pathfindr | 7:e9a19750700d | 16 | return; |
| pathfindr | 7:e9a19750700d | 17 | } |
| pathfindr | 7:e9a19750700d | 18 | |
| pathfindr | 7:e9a19750700d | 19 | void Modem::ATsendCMD(char* cmd) |
| pathfindr | 7:e9a19750700d | 20 | { |
| pathfindr | 7:e9a19750700d | 21 | flushSerial(); |
| pathfindr | 7:e9a19750700d | 22 | _uart.puts(cmd); |
| pathfindr | 7:e9a19750700d | 23 | _uart.puts("\r"); |
| pathfindr | 7:e9a19750700d | 24 | } |
| pathfindr | 7:e9a19750700d | 25 | |
| pathfindr | 12:8345612bf867 | 26 | bool Modem::ATwaitForWord(char* word, uint32_t timeout) |
| pathfindr | 7:e9a19750700d | 27 | { |
| pathfindr | 7:e9a19750700d | 28 | int targetIndex = 0; |
| pathfindr | 7:e9a19750700d | 29 | bool havefullmatch = false; |
| pathfindr | 7:e9a19750700d | 30 | char captured[32]; |
| pathfindr | 7:e9a19750700d | 31 | memset (captured,0,sizeof(captured)); |
| pathfindr | 7:e9a19750700d | 32 | Timer t; |
| pathfindr | 7:e9a19750700d | 33 | t.start(); |
| pathfindr | 12:8345612bf867 | 34 | uint32_t startmillis = t.read_ms(); |
| pathfindr | 12:8345612bf867 | 35 | uint32_t runtime = 0; |
| pathfindr | 7:e9a19750700d | 36 | while(!havefullmatch && runtime < timeout) { |
| pathfindr | 7:e9a19750700d | 37 | runtime = (t.read_ms() - startmillis); |
| pathfindr | 7:e9a19750700d | 38 | if(_uart.readable()) { |
| pathfindr | 7:e9a19750700d | 39 | char c = _uart.getc(); |
| pathfindr | 7:e9a19750700d | 40 | if (c != word[targetIndex]) { //no match, reset |
| pathfindr | 7:e9a19750700d | 41 | targetIndex = 0; |
| pathfindr | 7:e9a19750700d | 42 | } |
| pathfindr | 7:e9a19750700d | 43 | if (c == word[targetIndex]) { //we have a match |
| pathfindr | 7:e9a19750700d | 44 | captured[targetIndex] = c; |
| pathfindr | 7:e9a19750700d | 45 | targetIndex ++; |
| pathfindr | 7:e9a19750700d | 46 | //check for full match |
| pathfindr | 7:e9a19750700d | 47 | if ( strcmp(word, captured) == 0 ) { |
| pathfindr | 7:e9a19750700d | 48 | havefullmatch = true; |
| pathfindr | 7:e9a19750700d | 49 | } |
| pathfindr | 7:e9a19750700d | 50 | } |
| pathfindr | 12:8345612bf867 | 51 | } |
| pathfindr | 7:e9a19750700d | 52 | } |
| pathfindr | 7:e9a19750700d | 53 | t.stop(); |
| pathfindr | 7:e9a19750700d | 54 | t.reset(); |
| pathfindr | 7:e9a19750700d | 55 | ThisThread::sleep_for(500); |
| pathfindr | 7:e9a19750700d | 56 | if (havefullmatch) { |
| pathfindr | 7:e9a19750700d | 57 | return true; |
| pathfindr | 7:e9a19750700d | 58 | } else { |
| pathfindr | 7:e9a19750700d | 59 | return false; |
| pathfindr | 7:e9a19750700d | 60 | } |
| pathfindr | 7:e9a19750700d | 61 | } |
| pathfindr | 7:e9a19750700d | 62 | |
| pathfindr | 12:8345612bf867 | 63 | bool Modem::on(void) |
| pathfindr | 12:8345612bf867 | 64 | { |
| pathfindr | 12:8345612bf867 | 65 | _vreg_en = 1; |
| pathfindr | 12:8345612bf867 | 66 | Thread::wait(500); |
| pathfindr | 12:8345612bf867 | 67 | _pwrkey = 0; |
| pathfindr | 12:8345612bf867 | 68 | Thread::wait(1500); |
| pathfindr | 12:8345612bf867 | 69 | _pwrkey = 1; |
| pathfindr | 12:8345612bf867 | 70 | if (ATwaitForWord("RDY",10000)) { |
| pathfindr | 12:8345612bf867 | 71 | //TURN OFF ECHO |
| pathfindr | 12:8345612bf867 | 72 | ATsendCMD("ATE 0"); |
| pathfindr | 12:8345612bf867 | 73 | ATwaitForWord("OK",5000); |
| pathfindr | 12:8345612bf867 | 74 | return true; |
| pathfindr | 12:8345612bf867 | 75 | } else { |
| pathfindr | 12:8345612bf867 | 76 | return false; |
| pathfindr | 12:8345612bf867 | 77 | } |
| pathfindr | 12:8345612bf867 | 78 | } |
| pathfindr | 12:8345612bf867 | 79 | |
| pathfindr | 12:8345612bf867 | 80 | void Modem::off(bool soft) |
| pathfindr | 12:8345612bf867 | 81 | { |
| pathfindr | 12:8345612bf867 | 82 | if (soft) { |
| pathfindr | 12:8345612bf867 | 83 | ATsendCMD("AT+QPOWD=0"); |
| pathfindr | 12:8345612bf867 | 84 | ATwaitForWord("OK\r",5000); |
| pathfindr | 12:8345612bf867 | 85 | } |
| pathfindr | 12:8345612bf867 | 86 | _vreg_en = 0; |
| pathfindr | 12:8345612bf867 | 87 | } |
| pathfindr | 12:8345612bf867 | 88 | |
| pathfindr | 12:8345612bf867 | 89 | bool Modem::registerOnNetwork(int maxAttempts, uint32_t timeout) |
| pathfindr | 12:8345612bf867 | 90 | { |
| pathfindr | 12:8345612bf867 | 91 | bool registered = false; |
| pathfindr | 12:8345612bf867 | 92 | int attempt = 0; |
| pathfindr | 12:8345612bf867 | 93 | Timer t; |
| pathfindr | 12:8345612bf867 | 94 | t.start(); |
| pathfindr | 12:8345612bf867 | 95 | //TRY X NUMBER OF TIMES |
| pathfindr | 12:8345612bf867 | 96 | while (attempt <= maxAttempts) { |
| pathfindr | 12:8345612bf867 | 97 | attempt ++; |
| pathfindr | 12:8345612bf867 | 98 | t.reset(); |
| pathfindr | 12:8345612bf867 | 99 | uint32_t startmillis = t.read_ms(); |
| pathfindr | 12:8345612bf867 | 100 | uint32_t runtime = 0; |
| pathfindr | 12:8345612bf867 | 101 | while(!registered && runtime < timeout) { |
| pathfindr | 12:8345612bf867 | 102 | runtime = (t.read_ms() - startmillis); |
| pathfindr | 12:8345612bf867 | 103 | Thread::wait(1000); |
| pathfindr | 12:8345612bf867 | 104 | ATsendCMD("AT+CREG?"); |
| pathfindr | 12:8345612bf867 | 105 | if (ATwaitForWord("+CREG: 0,5",5000)) { |
| pathfindr | 12:8345612bf867 | 106 | registered = true; |
| pathfindr | 12:8345612bf867 | 107 | }; |
| pathfindr | 12:8345612bf867 | 108 | } |
| pathfindr | 12:8345612bf867 | 109 | if (!registered) { |
| pathfindr | 12:8345612bf867 | 110 | off(true); |
| pathfindr | 12:8345612bf867 | 111 | on(); |
| pathfindr | 12:8345612bf867 | 112 | } |
| pathfindr | 12:8345612bf867 | 113 | } |
| pathfindr | 12:8345612bf867 | 114 | t.stop(); |
| pathfindr | 12:8345612bf867 | 115 | if (registered) { |
| pathfindr | 12:8345612bf867 | 116 | return true; |
| pathfindr | 12:8345612bf867 | 117 | } else { |
| pathfindr | 12:8345612bf867 | 118 | return false; |
| pathfindr | 12:8345612bf867 | 119 | } |
| pathfindr | 12:8345612bf867 | 120 | } |
| pathfindr | 12:8345612bf867 | 121 | |
| pathfindr | 12:8345612bf867 | 122 | bool Modem::USSDsend(char* message, int maxAttempts) |
| pathfindr | 12:8345612bf867 | 123 | { |
| pathfindr | 12:8345612bf867 | 124 | bool sent = false; |
| pathfindr | 12:8345612bf867 | 125 | int attempt = 0; |
| pathfindr | 12:8345612bf867 | 126 | //TRY X NUMBER OF TIMES |
| pathfindr | 12:8345612bf867 | 127 | while (!sent && attempt <= maxAttempts) { |
| pathfindr | 12:8345612bf867 | 128 | attempt ++; |
| pathfindr | 12:8345612bf867 | 129 | char bytestosend[160]; |
| pathfindr | 12:8345612bf867 | 130 | sprintf(bytestosend, "AT+CUSD=1,\"#469*%s#\"", message); |
| pathfindr | 12:8345612bf867 | 131 | ATsendCMD(bytestosend); |
| pathfindr | 12:8345612bf867 | 132 | if (ATwaitForWord("+CUSD: 0",10000)) { |
| pathfindr | 12:8345612bf867 | 133 | flushSerial(); |
| pathfindr | 12:8345612bf867 | 134 | sent = true; |
| pathfindr | 12:8345612bf867 | 135 | }; |
| pathfindr | 12:8345612bf867 | 136 | } |
| pathfindr | 12:8345612bf867 | 137 | if (sent) { |
| pathfindr | 12:8345612bf867 | 138 | return true; |
| pathfindr | 12:8345612bf867 | 139 | } else { |
| pathfindr | 12:8345612bf867 | 140 | return false; |
| pathfindr | 12:8345612bf867 | 141 | } |
| pathfindr | 12:8345612bf867 | 142 | } |
| pathfindr | 12:8345612bf867 | 143 | |
| pathfindr | 12:8345612bf867 | 144 | char* Modem::USSDreceive(uint32_t timeout) |
| pathfindr | 12:8345612bf867 | 145 | { |
| pathfindr | 12:8345612bf867 | 146 | bool received = false; |
| pathfindr | 12:8345612bf867 | 147 | int bufferIndex = 0; |
| pathfindr | 12:8345612bf867 | 148 | Timer t; |
| pathfindr | 12:8345612bf867 | 149 | t.start(); |
| pathfindr | 12:8345612bf867 | 150 | //TRY UNTIL TIMEOUT |
| pathfindr | 12:8345612bf867 | 151 | uint32_t startmillis = t.read_ms(); |
| pathfindr | 12:8345612bf867 | 152 | uint32_t runtime = 0; |
| pathfindr | 12:8345612bf867 | 153 | memset(ATinBuffer, 0, sizeof(ATinBuffer)); |
| pathfindr | 12:8345612bf867 | 154 | while(!received && runtime < timeout) { |
| pathfindr | 12:8345612bf867 | 155 | runtime = (t.read_ms() - startmillis); |
| pathfindr | 12:8345612bf867 | 156 | //ATsendCMD("AT+CUSD?"); |
| pathfindr | 12:8345612bf867 | 157 | if (ATwaitForWord("+CUSD: 0,\"%",2000)) { |
| pathfindr | 12:8345612bf867 | 158 | led1 = 0; |
| pathfindr | 12:8345612bf867 | 159 | while(!received && runtime < timeout) { |
| pathfindr | 12:8345612bf867 | 160 | runtime = (t.read_ms() - startmillis); |
| pathfindr | 12:8345612bf867 | 161 | if(_uart.readable()) { |
| pathfindr | 12:8345612bf867 | 162 | char c = _uart.getc(); |
| pathfindr | 12:8345612bf867 | 163 | if (c == '%') { |
| pathfindr | 12:8345612bf867 | 164 | led1 = 1; |
| pathfindr | 12:8345612bf867 | 165 | flushSerial(); |
| pathfindr | 12:8345612bf867 | 166 | received = true; |
| pathfindr | 12:8345612bf867 | 167 | } else { |
| pathfindr | 12:8345612bf867 | 168 | ATinBuffer[bufferIndex] = c; |
| pathfindr | 12:8345612bf867 | 169 | } |
| pathfindr | 12:8345612bf867 | 170 | bufferIndex ++; //this must got at end otherwise first digit is \0 |
| pathfindr | 12:8345612bf867 | 171 | } |
| pathfindr | 12:8345612bf867 | 172 | } |
| pathfindr | 12:8345612bf867 | 173 | } |
| pathfindr | 12:8345612bf867 | 174 | } |
| pathfindr | 12:8345612bf867 | 175 | if (received) { |
| pathfindr | 12:8345612bf867 | 176 | return ATinBuffer; |
| pathfindr | 12:8345612bf867 | 177 | } else { |
| pathfindr | 12:8345612bf867 | 178 | return "error"; |
| pathfindr | 12:8345612bf867 | 179 | } |
| pathfindr | 12:8345612bf867 | 180 | } |
| pathfindr | 12:8345612bf867 | 181 | |
| pathfindr | 12:8345612bf867 | 182 | |
| pathfindr | 7:e9a19750700d | 183 | Modem::~Modem(){}; |