Allows SMS to be sent to query Alexa. Also allows HTTP requests to be sent to Alexa via an ESP8266-hosted web server.

Dependencies:   emic2 mbed

Committer:
itatchi42
Date:
Tue May 02 03:13:27 2017 +0000
Revision:
0:1271d15b4d4b
Allows SMS communication with Amazon Dot's Alexa.; Also allows URL queries to be sent to ESP8266-hosted HTTP server to communicate with Alexa.

Who changed what in which revision?

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