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