Library For the Adafruit FONA 808

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Adafruit_FONA.h Source File

Adafruit_FONA.h

00001 /***************************************************
00002   This is a library for our Adafruit FONA Cellular Module
00003 
00004   Designed specifically to work with the Adafruit FONA
00005   ----> http://www.adafruit.com/products/1946
00006   ----> http://www.adafruit.com/products/1963
00007 
00008   These displays use TTL Serial to communicate, 2 pins are required to
00009   interface
00010   Adafruit invests time and resources providing this open source code,
00011   please support Adafruit and open-source hardware by purchasing
00012   products from Adafruit!
00013 
00014   Written by Limor Fried/Ladyada for Adafruit Industries.
00015   BSD license, all text above must be included in any redistribution
00016  ****************************************************/
00017 #ifndef ADAFRUIT_FONA_H
00018 #define ADAFRUIT_FONA_H
00019 
00020 #include "includes/FONAConfig.h"
00021 #include "includes/FONAExtIncludes.h"
00022 #include "includes/platform/FONAPlatform.h"
00023 
00024 
00025 
00026 #define FONA800L 1
00027 #define FONA800H 6
00028 
00029 #define FONA808_V1 2
00030 #define FONA808_V2 3
00031 
00032 #define FONA3G_A 4
00033 #define FONA3G_E 5
00034 
00035 // Uncomment to changed the preferred SMS storage
00036 //#define FONA_PREF_SMS_STORAGE "SM"
00037 
00038 #define FONA_HEADSETAUDIO 0
00039 #define FONA_EXTAUDIO 1
00040 
00041 #define FONA_STTONE_DIALTONE 1
00042 #define FONA_STTONE_BUSY 2
00043 #define FONA_STTONE_CONGESTION 3
00044 #define FONA_STTONE_PATHACK 4
00045 #define FONA_STTONE_DROPPED 5
00046 #define FONA_STTONE_ERROR 6
00047 #define FONA_STTONE_CALLWAIT 7
00048 #define FONA_STTONE_RINGING 8
00049 #define FONA_STTONE_BEEP 16
00050 #define FONA_STTONE_POSTONE 17
00051 #define FONA_STTONE_ERRTONE 18
00052 #define FONA_STTONE_INDIANDIALTONE 19
00053 #define FONA_STTONE_USADIALTONE 20
00054 
00055 #define FONA_DEFAULT_TIMEOUT_MS 500
00056 
00057 #define FONA_HTTP_GET   0
00058 #define FONA_HTTP_POST  1
00059 #define FONA_HTTP_HEAD  2
00060 
00061 #define FONA_CALL_READY 0
00062 #define FONA_CALL_FAILED 1
00063 #define FONA_CALL_UNKNOWN 2
00064 #define FONA_CALL_RINGING 3
00065 #define FONA_CALL_INPROGRESS 4
00066 
00067 class Adafruit_FONA : public FONAStreamType {
00068  public:
00069   Adafruit_FONA(int8_t r);
00070   boolean begin(FONAStreamType &port);
00071   uint8_t type();
00072 
00073   // Stream
00074   int available(void);
00075   size_t write(uint8_t x);
00076   int read(void);
00077   int peek(void);
00078   void flush();
00079 
00080   // FONA 3G requirements
00081   boolean setBaudrate(uint16_t baud);
00082 
00083   // RTC
00084   boolean enableRTC(uint8_t i);
00085   boolean readRTC(uint8_t *year, uint8_t *month, uint8_t *date, uint8_t *hr, uint8_t *min, uint8_t *sec);
00086 
00087   // Battery and ADC
00088   boolean getADCVoltage(uint16_t *v);
00089   boolean getBattPercent(uint16_t *p);
00090   boolean getBattVoltage(uint16_t *v);
00091 
00092   // SIM query
00093   uint8_t unlockSIM(char *pin);
00094   uint8_t getSIMCCID(char *ccid);
00095   uint8_t getNetworkStatus(void);
00096   uint8_t getRSSI(void);
00097 
00098   // IMEI
00099   uint8_t getIMEI(char *imei);
00100 
00101   // set Audio output
00102   boolean setAudio(uint8_t a);
00103   boolean setVolume(uint8_t i);
00104   uint8_t getVolume(void);
00105   boolean playToolkitTone(uint8_t t, uint16_t len);
00106   boolean setMicVolume(uint8_t a, uint8_t level);
00107   boolean playDTMF(char tone);
00108 
00109   // FM radio functions.
00110   boolean tuneFMradio(uint16_t station);
00111   boolean FMradio(boolean onoff, uint8_t a = FONA_HEADSETAUDIO);
00112   boolean setFMVolume(uint8_t i);
00113   int8_t getFMVolume();
00114   int8_t getFMSignalLevel(uint16_t station);
00115 
00116   // SMS handling
00117   boolean setSMSInterrupt(uint8_t i);
00118   uint8_t getSMSInterrupt(void);
00119   int8_t getNumSMS(void);
00120   boolean readSMS(uint8_t i, char *smsbuff, uint16_t max, uint16_t *readsize);
00121   boolean sendSMS(char *smsaddr, char *smsmsg);
00122   boolean deleteSMS(uint8_t i);
00123   boolean getSMSSender(uint8_t i, char *sender, int senderlen);
00124   boolean sendUSSD(char *ussdmsg, char *ussdbuff, uint16_t maxlen, uint16_t *readlen);
00125 
00126   // Time
00127   boolean enableNetworkTimeSync(boolean onoff);
00128   boolean enableNTPTimeSync(boolean onoff, FONAFlashStringPtr ntpserver=0);
00129   boolean getTime(char *buff, uint16_t maxlen);
00130 
00131   // GPRS handling
00132   boolean enableGPRS(boolean onoff);
00133   uint8_t GPRSstate(void);
00134   boolean getGSMLoc(uint16_t *replycode, char *buff, uint16_t maxlen);
00135   boolean getGSMLoc(float *lat, float *lon);
00136   void setGPRSNetworkSettings(FONAFlashStringPtr apn, FONAFlashStringPtr username=0, FONAFlashStringPtr password=0);
00137 
00138   // GPS handling
00139   boolean enableGPS(boolean onoff);
00140   int8_t GPSstatus(void);
00141   uint8_t getGPS(uint8_t arg, char *buffer, uint8_t maxbuff);
00142   boolean getGPS(float *lat, float *lon, float *speed_kph=0, float *heading=0, float *altitude=0);
00143   boolean enableGPSNMEA(uint8_t nmea);
00144 
00145   // TCP raw connections
00146   boolean TCPconnect(char *server, uint16_t port);
00147   boolean TCPclose(void);
00148   boolean TCPconnected(void);
00149   boolean TCPsend(char *packet, uint8_t len);
00150   uint16_t TCPavailable(void);
00151   uint16_t TCPread(uint8_t *buff, uint8_t len);
00152 
00153   // HTTP low level interface (maps directly to SIM800 commands).
00154   boolean HTTP_init();
00155   boolean HTTP_term();
00156   void HTTP_para_start(FONAFlashStringPtr parameter, boolean quoted = true);
00157   boolean HTTP_para_end(boolean quoted = true);
00158   boolean HTTP_para(FONAFlashStringPtr parameter, const char *value);
00159   boolean HTTP_para(FONAFlashStringPtr parameter, FONAFlashStringPtr value);
00160   boolean HTTP_para(FONAFlashStringPtr parameter, int32_t value);
00161   boolean HTTP_data(uint32_t size, uint32_t maxTime=10000);
00162   boolean HTTP_action(uint8_t method, uint16_t *status, uint16_t *datalen, int32_t timeout = 10000);
00163   boolean HTTP_readall(uint16_t *datalen);
00164   boolean HTTP_ssl(boolean onoff);
00165 
00166   // HTTP high level interface (easier to use, less flexible).
00167   boolean HTTP_GET_start(char *url, uint16_t *status, uint16_t *datalen);
00168   void HTTP_GET_end(void);
00169   boolean HTTP_POST_start(char *url, FONAFlashStringPtr contenttype, const uint8_t *postdata, uint16_t postdatalen,  uint16_t *status, uint16_t *datalen);
00170   void HTTP_POST_end(void);
00171   void setUserAgent(FONAFlashStringPtr useragent);
00172 
00173   // HTTPS
00174   void setHTTPSRedirect(boolean onoff);
00175 
00176   // PWM (buzzer)
00177   boolean setPWM(uint16_t period, uint8_t duty = 50);
00178 
00179   // Phone calls
00180   boolean callPhone(char *phonenum);
00181   uint8_t getCallStatus(void);
00182   boolean hangUp(void);
00183   boolean pickUp(void);
00184   boolean callerIdNotification(boolean enable, uint8_t interrupt = 0);
00185   boolean incomingCallNumber(char* phonenum);
00186 
00187   // Helper functions to verify responses.
00188   boolean expectReply(FONAFlashStringPtr reply, uint16_t timeout = 10000);
00189   boolean sendCheckReply(char *send, char *reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00190   boolean sendCheckReply(FONAFlashStringPtr send, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00191   boolean sendCheckReply(char* send, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00192 
00193 
00194  protected:
00195   int8_t _rstpin;
00196   uint8_t _type;
00197 
00198   char replybuffer[255];
00199   FONAFlashStringPtr apn;
00200   FONAFlashStringPtr apnusername;
00201   FONAFlashStringPtr apnpassword;
00202   boolean httpsredirect;
00203   FONAFlashStringPtr useragent;
00204   FONAFlashStringPtr ok_reply;
00205 
00206   // HTTP helpers
00207   boolean HTTP_setup(char *url);
00208 
00209   void flushInput();
00210   uint16_t readRaw(uint16_t b);
00211   uint8_t readline(uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS, boolean multiline = false);
00212   uint8_t getReply(char *send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00213   uint8_t getReply(FONAFlashStringPtr send, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00214   uint8_t getReply(FONAFlashStringPtr prefix, char *suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00215   uint8_t getReply(FONAFlashStringPtr prefix, int32_t suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00216   uint8_t getReply(FONAFlashStringPtr prefix, int32_t suffix1, int32_t suffix2, uint16_t timeout); // Don't set default value or else function call is ambiguous.
00217   uint8_t getReplyQuoted(FONAFlashStringPtr prefix, FONAFlashStringPtr suffix, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00218 
00219   boolean sendCheckReply(FONAFlashStringPtr prefix, char *suffix, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00220   boolean sendCheckReply(FONAFlashStringPtr prefix, int32_t suffix, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00221   boolean sendCheckReply(FONAFlashStringPtr prefix, int32_t suffix, int32_t suffix2, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00222   boolean sendCheckReplyQuoted(FONAFlashStringPtr prefix, FONAFlashStringPtr suffix, FONAFlashStringPtr reply, uint16_t timeout = FONA_DEFAULT_TIMEOUT_MS);
00223 
00224 
00225   boolean parseReply(FONAFlashStringPtr toreply,
00226           uint16_t *v, char divider  = ',', uint8_t index=0);
00227   boolean parseReply(FONAFlashStringPtr toreply,
00228           char *v, char divider  = ',', uint8_t index=0);
00229   boolean parseReplyQuoted(FONAFlashStringPtr toreply,
00230           char *v, int maxlen, char divider, uint8_t index);
00231 
00232   boolean sendParseReply(FONAFlashStringPtr tosend,
00233        FONAFlashStringPtr toreply,
00234        uint16_t *v, char divider = ',', uint8_t index=0);
00235 
00236   static boolean _incomingCall;
00237   static void onIncomingCall();
00238 
00239   FONAStreamType *mySerial;
00240 };
00241 
00242 class Adafruit_FONA_3G : public Adafruit_FONA {
00243 
00244  public:
00245   Adafruit_FONA_3G (int8_t r) : Adafruit_FONA(r) { _type = FONA3G_A; }
00246 
00247     boolean getBattVoltage(uint16_t *v);
00248     boolean playToolkitTone(uint8_t t, uint16_t len);
00249     boolean hangUp(void);
00250     boolean pickUp(void);
00251     boolean enableGPRS(boolean onoff);
00252     boolean enableGPS(boolean onoff);
00253 
00254  protected:
00255     boolean parseReply(FONAFlashStringPtr toreply,
00256                float *f, char divider, uint8_t index);
00257 
00258     boolean sendParseReply(FONAFlashStringPtr tosend,
00259                FONAFlashStringPtr toreply,
00260                float *f, char divider = ',', uint8_t index=0);
00261 };
00262 
00263 #endif