Library for the Adafruit FONA. This is a port of the original Arduino library available at: https://github.com/adafruit/Adafruit_FONA_Library . - Modified by Marc PLOUHINEC 27/06/2015 for use in mbed - Modified by lionel VOIRIN 05/08/2018 for use Adafruit FONA 3

Dependents:   Adafruit_FONA_3G_Library_Test

Committer:
Nels885
Date:
Sun Aug 05 14:06:59 2018 +0000
Revision:
0:bf67af6b3913
Child:
2:3fc229f5ec3f
Modified for use Adafruit FONA 3G

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nels885 0:bf67af6b3913 1 /***************************************************
Nels885 0:bf67af6b3913 2 This is a library for our Adafruit FONA Cellular Module
Nels885 0:bf67af6b3913 3
Nels885 0:bf67af6b3913 4 Designed specifically to work with the Adafruit FONA
Nels885 0:bf67af6b3913 5 ----> http://www.adafruit.com/products/1946
Nels885 0:bf67af6b3913 6 ----> http://www.adafruit.com/products/1963
Nels885 0:bf67af6b3913 7
Nels885 0:bf67af6b3913 8 These displays use TTL Serial to communicate, 2 pins are required to
Nels885 0:bf67af6b3913 9 interface
Nels885 0:bf67af6b3913 10 Adafruit invests time and resources providing this open source code,
Nels885 0:bf67af6b3913 11 please support Adafruit and open-source hardware by purchasing
Nels885 0:bf67af6b3913 12 products from Adafruit!
Nels885 0:bf67af6b3913 13
Nels885 0:bf67af6b3913 14 Written by Limor Fried/Ladyada for Adafruit Industries.
Nels885 0:bf67af6b3913 15 BSD license, all text above must be included in any redistribution
Nels885 0:bf67af6b3913 16 ****************************************************/
Nels885 0:bf67af6b3913 17
Nels885 0:bf67af6b3913 18 /*
Nels885 0:bf67af6b3913 19 * Modified by Marc PLOUHINEC 27/06/2015 for use in mbed
Nels885 0:bf67af6b3913 20 * Modified by lionel VOIRIN 05/08/2018 for use Adafruit FONA 3G
Nels885 0:bf67af6b3913 21 */
Nels885 0:bf67af6b3913 22
Nels885 0:bf67af6b3913 23 #ifndef ADAFRUIT_FONA_H
Nels885 0:bf67af6b3913 24 #define ADAFRUIT_FONA_H
Nels885 0:bf67af6b3913 25
Nels885 0:bf67af6b3913 26 #include "mbed.h"
Nels885 0:bf67af6b3913 27
Nels885 0:bf67af6b3913 28 //#define ADAFRUIT_FONA_DEBUG
Nels885 0:bf67af6b3913 29
Nels885 0:bf67af6b3913 30 #define FONA800L 1
Nels885 0:bf67af6b3913 31 #define FONA800H 6
Nels885 0:bf67af6b3913 32
Nels885 0:bf67af6b3913 33 #define FONA808_V1 2
Nels885 0:bf67af6b3913 34 #define FONA808_V2 3
Nels885 0:bf67af6b3913 35
Nels885 0:bf67af6b3913 36 #define FONA3G_A 4
Nels885 0:bf67af6b3913 37 #define FONA3G_E 5
Nels885 0:bf67af6b3913 38
Nels885 0:bf67af6b3913 39 // Set the preferred SMS storage.
Nels885 0:bf67af6b3913 40 // Use "SM" for storage on the SIM.
Nels885 0:bf67af6b3913 41 // Use "ME" for internal storage on the FONA chip
Nels885 0:bf67af6b3913 42 #define FONA_PREF_SMS_STORAGE "\"SM\""
Nels885 0:bf67af6b3913 43 //#define FONA_PREF_SMS_STORAGE "\"ME\""
Nels885 0:bf67af6b3913 44
Nels885 0:bf67af6b3913 45 #define FONA_HEADSETAUDIO 0
Nels885 0:bf67af6b3913 46 #define FONA_EXTAUDIO 1
Nels885 0:bf67af6b3913 47
Nels885 0:bf67af6b3913 48 #define FONA_STTONE_DIALTONE 1
Nels885 0:bf67af6b3913 49 #define FONA_STTONE_BUSY 2
Nels885 0:bf67af6b3913 50 #define FONA_STTONE_CONGESTION 3
Nels885 0:bf67af6b3913 51 #define FONA_STTONE_PATHACK 4
Nels885 0:bf67af6b3913 52 #define FONA_STTONE_DROPPED 5
Nels885 0:bf67af6b3913 53 #define FONA_STTONE_ERROR 6
Nels885 0:bf67af6b3913 54 #define FONA_STTONE_CALLWAIT 7
Nels885 0:bf67af6b3913 55 #define FONA_STTONE_RINGING 8
Nels885 0:bf67af6b3913 56 #define FONA_STTONE_BEEP 16
Nels885 0:bf67af6b3913 57 #define FONA_STTONE_POSTONE 17
Nels885 0:bf67af6b3913 58 #define FONA_STTONE_ERRTONE 18
Nels885 0:bf67af6b3913 59 #define FONA_STTONE_INDIANDIALTONE 19
Nels885 0:bf67af6b3913 60 #define FONA_STTONE_USADIALTONE 20
Nels885 0:bf67af6b3913 61
Nels885 0:bf67af6b3913 62 #define FONA_DEFAULT_TIMEOUT_MS 500
Nels885 0:bf67af6b3913 63
Nels885 0:bf67af6b3913 64 #define FONA_HTTP_GET 0
Nels885 0:bf67af6b3913 65 #define FONA_HTTP_POST 1
Nels885 0:bf67af6b3913 66 #define FONA_HTTP_HEAD 2
Nels885 0:bf67af6b3913 67
Nels885 0:bf67af6b3913 68 #define FONA_CALL_READY 0
Nels885 0:bf67af6b3913 69 #define FONA_CALL_FAILED 1
Nels885 0:bf67af6b3913 70 #define FONA_CALL_UNKNOWN 2
Nels885 0:bf67af6b3913 71 #define FONA_CALL_RINGING 3
Nels885 0:bf67af6b3913 72 #define FONA_CALL_INPROGRESS 4
Nels885 0:bf67af6b3913 73
Nels885 0:bf67af6b3913 74 #define RX_BUFFER_SIZE 255
Nels885 0:bf67af6b3913 75
Nels885 0:bf67af6b3913 76 class Adafruit_FONA : public Stream
Nels885 0:bf67af6b3913 77 {
Nels885 0:bf67af6b3913 78 public:
Nels885 0:bf67af6b3913 79 /**
Nels885 0:bf67af6b3913 80 * Listener for FONA events.
Nels885 0:bf67af6b3913 81 */
Nels885 0:bf67af6b3913 82 class EventListener
Nels885 0:bf67af6b3913 83 {
Nels885 0:bf67af6b3913 84 public:
Nels885 0:bf67af6b3913 85 /**
Nels885 0:bf67af6b3913 86 * Method called when somebody call the FONA.
Nels885 0:bf67af6b3913 87 */
Nels885 0:bf67af6b3913 88 virtual void onRing() = 0;
Nels885 0:bf67af6b3913 89
Nels885 0:bf67af6b3913 90 /**
Nels885 0:bf67af6b3913 91 * Method called when the calling person stop his call.
Nels885 0:bf67af6b3913 92 */
Nels885 0:bf67af6b3913 93 virtual void onNoCarrier() = 0;
Nels885 0:bf67af6b3913 94 };
Nels885 0:bf67af6b3913 95
Nels885 0:bf67af6b3913 96 public:
Nels885 0:bf67af6b3913 97 Adafruit_FONA(PinName tx, PinName rx, PinName rst);
Nels885 0:bf67af6b3913 98 bool begin(int baudrate);
Nels885 0:bf67af6b3913 99 uint8_t type();
Nels885 0:bf67af6b3913 100 void setEventListener(EventListener *eventListener);
Nels885 0:bf67af6b3913 101
Nels885 0:bf67af6b3913 102 // Stream
Nels885 0:bf67af6b3913 103 virtual int _putc(int value);
Nels885 0:bf67af6b3913 104 virtual int _getc();
Nels885 0:bf67af6b3913 105 int readable();
Nels885 0:bf67af6b3913 106
Nels885 0:bf67af6b3913 107 // FONA 3G requirements
Nels885 0:bf67af6b3913 108 bool setBaudrate(uint16_t baud);
Nels885 0:bf67af6b3913 109
Nels885 0:bf67af6b3913 110 // RTC
Nels885 0:bf67af6b3913 111 bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable
Nels885 0:bf67af6b3913 112
Nels885 0:bf67af6b3913 113 // Battery and ADC
Nels885 0:bf67af6b3913 114 bool getADCVoltage(uint16_t *v);
Nels885 0:bf67af6b3913 115 bool getBattPercent(uint16_t *p);
Nels885 0:bf67af6b3913 116 bool getBattVoltage(uint16_t *v);
Nels885 0:bf67af6b3913 117
Nels885 0:bf67af6b3913 118 // SIM query
Nels885 0:bf67af6b3913 119 uint8_t unlockSIM(char *pin);
Nels885 0:bf67af6b3913 120 uint8_t getSIMCCID(char *ccid);
Nels885 0:bf67af6b3913 121 uint8_t getNetworkStatus(void);
Nels885 0:bf67af6b3913 122 uint8_t getRSSI(void);
Nels885 0:bf67af6b3913 123
Nels885 0:bf67af6b3913 124 // IMEI
Nels885 0:bf67af6b3913 125 uint8_t getIMEI(char *imei);
Nels885 0:bf67af6b3913 126
Nels885 0:bf67af6b3913 127 // set Audio output
Nels885 0:bf67af6b3913 128 bool setAudio(uint8_t a);
Nels885 0:bf67af6b3913 129 bool setVolume(uint8_t i);
Nels885 0:bf67af6b3913 130 uint8_t getVolume(void);
Nels885 0:bf67af6b3913 131 bool playToolkitTone(uint8_t t, uint16_t len);
Nels885 0:bf67af6b3913 132 bool setMicVolume(uint8_t a, uint8_t level);
Nels885 0:bf67af6b3913 133 bool playDTMF(char tone);
Nels885 0:bf67af6b3913 134
Nels885 0:bf67af6b3913 135 // FM radio functions
Nels885 0:bf67af6b3913 136 bool tuneFMradio(uint16_t station);
Nels885 0:bf67af6b3913 137 bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO);
Nels885 0:bf67af6b3913 138 bool setFMVolume(uint8_t i);
Nels885 0:bf67af6b3913 139 int8_t getFMVolume();
Nels885 0:bf67af6b3913 140 int8_t getFMSignalLevel(uint16_t station);
Nels885 0:bf67af6b3913 141
Nels885 0:bf67af6b3913 142 // SMS handling
Nels885 0:bf67af6b3913 143 bool setSMSInterrupt(uint8_t i);
Nels885 0:bf67af6b3913 144 uint8_t getSMSInterrupt(void);
Nels885 0:bf67af6b3913 145 int8_t getNumSMS(void);
Nels885 0:bf67af6b3913 146 bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize);
Nels885 0:bf67af6b3913 147 bool sendSMS(char *smsaddr, char *smsmsg);
Nels885 0:bf67af6b3913 148 bool deleteSMS(uint8_t i);
Nels885 0:bf67af6b3913 149 bool getSMSSender(uint8_t i, char *sender, int senderlen);
Nels885 0:bf67af6b3913 150
Nels885 0:bf67af6b3913 151 // Time
Nels885 0:bf67af6b3913 152 bool enableNetworkTimeSync(bool onoff);
Nels885 0:bf67af6b3913 153 bool enableNTPTimeSync(bool onoff, const char* ntpserver=0);
Nels885 0:bf67af6b3913 154 bool getTime(char* buff, uint16_t maxlen);
Nels885 0:bf67af6b3913 155
Nels885 0:bf67af6b3913 156 // GPRS handling
Nels885 0:bf67af6b3913 157 bool enableGPRS(bool onoff);
Nels885 0:bf67af6b3913 158 uint8_t GPRSstate(void);
Nels885 0:bf67af6b3913 159 bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen);
Nels885 0:bf67af6b3913 160 bool getGSMLoc(float *lat, float *lon);
Nels885 0:bf67af6b3913 161 void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0);
Nels885 0:bf67af6b3913 162
Nels885 0:bf67af6b3913 163 // GPS handling
Nels885 0:bf67af6b3913 164 bool enableGPS(bool onoff);
Nels885 0:bf67af6b3913 165 int8_t GPSstatus(void);
Nels885 0:bf67af6b3913 166 uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff);
Nels885 0:bf67af6b3913 167 bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0);
Nels885 0:bf67af6b3913 168 bool enableGPSNMEA(uint8_t nmea);
Nels885 0:bf67af6b3913 169
Nels885 0:bf67af6b3913 170 // TCP raw connections
Nels885 0:bf67af6b3913 171 bool TCPconnect(char *server, uint16_t port);
Nels885 0:bf67af6b3913 172 bool TCPclose(void);
Nels885 0:bf67af6b3913 173 bool TCPconnected(void);
Nels885 0:bf67af6b3913 174 bool TCPsend(char *packet, uint8_t len);
Nels885 0:bf67af6b3913 175 uint16_t TCPavailable(void);
Nels885 0:bf67af6b3913 176 uint16_t TCPread(uint8_t *buff, uint8_t len);
Nels885 0:bf67af6b3913 177
Nels885 0:bf67af6b3913 178 // HTTP low level interface (maps directly to SIM800 commands).
Nels885 0:bf67af6b3913 179 bool HTTP_init();
Nels885 0:bf67af6b3913 180 bool HTTP_term();
Nels885 0:bf67af6b3913 181 void HTTP_para_start(const char* parameter, bool quoted = true);
Nels885 0:bf67af6b3913 182 bool HTTP_para_end(bool quoted = true);
Nels885 0:bf67af6b3913 183 bool HTTP_para(const char* parameter, const char *value);
Nels885 0:bf67af6b3913 184 bool HTTP_para(const char* parameter, int32_t value);
Nels885 0:bf67af6b3913 185 bool HTTP_data(uint32_t size, uint32_t maxTime=10000);
Nels885 0:bf67af6b3913 186 bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000);
Nels885 0:bf67af6b3913 187 bool HTTP_readall(uint16_t *datalen);
Nels885 0:bf67af6b3913 188 bool HTTP_ssl(bool onoff);
Nels885 0:bf67af6b3913 189
Nels885 0:bf67af6b3913 190 // HTTP high level interface (easier to use, less flexible).
Nels885 0:bf67af6b3913 191 bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen);
Nels885 0:bf67af6b3913 192 void HTTP_GET_end(void);
Nels885 0:bf67af6b3913 193 bool HTTP_POST_start(char *url, const char* contenttype, const uint8_t *postdata, uint16_t postdatalen, uint16_t *status, uint16_t *datalen);
Nels885 0:bf67af6b3913 194 void HTTP_POST_end(void);
Nels885 0:bf67af6b3913 195 void setUserAgent(const char* useragent);
Nels885 0:bf67af6b3913 196
Nels885 0:bf67af6b3913 197 // HTTPS
Nels885 0:bf67af6b3913 198 void setHTTPSRedirect(bool onoff);
Nels885 0:bf67af6b3913 199
Nels885 0:bf67af6b3913 200 // PWM (buzzer)
Nels885 0:bf67af6b3913 201 bool setPWM(uint16_t period, uint8_t duty = 50);
Nels885 0:bf67af6b3913 202
Nels885 0:bf67af6b3913 203 // Phone calls
Nels885 0:bf67af6b3913 204 bool callPhone(char *phonenum);
Nels885 0:bf67af6b3913 205 bool hangUp(void);
Nels885 0:bf67af6b3913 206 bool pickUp(void);
Nels885 0:bf67af6b3913 207 bool callerIdNotification(bool enable);
Nels885 0:bf67af6b3913 208 bool incomingCallNumber(char* phonenum);
Nels885 0:bf67af6b3913 209
Nels885 0:bf67af6b3913 210 // Helper functions to verify responses.
Nels885 0:bf67af6b3913 211 bool expectReply(const char* reply, uint16_t timeout = 10000);
Nels885 0:bf67af6b3913 212
Nels885 0:bf67af6b3913 213 protected:
Nels885 0:bf67af6b3913 214 DigitalOut _rstpin;
Nels885 0:bf67af6b3913 215 uint8_t _type;
Nels885 0:bf67af6b3913 216
Nels885 0:bf67af6b3913 217 char replybuffer[255];
Nels885 0:bf67af6b3913 218 char* apn;
Nels885 0:bf67af6b3913 219 char* apnusername;
Nels885 0:bf67af6b3913 220 char* apnpassword;
Nels885 0:bf67af6b3913 221 bool httpsredirect;
Nels885 0:bf67af6b3913 222 char* useragent;
Nels885 0:bf67af6b3913 223 char* ok_reply;
Nels885 0:bf67af6b3913 224
Nels885 0:bf67af6b3913 225 volatile bool _incomingCall;
Nels885 0:bf67af6b3913 226 EventListener *eventListener;
Nels885 0:bf67af6b3913 227 Serial mySerial;
Nels885 0:bf67af6b3913 228
Nels885 0:bf67af6b3913 229 // Circular buffer used to receive serial data from an interruption
Nels885 0:bf67af6b3913 230 int rxBuffer[RX_BUFFER_SIZE + 1];
Nels885 0:bf67af6b3913 231 volatile int rxBufferInIndex; // Index where new data is added to the buffer
Nels885 0:bf67af6b3913 232 volatile int rxBufferOutIndex; // Index where data is removed from the buffer
Nels885 0:bf67af6b3913 233 char currentReceivedLine[RX_BUFFER_SIZE]; // Array containing the current received line
Nels885 0:bf67af6b3913 234 int currentReceivedLineSize;
Nels885 0:bf67af6b3913 235
Nels885 0:bf67af6b3913 236 inline bool isRxBufferFull() {
Nels885 0:bf67af6b3913 237 return ((rxBufferInIndex + 1) % RX_BUFFER_SIZE) == rxBufferOutIndex;
Nels885 0:bf67af6b3913 238 }
Nels885 0:bf67af6b3913 239
Nels885 0:bf67af6b3913 240 inline bool isRxBufferEmpty() {
Nels885 0:bf67af6b3913 241 return rxBufferInIndex == rxBufferOutIndex;
Nels885 0:bf67af6b3913 242 }
Nels885 0:bf67af6b3913 243
Nels885 0:bf67af6b3913 244 inline void incrementRxBufferInIndex() {
Nels885 0:bf67af6b3913 245 rxBufferInIndex = (rxBufferInIndex + 1) % RX_BUFFER_SIZE;
Nels885 0:bf67af6b3913 246 }
Nels885 0:bf67af6b3913 247
Nels885 0:bf67af6b3913 248 inline void incrementRxBufferOutIndex() {
Nels885 0:bf67af6b3913 249 rxBufferOutIndex = (rxBufferOutIndex + 1) % RX_BUFFER_SIZE;
Nels885 0:bf67af6b3913 250 }
Nels885 0:bf67af6b3913 251
Nels885 0:bf67af6b3913 252 /**
Nels885 0:bf67af6b3913 253 * Method called when Serial data is received (interrupt routine).
Nels885 0:bf67af6b3913 254 */
Nels885 0:bf67af6b3913 255 void onSerialDataReceived();
Nels885 0:bf67af6b3913 256
Nels885 0:bf67af6b3913 257 // HTTP helpers
Nels885 0:bf67af6b3913 258 bool HTTP_setup(char *url);
Nels885 0:bf67af6b3913 259
Nels885 0:bf67af6b3913 260 void flushInput();
Nels885 0:bf67af6b3913 261 uint16_t readRaw(uint16_t b);
Nels885 0:bf67af6b3913 262 uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false);
Nels885 0:bf67af6b3913 263 uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 264 uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 265 uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 266 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.
Nels885 0:bf67af6b3913 267 uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 268
Nels885 0:bf67af6b3913 269 bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 270 bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 271 bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 272 bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 273 bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
Nels885 0:bf67af6b3913 274
Nels885 0:bf67af6b3913 275 bool parseReply(const char* toreply, uint16_t *v, char divider = ',', uint8_t index=0);
Nels885 0:bf67af6b3913 276 bool parseReply(const char* toreply, char *v, char divider = ',', uint8_t index=0);
Nels885 0:bf67af6b3913 277 bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index);
Nels885 0:bf67af6b3913 278
Nels885 0:bf67af6b3913 279 bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0);
Nels885 0:bf67af6b3913 280
Nels885 0:bf67af6b3913 281 void onIncomingCall();
Nels885 0:bf67af6b3913 282 };
Nels885 0:bf67af6b3913 283
Nels885 0:bf67af6b3913 284 class Adafruit_FONA_3G : public Adafruit_FONA
Nels885 0:bf67af6b3913 285 {
Nels885 0:bf67af6b3913 286 public:
Nels885 0:bf67af6b3913 287 Adafruit_FONA_3G(PinName tx, PinName rx, PinName rst) : Adafruit_FONA(tx, rx, rst) {
Nels885 0:bf67af6b3913 288 _type = FONA3G_A;
Nels885 0:bf67af6b3913 289 }
Nels885 0:bf67af6b3913 290
Nels885 0:bf67af6b3913 291 bool getBattVoltage(uint16_t *v);
Nels885 0:bf67af6b3913 292 bool hangUp(void);
Nels885 0:bf67af6b3913 293 bool pickUp(void);
Nels885 0:bf67af6b3913 294 bool enableGPS(bool onoff);
Nels885 0:bf67af6b3913 295
Nels885 0:bf67af6b3913 296 protected:
Nels885 0:bf67af6b3913 297 bool sendParseReply(const char* tosend, const char* toreply, float *f, char divider, uint8_t index);
Nels885 0:bf67af6b3913 298 bool parseReply(const char* toreply, float *f, char divider, uint8_t index);
Nels885 0:bf67af6b3913 299 };
Nels885 0:bf67af6b3913 300
Nels885 0:bf67af6b3913 301 #endif