Library for the Adafruit FONA. This is a port of the original Arduino library available at: https://github.com/adafruit/Adafruit_FONA_Library .
Dependents: Adafruit_FONA_Library_FONAtest
Library for the Adafruit FONA. This is a port of the original Arduino library available at: https://github.com/adafruit/Adafruit_FONA_Library .
Adafruit_FONA.h@0:d567815b7a5f, 2015-06-27 (annotated)
- Committer:
- marcpl
- Date:
- Sat Jun 27 09:25:38 2015 +0000
- Revision:
- 0:d567815b7a5f
- Child:
- 1:89299f09929c
Port the Adafruit_FONA_Library for mbed.
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 | 0:d567815b7a5f | 52 | class Adafruit_FONA { |
marcpl | 0:d567815b7a5f | 53 | public: |
marcpl | 0:d567815b7a5f | 54 | Adafruit_FONA(PinName rst, PinName ringIndicator) : |
marcpl | 0:d567815b7a5f | 55 | _rstpin(rst, false), _ringIndicatorInterruptIn(ringIndicator), |
marcpl | 0:d567815b7a5f | 56 | apn("FONAnet"), apnusername(NULL), apnpassword(NULL), httpsredirect(false), useragent("FONA"), |
marcpl | 0:d567815b7a5f | 57 | _incomingCall(false), mySerial(NULL) {} |
marcpl | 0:d567815b7a5f | 58 | bool begin(Serial &port); |
marcpl | 0:d567815b7a5f | 59 | |
marcpl | 0:d567815b7a5f | 60 | // RTC |
marcpl | 0:d567815b7a5f | 61 | bool enableRTC(uint8_t i); // i = 0 <=> disable, i = 1 <=> enable |
marcpl | 0:d567815b7a5f | 62 | |
marcpl | 0:d567815b7a5f | 63 | // Battery and ADC |
marcpl | 0:d567815b7a5f | 64 | bool getADCVoltage(uint16_t *v); |
marcpl | 0:d567815b7a5f | 65 | bool getBattPercent(uint16_t *p); |
marcpl | 0:d567815b7a5f | 66 | bool getBattVoltage(uint16_t *v); |
marcpl | 0:d567815b7a5f | 67 | |
marcpl | 0:d567815b7a5f | 68 | // SIM query |
marcpl | 0:d567815b7a5f | 69 | uint8_t unlockSIM(char *pin); |
marcpl | 0:d567815b7a5f | 70 | uint8_t getSIMCCID(char *ccid); |
marcpl | 0:d567815b7a5f | 71 | uint8_t getNetworkStatus(void); |
marcpl | 0:d567815b7a5f | 72 | uint8_t getRSSI(void); |
marcpl | 0:d567815b7a5f | 73 | |
marcpl | 0:d567815b7a5f | 74 | // IMEI |
marcpl | 0:d567815b7a5f | 75 | uint8_t getIMEI(char *imei); |
marcpl | 0:d567815b7a5f | 76 | |
marcpl | 0:d567815b7a5f | 77 | // set Audio output |
marcpl | 0:d567815b7a5f | 78 | bool setAudio(uint8_t a); |
marcpl | 0:d567815b7a5f | 79 | bool setVolume(uint8_t i); |
marcpl | 0:d567815b7a5f | 80 | uint8_t getVolume(void); |
marcpl | 0:d567815b7a5f | 81 | bool playToolkitTone(uint8_t t, uint16_t len); |
marcpl | 0:d567815b7a5f | 82 | bool setMicVolume(uint8_t a, uint8_t level); |
marcpl | 0:d567815b7a5f | 83 | bool playDTMF(char tone); |
marcpl | 0:d567815b7a5f | 84 | |
marcpl | 0:d567815b7a5f | 85 | // FM radio functions |
marcpl | 0:d567815b7a5f | 86 | bool tuneFMradio(uint16_t station); |
marcpl | 0:d567815b7a5f | 87 | bool FMradio(bool onoff, uint8_t a = FONA_HEADSETAUDIO); |
marcpl | 0:d567815b7a5f | 88 | bool setFMVolume(uint8_t i); |
marcpl | 0:d567815b7a5f | 89 | int8_t getFMVolume(); |
marcpl | 0:d567815b7a5f | 90 | int8_t getFMSignalLevel(uint16_t station); |
marcpl | 0:d567815b7a5f | 91 | |
marcpl | 0:d567815b7a5f | 92 | // SMS handling |
marcpl | 0:d567815b7a5f | 93 | bool setSMSInterrupt(uint8_t i); |
marcpl | 0:d567815b7a5f | 94 | uint8_t getSMSInterrupt(void); |
marcpl | 0:d567815b7a5f | 95 | int8_t getNumSMS(void); |
marcpl | 0:d567815b7a5f | 96 | bool readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize); |
marcpl | 0:d567815b7a5f | 97 | bool sendSMS(char *smsaddr, char *smsmsg); |
marcpl | 0:d567815b7a5f | 98 | bool deleteSMS(uint8_t i); |
marcpl | 0:d567815b7a5f | 99 | bool getSMSSender(uint8_t i, char *sender, int senderlen); |
marcpl | 0:d567815b7a5f | 100 | |
marcpl | 0:d567815b7a5f | 101 | // Time |
marcpl | 0:d567815b7a5f | 102 | bool enableNetworkTimeSync(bool onoff); |
marcpl | 0:d567815b7a5f | 103 | bool enableNTPTimeSync(bool onoff, const char* ntpserver=0); |
marcpl | 0:d567815b7a5f | 104 | bool getTime(char* buff, uint16_t maxlen); |
marcpl | 0:d567815b7a5f | 105 | |
marcpl | 0:d567815b7a5f | 106 | // GPRS handling |
marcpl | 0:d567815b7a5f | 107 | bool enableGPRS(bool onoff); |
marcpl | 0:d567815b7a5f | 108 | uint8_t GPRSstate(void); |
marcpl | 0:d567815b7a5f | 109 | bool getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen); |
marcpl | 0:d567815b7a5f | 110 | bool getGSMLoc(float *lat, float *lon); |
marcpl | 0:d567815b7a5f | 111 | void setGPRSNetworkSettings(const char* apn, const char* username=0, const char* password=0); |
marcpl | 0:d567815b7a5f | 112 | |
marcpl | 0:d567815b7a5f | 113 | // GPS handling |
marcpl | 0:d567815b7a5f | 114 | bool enableGPS(bool onoff); |
marcpl | 0:d567815b7a5f | 115 | int8_t GPSstatus(void); |
marcpl | 0:d567815b7a5f | 116 | uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff); |
marcpl | 0:d567815b7a5f | 117 | bool getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0); |
marcpl | 0:d567815b7a5f | 118 | bool enableGPSNMEA(uint8_t nmea); |
marcpl | 0:d567815b7a5f | 119 | |
marcpl | 0:d567815b7a5f | 120 | // TCP raw connections |
marcpl | 0:d567815b7a5f | 121 | bool TCPconnect(char *server, uint16_t port); |
marcpl | 0:d567815b7a5f | 122 | bool TCPclose(void); |
marcpl | 0:d567815b7a5f | 123 | bool TCPconnected(void); |
marcpl | 0:d567815b7a5f | 124 | bool TCPsend(char *packet, uint8_t len); |
marcpl | 0:d567815b7a5f | 125 | uint16_t TCPavailable(void); |
marcpl | 0:d567815b7a5f | 126 | uint16_t TCPread(uint8_t *buff, uint8_t len); |
marcpl | 0:d567815b7a5f | 127 | |
marcpl | 0:d567815b7a5f | 128 | // HTTP low level interface (maps directly to SIM800 commands). |
marcpl | 0:d567815b7a5f | 129 | bool HTTP_init(); |
marcpl | 0:d567815b7a5f | 130 | bool HTTP_term(); |
marcpl | 0:d567815b7a5f | 131 | void HTTP_para_start(const char* parameter, bool quoted = true); |
marcpl | 0:d567815b7a5f | 132 | bool HTTP_para_end(bool quoted = true); |
marcpl | 0:d567815b7a5f | 133 | bool HTTP_para(const char* parameter, const char *value); |
marcpl | 0:d567815b7a5f | 134 | bool HTTP_para(const char* parameter, int32_t value); |
marcpl | 0:d567815b7a5f | 135 | bool HTTP_data(uint32_t size, uint32_t maxTime=10000); |
marcpl | 0:d567815b7a5f | 136 | bool HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000); |
marcpl | 0:d567815b7a5f | 137 | bool HTTP_readall(uint16_t *datalen); |
marcpl | 0:d567815b7a5f | 138 | bool HTTP_ssl(bool onoff); |
marcpl | 0:d567815b7a5f | 139 | |
marcpl | 0:d567815b7a5f | 140 | // HTTP high level interface (easier to use, less flexible). |
marcpl | 0:d567815b7a5f | 141 | bool HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen); |
marcpl | 0:d567815b7a5f | 142 | void HTTP_GET_end(void); |
marcpl | 0:d567815b7a5f | 143 | 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 | 144 | void HTTP_POST_end(void); |
marcpl | 0:d567815b7a5f | 145 | void setUserAgent(const char* useragent); |
marcpl | 0:d567815b7a5f | 146 | |
marcpl | 0:d567815b7a5f | 147 | // HTTPS |
marcpl | 0:d567815b7a5f | 148 | void setHTTPSRedirect(bool onoff); |
marcpl | 0:d567815b7a5f | 149 | |
marcpl | 0:d567815b7a5f | 150 | // PWM (buzzer) |
marcpl | 0:d567815b7a5f | 151 | bool setPWM(uint16_t period, uint8_t duty = 50); |
marcpl | 0:d567815b7a5f | 152 | |
marcpl | 0:d567815b7a5f | 153 | // Phone calls |
marcpl | 0:d567815b7a5f | 154 | bool callPhone(char *phonenum); |
marcpl | 0:d567815b7a5f | 155 | bool hangUp(void); |
marcpl | 0:d567815b7a5f | 156 | bool pickUp(void); |
marcpl | 0:d567815b7a5f | 157 | bool callerIdNotification(bool enable); |
marcpl | 0:d567815b7a5f | 158 | bool incomingCallNumber(char* phonenum); |
marcpl | 0:d567815b7a5f | 159 | |
marcpl | 0:d567815b7a5f | 160 | // Helper functions to verify responses. |
marcpl | 0:d567815b7a5f | 161 | bool expectReply(const char* reply, uint16_t timeout = 10000); |
marcpl | 0:d567815b7a5f | 162 | |
marcpl | 0:d567815b7a5f | 163 | private: |
marcpl | 0:d567815b7a5f | 164 | DigitalOut _rstpin; |
marcpl | 0:d567815b7a5f | 165 | InterruptIn _ringIndicatorInterruptIn; |
marcpl | 0:d567815b7a5f | 166 | |
marcpl | 0:d567815b7a5f | 167 | char replybuffer[255]; |
marcpl | 0:d567815b7a5f | 168 | char* apn; |
marcpl | 0:d567815b7a5f | 169 | char* apnusername; |
marcpl | 0:d567815b7a5f | 170 | char* apnpassword; |
marcpl | 0:d567815b7a5f | 171 | bool httpsredirect; |
marcpl | 0:d567815b7a5f | 172 | char* useragent; |
marcpl | 0:d567815b7a5f | 173 | |
marcpl | 0:d567815b7a5f | 174 | // HTTP helpers |
marcpl | 0:d567815b7a5f | 175 | bool HTTP_setup(char *url); |
marcpl | 0:d567815b7a5f | 176 | |
marcpl | 0:d567815b7a5f | 177 | void flushInput(); |
marcpl | 0:d567815b7a5f | 178 | uint16_t readRaw(uint16_t b); |
marcpl | 0:d567815b7a5f | 179 | uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, bool multiline = false); |
marcpl | 0:d567815b7a5f | 180 | uint8_t getReply(const char* send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 181 | uint8_t getReply(const char* prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 182 | uint8_t getReply(const char* prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 183 | 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 | 184 | uint8_t getReplyQuoted(const char* prefix, const char* suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 185 | |
marcpl | 0:d567815b7a5f | 186 | bool sendCheckReply(const char* send, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 187 | bool sendCheckReply(const char* prefix, char *suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 188 | bool sendCheckReply(const char* prefix, int32_t suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 189 | bool sendCheckReply(const char* prefix, int32_t suffix, int32_t suffix2, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 190 | bool sendCheckReplyQuoted(const char* prefix, const char* suffix, const char* reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS); |
marcpl | 0:d567815b7a5f | 191 | |
marcpl | 0:d567815b7a5f | 192 | bool parseReply(const char* toreply, uint16_t *v, char divider = ',', uint8_t index=0); |
marcpl | 0:d567815b7a5f | 193 | bool parseReply(const char* toreply, char *v, char divider = ',', uint8_t index=0); |
marcpl | 0:d567815b7a5f | 194 | bool parseReplyQuoted(const char* toreply, char* v, int maxlen, char divider, uint8_t index); |
marcpl | 0:d567815b7a5f | 195 | |
marcpl | 0:d567815b7a5f | 196 | bool sendParseReply(const char* tosend, const char* toreply, uint16_t *v, char divider = ',', uint8_t index = 0); |
marcpl | 0:d567815b7a5f | 197 | |
marcpl | 0:d567815b7a5f | 198 | volatile bool _incomingCall; |
marcpl | 0:d567815b7a5f | 199 | void onIncomingCall(); |
marcpl | 0:d567815b7a5f | 200 | |
marcpl | 0:d567815b7a5f | 201 | Serial *mySerial; |
marcpl | 0:d567815b7a5f | 202 | }; |
marcpl | 0:d567815b7a5f | 203 | |
marcpl | 0:d567815b7a5f | 204 | #endif |