Demo for the Adafruit_FONA_3G_Library. This is a port of the FONAtest available here: https://github.com/adafruit/Adafruit_FONA_Library/tree/master/examples/FONAtest .
Dependencies: mbed Adafruit_FONA_3G
main.cpp@0:ea718d60afce, 2019-02-27 (annotated)
- Committer:
- Nels885
- Date:
- Wed Feb 27 19:13:46 2019 +0000
- Revision:
- 0:ea718d60afce
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nels885 | 0:ea718d60afce | 1 | /*************************************************** |
Nels885 | 0:ea718d60afce | 2 | This is an example for our Adafruit FONA Cellular Module |
Nels885 | 0:ea718d60afce | 3 | |
Nels885 | 0:ea718d60afce | 4 | Designed specifically to work with the Adafruit FONA |
Nels885 | 0:ea718d60afce | 5 | ----> http://www.adafruit.com/products/1946 |
Nels885 | 0:ea718d60afce | 6 | ----> http://www.adafruit.com/products/1963 |
Nels885 | 0:ea718d60afce | 7 | ----> http://www.adafruit.com/products/2468 |
Nels885 | 0:ea718d60afce | 8 | ----> http://www.adafruit.com/products/2542 |
Nels885 | 0:ea718d60afce | 9 | |
Nels885 | 0:ea718d60afce | 10 | These cellular modules use TTL Serial to communicate, 2 pins are |
Nels885 | 0:ea718d60afce | 11 | required to interface |
Nels885 | 0:ea718d60afce | 12 | Adafruit invests time and resources providing this open source code, |
Nels885 | 0:ea718d60afce | 13 | please support Adafruit and open-source hardware by purchasing |
Nels885 | 0:ea718d60afce | 14 | products from Adafruit! |
Nels885 | 0:ea718d60afce | 15 | |
Nels885 | 0:ea718d60afce | 16 | Written by Limor Fried/Ladyada for Adafruit Industries. |
Nels885 | 0:ea718d60afce | 17 | BSD license, all text above must be included in any redistribution |
Nels885 | 0:ea718d60afce | 18 | ****************************************************/ |
Nels885 | 0:ea718d60afce | 19 | |
Nels885 | 0:ea718d60afce | 20 | /* |
Nels885 | 0:ea718d60afce | 21 | * Modified by Marc PLOUHINEC 27/06/2015 for use in mbed |
Nels885 | 0:ea718d60afce | 22 | * Modified by Lionel VOIRIN 27/02/2019 for use FONA 3G |
Nels885 | 0:ea718d60afce | 23 | */ |
Nels885 | 0:ea718d60afce | 24 | |
Nels885 | 0:ea718d60afce | 25 | /* |
Nels885 | 0:ea718d60afce | 26 | THIS CODE IS STILL IN PROGRESS! |
Nels885 | 0:ea718d60afce | 27 | |
Nels885 | 0:ea718d60afce | 28 | Open up the serial console on the Arduino at 4800 baud to interact with FONA |
Nels885 | 0:ea718d60afce | 29 | |
Nels885 | 0:ea718d60afce | 30 | Note that if you need to set a GPRS APN, username, and password scroll down to |
Nels885 | 0:ea718d60afce | 31 | the commented section below just before the main "while (true)" loop. |
Nels885 | 0:ea718d60afce | 32 | */ |
Nels885 | 0:ea718d60afce | 33 | |
Nels885 | 0:ea718d60afce | 34 | #include <ctype.h> |
Nels885 | 0:ea718d60afce | 35 | #include "mbed.h" |
Nels885 | 0:ea718d60afce | 36 | |
Nels885 | 0:ea718d60afce | 37 | #include "Adafruit_FONA.h" |
Nels885 | 0:ea718d60afce | 38 | |
Nels885 | 0:ea718d60afce | 39 | #define FONA_KEY p8 |
Nels885 | 0:ea718d60afce | 40 | #define FONA_RST p15 |
Nels885 | 0:ea718d60afce | 41 | #define FONA_TX p13 |
Nels885 | 0:ea718d60afce | 42 | #define FONA_RX p14 |
Nels885 | 0:ea718d60afce | 43 | |
Nels885 | 0:ea718d60afce | 44 | #define FONA_BAUDRATE 115200 |
Nels885 | 0:ea718d60afce | 45 | #define PC_BAUDRATE 115200 |
Nels885 | 0:ea718d60afce | 46 | |
Nels885 | 0:ea718d60afce | 47 | // this is a large buffer for replies |
Nels885 | 0:ea718d60afce | 48 | char replybuffer[255]; |
Nels885 | 0:ea718d60afce | 49 | |
Nels885 | 0:ea718d60afce | 50 | Serial pcSerial(USBTX, USBRX); |
Nels885 | 0:ea718d60afce | 51 | Adafruit_FONA_3G fona(FONA_TX, FONA_RX, FONA_RST, FONA_KEY); |
Nels885 | 0:ea718d60afce | 52 | |
Nels885 | 0:ea718d60afce | 53 | // Functions defined after main() |
Nels885 | 0:ea718d60afce | 54 | uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0); |
Nels885 | 0:ea718d60afce | 55 | uint8_t type; |
Nels885 | 0:ea718d60afce | 56 | void printMenu(void); |
Nels885 | 0:ea718d60afce | 57 | void flushSerial(); |
Nels885 | 0:ea718d60afce | 58 | char readBlocking(); |
Nels885 | 0:ea718d60afce | 59 | uint16_t readnumber(); |
Nels885 | 0:ea718d60afce | 60 | long map(long x, long in_min, long in_max, long out_min, long out_max); |
Nels885 | 0:ea718d60afce | 61 | |
Nels885 | 0:ea718d60afce | 62 | int main() |
Nels885 | 0:ea718d60afce | 63 | { |
Nels885 | 0:ea718d60afce | 64 | pcSerial.baud(PC_BAUDRATE); |
Nels885 | 0:ea718d60afce | 65 | wait(1); |
Nels885 | 0:ea718d60afce | 66 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 67 | |
Nels885 | 0:ea718d60afce | 68 | pcSerial.printf("FONA basic test\r\n"); |
Nels885 | 0:ea718d60afce | 69 | pcSerial.printf("Initializing....(May take 3 seconds)\r\n"); |
Nels885 | 0:ea718d60afce | 70 | |
Nels885 | 0:ea718d60afce | 71 | // See if the FONA is responding |
Nels885 | 0:ea718d60afce | 72 | if (! fona.begin(FONA_BAUDRATE)) { |
Nels885 | 0:ea718d60afce | 73 | pcSerial.printf("Couldn't find FONA\r\n"); |
Nels885 | 0:ea718d60afce | 74 | while (1); |
Nels885 | 0:ea718d60afce | 75 | } |
Nels885 | 0:ea718d60afce | 76 | type = fona.type(); |
Nels885 | 0:ea718d60afce | 77 | pcSerial.printf("FONA is OK\r\n"); |
Nels885 | 0:ea718d60afce | 78 | pcSerial.printf("Found "); |
Nels885 | 0:ea718d60afce | 79 | switch (type) { |
Nels885 | 0:ea718d60afce | 80 | case FONA800L: |
Nels885 | 0:ea718d60afce | 81 | pcSerial.printf("FONA 800L\r\n"); |
Nels885 | 0:ea718d60afce | 82 | break; |
Nels885 | 0:ea718d60afce | 83 | case FONA800H: |
Nels885 | 0:ea718d60afce | 84 | pcSerial.printf("FONA 800H\r\n"); |
Nels885 | 0:ea718d60afce | 85 | break; |
Nels885 | 0:ea718d60afce | 86 | case FONA808_V1: |
Nels885 | 0:ea718d60afce | 87 | pcSerial.printf("FONA 808 (v1)\r\n"); |
Nels885 | 0:ea718d60afce | 88 | break; |
Nels885 | 0:ea718d60afce | 89 | case FONA808_V2: |
Nels885 | 0:ea718d60afce | 90 | pcSerial.printf("FONA 808 (v2)\r\n"); |
Nels885 | 0:ea718d60afce | 91 | break; |
Nels885 | 0:ea718d60afce | 92 | case FONA3G_A: |
Nels885 | 0:ea718d60afce | 93 | pcSerial.printf("FONA 3G (American)\r\n"); |
Nels885 | 0:ea718d60afce | 94 | break; |
Nels885 | 0:ea718d60afce | 95 | case FONA3G_E: |
Nels885 | 0:ea718d60afce | 96 | pcSerial.printf("FONA 3G (European)\r\n"); |
Nels885 | 0:ea718d60afce | 97 | break; |
Nels885 | 0:ea718d60afce | 98 | default: |
Nels885 | 0:ea718d60afce | 99 | pcSerial.printf("???\r\n"); |
Nels885 | 0:ea718d60afce | 100 | break; |
Nels885 | 0:ea718d60afce | 101 | } |
Nels885 | 0:ea718d60afce | 102 | |
Nels885 | 0:ea718d60afce | 103 | // Print SIM card IMEI number. |
Nels885 | 0:ea718d60afce | 104 | char imei[15] = {0}; // MUST use a 16 character buffer for IMEI! |
Nels885 | 0:ea718d60afce | 105 | uint8_t imeiLen = fona.getIMEI(imei); |
Nels885 | 0:ea718d60afce | 106 | if (imeiLen > 0) { |
Nels885 | 0:ea718d60afce | 107 | pcSerial.printf("SIM card IMEI: %s\r\n", imei); |
Nels885 | 0:ea718d60afce | 108 | } |
Nels885 | 0:ea718d60afce | 109 | |
Nels885 | 0:ea718d60afce | 110 | printMenu(); |
Nels885 | 0:ea718d60afce | 111 | |
Nels885 | 0:ea718d60afce | 112 | while (true) { |
Nels885 | 0:ea718d60afce | 113 | pcSerial.printf("FONA> "); |
Nels885 | 0:ea718d60afce | 114 | while (! pcSerial.readable() ) { |
Nels885 | 0:ea718d60afce | 115 | if (fona.readable()) { |
Nels885 | 0:ea718d60afce | 116 | pcSerial.putc(fona.getc()); |
Nels885 | 0:ea718d60afce | 117 | } |
Nels885 | 0:ea718d60afce | 118 | } |
Nels885 | 0:ea718d60afce | 119 | |
Nels885 | 0:ea718d60afce | 120 | char command = pcSerial.getc(); |
Nels885 | 0:ea718d60afce | 121 | pcSerial.printf("%c\r\n", command); |
Nels885 | 0:ea718d60afce | 122 | |
Nels885 | 0:ea718d60afce | 123 | |
Nels885 | 0:ea718d60afce | 124 | switch (command) { |
Nels885 | 0:ea718d60afce | 125 | case '?': { |
Nels885 | 0:ea718d60afce | 126 | printMenu(); |
Nels885 | 0:ea718d60afce | 127 | break; |
Nels885 | 0:ea718d60afce | 128 | } |
Nels885 | 0:ea718d60afce | 129 | |
Nels885 | 0:ea718d60afce | 130 | case 'a': { |
Nels885 | 0:ea718d60afce | 131 | // read the ADC |
Nels885 | 0:ea718d60afce | 132 | uint16_t adc; |
Nels885 | 0:ea718d60afce | 133 | if (! fona.getADCVoltage(&adc)) { |
Nels885 | 0:ea718d60afce | 134 | pcSerial.printf("Failed to read ADC\r\n"); |
Nels885 | 0:ea718d60afce | 135 | } else { |
Nels885 | 0:ea718d60afce | 136 | pcSerial.printf("ADC = %d mV\r\n", adc); |
Nels885 | 0:ea718d60afce | 137 | } |
Nels885 | 0:ea718d60afce | 138 | break; |
Nels885 | 0:ea718d60afce | 139 | } |
Nels885 | 0:ea718d60afce | 140 | |
Nels885 | 0:ea718d60afce | 141 | case 'b': { |
Nels885 | 0:ea718d60afce | 142 | // read the battery voltage and percentage |
Nels885 | 0:ea718d60afce | 143 | uint16_t vbat; |
Nels885 | 0:ea718d60afce | 144 | if (! fona.getBattVoltage(&vbat)) { |
Nels885 | 0:ea718d60afce | 145 | pcSerial.printf("Failed to read Batt\r\n"); |
Nels885 | 0:ea718d60afce | 146 | } else { |
Nels885 | 0:ea718d60afce | 147 | pcSerial.printf("VBat = %d mV\r\n", vbat); |
Nels885 | 0:ea718d60afce | 148 | } |
Nels885 | 0:ea718d60afce | 149 | |
Nels885 | 0:ea718d60afce | 150 | if (! fona.getBattPercent(&vbat)) { |
Nels885 | 0:ea718d60afce | 151 | pcSerial.printf("Failed to read Batt\r\n"); |
Nels885 | 0:ea718d60afce | 152 | } else { |
Nels885 | 0:ea718d60afce | 153 | pcSerial.printf("VPct = %d%%\r\n", vbat); |
Nels885 | 0:ea718d60afce | 154 | } |
Nels885 | 0:ea718d60afce | 155 | |
Nels885 | 0:ea718d60afce | 156 | break; |
Nels885 | 0:ea718d60afce | 157 | } |
Nels885 | 0:ea718d60afce | 158 | |
Nels885 | 0:ea718d60afce | 159 | case 'U': { |
Nels885 | 0:ea718d60afce | 160 | // Unlock the SIM with a PIN code |
Nels885 | 0:ea718d60afce | 161 | char PIN[5]; |
Nels885 | 0:ea718d60afce | 162 | flushSerial(); |
Nels885 | 0:ea718d60afce | 163 | pcSerial.printf("Enter 4-digit PIN\r\n"); |
Nels885 | 0:ea718d60afce | 164 | readline(PIN, 3); |
Nels885 | 0:ea718d60afce | 165 | pcSerial.printf("%s\r\n", PIN); |
Nels885 | 0:ea718d60afce | 166 | pcSerial.printf("Unlocking SIM card: "); |
Nels885 | 0:ea718d60afce | 167 | if (! fona.unlockSIM(PIN)) { |
Nels885 | 0:ea718d60afce | 168 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 169 | } else { |
Nels885 | 0:ea718d60afce | 170 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 171 | } |
Nels885 | 0:ea718d60afce | 172 | break; |
Nels885 | 0:ea718d60afce | 173 | } |
Nels885 | 0:ea718d60afce | 174 | |
Nels885 | 0:ea718d60afce | 175 | case 'C': { |
Nels885 | 0:ea718d60afce | 176 | // read the CCID |
Nels885 | 0:ea718d60afce | 177 | fona.getSIMCCID(replybuffer); // make sure replybuffer is at least 21 bytes! |
Nels885 | 0:ea718d60afce | 178 | pcSerial.printf("SIM CCID = %s\r\n", replybuffer); |
Nels885 | 0:ea718d60afce | 179 | break; |
Nels885 | 0:ea718d60afce | 180 | } |
Nels885 | 0:ea718d60afce | 181 | |
Nels885 | 0:ea718d60afce | 182 | case 'i': { |
Nels885 | 0:ea718d60afce | 183 | // read the RSSI |
Nels885 | 0:ea718d60afce | 184 | uint8_t n = fona.getRSSI(); |
Nels885 | 0:ea718d60afce | 185 | int8_t r = 0; |
Nels885 | 0:ea718d60afce | 186 | |
Nels885 | 0:ea718d60afce | 187 | pcSerial.printf("RSSI = %d: ", n); |
Nels885 | 0:ea718d60afce | 188 | if (n == 0) r = -115; |
Nels885 | 0:ea718d60afce | 189 | if (n == 1) r = -111; |
Nels885 | 0:ea718d60afce | 190 | if (n == 31) r = -52; |
Nels885 | 0:ea718d60afce | 191 | if ((n >= 2) && (n <= 30)) { |
Nels885 | 0:ea718d60afce | 192 | r = map(n, 2, 30, -110, -54); |
Nels885 | 0:ea718d60afce | 193 | } |
Nels885 | 0:ea718d60afce | 194 | pcSerial.printf("%d dBm\r\n", r); |
Nels885 | 0:ea718d60afce | 195 | |
Nels885 | 0:ea718d60afce | 196 | break; |
Nels885 | 0:ea718d60afce | 197 | } |
Nels885 | 0:ea718d60afce | 198 | |
Nels885 | 0:ea718d60afce | 199 | case 'n': { |
Nels885 | 0:ea718d60afce | 200 | // read the network/cellular status |
Nels885 | 0:ea718d60afce | 201 | uint8_t n = fona.getNetworkStatus(); |
Nels885 | 0:ea718d60afce | 202 | pcSerial.printf("Network status %d: ", n); |
Nels885 | 0:ea718d60afce | 203 | if (n == 0) pcSerial.printf("Not registered\r\n"); |
Nels885 | 0:ea718d60afce | 204 | if (n == 1) pcSerial.printf("Registered (home)\r\n"); |
Nels885 | 0:ea718d60afce | 205 | if (n == 2) pcSerial.printf("Not registered (searching)\r\n"); |
Nels885 | 0:ea718d60afce | 206 | if (n == 3) pcSerial.printf("Denied\r\n"); |
Nels885 | 0:ea718d60afce | 207 | if (n == 4) pcSerial.printf("Unknown\r\n"); |
Nels885 | 0:ea718d60afce | 208 | if (n == 5) pcSerial.printf("Registered roaming\r\n"); |
Nels885 | 0:ea718d60afce | 209 | break; |
Nels885 | 0:ea718d60afce | 210 | } |
Nels885 | 0:ea718d60afce | 211 | |
Nels885 | 0:ea718d60afce | 212 | /*** Audio ***/ |
Nels885 | 0:ea718d60afce | 213 | case 'v': { |
Nels885 | 0:ea718d60afce | 214 | // set volume |
Nels885 | 0:ea718d60afce | 215 | flushSerial(); |
Nels885 | 0:ea718d60afce | 216 | pcSerial.printf("Set Vol %%"); |
Nels885 | 0:ea718d60afce | 217 | uint8_t vol = readnumber(); |
Nels885 | 0:ea718d60afce | 218 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 219 | if (! fona.setVolume(vol)) { |
Nels885 | 0:ea718d60afce | 220 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 221 | } else { |
Nels885 | 0:ea718d60afce | 222 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 223 | } |
Nels885 | 0:ea718d60afce | 224 | break; |
Nels885 | 0:ea718d60afce | 225 | } |
Nels885 | 0:ea718d60afce | 226 | |
Nels885 | 0:ea718d60afce | 227 | case 'V': { |
Nels885 | 0:ea718d60afce | 228 | uint8_t v = fona.getVolume(); |
Nels885 | 0:ea718d60afce | 229 | pcSerial.printf("%d%%\r\n", v); |
Nels885 | 0:ea718d60afce | 230 | |
Nels885 | 0:ea718d60afce | 231 | break; |
Nels885 | 0:ea718d60afce | 232 | } |
Nels885 | 0:ea718d60afce | 233 | |
Nels885 | 0:ea718d60afce | 234 | case 'H': { |
Nels885 | 0:ea718d60afce | 235 | // Set Headphone output |
Nels885 | 0:ea718d60afce | 236 | if (! fona.setAudio(FONA_HEADSETAUDIO)) { |
Nels885 | 0:ea718d60afce | 237 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 238 | } else { |
Nels885 | 0:ea718d60afce | 239 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 240 | } |
Nels885 | 0:ea718d60afce | 241 | fona.setMicVolume(FONA_HEADSETAUDIO, 15); |
Nels885 | 0:ea718d60afce | 242 | break; |
Nels885 | 0:ea718d60afce | 243 | } |
Nels885 | 0:ea718d60afce | 244 | case 'e': { |
Nels885 | 0:ea718d60afce | 245 | // Set External output |
Nels885 | 0:ea718d60afce | 246 | if (! fona.setAudio(FONA_EXTAUDIO)) { |
Nels885 | 0:ea718d60afce | 247 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 248 | } else { |
Nels885 | 0:ea718d60afce | 249 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 250 | } |
Nels885 | 0:ea718d60afce | 251 | |
Nels885 | 0:ea718d60afce | 252 | fona.setMicVolume(FONA_EXTAUDIO, 10); |
Nels885 | 0:ea718d60afce | 253 | break; |
Nels885 | 0:ea718d60afce | 254 | } |
Nels885 | 0:ea718d60afce | 255 | |
Nels885 | 0:ea718d60afce | 256 | case 'T': { |
Nels885 | 0:ea718d60afce | 257 | // play tone |
Nels885 | 0:ea718d60afce | 258 | flushSerial(); |
Nels885 | 0:ea718d60afce | 259 | pcSerial.printf("Play tone #"); |
Nels885 | 0:ea718d60afce | 260 | uint8_t kittone = readnumber(); |
Nels885 | 0:ea718d60afce | 261 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 262 | // play for 1 second (1000 ms) |
Nels885 | 0:ea718d60afce | 263 | if (! fona.playToolkitTone(kittone, 1000)) { |
Nels885 | 0:ea718d60afce | 264 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 265 | } else { |
Nels885 | 0:ea718d60afce | 266 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 267 | } |
Nels885 | 0:ea718d60afce | 268 | break; |
Nels885 | 0:ea718d60afce | 269 | } |
Nels885 | 0:ea718d60afce | 270 | |
Nels885 | 0:ea718d60afce | 271 | /*** FM Radio ***/ |
Nels885 | 0:ea718d60afce | 272 | |
Nels885 | 0:ea718d60afce | 273 | case 'f': { |
Nels885 | 0:ea718d60afce | 274 | // get freq |
Nels885 | 0:ea718d60afce | 275 | flushSerial(); |
Nels885 | 0:ea718d60afce | 276 | pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): "); |
Nels885 | 0:ea718d60afce | 277 | uint16_t station = readnumber(); |
Nels885 | 0:ea718d60afce | 278 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 279 | // FM radio ON using headset |
Nels885 | 0:ea718d60afce | 280 | if (fona.FMradio(true, FONA_HEADSETAUDIO)) { |
Nels885 | 0:ea718d60afce | 281 | pcSerial.printf("Opened\r\n"); |
Nels885 | 0:ea718d60afce | 282 | } |
Nels885 | 0:ea718d60afce | 283 | if (! fona.tuneFMradio(station)) { |
Nels885 | 0:ea718d60afce | 284 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 285 | } else { |
Nels885 | 0:ea718d60afce | 286 | pcSerial.printf("Tuned\r\n"); |
Nels885 | 0:ea718d60afce | 287 | } |
Nels885 | 0:ea718d60afce | 288 | break; |
Nels885 | 0:ea718d60afce | 289 | } |
Nels885 | 0:ea718d60afce | 290 | case 'F': { |
Nels885 | 0:ea718d60afce | 291 | // FM radio off |
Nels885 | 0:ea718d60afce | 292 | if (! fona.FMradio(false)) { |
Nels885 | 0:ea718d60afce | 293 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 294 | } else { |
Nels885 | 0:ea718d60afce | 295 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 296 | } |
Nels885 | 0:ea718d60afce | 297 | break; |
Nels885 | 0:ea718d60afce | 298 | } |
Nels885 | 0:ea718d60afce | 299 | case 'm': { |
Nels885 | 0:ea718d60afce | 300 | // Set FM volume. |
Nels885 | 0:ea718d60afce | 301 | flushSerial(); |
Nels885 | 0:ea718d60afce | 302 | pcSerial.printf("Set FM Vol [0-6]:"); |
Nels885 | 0:ea718d60afce | 303 | uint8_t vol = readnumber(); |
Nels885 | 0:ea718d60afce | 304 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 305 | if (!fona.setFMVolume(vol)) { |
Nels885 | 0:ea718d60afce | 306 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 307 | } else { |
Nels885 | 0:ea718d60afce | 308 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 309 | } |
Nels885 | 0:ea718d60afce | 310 | break; |
Nels885 | 0:ea718d60afce | 311 | } |
Nels885 | 0:ea718d60afce | 312 | case 'M': { |
Nels885 | 0:ea718d60afce | 313 | // Get FM volume. |
Nels885 | 0:ea718d60afce | 314 | int8_t fmvol = fona.getFMVolume(); |
Nels885 | 0:ea718d60afce | 315 | if (fmvol < 0) { |
Nels885 | 0:ea718d60afce | 316 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 317 | } else { |
Nels885 | 0:ea718d60afce | 318 | pcSerial.printf("FM volume: %d\r\n", fmvol); |
Nels885 | 0:ea718d60afce | 319 | } |
Nels885 | 0:ea718d60afce | 320 | break; |
Nels885 | 0:ea718d60afce | 321 | } |
Nels885 | 0:ea718d60afce | 322 | case 'q': { |
Nels885 | 0:ea718d60afce | 323 | // Get FM station signal level (in decibels). |
Nels885 | 0:ea718d60afce | 324 | flushSerial(); |
Nels885 | 0:ea718d60afce | 325 | pcSerial.printf("FM Freq (eg 1011 == 101.1 MHz): "); |
Nels885 | 0:ea718d60afce | 326 | uint16_t station = readnumber(); |
Nels885 | 0:ea718d60afce | 327 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 328 | int8_t level = fona.getFMSignalLevel(station); |
Nels885 | 0:ea718d60afce | 329 | if (level < 0) { |
Nels885 | 0:ea718d60afce | 330 | pcSerial.printf("Failed! Make sure FM radio is on (tuned to station).\r\n"); |
Nels885 | 0:ea718d60afce | 331 | } else { |
Nels885 | 0:ea718d60afce | 332 | pcSerial.printf("Signal level (dB): %d\r\n", level); |
Nels885 | 0:ea718d60afce | 333 | } |
Nels885 | 0:ea718d60afce | 334 | break; |
Nels885 | 0:ea718d60afce | 335 | } |
Nels885 | 0:ea718d60afce | 336 | |
Nels885 | 0:ea718d60afce | 337 | /*** PWM ***/ |
Nels885 | 0:ea718d60afce | 338 | |
Nels885 | 0:ea718d60afce | 339 | case 'P': { |
Nels885 | 0:ea718d60afce | 340 | // PWM Buzzer output @ 2KHz max |
Nels885 | 0:ea718d60afce | 341 | flushSerial(); |
Nels885 | 0:ea718d60afce | 342 | pcSerial.printf("PWM Freq, 0 = Off, (1-2000): "); |
Nels885 | 0:ea718d60afce | 343 | uint16_t freq = readnumber(); |
Nels885 | 0:ea718d60afce | 344 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 345 | if (! fona.setPWM(freq)) { |
Nels885 | 0:ea718d60afce | 346 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 347 | } else { |
Nels885 | 0:ea718d60afce | 348 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 349 | } |
Nels885 | 0:ea718d60afce | 350 | break; |
Nels885 | 0:ea718d60afce | 351 | } |
Nels885 | 0:ea718d60afce | 352 | |
Nels885 | 0:ea718d60afce | 353 | /*** Call ***/ |
Nels885 | 0:ea718d60afce | 354 | case 'c': { |
Nels885 | 0:ea718d60afce | 355 | // call a phone! |
Nels885 | 0:ea718d60afce | 356 | char number[30]; |
Nels885 | 0:ea718d60afce | 357 | flushSerial(); |
Nels885 | 0:ea718d60afce | 358 | pcSerial.printf("Call #"); |
Nels885 | 0:ea718d60afce | 359 | readline(number, 30); |
Nels885 | 0:ea718d60afce | 360 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 361 | pcSerial.printf("Calling %s\r\n", number); |
Nels885 | 0:ea718d60afce | 362 | if (!fona.callPhone(number)) { |
Nels885 | 0:ea718d60afce | 363 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 364 | } else { |
Nels885 | 0:ea718d60afce | 365 | pcSerial.printf("Sent!\r\n"); |
Nels885 | 0:ea718d60afce | 366 | } |
Nels885 | 0:ea718d60afce | 367 | |
Nels885 | 0:ea718d60afce | 368 | break; |
Nels885 | 0:ea718d60afce | 369 | } |
Nels885 | 0:ea718d60afce | 370 | case 'h': { |
Nels885 | 0:ea718d60afce | 371 | // hang up! |
Nels885 | 0:ea718d60afce | 372 | if (! fona.hangUp()) { |
Nels885 | 0:ea718d60afce | 373 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 374 | } else { |
Nels885 | 0:ea718d60afce | 375 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 376 | } |
Nels885 | 0:ea718d60afce | 377 | break; |
Nels885 | 0:ea718d60afce | 378 | } |
Nels885 | 0:ea718d60afce | 379 | |
Nels885 | 0:ea718d60afce | 380 | case 'p': { |
Nels885 | 0:ea718d60afce | 381 | // pick up! |
Nels885 | 0:ea718d60afce | 382 | if (! fona.pickUp()) { |
Nels885 | 0:ea718d60afce | 383 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 384 | } else { |
Nels885 | 0:ea718d60afce | 385 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 386 | } |
Nels885 | 0:ea718d60afce | 387 | break; |
Nels885 | 0:ea718d60afce | 388 | } |
Nels885 | 0:ea718d60afce | 389 | |
Nels885 | 0:ea718d60afce | 390 | /*** SMS ***/ |
Nels885 | 0:ea718d60afce | 391 | |
Nels885 | 0:ea718d60afce | 392 | case 'N': { |
Nels885 | 0:ea718d60afce | 393 | // read the number of SMS's! |
Nels885 | 0:ea718d60afce | 394 | int8_t smsnum = fona.getNumSMS(); |
Nels885 | 0:ea718d60afce | 395 | if (smsnum < 0) { |
Nels885 | 0:ea718d60afce | 396 | pcSerial.printf("Could not read # SMS\r\n"); |
Nels885 | 0:ea718d60afce | 397 | } else { |
Nels885 | 0:ea718d60afce | 398 | pcSerial.printf("%d SMS's on SIM card!\r\n", smsnum); |
Nels885 | 0:ea718d60afce | 399 | } |
Nels885 | 0:ea718d60afce | 400 | break; |
Nels885 | 0:ea718d60afce | 401 | } |
Nels885 | 0:ea718d60afce | 402 | case 'r': { |
Nels885 | 0:ea718d60afce | 403 | // read an SMS |
Nels885 | 0:ea718d60afce | 404 | flushSerial(); |
Nels885 | 0:ea718d60afce | 405 | pcSerial.printf("Read #"); |
Nels885 | 0:ea718d60afce | 406 | uint8_t smsn = readnumber(); |
Nels885 | 0:ea718d60afce | 407 | pcSerial.printf("\r\nReading SMS #%d\r\n", smsn); |
Nels885 | 0:ea718d60afce | 408 | |
Nels885 | 0:ea718d60afce | 409 | // Retrieve SMS sender address/phone number. |
Nels885 | 0:ea718d60afce | 410 | if (! fona.getSMSSender(smsn, replybuffer, 250)) { |
Nels885 | 0:ea718d60afce | 411 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 412 | break; |
Nels885 | 0:ea718d60afce | 413 | } |
Nels885 | 0:ea718d60afce | 414 | pcSerial.printf("FROM: %s\r\n", replybuffer); |
Nels885 | 0:ea718d60afce | 415 | |
Nels885 | 0:ea718d60afce | 416 | // Retrieve SMS value. |
Nels885 | 0:ea718d60afce | 417 | uint16_t smslen; |
Nels885 | 0:ea718d60afce | 418 | if (! fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len! |
Nels885 | 0:ea718d60afce | 419 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 420 | break; |
Nels885 | 0:ea718d60afce | 421 | } |
Nels885 | 0:ea718d60afce | 422 | pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); |
Nels885 | 0:ea718d60afce | 423 | pcSerial.printf("%s\r\n", replybuffer); |
Nels885 | 0:ea718d60afce | 424 | pcSerial.printf("*****\r\n"); |
Nels885 | 0:ea718d60afce | 425 | |
Nels885 | 0:ea718d60afce | 426 | break; |
Nels885 | 0:ea718d60afce | 427 | } |
Nels885 | 0:ea718d60afce | 428 | case 'R': { |
Nels885 | 0:ea718d60afce | 429 | // read all SMS |
Nels885 | 0:ea718d60afce | 430 | int8_t smsnum = fona.getNumSMS(); |
Nels885 | 0:ea718d60afce | 431 | uint16_t smslen; |
Nels885 | 0:ea718d60afce | 432 | for (int8_t smsn=1; smsn<=smsnum; smsn++) { |
Nels885 | 0:ea718d60afce | 433 | pcSerial.printf("\r\nReading SMS #%d\r\n", smsn); |
Nels885 | 0:ea718d60afce | 434 | if (!fona.readSMS(smsn, replybuffer, 250, &smslen)) { // pass in buffer and max len! |
Nels885 | 0:ea718d60afce | 435 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 436 | break; |
Nels885 | 0:ea718d60afce | 437 | } |
Nels885 | 0:ea718d60afce | 438 | // if the length is zero, its a special case where the index number is higher |
Nels885 | 0:ea718d60afce | 439 | // so increase the max we'll look at! |
Nels885 | 0:ea718d60afce | 440 | if (smslen == 0) { |
Nels885 | 0:ea718d60afce | 441 | pcSerial.printf("[empty slot]\r\n"); |
Nels885 | 0:ea718d60afce | 442 | smsnum++; |
Nels885 | 0:ea718d60afce | 443 | continue; |
Nels885 | 0:ea718d60afce | 444 | } |
Nels885 | 0:ea718d60afce | 445 | |
Nels885 | 0:ea718d60afce | 446 | pcSerial.printf("***** SMS #%d (%d) bytes *****\r\n", smsn, smslen); |
Nels885 | 0:ea718d60afce | 447 | pcSerial.printf("%s\r\n", replybuffer); |
Nels885 | 0:ea718d60afce | 448 | pcSerial.printf("*****\r\n"); |
Nels885 | 0:ea718d60afce | 449 | } |
Nels885 | 0:ea718d60afce | 450 | break; |
Nels885 | 0:ea718d60afce | 451 | } |
Nels885 | 0:ea718d60afce | 452 | |
Nels885 | 0:ea718d60afce | 453 | case 'd': { |
Nels885 | 0:ea718d60afce | 454 | // delete an SMS |
Nels885 | 0:ea718d60afce | 455 | flushSerial(); |
Nels885 | 0:ea718d60afce | 456 | pcSerial.printf("Delete #"); |
Nels885 | 0:ea718d60afce | 457 | uint8_t smsn = readnumber(); |
Nels885 | 0:ea718d60afce | 458 | |
Nels885 | 0:ea718d60afce | 459 | pcSerial.printf("\r\nDeleting SMS #%d\r\n", smsn); |
Nels885 | 0:ea718d60afce | 460 | if (fona.deleteSMS(smsn)) { |
Nels885 | 0:ea718d60afce | 461 | pcSerial.printf("OK!\r\n"); |
Nels885 | 0:ea718d60afce | 462 | } else { |
Nels885 | 0:ea718d60afce | 463 | pcSerial.printf("Couldn't delete\r\n"); |
Nels885 | 0:ea718d60afce | 464 | } |
Nels885 | 0:ea718d60afce | 465 | break; |
Nels885 | 0:ea718d60afce | 466 | } |
Nels885 | 0:ea718d60afce | 467 | |
Nels885 | 0:ea718d60afce | 468 | case 's': { |
Nels885 | 0:ea718d60afce | 469 | // send an SMS! |
Nels885 | 0:ea718d60afce | 470 | char sendto[21], message[141]; |
Nels885 | 0:ea718d60afce | 471 | flushSerial(); |
Nels885 | 0:ea718d60afce | 472 | pcSerial.printf("Send to #"); |
Nels885 | 0:ea718d60afce | 473 | readline(sendto, 11); |
Nels885 | 0:ea718d60afce | 474 | pcSerial.printf("%s\r\n", sendto); |
Nels885 | 0:ea718d60afce | 475 | pcSerial.printf("Type out one-line message (140 char): "); |
Nels885 | 0:ea718d60afce | 476 | readline(message, 10); |
Nels885 | 0:ea718d60afce | 477 | pcSerial.printf("%s\r\n", message); |
Nels885 | 0:ea718d60afce | 478 | if (!fona.sendSMS(sendto, message)) { |
Nels885 | 0:ea718d60afce | 479 | pcSerial.printf("Failed\r\n"); |
Nels885 | 0:ea718d60afce | 480 | } else { |
Nels885 | 0:ea718d60afce | 481 | pcSerial.printf("Sent!\r\n"); |
Nels885 | 0:ea718d60afce | 482 | } |
Nels885 | 0:ea718d60afce | 483 | |
Nels885 | 0:ea718d60afce | 484 | break; |
Nels885 | 0:ea718d60afce | 485 | } |
Nels885 | 0:ea718d60afce | 486 | |
Nels885 | 0:ea718d60afce | 487 | /*** Time ***/ |
Nels885 | 0:ea718d60afce | 488 | |
Nels885 | 0:ea718d60afce | 489 | case 'y': { |
Nels885 | 0:ea718d60afce | 490 | // enable network time sync |
Nels885 | 0:ea718d60afce | 491 | if (!fona.enableNetworkTimeSync(true)) |
Nels885 | 0:ea718d60afce | 492 | pcSerial.printf("Failed to enable\r\n"); |
Nels885 | 0:ea718d60afce | 493 | break; |
Nels885 | 0:ea718d60afce | 494 | } |
Nels885 | 0:ea718d60afce | 495 | |
Nels885 | 0:ea718d60afce | 496 | case 'Y': { |
Nels885 | 0:ea718d60afce | 497 | // enable NTP time sync |
Nels885 | 0:ea718d60afce | 498 | if (!fona.enableNTPTimeSync(true, "pool.ntp.org")) |
Nels885 | 0:ea718d60afce | 499 | pcSerial.printf("Failed to enable\r\n"); |
Nels885 | 0:ea718d60afce | 500 | break; |
Nels885 | 0:ea718d60afce | 501 | } |
Nels885 | 0:ea718d60afce | 502 | |
Nels885 | 0:ea718d60afce | 503 | case 't': { |
Nels885 | 0:ea718d60afce | 504 | // read the time |
Nels885 | 0:ea718d60afce | 505 | char buffer[23]; |
Nels885 | 0:ea718d60afce | 506 | |
Nels885 | 0:ea718d60afce | 507 | fona.getTime(buffer, 23); // make sure replybuffer is at least 23 bytes! |
Nels885 | 0:ea718d60afce | 508 | pcSerial.printf("Time = %s\r\n", buffer); |
Nels885 | 0:ea718d60afce | 509 | break; |
Nels885 | 0:ea718d60afce | 510 | } |
Nels885 | 0:ea718d60afce | 511 | |
Nels885 | 0:ea718d60afce | 512 | /*********************************** GPS (SIM808 only) */ |
Nels885 | 0:ea718d60afce | 513 | |
Nels885 | 0:ea718d60afce | 514 | case 'o': { |
Nels885 | 0:ea718d60afce | 515 | // turn GPS off |
Nels885 | 0:ea718d60afce | 516 | if (!fona.enableGPS(false)) |
Nels885 | 0:ea718d60afce | 517 | pcSerial.printf("Failed to turn off\r\n"); |
Nels885 | 0:ea718d60afce | 518 | break; |
Nels885 | 0:ea718d60afce | 519 | } |
Nels885 | 0:ea718d60afce | 520 | case 'O': { |
Nels885 | 0:ea718d60afce | 521 | // turn GPS on |
Nels885 | 0:ea718d60afce | 522 | if (!fona.enableGPS(true)) |
Nels885 | 0:ea718d60afce | 523 | pcSerial.printf("Failed to turn on\r\n"); |
Nels885 | 0:ea718d60afce | 524 | break; |
Nels885 | 0:ea718d60afce | 525 | } |
Nels885 | 0:ea718d60afce | 526 | case 'x': { |
Nels885 | 0:ea718d60afce | 527 | int8_t stat; |
Nels885 | 0:ea718d60afce | 528 | // check GPS fix |
Nels885 | 0:ea718d60afce | 529 | stat = fona.GPSstatus(); |
Nels885 | 0:ea718d60afce | 530 | if (stat < 0) |
Nels885 | 0:ea718d60afce | 531 | pcSerial.printf("Failed to query\r\n"); |
Nels885 | 0:ea718d60afce | 532 | if (stat == 0) pcSerial.printf("GPS off\r\n"); |
Nels885 | 0:ea718d60afce | 533 | if (stat == 1) pcSerial.printf("No fix\r\n"); |
Nels885 | 0:ea718d60afce | 534 | if (stat == 2) pcSerial.printf("2D fix\r\n"); |
Nels885 | 0:ea718d60afce | 535 | if (stat == 3) pcSerial.printf("3D fix\r\n"); |
Nels885 | 0:ea718d60afce | 536 | break; |
Nels885 | 0:ea718d60afce | 537 | } |
Nels885 | 0:ea718d60afce | 538 | |
Nels885 | 0:ea718d60afce | 539 | case 'L': { |
Nels885 | 0:ea718d60afce | 540 | // check for GPS location |
Nels885 | 0:ea718d60afce | 541 | char gpsdata[80]; |
Nels885 | 0:ea718d60afce | 542 | fona.getGPS(0, gpsdata, 80); |
Nels885 | 0:ea718d60afce | 543 | pcSerial.printf("Reply in format: mode,longitude,latitude,altitude,utctime(yyyymmddHHMMSS),ttff,satellites,speed,course\r\n"); |
Nels885 | 0:ea718d60afce | 544 | pcSerial.printf("%s\r\n", gpsdata); |
Nels885 | 0:ea718d60afce | 545 | |
Nels885 | 0:ea718d60afce | 546 | break; |
Nels885 | 0:ea718d60afce | 547 | } |
Nels885 | 0:ea718d60afce | 548 | |
Nels885 | 0:ea718d60afce | 549 | case 'E': { |
Nels885 | 0:ea718d60afce | 550 | flushSerial(); |
Nels885 | 0:ea718d60afce | 551 | pcSerial.printf("GPS NMEA output sentences (0 = off, 34 = RMC+GGA, 255 = all)\r\n"); |
Nels885 | 0:ea718d60afce | 552 | uint8_t nmeaout = readnumber(); |
Nels885 | 0:ea718d60afce | 553 | |
Nels885 | 0:ea718d60afce | 554 | // turn on NMEA output |
Nels885 | 0:ea718d60afce | 555 | fona.enableGPSNMEA(nmeaout); |
Nels885 | 0:ea718d60afce | 556 | |
Nels885 | 0:ea718d60afce | 557 | break; |
Nels885 | 0:ea718d60afce | 558 | } |
Nels885 | 0:ea718d60afce | 559 | |
Nels885 | 0:ea718d60afce | 560 | /*********************************** GPRS */ |
Nels885 | 0:ea718d60afce | 561 | |
Nels885 | 0:ea718d60afce | 562 | case 'g': { |
Nels885 | 0:ea718d60afce | 563 | // turn GPRS off |
Nels885 | 0:ea718d60afce | 564 | if (!fona.enableGPRS(false)) |
Nels885 | 0:ea718d60afce | 565 | pcSerial.printf("Failed to turn off\r\n"); |
Nels885 | 0:ea718d60afce | 566 | break; |
Nels885 | 0:ea718d60afce | 567 | } |
Nels885 | 0:ea718d60afce | 568 | case 'G': { |
Nels885 | 0:ea718d60afce | 569 | // turn GPRS on |
Nels885 | 0:ea718d60afce | 570 | if (!fona.enableGPRS(true)) |
Nels885 | 0:ea718d60afce | 571 | pcSerial.printf("Failed to turn on\r\n"); |
Nels885 | 0:ea718d60afce | 572 | break; |
Nels885 | 0:ea718d60afce | 573 | } |
Nels885 | 0:ea718d60afce | 574 | case 'l': { |
Nels885 | 0:ea718d60afce | 575 | // check for GSMLOC (requires GPRS) |
Nels885 | 0:ea718d60afce | 576 | uint16_t returncode; |
Nels885 | 0:ea718d60afce | 577 | |
Nels885 | 0:ea718d60afce | 578 | if (!fona.getGSMLoc(&returncode, replybuffer, 250)) |
Nels885 | 0:ea718d60afce | 579 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 580 | if (returncode == 0) { |
Nels885 | 0:ea718d60afce | 581 | pcSerial.printf("%s\r\n", replybuffer); |
Nels885 | 0:ea718d60afce | 582 | } else { |
Nels885 | 0:ea718d60afce | 583 | pcSerial.printf("Fail code #%d\r\n", returncode); |
Nels885 | 0:ea718d60afce | 584 | } |
Nels885 | 0:ea718d60afce | 585 | |
Nels885 | 0:ea718d60afce | 586 | break; |
Nels885 | 0:ea718d60afce | 587 | } |
Nels885 | 0:ea718d60afce | 588 | case 'w': { |
Nels885 | 0:ea718d60afce | 589 | // read website URL |
Nels885 | 0:ea718d60afce | 590 | uint16_t statuscode; |
Nels885 | 0:ea718d60afce | 591 | int16_t length; |
Nels885 | 0:ea718d60afce | 592 | char url[80]; |
Nels885 | 0:ea718d60afce | 593 | |
Nels885 | 0:ea718d60afce | 594 | flushSerial(); |
Nels885 | 0:ea718d60afce | 595 | pcSerial.printf("NOTE: in beta! Use small webpages to read!\r\n"); |
Nels885 | 0:ea718d60afce | 596 | pcSerial.printf("URL to read (e.g. www.adafruit.com/testwifi/index.html):\r\n"); |
Nels885 | 0:ea718d60afce | 597 | pcSerial.printf("http://"); |
Nels885 | 0:ea718d60afce | 598 | readline(url, 79); |
Nels885 | 0:ea718d60afce | 599 | pcSerial.printf("%s\r\n", url); |
Nels885 | 0:ea718d60afce | 600 | |
Nels885 | 0:ea718d60afce | 601 | pcSerial.printf("****\r\n"); |
Nels885 | 0:ea718d60afce | 602 | if (!fona.HTTP_GET_start(url, &statuscode, (uint16_t *)&length)) { |
Nels885 | 0:ea718d60afce | 603 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 604 | break; |
Nels885 | 0:ea718d60afce | 605 | } |
Nels885 | 0:ea718d60afce | 606 | while (length > 0) { |
Nels885 | 0:ea718d60afce | 607 | while (fona.readable()) { |
Nels885 | 0:ea718d60afce | 608 | char c = fona.getc(); |
Nels885 | 0:ea718d60afce | 609 | pcSerial.putc(c); |
Nels885 | 0:ea718d60afce | 610 | length--; |
Nels885 | 0:ea718d60afce | 611 | if (! length) break; |
Nels885 | 0:ea718d60afce | 612 | } |
Nels885 | 0:ea718d60afce | 613 | } |
Nels885 | 0:ea718d60afce | 614 | pcSerial.printf("\r\n****\r\n"); |
Nels885 | 0:ea718d60afce | 615 | fona.HTTP_GET_end(); |
Nels885 | 0:ea718d60afce | 616 | break; |
Nels885 | 0:ea718d60afce | 617 | } |
Nels885 | 0:ea718d60afce | 618 | |
Nels885 | 0:ea718d60afce | 619 | case 'W': { |
Nels885 | 0:ea718d60afce | 620 | // Post data to website |
Nels885 | 0:ea718d60afce | 621 | uint16_t statuscode; |
Nels885 | 0:ea718d60afce | 622 | int16_t length; |
Nels885 | 0:ea718d60afce | 623 | char url[80]; |
Nels885 | 0:ea718d60afce | 624 | char data[80]; |
Nels885 | 0:ea718d60afce | 625 | |
Nels885 | 0:ea718d60afce | 626 | flushSerial(); |
Nels885 | 0:ea718d60afce | 627 | pcSerial.printf("NOTE: in beta! Use simple websites to post!\r\n"); |
Nels885 | 0:ea718d60afce | 628 | pcSerial.printf("URL to post (e.g. httpbin.org/post):\r\n"); |
Nels885 | 0:ea718d60afce | 629 | pcSerial.printf("http://"); |
Nels885 | 0:ea718d60afce | 630 | readline(url, 79); |
Nels885 | 0:ea718d60afce | 631 | pcSerial.printf("%s\r\n", url); |
Nels885 | 0:ea718d60afce | 632 | pcSerial.printf("Data to post (e.g. \"foo\" or \"{\"simple\":\"json\"}\"):\r\n"); |
Nels885 | 0:ea718d60afce | 633 | readline(data, 79); |
Nels885 | 0:ea718d60afce | 634 | pcSerial.printf("%s\r\n", data); |
Nels885 | 0:ea718d60afce | 635 | |
Nels885 | 0:ea718d60afce | 636 | pcSerial.printf("****\r\n"); |
Nels885 | 0:ea718d60afce | 637 | if (!fona.HTTP_POST_start(url, "text/plain", (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) { |
Nels885 | 0:ea718d60afce | 638 | pcSerial.printf("Failed!\r\n"); |
Nels885 | 0:ea718d60afce | 639 | break; |
Nels885 | 0:ea718d60afce | 640 | } |
Nels885 | 0:ea718d60afce | 641 | while (length > 0) { |
Nels885 | 0:ea718d60afce | 642 | while (fona.readable()) { |
Nels885 | 0:ea718d60afce | 643 | char c = fona.getc(); |
Nels885 | 0:ea718d60afce | 644 | pcSerial.putc(c); |
Nels885 | 0:ea718d60afce | 645 | length--; |
Nels885 | 0:ea718d60afce | 646 | if (! length) break; |
Nels885 | 0:ea718d60afce | 647 | } |
Nels885 | 0:ea718d60afce | 648 | } |
Nels885 | 0:ea718d60afce | 649 | pcSerial.printf("\r\n****\r\n"); |
Nels885 | 0:ea718d60afce | 650 | fona.HTTP_POST_end(); |
Nels885 | 0:ea718d60afce | 651 | break; |
Nels885 | 0:ea718d60afce | 652 | } |
Nels885 | 0:ea718d60afce | 653 | /*****************************************/ |
Nels885 | 0:ea718d60afce | 654 | |
Nels885 | 0:ea718d60afce | 655 | case 'S': { |
Nels885 | 0:ea718d60afce | 656 | pcSerial.printf("Creating SERIAL TUBE\r\n"); |
Nels885 | 0:ea718d60afce | 657 | while (1) { |
Nels885 | 0:ea718d60afce | 658 | while (pcSerial.readable()) { |
Nels885 | 0:ea718d60afce | 659 | wait_ms(1); |
Nels885 | 0:ea718d60afce | 660 | fona.putc(pcSerial.getc()); |
Nels885 | 0:ea718d60afce | 661 | } |
Nels885 | 0:ea718d60afce | 662 | if (fona.readable()) { |
Nels885 | 0:ea718d60afce | 663 | pcSerial.putc(fona.getc()); |
Nels885 | 0:ea718d60afce | 664 | } |
Nels885 | 0:ea718d60afce | 665 | } |
Nels885 | 0:ea718d60afce | 666 | } |
Nels885 | 0:ea718d60afce | 667 | |
Nels885 | 0:ea718d60afce | 668 | default: { |
Nels885 | 0:ea718d60afce | 669 | pcSerial.printf("Unknown command\r\n"); |
Nels885 | 0:ea718d60afce | 670 | break; |
Nels885 | 0:ea718d60afce | 671 | } |
Nels885 | 0:ea718d60afce | 672 | } |
Nels885 | 0:ea718d60afce | 673 | // flush input |
Nels885 | 0:ea718d60afce | 674 | flushSerial(); |
Nels885 | 0:ea718d60afce | 675 | while (fona.readable()) { |
Nels885 | 0:ea718d60afce | 676 | pcSerial.putc(fona.getc()); |
Nels885 | 0:ea718d60afce | 677 | } |
Nels885 | 0:ea718d60afce | 678 | } |
Nels885 | 0:ea718d60afce | 679 | |
Nels885 | 0:ea718d60afce | 680 | } |
Nels885 | 0:ea718d60afce | 681 | |
Nels885 | 0:ea718d60afce | 682 | |
Nels885 | 0:ea718d60afce | 683 | void printMenu(void) |
Nels885 | 0:ea718d60afce | 684 | { |
Nels885 | 0:ea718d60afce | 685 | pcSerial.printf("-------------------------------------\r\n"); |
Nels885 | 0:ea718d60afce | 686 | pcSerial.printf("[?] Print this menu\r\n"); |
Nels885 | 0:ea718d60afce | 687 | pcSerial.printf("[a] read the ADC (2.8V max)\r\n"); |
Nels885 | 0:ea718d60afce | 688 | pcSerial.printf("[b] read the Battery V and %% charged\r\n"); |
Nels885 | 0:ea718d60afce | 689 | pcSerial.printf("[C] read the SIM CCID\r\n"); |
Nels885 | 0:ea718d60afce | 690 | pcSerial.printf("[U] Unlock SIM with PIN code\r\n"); |
Nels885 | 0:ea718d60afce | 691 | pcSerial.printf("[i] read RSSI\r\n"); |
Nels885 | 0:ea718d60afce | 692 | pcSerial.printf("[n] get Network status\r\n"); |
Nels885 | 0:ea718d60afce | 693 | pcSerial.printf("[v] set audio Volume\r\n"); |
Nels885 | 0:ea718d60afce | 694 | pcSerial.printf("[V] get Volume\r\n"); |
Nels885 | 0:ea718d60afce | 695 | pcSerial.printf("[H] set Headphone audio\r\n"); |
Nels885 | 0:ea718d60afce | 696 | pcSerial.printf("[e] set External audio\r\n"); |
Nels885 | 0:ea718d60afce | 697 | pcSerial.printf("[T] play audio Tone\r\n"); |
Nels885 | 0:ea718d60afce | 698 | pcSerial.printf("[P] PWM/Buzzer out\r\n"); |
Nels885 | 0:ea718d60afce | 699 | |
Nels885 | 0:ea718d60afce | 700 | // FM (SIM800 only) |
Nels885 | 0:ea718d60afce | 701 | pcSerial.printf("[f] tune FM radio\r\n"); |
Nels885 | 0:ea718d60afce | 702 | pcSerial.printf("[F] turn off FM\r\n"); |
Nels885 | 0:ea718d60afce | 703 | pcSerial.printf("[m] set FM volume\r\n"); |
Nels885 | 0:ea718d60afce | 704 | pcSerial.printf("[M] get FM volume\r\n"); |
Nels885 | 0:ea718d60afce | 705 | pcSerial.printf("[q] get FM station signal level\r\n"); |
Nels885 | 0:ea718d60afce | 706 | |
Nels885 | 0:ea718d60afce | 707 | // Phone |
Nels885 | 0:ea718d60afce | 708 | pcSerial.printf("[c] make phone Call\r\n"); |
Nels885 | 0:ea718d60afce | 709 | pcSerial.printf("[h] Hang up phone\r\n"); |
Nels885 | 0:ea718d60afce | 710 | pcSerial.printf("[p] Pick up phone\r\n"); |
Nels885 | 0:ea718d60afce | 711 | |
Nels885 | 0:ea718d60afce | 712 | // SMS |
Nels885 | 0:ea718d60afce | 713 | pcSerial.printf("[N] Number of SMSs\r\n"); |
Nels885 | 0:ea718d60afce | 714 | pcSerial.printf("[r] Read SMS #\r\n"); |
Nels885 | 0:ea718d60afce | 715 | pcSerial.printf("[R] Read All SMS\r\n"); |
Nels885 | 0:ea718d60afce | 716 | pcSerial.printf("[d] Delete SMS #\r\n"); |
Nels885 | 0:ea718d60afce | 717 | pcSerial.printf("[s] Send SMS\r\n"); |
Nels885 | 0:ea718d60afce | 718 | |
Nels885 | 0:ea718d60afce | 719 | // Time |
Nels885 | 0:ea718d60afce | 720 | pcSerial.printf("[y] Enable network time sync\r\n"); |
Nels885 | 0:ea718d60afce | 721 | pcSerial.printf("[Y] Enable NTP time sync (GPRS)\r\n"); |
Nels885 | 0:ea718d60afce | 722 | pcSerial.printf("[t] Get network time\r\n"); |
Nels885 | 0:ea718d60afce | 723 | |
Nels885 | 0:ea718d60afce | 724 | // GPRS |
Nels885 | 0:ea718d60afce | 725 | pcSerial.printf("[G] Enable GPRS\r\n"); |
Nels885 | 0:ea718d60afce | 726 | pcSerial.printf("[g] Disable GPRS\r\n"); |
Nels885 | 0:ea718d60afce | 727 | pcSerial.printf("[l] Query GSMLOC (GPRS)\r\n"); |
Nels885 | 0:ea718d60afce | 728 | pcSerial.printf("[w] Read webpage (GPRS)\r\n"); |
Nels885 | 0:ea718d60afce | 729 | pcSerial.printf("[W] Post to website (GPRS)\r\n"); |
Nels885 | 0:ea718d60afce | 730 | |
Nels885 | 0:ea718d60afce | 731 | // GPS |
Nels885 | 0:ea718d60afce | 732 | pcSerial.printf("[O] Turn GPS on (SIM808)\r\n"); |
Nels885 | 0:ea718d60afce | 733 | pcSerial.printf("[o] Turn GPS off (SIM808)\r\n"); |
Nels885 | 0:ea718d60afce | 734 | pcSerial.printf("[x] GPS fix status (SIM808)\r\n"); |
Nels885 | 0:ea718d60afce | 735 | pcSerial.printf("[L] Query GPS location (SIM808)\r\n"); |
Nels885 | 0:ea718d60afce | 736 | pcSerial.printf("[E] Raw NMEA out (SIM808)\r\n"); |
Nels885 | 0:ea718d60afce | 737 | |
Nels885 | 0:ea718d60afce | 738 | pcSerial.printf("[S] create Serial passthru tunnel\r\n"); |
Nels885 | 0:ea718d60afce | 739 | pcSerial.printf("-------------------------------------\r\n"); |
Nels885 | 0:ea718d60afce | 740 | pcSerial.printf("\r\n"); |
Nels885 | 0:ea718d60afce | 741 | } |
Nels885 | 0:ea718d60afce | 742 | |
Nels885 | 0:ea718d60afce | 743 | void flushSerial() |
Nels885 | 0:ea718d60afce | 744 | { |
Nels885 | 0:ea718d60afce | 745 | while (pcSerial.readable()) |
Nels885 | 0:ea718d60afce | 746 | pcSerial.getc(); |
Nels885 | 0:ea718d60afce | 747 | } |
Nels885 | 0:ea718d60afce | 748 | |
Nels885 | 0:ea718d60afce | 749 | char readBlocking() |
Nels885 | 0:ea718d60afce | 750 | { |
Nels885 | 0:ea718d60afce | 751 | while (!pcSerial.readable()); |
Nels885 | 0:ea718d60afce | 752 | return pcSerial.getc(); |
Nels885 | 0:ea718d60afce | 753 | } |
Nels885 | 0:ea718d60afce | 754 | |
Nels885 | 0:ea718d60afce | 755 | uint16_t readnumber() |
Nels885 | 0:ea718d60afce | 756 | { |
Nels885 | 0:ea718d60afce | 757 | uint16_t x = 0; |
Nels885 | 0:ea718d60afce | 758 | char c; |
Nels885 | 0:ea718d60afce | 759 | while (! isdigit(c = readBlocking())) { |
Nels885 | 0:ea718d60afce | 760 | //pcSerial.putc(c); |
Nels885 | 0:ea718d60afce | 761 | } |
Nels885 | 0:ea718d60afce | 762 | pcSerial.putc(c); |
Nels885 | 0:ea718d60afce | 763 | x = c - '0'; |
Nels885 | 0:ea718d60afce | 764 | while (isdigit(c = readBlocking())) { |
Nels885 | 0:ea718d60afce | 765 | pcSerial.putc(c); |
Nels885 | 0:ea718d60afce | 766 | x *= 10; |
Nels885 | 0:ea718d60afce | 767 | x += c - '0'; |
Nels885 | 0:ea718d60afce | 768 | } |
Nels885 | 0:ea718d60afce | 769 | return x; |
Nels885 | 0:ea718d60afce | 770 | } |
Nels885 | 0:ea718d60afce | 771 | |
Nels885 | 0:ea718d60afce | 772 | uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) |
Nels885 | 0:ea718d60afce | 773 | { |
Nels885 | 0:ea718d60afce | 774 | uint16_t buffidx = 0; |
Nels885 | 0:ea718d60afce | 775 | bool timeoutvalid = true; |
Nels885 | 0:ea718d60afce | 776 | if (timeout == 0) timeoutvalid = false; |
Nels885 | 0:ea718d60afce | 777 | |
Nels885 | 0:ea718d60afce | 778 | while (true) { |
Nels885 | 0:ea718d60afce | 779 | if (buffidx > maxbuff) { |
Nels885 | 0:ea718d60afce | 780 | //pcSerial.printf("SPACE\r\n"); |
Nels885 | 0:ea718d60afce | 781 | break; |
Nels885 | 0:ea718d60afce | 782 | } |
Nels885 | 0:ea718d60afce | 783 | |
Nels885 | 0:ea718d60afce | 784 | while(pcSerial.readable()) { |
Nels885 | 0:ea718d60afce | 785 | char c = pcSerial.getc(); |
Nels885 | 0:ea718d60afce | 786 | |
Nels885 | 0:ea718d60afce | 787 | //pcSerial.printf("%02x#%c\r\n", c, c); |
Nels885 | 0:ea718d60afce | 788 | |
Nels885 | 0:ea718d60afce | 789 | if (c == '\r') continue; |
Nels885 | 0:ea718d60afce | 790 | if (c == 0xA) { |
Nels885 | 0:ea718d60afce | 791 | if (buffidx == 0) // the first 0x0A is ignored |
Nels885 | 0:ea718d60afce | 792 | continue; |
Nels885 | 0:ea718d60afce | 793 | |
Nels885 | 0:ea718d60afce | 794 | timeout = 0; // the second 0x0A is the end of the line |
Nels885 | 0:ea718d60afce | 795 | timeoutvalid = true; |
Nels885 | 0:ea718d60afce | 796 | break; |
Nels885 | 0:ea718d60afce | 797 | } |
Nels885 | 0:ea718d60afce | 798 | buff[buffidx] = c; |
Nels885 | 0:ea718d60afce | 799 | buffidx++; |
Nels885 | 0:ea718d60afce | 800 | } |
Nels885 | 0:ea718d60afce | 801 | |
Nels885 | 0:ea718d60afce | 802 | if (timeoutvalid && timeout == 0) { |
Nels885 | 0:ea718d60afce | 803 | //pcSerial.printf("TIMEOUT\r\n"); |
Nels885 | 0:ea718d60afce | 804 | break; |
Nels885 | 0:ea718d60afce | 805 | } |
Nels885 | 0:ea718d60afce | 806 | wait_ms(1); |
Nels885 | 0:ea718d60afce | 807 | } |
Nels885 | 0:ea718d60afce | 808 | buff[buffidx] = 0; // null term |
Nels885 | 0:ea718d60afce | 809 | return buffidx; |
Nels885 | 0:ea718d60afce | 810 | } |
Nels885 | 0:ea718d60afce | 811 | |
Nels885 | 0:ea718d60afce | 812 | long map(long x, long in_min, long in_max, long out_min, long out_max) |
Nels885 | 0:ea718d60afce | 813 | { |
Nels885 | 0:ea718d60afce | 814 | return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; |
Nels885 | 0:ea718d60afce | 815 | } |