Library for Adafruit FONA breakout board

Committer:
jbaker66
Date:
Wed Mar 30 17:18:50 2016 +0000
Revision:
0:52654f3080d9
Publishing Library JB;

Who changed what in which revision?

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