Skip to content

Commit a5a690d

Browse files
authored
Merge pull request #4 from adafruit/wrapper
IO Wrapper Beta
2 parents 6ede662 + 6f5c8b2 commit a5a690d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2733
-1475
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.swo
2+
*.swp
3+
.DS_Store

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sudo: false
33
before_install:
44
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
55
install:
6-
- arduino --install-library "Adafruit FONA Library,Adafruit CC3000 Library"
6+
- arduino --install-library "Adafruit FONA Library,Adafruit MQTT Library"
77
script:
88
- build_main_platforms
99
notifications:

AdafruitIO.cpp

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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+
#include "AdafruitIO.h"
13+
14+
AdafruitIO::AdafruitIO(const char *user, const char *key)
15+
{
16+
_mqtt = 0;
17+
_username = user;
18+
_key = key;
19+
_err_topic = 0;
20+
_throttle_topic = 0;
21+
_err_sub = 0;
22+
_throttle_sub = 0;
23+
_packetread_timeout = 100;
24+
25+
_init();
26+
}
27+
28+
AdafruitIO::AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key)
29+
{
30+
_mqtt = 0;
31+
_username = (const char*)user;
32+
_key = (const char*)key;
33+
_err_topic = 0;
34+
_throttle_topic = 0;
35+
_err_sub = 0;
36+
_throttle_sub = 0;
37+
_packetread_timeout = 100;
38+
39+
_init();
40+
}
41+
42+
void errorCallback(char *err, uint16_t len)
43+
{
44+
AIO_ERR_PRINTLN();
45+
AIO_ERR_PRINT("ERROR: ");
46+
AIO_ERR_PRINTLN(err);
47+
AIO_ERR_PRINTLN();
48+
}
49+
50+
void AdafruitIO::connect()
51+
{
52+
53+
if(_err_sub) {
54+
// setup error sub
55+
_err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic);
56+
_mqtt->subscribe(_err_sub);
57+
_err_sub->setCallback(errorCallback);
58+
}
59+
60+
if(_throttle_sub) {
61+
// setup throttle sub
62+
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
63+
_mqtt->subscribe(_throttle_sub);
64+
_throttle_sub->setCallback(errorCallback);
65+
}
66+
67+
_connect();
68+
69+
}
70+
71+
AdafruitIO::~AdafruitIO()
72+
{
73+
if(_err_topic)
74+
free(_err_topic);
75+
76+
if(_throttle_topic)
77+
free(_throttle_topic);
78+
79+
if(_err_sub)
80+
delete _err_sub;
81+
82+
if(_throttle_sub)
83+
delete _throttle_sub;
84+
}
85+
86+
AdafruitIO_Feed* AdafruitIO::feed(const char* name)
87+
{
88+
return new AdafruitIO_Feed(this, name);
89+
}
90+
91+
AdafruitIO_Feed* AdafruitIO::feed(const __FlashStringHelper *name)
92+
{
93+
return new AdafruitIO_Feed(this, name);
94+
}
95+
96+
void AdafruitIO::_init()
97+
{
98+
99+
// we have never pinged, so set last ping to now
100+
_last_ping = millis();
101+
102+
// dynamically allocate memory for err topic
103+
_err_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_ERROR_TOPIC) + 1));
104+
105+
if(_err_topic) {
106+
107+
// build error topic
108+
strcpy(_err_topic, _username);
109+
strcat(_err_topic, AIO_ERROR_TOPIC);
110+
111+
} else {
112+
113+
// malloc failed
114+
_err_topic = 0;
115+
116+
}
117+
118+
// dynamically allocate memory for throttle topic
119+
_throttle_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_THROTTLE_TOPIC) + 1));
120+
121+
if(_throttle_topic) {
122+
123+
// build throttle topic
124+
strcpy(_throttle_topic, _username);
125+
strcat(_throttle_topic, AIO_THROTTLE_TOPIC);
126+
127+
} else {
128+
129+
// malloc failed
130+
_throttle_topic = 0;
131+
132+
}
133+
134+
}
135+
136+
const __FlashStringHelper* AdafruitIO::statusText()
137+
{
138+
switch(_status) {
139+
140+
// CONNECTING
141+
case AIO_IDLE: return F("Idle. Waiting for connect to be called...");
142+
case AIO_NET_DISCONNECTED: return F("Network disconnected.");
143+
case AIO_DISCONNECTED: return F("Disconnected from Adafruit IO.");
144+
145+
// FAILURE
146+
case AIO_NET_CONNECT_FAILED: return F("Network connection failed.");
147+
case AIO_CONNECT_FAILED: return F("Adafruit IO connection failed.");
148+
case AIO_FINGERPRINT_INVALID: return F("Adafruit IO SSL fingerprint verification failed.");
149+
case AIO_AUTH_FAILED: return F("Adafruit IO authentication failed.");
150+
151+
// SUCCESS
152+
case AIO_NET_CONNECTED: return F("Network connected.");
153+
case AIO_CONNECTED: return F("Adafruit IO connected.");
154+
case AIO_CONNECTED_INSECURE: return F("Adafruit IO connected. **THIS CONNECTION IS INSECURE** SSL/TLS not supported for this platform.");
155+
case AIO_FINGERPRINT_UNSUPPORTED: return F("Adafruit IO connected over SSL/TLS. Fingerprint verification unsupported.");
156+
case AIO_FINGERPRINT_VALID: return F("Adafruit IO connected over SSL/TLS. Fingerprint valid.");
157+
158+
default: return F("Unknown status code");
159+
160+
}
161+
}
162+
163+
void AdafruitIO::run(uint16_t busywait_ms)
164+
{
165+
// loop until we have a connection
166+
while(mqttStatus() != AIO_CONNECTED){}
167+
168+
if(busywait_ms > 0)
169+
_packetread_timeout = busywait_ms;
170+
171+
_mqtt->processPackets(_packetread_timeout);
172+
173+
// ping to keep connection alive if needed
174+
if(millis() > (_last_ping + AIO_PING_INTERVAL)) {
175+
_mqtt->ping();
176+
_last_ping = millis();
177+
}
178+
}
179+
180+
aio_status_t AdafruitIO::status()
181+
{
182+
aio_status_t net_status = networkStatus();
183+
184+
// if we aren't connected, return network status
185+
if(net_status != AIO_NET_CONNECTED) {
186+
_status = net_status;
187+
return _status;
188+
}
189+
190+
// check mqtt status and return
191+
_status = mqttStatus();
192+
return _status;
193+
}
194+
195+
aio_status_t AdafruitIO::mqttStatus()
196+
{
197+
// if the connection failed,
198+
// return so we don't hammer IO
199+
if(_status == AIO_CONNECT_FAILED)
200+
return _status;
201+
202+
if(_mqtt->connected())
203+
return AIO_CONNECTED;
204+
205+
switch(_mqtt->connect(_username, _key)) {
206+
case 0:
207+
return AIO_CONNECTED;
208+
case 1: // invalid mqtt protocol
209+
case 2: // client id rejected
210+
case 4: // malformed user/pass
211+
case 5: // unauthorized
212+
case 7: // banned
213+
return AIO_CONNECT_FAILED;
214+
case 3: // mqtt service unavailable
215+
case 6: // throttled
216+
// delay to prevent fast reconnects
217+
delay(AIO_THROTTLE_RECONNECT_INTERVAL);
218+
return AIO_DISCONNECTED;
219+
default:
220+
return AIO_DISCONNECTED;
221+
}
222+
}

