Adafruit IO Arduino Library
AdafruitIO_FONA.h
1 //
2 // Adafruit invests time and resources providing this open source code.
3 // Please support Adafruit and open source hardware by purchasing
4 // products from Adafruit!
5 //
6 // Copyright (c) 2015-2016 Adafruit Industries
7 // Authors: Tony DiCola, Todd Treece
8 // Licensed under the MIT license.
9 //
10 // All text above must be included in any redistribution.
11 //
12 #ifndef ADAFRUITIO_FONA_H
13 #define ADAFRUITIO_FONA_H
14 
15 #include "AdafruitIO.h"
16 #include "Adafruit_FONA.h"
17 #include "Adafruit_MQTT.h"
18 #include "Adafruit_MQTT_FONA.h"
19 #include "Arduino.h"
20 #include <SoftwareSerial.h>
21 
22 #define FONA_RX 9
23 #define FONA_TX 8
24 #define FONA_RST 4
25 #define FONA_RI 7
26 #define FONA_BAUD 4800
27 
28 /**************************************************************************/
32 /**************************************************************************/
33 class AdafruitIO_FONA : public AdafruitIO {
34 
35 public:
36  /**************************************************************************/
45  /**************************************************************************/
46  AdafruitIO_FONA(const char *user, const char *key) : AdafruitIO(user, key) {
47  _serial = new SoftwareSerial(FONA_TX, FONA_RX);
48  _fona = new Adafruit_FONA(FONA_RST);
49  _mqtt = new Adafruit_MQTT_FONA(_fona, _host, _mqtt_port);
50  _packetread_timeout = 500;
51  }
52 
53  /**************************************************************************/
63  /**************************************************************************/
64  void setAPN(FONAFlashStringPtr apn, FONAFlashStringPtr username = 0,
65  FONAFlashStringPtr password = 0) {
66  _fona->setGPRSNetworkSettings(apn, username, password);
67  }
68 
69  /**************************************************************************/
74  /**************************************************************************/
76  // return if in a failed state
77  if (_status == AIO_NET_CONNECT_FAILED)
78  return _status;
79 
80  // if we are connected, return
81  if (_fona->GPRSstate())
82  return AIO_NET_CONNECTED;
83 
84  // wait for connection to network
85  if (_fona->getNetworkStatus() != 1)
86  return AIO_NET_DISCONNECTED;
87 
88  _fona->enableGPRS(true);
89  return AIO_NET_CONNECTED;
90  }
91 
92  /**************************************************************************/
97  /**************************************************************************/
98  const char *connectionType() { return "fona"; }
99 
100 protected:
101  uint16_t _mqtt_port = 1883;
103  SoftwareSerial *_serial;
104  Adafruit_FONA *_fona;
106  /**************************************************************************/
110  /**************************************************************************/
111  void _connect() {
112  // set software serial baud rate
113  _serial->begin(FONA_BAUD);
114 
115  // if fona can't be found, bail
116  if (!_fona->begin(*_serial)) {
117  _status = AIO_NET_CONNECT_FAILED;
118  return;
119  }
120 
121  // disable cme error reporting
122  _serial->println("AT+CMEE=2");
123 
124  _status = AIO_NET_DISCONNECTED;
125  }
126 
127  /**************************************************************************/
131  /**************************************************************************/
132  void _disconnect() {
133  if (!_fona->enableGPRS(false)) {
134  AIO_DEBUG_PRINTLN("Failed to turn off GPRS.");
135  }
136  _status = AIO_NET_DISCONNECTED;
137  }
138 };
139 
140 #endif // ADAFRUITIO_FONA_H
Class for interacting with Adafruit IO.
Definition: AdafruitIO.h:45
AdafruitIO_FONA(const char *user, const char *key)
Initializes a new AdafruitIO_FONA instance.
Definition: AdafruitIO_FONA.h:46
void setAPN(FONAFlashStringPtr apn, FONAFlashStringPtr username=0, FONAFlashStringPtr password=0)
Sets Adafruit Fona APN name.
Definition: AdafruitIO_FONA.h:64
aio_status_t _status
Definition: AdafruitIO.h:138
void _disconnect()
Disconnects from Adafruit IO and the cellular network.
Definition: AdafruitIO_FONA.h:132
aio_status_t networkStatus()
Returns network connection status.
Definition: AdafruitIO_FONA.h:75
void _connect()
Establishes a connection to Adafruit IO.
Definition: AdafruitIO_FONA.h:111
Adafruit_MQTT * _mqtt
Definition: AdafruitIO.h:144
uint16_t _packetread_timeout
Definition: AdafruitIO.h:155
Class for interfacing with an Adafruit FONA Ceullar Module.
Definition: AdafruitIO_FONA.h:33
#define AIO_DEBUG_PRINTLN(...)
Prints line from debug output.
Definition: AdafruitIO_Definitions.h:89
SoftwareSerial * _serial
Definition: AdafruitIO_FONA.h:103
const char * connectionType()
Returns network module type.
Definition: AdafruitIO_FONA.h:98
Adafruit_FONA * _fona
Definition: AdafruitIO_FONA.h:104
const char * _host
Definition: AdafruitIO.h:149
aio_status_t
Definition: AdafruitIO_Definitions.h:142
uint16_t _mqtt_port
Definition: AdafruitIO_FONA.h:101