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