AdafruitIO.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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_H
13+
#define ADAFRUITIO_H
14+
15+
#include "Arduino.h"
16+
#include "Adafruit_MQTT.h"
17+
#include "AdafruitIO_Definitions.h"
18+
#include "AdafruitIO_Feed.h"
19+
#include "AdafruitIO_Data.h"
20+
21+
#ifndef ADAFRUIT_MQTT_VERSION_MAJOR
22+
#error "This sketch requires Adafruit MQTT Library v0.16.0 or higher. Please install or upgrade using the Library Manager."
23+
#endif
24+
25+
#if ADAFRUIT_MQTT_VERSION_MAJOR == 0 && ADAFRUIT_MQTT_VERSION_MINOR < 16
26+
#error "This sketch requires Adafruit MQTT Library v0.16.0 or higher. Please install or upgrade using the Library Manager."
27+
#endif
28+
29+
class AdafruitIO {
30+
31+
friend class AdafruitIO_Feed;
32+
33+
public:
34+
AdafruitIO(const char *user, const char *key);
35+
AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key);
36+
virtual ~AdafruitIO();
37+
38+
void connect();
39+
40+
void run(uint16_t busywait_ms = 0);
41+
42+
AdafruitIO_Feed* feed(const char *name);
43+
AdafruitIO_Feed* feed(const __FlashStringHelper *name);
44+
45+
const __FlashStringHelper* statusText();
46+
47+
aio_status_t status();
48+
virtual aio_status_t networkStatus() = 0;
49+
aio_status_t mqttStatus();
50+
51+
protected:
52+
virtual void _connect() = 0;
53+
aio_status_t _status = AIO_IDLE;
54+
uint32_t _last_ping = 0;
55+
56+
Adafruit_MQTT *_mqtt;
57+
58+
const char *_host = "io.adafruit.com";
59+
uint16_t _port = 8883;
60+
61+
uint16_t _packetread_timeout;
62+
63+
const char *_username;
64+
const char *_key;
65+
66+
char *_err_topic;
67+
char *_throttle_topic;
68+
69+
Adafruit_MQTT_Subscribe *_err_sub;
70+
Adafruit_MQTT_Subscribe *_throttle_sub;
71+
72+
private:
73+
void _init();
74+
75+
};
76+
77+
#endif // ADAFRUITIO_H

0 commit comments

Comments
 (0)