Based on https://developer.mbed.org/users/jbaker66/notebook/adafruit-fona-800-minigsm/

Dependents:   Smart_Car_Seat_v1

Committer:
jd0205
Date:
Wed May 03 21:05:54 2017 +0000
Revision:
0:58ff0234d7bb
personal changes;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jd0205 0:58ff0234d7bb 1 /***************************************************
jd0205 0:58ff0234d7bb 2 This is a library for our Adafruit FONA Cellular Module
jd0205 0:58ff0234d7bb 3
jd0205 0:58ff0234d7bb 4 Designed specifically to work with the Adafruit FONA
jd0205 0:58ff0234d7bb 5 ----> http://www.adafruit.com/products/1946
jd0205 0:58ff0234d7bb 6 ----> http://www.adafruit.com/products/1963
jd0205 0:58ff0234d7bb 7
jd0205 0:58ff0234d7bb 8 These displays use TTL Serial to communicate, 2 pins are required to
jd0205 0:58ff0234d7bb 9 interface
jd0205 0:58ff0234d7bb 10 Adafruit invests time and resources providing this open source code,
jd0205 0:58ff0234d7bb 11 please support Adafruit and open-source hardware by purchasing
jd0205 0:58ff0234d7bb 12 products from Adafruit!
jd0205 0:58ff0234d7bb 13
jd0205 0:58ff0234d7bb 14 Written by Limor Fried/Ladyada for Adafruit Industries.
jd0205 0:58ff0234d7bb 15 BSD license, all text above must be included in any redistribution
jd0205 0:58ff0234d7bb 16 ****************************************************/
jd0205 0:58ff0234d7bb 17
jd0205 0:58ff0234d7bb 18 /*
jd0205 0:58ff0234d7bb 19 * Modified by George Tzintzarov & Jesse Baker 03/14/2016 for use in mbed LPC1768
jd0205 0:58ff0234d7bb 20 */
jd0205 0:58ff0234d7bb 21
jd0205 0:58ff0234d7bb 22 #ifndef ADAFRUIT_FONA_H
jd0205 0:58ff0234d7bb 23 #define ADAFRUIT_FONA_H
jd0205 0:58ff0234d7bb 24
jd0205 0:58ff0234d7bb 25 #include "mbed.h"
jd0205 0:58ff0234d7bb 26
jd0205 0:58ff0234d7bb 27 //#define ADAFRUIT_FONA_DEBUG
jd0205 0:58ff0234d7bb 28
jd0205 0:58ff0234d7bb 29 #define FONA_HEADSETAUDIO 0
jd0205 0:58ff0234d7bb 30 #define FONA_EXTAUDIO 1
jd0205 0:58ff0234d7bb 31
jd0205 0:58ff0234d7bb 32 #define FONA_STTONE_DIALTONE 1
jd0205 0:58ff0234d7bb 33 #define FONA_STTONE_BUSY 2
jd0205 0:58ff0234d7bb 34 #define FONA_STTONE_CONGESTION 3
jd0205 0:58ff0234d7bb 35 #define FONA_STTONE_PATHACK 4
jd0205 0:58ff0234d7bb 36 #define FONA_STTONE_DROPPED 5
jd0205 0:58ff0234d7bb 37 #define FONA_STTONE_ERROR 6
jd0205 0:58ff0234d7bb 38 #define FONA_STTONE_CALLWAIT 7
jd0205 0:58ff0234d7bb 39 #define FONA_STTONE_RINGING 8
jd0205 0:58ff0234d7bb 40 #define FONA_STTONE_BEEP 16
jd0205 0:58ff0234d7bb 41 #define FONA_STTONE_POSTONE 17
jd0205 0:58ff0234d7bb 42 #define FONA_STTONE_ERRTONE 18
jd0205 0:58ff0234d7bb 43 #define FONA_STTONE_INDIANDIALTONE 19
jd0205 0:58ff0234d7bb 44 #define FONA_STTONE_USADIALTONE 20
jd0205 0:58ff0234d7bb 45
jd0205 0:58ff0234d7bb 46 #define FONA_DEFAULT_TIMEOUT_MS 500 //timeout between send AT and reply from FONA
jd0205 0:58ff0234d7bb 47
jd0205 0:58ff0234d7bb 48 #define FONA_HTTP_GET 0
jd0205 0:58ff0234d7bb 49 #define FONA_HTTP_POST 1
jd0205 0:58ff0234d7bb 50 #define FONA_HTTP_HEAD 2
jd0205 0:58ff0234d7bb 51
jd0205 0:58ff0234d7bb 52 #define RX_BUFFER_SIZE 255
jd0205 0:58ff0234d7bb 53
jd0205 0:58ff0234d7bb 54 /** Adafruit FONA 800H Class
jd0205 0:58ff0234d7bb 55 * Modified by George Tzintzarov & Jesse Baker 03/14/2016 for use in mbed LPC1768
jd0205 0:58ff0234d7bb 56 */
jd0205 0:58ff0234d7bb 57
jd0205 0:58ff0234d7bb 58 class Adafruit_FONA : public Stream {
jd0205 0:58ff0234d7bb 59 public:
jd0205 0:58ff0234d7bb 60 /**
jd0205 0:58ff0234d7bb 61 Listener for FONA events. Inherit this class to customize.
jd0205 0:58ff0234d7bb 62 @code
jd0205 0:58ff0234d7bb 63 #define FONA_RST p12
jd0205 0:58ff0234d7bb 64 #define FONA_TX p13
jd0205 0:58ff0234d7bb 65 #define FONA_RX p14
jd0205 0:58ff0234d7bb 66 #define FONA_RI p11
jd0205 0:58ff0234d7bb 67
jd0205 0:58ff0234d7bb 68 Adafruit_FONA my_fona(FONA_TX, FONA_RX, FONA_RST, FONA_RI);
jd0205 0:58ff0234d7bb 69 DigitalOut led1(LED1);
jd0205 0:58ff0234d7bb 70 class FonaEventListener : public Adafruit_FONA::EventListener {
jd0205 0:58ff0234d7bb 71 virtual void onRing() {
jd0205 0:58ff0234d7bb 72 led1 = 1;
jd0205 0:58ff0234d7bb 73 }
jd0205 0:58ff0234d7bb 74
jd0205 0:58ff0234d7bb 75 virtual void onNoCarrier() {
jd0205 0:58ff0234d7bb 76 led1 = 0;
jd0205 0:58ff0234d7bb 77 }
jd0205 0:58ff0234d7bb 78 };
jd0205 0:58ff0234d7bb 79 FonaEventListener fonaEventListener;
jd0205 0:58ff0234d7bb 80 my_fona.setEventListener(&fonaEventListener);
jd0205 0:58ff0234d7bb 81 @endcode
jd0205 0:58ff0234d7bb 82 */
jd0205 0:58ff0234d7bb 83
jd0205 0:58ff0234d7bb 84 class EventListener {
jd0205 0:58ff0234d7bb 85 public:
jd0205 0:58ff0234d7bb 86 /**
jd0205 0:58ff0234d7bb 87 * Method called when somebody call the FONA.
jd0205 0:58ff0234d7bb 88 */
jd0205 0:58ff0234d7bb 89 virtual void onRing() = 0;
jd0205 0:58ff0234d7bb 90
jd0205 0:58ff0234d7bb 91 /**
jd0205 0:58ff0234d7bb 92 * Method called when the calling person stop his call.
jd0205 0:58ff0234d7bb 93 */
jd0205 0:58ff0234d7bb 94 virtual void onNoCarrier() = 0;
jd0205 0:58ff0234d7bb 95 };
jd0205 0:58ff0234d7bb 96
jd0205 0:58ff0234d7bb 97 public:
jd0205 0:58ff0234d7bb 98 /** Create instance of the Adafruit_FONA
jd0205 0:58ff0234d7bb 99 @param tx Set mbed TX
jd0205 0:58ff0234d7bb 100 @param rx Set mbed RX
jd0205 0:58ff0234d7bb 101 @param rst Set reset pin
jd0205 0:58ff0234d7bb 102 @param ringIndicator Set ring indicator pin. This is to let mbed know if there is an incoming call
jd0205 0:58ff0234d7bb 103 */
jd0205 0:58ff0234d7bb 104
jd0205 0:58ff0234d7bb 105 Adafruit_FONA(PinName tx, PinName rx, PinName rst, PinName ringIndicator) :
jd0205 0:58ff0234d7bb 106 _rstpin(rst, false), _ringIndicatorInterruptIn(ringIndicator),
jd0205 0:58ff0234d7bb 107 apn("FONAnet"), apnusername(NULL), apnpassword(NULL), httpsredirect(false), useragent("FONA"),
jd0205 0:58ff0234d7bb 108 _incomingCall(false), eventListener(NULL), mySerial(tx, rx), rxBufferInIndex(0), rxBufferOutIndex(0),
jd0205 0:58ff0234d7bb 109 currentReceivedLineSize(0) {}
jd0205 0:58ff0234d7bb 110
jd0205 0:58ff0234d7bb 111 /** Built-in Test to see if FONA is connected
jd0205 0:58ff0234d7bb 112 @param baudrate test and set at baudrate
jd0205 0:58ff0234d7bb 113 @return true upon success
jd0205 0:58ff0234d7bb 114 @return false upon failure. Most likely something is not hooked up.
jd0205 0:58ff0234d7bb 115
jd0205 0:58ff0234d7bb 116 EXAMPLE CODE:
jd0205 0:58ff0234d7bb 117 @code
jd0205 0:58ff0234d7bb 118 // See if the FONA is responding
jd0205 0:58ff0234d7bb 119 // fona is an instance of Adafruit_FONA
jd0205 0:58ff0234d7bb 120 if (! fona.begin(9600)) {
jd0205 0:58ff0234d7bb 121 printf("Couldn't find FONA\r\n");
jd0205 0:58ff0234d7bb 122 while (1);
jd0205 0:58ff0234d7bb 123 }
jd0205 0:58ff0234d7bb 124 @endcode
jd0205 0:58ff0234d7bb 125 */
jd0205 0:58ff0234d7bb 126 bool begin(int baudrate);
jd0205 0:58ff0234d7bb 127
jd0205 0:58ff0234d7bb 128 /** Set the event listener for incoming calls
jd0205 0:58ff0234d7bb 129 @param eventListener A pointer to the event listener
jd0205 0:58ff0234d7bb 130 @see Adafruit_FONA::EventListener for specific example
jd0205 0:58ff0234d7bb 131 */
jd0205 0:58ff0234d7bb 132
jd0205 0:58ff0234d7bb 133 void setEventListener(EventListener *eventListener);
jd0205 0:58ff0234d7bb 134
jd0205 0:58ff0234d7bb 135 // Stream----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 136 virtual int _putc(int value);
jd0205 0:58ff0234d7bb 137 virtual int _getc();
jd0205 0:58ff0234d7bb 138
jd0205 0:58ff0234d7bb 139 /** Check if FONA has anything in its output buffer
jd0205 0:58ff0234d7bb 140 @return 0 if nothing
jd0205 0:58ff0234d7bb 141 */
jd0205 0:58ff0234d7bb 142 int readable(void);
jd0205 0:58ff0234d7bb 143
jd0205 0:58ff0234d7bb 144 // RTC----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 145 bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable
jd0205 0:58ff0234d7bb 146
jd0205 0:58ff0234d7bb 147 // Battery and ADC----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 148 /** Get ADC voltage from external pin
jd0205 0:58ff0234d7bb 149 @param v uint16_t pointer to insert ADC voltage data
jd0205 0:58ff0234d7bb 150 @return TRUE if successful
jd0205 0:58ff0234d7bb 151
jd0205 0:58ff0234d7bb 152 EXAMPLE CODE:
jd0205 0:58ff0234d7bb 153 @code
jd0205 0:58ff0234d7bb 154 // read the ADC
jd0205 0:58ff0234d7bb 155 // fona is an instance of Adafruit_FONA
jd0205 0:58ff0234d7bb 156 uint16_t adc;
jd0205 0:58ff0234d7bb 157 if (! fona.getADCVoltage(&adc)) {
jd0205 0:58ff0234d7bb 158 printf("Failed to read ADC\r\n");
jd0205 0:58ff0234d7bb 159 }
jd0205 0:58ff0234d7bb 160 else {
jd0205 0:58ff0234d7bb 161 printf("ADC = %d mV\r\n", adc);
jd0205 0:58ff0234d7bb 162 }
jd0205 0:58ff0234d7bb 163 @endcode
jd0205 0:58ff0234d7bb 164 */
jd0205 0:58ff0234d7bb 165 bool getADCVoltage(uint16_t *v);
jd0205 0:58ff0234d7bb 166
jd0205 0:58ff0234d7bb 167 /** Get battery percentage level
jd0205 0:58ff0234d7bb 168 @param p uint16_t pointer to insert battery percent data
jd0205 0:58ff0234d7bb 169 @return TRUE if successful
jd0205 0:58ff0234d7bb 170
jd0205 0:58ff0234d7bb 171 EXAMPLE CODE:
jd0205 0:58ff0234d7bb 172 @code
jd0205 0:58ff0234d7bb 173 // read the battery percent level
jd0205 0:58ff0234d7bb 174 // fona is an instance of Adafruit_FONA
jd0205 0:58ff0234d7bb 175 uint16_t vbatPer;
jd0205 0:58ff0234d7bb 176 if (! fona.getBattPercent(&vbatPer)) {
jd0205 0:58ff0234d7bb 177 printf("Failed to read Batt\r\n");
jd0205 0:58ff0234d7bb 178 }
jd0205 0:58ff0234d7bb 179 else {
jd0205 0:58ff0234d7bb 180 printf("VPct = %d%%\r\n", vbatPer);
jd0205 0:58ff0234d7bb 181 }
jd0205 0:58ff0234d7bb 182 @endcode
jd0205 0:58ff0234d7bb 183 */
jd0205 0:58ff0234d7bb 184
jd0205 0:58ff0234d7bb 185 bool getBattPercent(uint16_t *p);
jd0205 0:58ff0234d7bb 186
jd0205 0:58ff0234d7bb 187 /** Get battery voltage level
jd0205 0:58ff0234d7bb 188 @param v uint16_t pointer to insert battery voltage data
jd0205 0:58ff0234d7bb 189 @return TRUE if successful
jd0205 0:58ff0234d7bb 190
jd0205 0:58ff0234d7bb 191 EXAMPLE CODE:
jd0205 0:58ff0234d7bb 192 @code
jd0205 0:58ff0234d7bb 193 // read the battery voltage
jd0205 0:58ff0234d7bb 194 // fona is an instance of Adafruit_FONA
jd0205 0:58ff0234d7bb 195 uint16_t vbat;
jd0205 0:58ff0234d7bb 196 if (! fona.getBattPercent(&vbat)) {
jd0205 0:58ff0234d7bb 197 printf("Failed to read Batt\r\n");
jd0205 0:58ff0234d7bb 198 }
jd0205 0:58ff0234d7bb 199 else {
jd0205 0:58ff0234d7bb 200 printf("Vbat = %d%%\r\n", vbat);
jd0205 0:58ff0234d7bb 201 }
jd0205 0:58ff0234d7bb 202 @endcode
jd0205 0:58ff0234d7bb 203 */
jd0205 0:58ff0234d7bb 204 bool getBattVoltage(uint16_t *v);
jd0205 0:58ff0234d7bb 205
jd0205 0:58ff0234d7bb 206 // SIM query----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 207 /** Unlock SIM if needed
jd0205 0:58ff0234d7bb 208 @param pin 4 digit char arrary
jd0205 0:58ff0234d7bb 209 @return TRUE if successful
jd0205 0:58ff0234d7bb 210 */
jd0205 0:58ff0234d7bb 211 bool unlockSIM(char *pin);
jd0205 0:58ff0234d7bb 212 /** Get the SIM chip card interface device (CCID)
jd0205 0:58ff0234d7bb 213 @param ccid make sure it is at least 21 bytes long
jd0205 0:58ff0234d7bb 214 @return length of CCID
jd0205 0:58ff0234d7bb 215 */
jd0205 0:58ff0234d7bb 216
jd0205 0:58ff0234d7bb 217 uint8_t getSIMCCID(char *ccid);
jd0205 0:58ff0234d7bb 218 /** Get the Network Status of FONA
jd0205 0:58ff0234d7bb 219 @return Code 0-5
jd0205 0:58ff0234d7bb 220 @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 80
jd0205 0:58ff0234d7bb 221 */
jd0205 0:58ff0234d7bb 222 uint8_t getNetworkStatus(void);
jd0205 0:58ff0234d7bb 223
jd0205 0:58ff0234d7bb 224 /** Get the RSSI of the network signal
jd0205 0:58ff0234d7bb 225 @return RSSI value in dBm per below reference
jd0205 0:58ff0234d7bb 226
jd0205 0:58ff0234d7bb 227 EXAMPLE
jd0205 0:58ff0234d7bb 228 @code
jd0205 0:58ff0234d7bb 229 // read the RSSI
jd0205 0:58ff0234d7bb 230 uint8_t n = fona.getRSSI();
jd0205 0:58ff0234d7bb 231 int8_t r = 0;
jd0205 0:58ff0234d7bb 232
jd0205 0:58ff0234d7bb 233 pcSerial.printf("RSSI = %d: ", n);
jd0205 0:58ff0234d7bb 234 if (n == 0) r = -115;
jd0205 0:58ff0234d7bb 235 if (n == 1) r = -111;
jd0205 0:58ff0234d7bb 236 if (n == 31) r = -52;
jd0205 0:58ff0234d7bb 237 if ((n >= 2) && (n <= 30)) {
jd0205 0:58ff0234d7bb 238 r = map(n, 2, 30, -110, -54);
jd0205 0:58ff0234d7bb 239 }
jd0205 0:58ff0234d7bb 240 printf("%d dBm\r\n", r);
jd0205 0:58ff0234d7bb 241
jd0205 0:58ff0234d7bb 242 // helper function MAP to do calculations
jd0205 0:58ff0234d7bb 243 long MAP(long x, long in_min, long in_max, long out_min, long out_max)
jd0205 0:58ff0234d7bb 244 {
jd0205 0:58ff0234d7bb 245 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
jd0205 0:58ff0234d7bb 246 }
jd0205 0:58ff0234d7bb 247 @endcode
jd0205 0:58ff0234d7bb 248 @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 82
jd0205 0:58ff0234d7bb 249 */
jd0205 0:58ff0234d7bb 250 uint8_t getRSSI(void);
jd0205 0:58ff0234d7bb 251
jd0205 0:58ff0234d7bb 252 // IMEI----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 253 /** Get the International Mobile Station Equipment Identity (IMEI)
jd0205 0:58ff0234d7bb 254 @param imei A char array with minimum length 16
jd0205 0:58ff0234d7bb 255 @return The IMEI of the device
jd0205 0:58ff0234d7bb 256
jd0205 0:58ff0234d7bb 257 EXAMPLE CODE:
jd0205 0:58ff0234d7bb 258 @code
jd0205 0:58ff0234d7bb 259 // Print SIM card IMEI number.
jd0205 0:58ff0234d7bb 260 char imei[15] = {0}; // MUST use a 16 character buffer for IMEI!
jd0205 0:58ff0234d7bb 261 uint8_t imeiLen = fona.getIMEI(imei); //fona is an instance of Adafruit_FONA
jd0205 0:58ff0234d7bb 262 if (imeiLen > 0) {
jd0205 0:58ff0234d7bb 263 pcSerial.printf("SIM card IMEI: %s\r\n", imei);
jd0205 0:58ff0234d7bb 264 }
jd0205 0:58ff0234d7bb 265 @endcode
jd0205 0:58ff0234d7bb 266 */
jd0205 0:58ff0234d7bb 267 uint8_t getIMEI(char *imei);
jd0205 0:58ff0234d7bb 268
jd0205 0:58ff0234d7bb 269 // set Audio output----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 270 /** Set the Audio Output interface
jd0205 0:58ff0234d7bb 271 @param a 0 is headset, 1 is external audio
jd0205 0:58ff0234d7bb 272 @return TRUE if successful
jd0205 0:58ff0234d7bb 273 */
jd0205 0:58ff0234d7bb 274 bool setAudio(uint8_t a);
jd0205 0:58ff0234d7bb 275
jd0205 0:58ff0234d7bb 276 /** Set the Audio Volume
jd0205 0:58ff0234d7bb 277 @param i a unit8_t volume number
jd0205 0:58ff0234d7bb 278 @return TRUE if successful
jd0205 0:58ff0234d7bb 279 */
jd0205 0:58ff0234d7bb 280 bool setVolume(uint8_t i);
jd0205 0:58ff0234d7bb 281
jd0205 0:58ff0234d7bb 282 /** Get the Audio Volume
jd0205 0:58ff0234d7bb 283 @return the current volume
jd0205 0:58ff0234d7bb 284 */
jd0205 0:58ff0234d7bb 285 uint8_t getVolume(void);
jd0205 0:58ff0234d7bb 286 bool playToolkitTone(uint8_t t, uint16_t len);
jd0205 0:58ff0234d7bb 287 bool setMicVolume(uint8_t a, uint8_t level);
jd0205 0:58ff0234d7bb 288 bool playDTMF(char tone);
jd0205 0:58ff0234d7bb 289
jd0205 0:58ff0234d7bb 290 // FM radio functions----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 291 /** Tune the FM radio
jd0205 0:58ff0234d7bb 292 @param station frequency, for example 107.9 MHz -> 1079
jd0205 0:58ff0234d7bb 293 @return TRUE if successful
jd0205 0:58ff0234d7bb 294 */
jd0205 0:58ff0234d7bb 295 bool tuneFMradio(uint16_t station);
jd0205 0:58ff0234d7bb 296
jd0205 0:58ff0234d7bb 297 /** FM radio set output
jd0205 0:58ff0234d7bb 298 @param onoff bool to turn on if TRUE
jd0205 0:58ff0234d7bb 299 @param a 0 (default) is headset, 1 is external audio
jd0205 0:58ff0234d7bb 300 @return TRUE if successful
jd0205 0:58ff0234d7bb 301 */
jd0205 0:58ff0234d7bb 302 bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO);
jd0205 0:58ff0234d7bb 303
jd0205 0:58ff0234d7bb 304 /** Set the FM Radio Volume
jd0205 0:58ff0234d7bb 305 @param i a unit8_t volume number
jd0205 0:58ff0234d7bb 306 @return TRUE if successful
jd0205 0:58ff0234d7bb 307 */
jd0205 0:58ff0234d7bb 308 bool setFMVolume(uint8_t i);
jd0205 0:58ff0234d7bb 309
jd0205 0:58ff0234d7bb 310 /** Get the FM Volume
jd0205 0:58ff0234d7bb 311 @return the current FM volume
jd0205 0:58ff0234d7bb 312 */
jd0205 0:58ff0234d7bb 313 int8_t getFMVolume();
jd0205 0:58ff0234d7bb 314
jd0205 0:58ff0234d7bb 315 /** Get the FM signal strength
jd0205 0:58ff0234d7bb 316 @param station a unit8_t volume number
jd0205 0:58ff0234d7bb 317 @return TRUE if successful
jd0205 0:58ff0234d7bb 318 */
jd0205 0:58ff0234d7bb 319 int8_t getFMSignalLevel(uint16_t station);
jd0205 0:58ff0234d7bb 320
jd0205 0:58ff0234d7bb 321 // SMS handling----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 322 /** Set the SMS Interrupt
jd0205 0:58ff0234d7bb 323 @param i 0 = OFF, 1 = ON with TCPIP, FTP, and URC control Ring Indicator Pin, 2 = ON with only TCPIP control
jd0205 0:58ff0234d7bb 324 @return TRUE if successful
jd0205 0:58ff0234d7bb 325 @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 152
jd0205 0:58ff0234d7bb 326 */
jd0205 0:58ff0234d7bb 327 bool setSMSInterrupt(uint8_t i);
jd0205 0:58ff0234d7bb 328
jd0205 0:58ff0234d7bb 329 /** Get SMS Interrupt Setting
jd0205 0:58ff0234d7bb 330 @return setting
jd0205 0:58ff0234d7bb 331 @see https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf page 152
jd0205 0:58ff0234d7bb 332 */
jd0205 0:58ff0234d7bb 333 uint8_t getSMSInterrupt(void);
jd0205 0:58ff0234d7bb 334
jd0205 0:58ff0234d7bb 335 /** Set the SMS Interrupt
jd0205 0:58ff0234d7bb 336 @return number of SMS messages in inbox
jd0205 0:58ff0234d7bb 337 */
jd0205 0:58ff0234d7bb 338 int8_t getNumSMS(void);
jd0205 0:58ff0234d7bb 339
jd0205 0:58ff0234d7bb 340 /** Read SMS
jd0205 0:58ff0234d7bb 341 @param i sms number in memory
jd0205 0:58ff0234d7bb 342 @param smsbuff char pointer to char array
jd0205 0:58ff0234d7bb 343 @param max Maximum length of smsbuff
jd0205 0:58ff0234d7bb 344 @param readsize the size in bytes of the SMS
jd0205 0:58ff0234d7bb 345 @return TRUE if successful
jd0205 0:58ff0234d7bb 346 */
jd0205 0:58ff0234d7bb 347 bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize);
jd0205 0:58ff0234d7bb 348
jd0205 0:58ff0234d7bb 349 /** Send SMS
jd0205 0:58ff0234d7bb 350 @param smsaddr Phone number to send out
jd0205 0:58ff0234d7bb 351 @param smsmsg Char array containing message
jd0205 0:58ff0234d7bb 352 @return TRUE if successful
jd0205 0:58ff0234d7bb 353 */
jd0205 0:58ff0234d7bb 354 bool sendSMS(char *smsaddr, char *smsmsg);
jd0205 0:58ff0234d7bb 355
jd0205 0:58ff0234d7bb 356 /** Delete SMS
jd0205 0:58ff0234d7bb 357 @param i number of SMS in memory
jd0205 0:58ff0234d7bb 358 @return TRUE if successful
jd0205 0:58ff0234d7bb 359 */
jd0205 0:58ff0234d7bb 360 bool deleteSMS(uint8_t i);
jd0205 0:58ff0234d7bb 361
jd0205 0:58ff0234d7bb 362 /** Send SMS
jd0205 0:58ff0234d7bb 363 @param i Number of SMS in memory
jd0205 0:58ff0234d7bb 364 @param sender Char array to store the sender number
jd0205 0:58ff0234d7bb 365 @param senderlen length of sender
jd0205 0:58ff0234d7bb 366 @return TRUE if successful
jd0205 0:58ff0234d7bb 367 */
jd0205 0:58ff0234d7bb 368 bool getSMSSender(uint8_t i, char *sender, int senderlen);
jd0205 0:58ff0234d7bb 369
jd0205 0:58ff0234d7bb 370 // Time----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 371 /** Enable FONA to sync time with the cellular network
jd0205 0:58ff0234d7bb 372 @param onoff on = true, off = false
jd0205 0:58ff0234d7bb 373 @return TRUE if successful
jd0205 0:58ff0234d7bb 374 */
jd0205 0:58ff0234d7bb 375 bool enableNetworkTimeSync(bool onoff);
jd0205 0:58ff0234d7bb 376
jd0205 0:58ff0234d7bb 377 /** Enable FONA to sync time with the time server
jd0205 0:58ff0234d7bb 378 @param onoff true = on, false = off
jd0205 0:58ff0234d7bb 379 @return TRUE if successful
jd0205 0:58ff0234d7bb 380 */
jd0205 0:58ff0234d7bb 381 bool enableNTPTimeSync(bool onoff, const char* ntpserver=0);
jd0205 0:58ff0234d7bb 382
jd0205 0:58ff0234d7bb 383 /** Retrieve the current time from the enabled server
jd0205 0:58ff0234d7bb 384 @param buff char array to store time value. Given as "yy/MM/dd,hh:mm:ss+zz"
jd0205 0:58ff0234d7bb 385 @param maxlen Maximum length of the char array
jd0205 0:58ff0234d7bb 386 @return TRUE if successful
jd0205 0:58ff0234d7bb 387 */
jd0205 0:58ff0234d7bb 388 bool getTime(char* buff, uint16_t maxlen);
jd0205 0:58ff0234d7bb 389
jd0205 0:58ff0234d7bb 390 // GPRS handling----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 391 bool enableGPRS(bool onoff);
jd0205 0:58ff0234d7bb 392 uint8_t GPRSstate(void);
jd0205 0:58ff0234d7bb 393 bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen);
jd0205 0:58ff0234d7bb 394 bool getGSMLoc(float *lat, float *lon);
jd0205 0:58ff0234d7bb 395 void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0);
jd0205 0:58ff0234d7bb 396
jd0205 0:58ff0234d7bb 397 // GPS handling----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 398 bool enableGPS(bool onoff);
jd0205 0:58ff0234d7bb 399 int8_t GPSstatus(void);
jd0205 0:58ff0234d7bb 400 uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff);
jd0205 0:58ff0234d7bb 401 bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0);
jd0205 0:58ff0234d7bb 402 bool enableGPSNMEA(uint8_t nmea);
jd0205 0:58ff0234d7bb 403
jd0205 0:58ff0234d7bb 404 // TCP raw connections----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 405 bool TCPconnect(char *server, uint16_t port);
jd0205 0:58ff0234d7bb 406 bool TCPclose(void);
jd0205 0:58ff0234d7bb 407 bool TCPconnected(void);
jd0205 0:58ff0234d7bb 408 bool TCPsend(char *packet, uint8_t len);
jd0205 0:58ff0234d7bb 409 uint16_t TCPavailable(void);
jd0205 0:58ff0234d7bb 410 uint16_t TCPread(uint8_t *buff, uint8_t len);
jd0205 0:58ff0234d7bb 411
jd0205 0:58ff0234d7bb 412 // HTTP low level interface (maps directly to SIM800 commands).----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 413 bool HTTP_init();
jd0205 0:58ff0234d7bb 414 bool HTTP_term();
jd0205 0:58ff0234d7bb 415 void HTTP_para_start(const char* parameter, bool quoted = true);
jd0205 0:58ff0234d7bb 416 bool HTTP_para_end(bool quoted = true);
jd0205 0:58ff0234d7bb 417 bool HTTP_para(const char* parameter, const char *value);
jd0205 0:58ff0234d7bb 418 bool HTTP_para(const char* parameter, int32_t value);
jd0205 0:58ff0234d7bb 419 bool HTTP_data(uint32_t size, uint32_t maxTime=10000);
jd0205 0:58ff0234d7bb 420 bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000);
jd0205 0:58ff0234d7bb 421 bool HTTP_readall(uint16_t *datalen);
jd0205 0:58ff0234d7bb 422 bool HTTP_ssl(bool onoff);
jd0205 0:58ff0234d7bb 423
jd0205 0:58ff0234d7bb 424 // HTTP high level interface (easier to use, less flexible).----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 425 bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen);
jd0205 0:58ff0234d7bb 426 void HTTP_GET_end(void);
jd0205 0:58ff0234d7bb 427 bool HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen);
jd0205 0:58ff0234d7bb 428 void HTTP_POST_end(void);
jd0205 0:58ff0234d7bb 429 void setUserAgent(const char* useragent);
jd0205 0:58ff0234d7bb 430
jd0205 0:58ff0234d7bb 431 // HTTPS----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 432 void setHTTPSRedirect(bool onoff);
jd0205 0:58ff0234d7bb 433
jd0205 0:58ff0234d7bb 434 // PWM (buzzer)----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 435 /** Control the buzzer capability of the PWM out on the FONA
jd0205 0:58ff0234d7bb 436 @param period of the buzzing cycle (max 2000)
jd0205 0:58ff0234d7bb 437 @param duty the duty cycle of the buzzer (0 to 100)
jd0205 0:58ff0234d7bb 438 @return TRUE if successful
jd0205 0:58ff0234d7bb 439 */
jd0205 0:58ff0234d7bb 440 bool setPWM(uint16_t period, uint8_t duty = 50);
jd0205 0:58ff0234d7bb 441
jd0205 0:58ff0234d7bb 442 // Phone calls----------------------------------------------------------------------
jd0205 0:58ff0234d7bb 443 /** Call a phone
jd0205 0:58ff0234d7bb 444 @param phonenum a character array of the phone number
jd0205 0:58ff0234d7bb 445 @return TRUE if successful
jd0205 0:58ff0234d7bb 446 */
jd0205 0:58ff0234d7bb 447 bool callPhone(char *phonenum);
jd0205 0:58ff0234d7bb 448
jd0205 0:58ff0234d7bb 449 /** Hang up a phone call
jd0205 0:58ff0234d7bb 450 */
jd0205 0:58ff0234d7bb 451 bool hangUp(void);
jd0205 0:58ff0234d7bb 452
jd0205 0:58ff0234d7bb 453 /** Answer a phone call
jd0205 0:58ff0234d7bb 454 */
jd0205 0:58ff0234d7bb 455 bool pickUp(void);
jd0205 0:58ff0234d7bb 456
jd0205 0:58ff0234d7bb 457 /** Enable/disable caller ID
jd0205 0:58ff0234d7bb 458 @param enable true to enable, false to disable
jd0205 0:58ff0234d7bb 459 @return TRUE if successful
jd0205 0:58ff0234d7bb 460 */
jd0205 0:58ff0234d7bb 461 bool callerIdNotification(bool enable);
jd0205 0:58ff0234d7bb 462
jd0205 0:58ff0234d7bb 463 /** Retrieve the incoming call number
jd0205 0:58ff0234d7bb 464 @param phonenum a character array of the phone number calling
jd0205 0:58ff0234d7bb 465 @return TRUE if successful
jd0205 0:58ff0234d7bb 466 */
jd0205 0:58ff0234d7bb 467 bool incomingCallNumber(char* phonenum);
jd0205 0:58ff0234d7bb 468
jd0205 0:58ff0234d7bb 469 // Helper functions to verify responses.
jd0205 0:58ff0234d7bb 470 bool expectReply(const char* reply, uint16_t timeout = 10000);
jd0205 0:58ff0234d7bb 471
jd0205 0:58ff0234d7bb 472 private:
jd0205 0:58ff0234d7bb 473 DigitalOut _rstpin;
jd0205 0:58ff0234d7bb 474 InterruptIn _ringIndicatorInterruptIn;
jd0205 0:58ff0234d7bb 475
jd0205 0:58ff0234d7bb 476 char replybuffer[255]; // the output of getreply(), readline() is the function that changes the replybuffer
jd0205 0:58ff0234d7bb 477 char* apn;
jd0205 0:58ff0234d7bb 478 char* apnusername;
jd0205 0:58ff0234d7bb 479 char* apnpassword;
jd0205 0:58ff0234d7bb 480 bool httpsredirect;
jd0205 0:58ff0234d7bb 481 char* useragent;
jd0205 0:58ff0234d7bb 482
jd0205 0:58ff0234d7bb 483 volatile bool _incomingCall;
jd0205 0:58ff0234d7bb 484 EventListener *eventListener;
jd0205 0:58ff0234d7bb 485 Serial mySerial;
jd0205 0:58ff0234d7bb 486
jd0205 0:58ff0234d7bb 487 // Circular buffer used to receive serial data from an interruption
jd0205 0:58ff0234d7bb 488 int rxBuffer[RX_BUFFER_SIZE + 1];
jd0205 0:58ff0234d7bb 489 volatile int rxBufferInIndex; // Index where new data is added to the buffer
jd0205 0:58ff0234d7bb 490 volatile int rxBufferOutIndex; // Index where data is removed from the buffer
jd0205 0:58ff0234d7bb 491 char currentReceivedLine[RX_BUFFER_SIZE]; // Array containing the current received line
jd0205 0:58ff0234d7bb 492 int currentReceivedLineSize;
jd0205 0:58ff0234d7bb 493
jd0205 0:58ff0234d7bb 494 inline bool isRxBufferFull() {
jd0205 0:58ff0234d7bb 495 return ((rxBufferInIndex + 1) % RX_BUFFER_SIZE) == rxBufferOutIndex;
jd0205 0:58ff0234d7bb 496 }
jd0205 0:58ff0234d7bb 497
jd0205 0:58ff0234d7bb 498 inline bool isRxBufferEmpty() {
jd0205 0:58ff0234d7bb 499 return rxBufferInIndex == rxBufferOutIndex;
jd0205 0:58ff0234d7bb 500 }
jd0205 0:58ff0234d7bb 501
jd0205 0:58ff0234d7bb 502 inline void incrementRxBufferInIndex() {
jd0205 0:58ff0234d7bb 503 rxBufferInIndex = (rxBufferInIndex + 1) % RX_BUFFER_SIZE;
jd0205 0:58ff0234d7bb 504 }
jd0205 0:58ff0234d7bb 505
jd0205 0:58ff0234d7bb 506 inline void incrementRxBufferOutIndex() {
jd0205 0:58ff0234d7bb 507 rxBufferOutIndex = (rxBufferOutIndex + 1) % RX_BUFFER_SIZE;
jd0205 0:58ff0234d7bb 508 }
jd0205 0:58ff0234d7bb 509
jd0205 0:58ff0234d7bb 510 /**
jd0205 0:58ff0234d7bb 511 * Method called when Serial data is received (interrupt routine).
jd0205 0:58ff0234d7bb 512 */
jd0205 0:58ff0234d7bb 513 void onSerialDataReceived();
jd0205 0:58ff0234d7bb 514
jd0205 0:58ff0234d7bb 515 // HTTP helpers
jd0205 0:58ff0234d7bb 516 bool HTTP_setup(char *url);
jd0205 0:58ff0234d7bb 517
jd0205 0:58ff0234d7bb 518 void flushInput();
jd0205 0:58ff0234d7bb 519 uint16_t readRaw(uint16_t b);
jd0205 0:58ff0234d7bb 520 uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false);
jd0205 0:58ff0234d7bb 521 uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 522 uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 523 uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 524 uint8_t getReply(const char* prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout); // Don't set default value or else function call is ambiguous.
jd0205 0:58ff0234d7bb 525 uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 526
jd0205 0:58ff0234d7bb 527 bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 528 bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 529 bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 530 bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 531 bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
jd0205 0:58ff0234d7bb 532
jd0205 0:58ff0234d7bb 533 bool parseReply(const char* toreply, uint16_t *v, char divider = ',', uint8_t index=0);
jd0205 0:58ff0234d7bb 534 bool parseReply(const char* toreply, char *v, char divider = ',', uint8_t index=0);
jd0205 0:58ff0234d7bb 535 bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index);
jd0205 0:58ff0234d7bb 536
jd0205 0:58ff0234d7bb 537 bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0);
jd0205 0:58ff0234d7bb 538
jd0205 0:58ff0234d7bb 539 void onIncomingCall();
jd0205 0:58ff0234d7bb 540 };
jd0205 0:58ff0234d7bb 541
jd0205 0:58ff0234d7bb 542 #endif