Version using RawSerial instead of Serial (thread safe)

Fork of FONA_Cellphone_Library by Dream Team

Committer:
zathorix
Date:
Tue Jul 11 20:16:43 2017 +0000
Revision:
4:243ae42bfd65
Parent:
3:7916fb0cc4b0
Added roaming set function

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