Skip to content

Commit 8409945

Browse files
author
brentru
committed
Merge remote-tracking branch 'adafruit/master'
2 parents 03bc6d2 + 682041c commit 8409945

File tree

10 files changed

+298
-4
lines changed

10 files changed

+298
-4
lines changed

Jenkinsfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
arduino {
22
verify = false
3-
platforms = ["esp8266:esp8266", "adafruit:avr", "arduino:samd", "adafruit:samd"]
3+
platforms = ["esp8266:esp8266", "esp32:esp32", "adafruit:avr", "arduino:samd", "adafruit:samd"]
44
libraries = ["Adafruit MQTT Library", "Adafruit FONA Library", "Ethernet2", "WiFi101"]
5-
boards = ["ESP8266", "M0_WINC1500", "WICED", "MKR1000", "M0_ETHERNETWING", "FONA_32U4"]
5+
boards = ["ESP8266", "M0_WINC1500", "WICED", "MKR1000", "M0_ETHERNETWING", "FONA_32U4", "ESP32"]
66
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[![Documentation Status](https://readthedocs.org/projects/adafruit-io-arduino/badge/?version=latest)](https://adafruit-io-arduino.readthedocs.io/en/latest/) [![Build Status](https://travis-ci.org/adafruit/Adafruit_IO_Arduino.svg?branch=master)](https://travis-ci.org/adafruit/Adafruit_IO_Arduino) [![Discord](https://img.shields.io/discord/327254708534116352.svg)](https://discord.gg/nBQh6qu)
44

5+
![AIOArduino](https://cdn-learn.adafruit.com/assets/assets/000/057/496/original/adafruit_io_AIOA.png?1531335660)
56

67
This library provides a simple device independent interface for interacting with [Adafruit IO](https://io.adafruit.com) using Arduino. It allows you to switch between WiFi (ESP8266, M0 WINC1500, & WICED), Cellular (32u4 FONA), and Ethernet (Ethernet FeatherWing)
78

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Adafruit IO Time Topic Subscription Example
2+
//
3+
// Adafruit invests time and resources providing this open source code.
4+
// Please support Adafruit and open source hardware by purchasing
5+
// products from Adafruit!
6+
//
7+
// Written by Adam Bachman, Brent Rubell for Adafruit Industries
8+
// Copyright (c) 2018 Adafruit Industries
9+
// Licensed under the MIT license.
10+
//
11+
// All text above must be included in any redistribution.
12+
13+
/************************** Configuration ***********************************/
14+
15+
// edit the config.h tab and enter your Adafruit IO credentials
16+
// and any additional configuration needed for WiFi, cellular,
17+
// or ethernet clients.
18+
#include "config.h"
19+
20+
/************************ Example Starts Here *******************************/
21+
22+
// set up the 'time/seconds' topic
23+
AdafruitIO_Time *seconds = io.time(AIO_TIME_SECONDS);
24+
25+
// set up the 'time/milliseconds' topic
26+
AdafruitIO_Time *msecs = io.time(AIO_TIME_MILLIS);
27+
28+
// set up the 'time/ISO-8601' topic
29+
AdafruitIO_Time *iso = io.time(AIO_TIME_ISO);
30+
31+
void setup() {
32+
33+
// start the serial connection
34+
Serial.begin(115200);
35+
36+
// wait for serial monitor to open
37+
while(! Serial);
38+
39+
Serial.print("Connecting to Adafruit IO");
40+
41+
// start MQTT connection to io.adafruit.com
42+
io.connect();
43+
44+
// attach message handler for the seconds feed
45+
seconds->onMessage(handleSecs);
46+
47+
// attach a message handler for the msecs feed
48+
msecs->onMessage(handleMillis);
49+
50+
// attach a message handler for the ISO feed
51+
iso->onMessage(handleISO);
52+
53+
// wait for an MQTT connection
54+
// NOTE: when blending the HTTP and MQTT API, always use the mqttStatus
55+
// method to check on MQTT connection status specifically
56+
while(io.mqttStatus() < AIO_CONNECTED) {
57+
Serial.print(".");
58+
delay(500);
59+
}
60+
61+
// we are connected
62+
Serial.println();
63+
Serial.println(io.statusText());
64+
65+
}
66+
67+
void loop() {
68+
69+
// io.run(); is required for all sketches.
70+
// it should always be present at the top of your loop
71+
// function. it keeps the client connected to
72+
// io.adafruit.com, and processes any incoming data.
73+
io.run();
74+
75+
// Because this sketch isn't publishing, we don't need
76+
// a delay() in the main program loop.
77+
78+
}
79+
80+
81+
// message handler for the seconds feed
82+
void handleSecs(char *data, uint16_t len) {
83+
Serial.print("Seconds Feed: ");
84+
Serial.println(data);
85+
}
86+
87+
88+
// message handler for the milliseconds feed
89+
void handleMillis(char *data, uint16_t len) {
90+
Serial.print("Millis Feed: ");
91+
Serial.println(data);
92+
}
93+
94+
95+
// message handler for the ISO-8601 feed
96+
void handleISO(char *data, uint16_t len) {
97+
Serial.print("ISO Feed: ");
98+
Serial.println(data);
99+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
14+
// - Feather WICED -> https://www.adafruit.com/products/3056
15+
16+
#define WIFI_SSID "your_ssid"
17+
#define WIFI_PASS "your_pass"
18+
19+
// comment out the following two lines if you are using fona or ethernet
20+
#include "AdafruitIO_WiFi.h"
21+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
22+
23+
24+
/******************************* FONA **************************************/
25+
26+
// the AdafruitIO_FONA client will work with the following boards:
27+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
28+
29+
// uncomment the following two lines for 32u4 FONA,
30+
// and comment out the AdafruitIO_WiFi client in the WIFI section
31+
// #include "AdafruitIO_FONA.h"
32+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
33+
34+
35+
/**************************** ETHERNET ************************************/
36+
37+
// the AdafruitIO_Ethernet client will work with the following boards:
38+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
39+
40+
// uncomment the following two lines for ethernet,
41+
// and comment out the AdafruitIO_WiFi client in the WIFI section
42+
// #include "AdafruitIO_Ethernet.h"
43+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Adafruit IO Arduino
2-
version=2.7.4
2+
version=2.7.7
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Arduino library to access Adafruit IO.
6-
paragraph=Arduino library to access Adafruit IO using the Adafruit ESP8266, M0 WINC1500, WICED, MKR1000, Ethernet, or FONA hardware.
6+
paragraph=Arduino library to access Adafruit IO using the Adafruit ESP8266, ESP32, M0 WINC1500, WICED, MKR1000, Ethernet, or FONA hardware.
77
category=Communication
88
url=https://github.com/adafruit/Adafruit_IO_Arduino
99
architectures=*

src/AdafruitIO.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ AdafruitIO_Feed* AdafruitIO::feed(const char* name)
7878
return new AdafruitIO_Feed(this, name);
7979
}
8080

81+
AdafruitIO_Time* AdafruitIO::time(aio_time_format_t format)
82+
{
83+
return new AdafruitIO_Time(this, format);
84+
}
85+
8186
AdafruitIO_Group* AdafruitIO::group(const char* name)
8287
{
8388
return new AdafruitIO_Group(this, name);

src/AdafruitIO.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "AdafruitIO_Group.h"
2020
#include "AdafruitIO_Dashboard.h"
2121
#include "AdafruitIO_Data.h"
22+
#include "AdafruitIO_Time.h"
2223
#include "ArduinoHttpClient.h"
2324
#include "util/AdafruitIO_Board.h"
2425

@@ -37,6 +38,7 @@ class AdafruitIO {
3738
friend class AdafruitIO_Group;
3839
friend class AdafruitIO_Dashboard;
3940
friend class AdafruitIO_Block;
41+
friend class AdafruitIO_Time;
4042

4143
public:
4244
AdafruitIO(const char *user, const char *key);
@@ -49,6 +51,7 @@ class AdafruitIO {
4951
AdafruitIO_Feed* feed(const char *name);
5052
AdafruitIO_Group* group(const char *name);
5153
AdafruitIO_Dashboard* dashboard(const char *name);
54+
AdafruitIO_Time* time(aio_time_format_t format);
5255

5356
const __FlashStringHelper* statusText();
5457

src/AdafruitIO_Definitions.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,13 @@ typedef enum {
104104

105105
} aio_status_t;
106106

107+
typedef enum {
108+
109+
AIO_TIME_SECONDS = 0,
110+
AIO_TIME_MILLIS = 1,
111+
AIO_TIME_ISO = 2
112+
113+
} aio_time_format_t;
114+
107115
#endif /* ADAFRUITIO_DEFINITIONS_H_ */
116+

src/AdafruitIO_Time.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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, Adam Bachman
8+
// Licensed under the MIT license.
9+
//
10+
// All text above must be included in any redistribution.
11+
//
12+
#include "AdafruitIO_Time.h"
13+
#include "AdafruitIO.h"
14+
15+
AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f):AdafruitIO_MQTT()
16+
{
17+
_io = io;
18+
_sub = 0;
19+
_dataCallback = 0;
20+
format = f;
21+
22+
_init();
23+
}
24+
25+
AdafruitIO_Time::~AdafruitIO_Time()
26+
{
27+
if(_sub)
28+
delete _sub;
29+
30+
if(data)
31+
delete data;
32+
33+
if(_topic)
34+
free(_topic);
35+
}
36+
37+
void AdafruitIO_Time::onMessage(AdafruitIOTimeCallbackType cb)
38+
{
39+
_dataCallback = cb;
40+
}
41+
42+
void AdafruitIO_Time::subCallback(char *val, uint16_t len)
43+
{
44+
data = val;
45+
46+
// call callback with data
47+
if(_dataCallback)
48+
_dataCallback(data, len);
49+
}
50+
51+
void AdafruitIO_Time::_init()
52+
{
53+
54+
// dynamically allocate memory for mqtt topic and REST URLs
55+
const char *formatString;
56+
switch (format) {
57+
case AIO_TIME_SECONDS:
58+
formatString = "seconds";
59+
break;
60+
case AIO_TIME_MILLIS:
61+
formatString = "millis";
62+
break;
63+
case AIO_TIME_ISO:
64+
formatString = "ISO-8601";
65+
break;
66+
}
67+
68+
_topic = (char *) malloc(sizeof(char) * (strlen(formatString) + 6)); // 6 extra chars for "time/" and null termination
69+
70+
if(_topic) {
71+
72+
// build topic string
73+
strcpy(_topic, "time/");
74+
strcat(_topic, formatString);
75+
76+
// setup subscription
77+
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic);
78+
_io->_mqtt->subscribe(_sub);
79+
_sub->setCallback(this, &AdafruitIO_MQTT::subCallback);
80+
81+
} else {
82+
83+
// malloc failed
84+
_topic = 0;
85+
_sub = 0;
86+
data = 0;
87+
88+
}
89+
90+
}

src/AdafruitIO_Time.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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, Adam Bachman
8+
// Licensed under the MIT license.
9+
//
10+
// All text above must be included in any redistribution.
11+
//
12+
#ifndef ADAFRUITIO_TIME_H
13+
#define ADAFRUITIO_TIME_H
14+
15+
#include "Arduino.h"
16+
#include "Adafruit_MQTT.h"
17+
#include "AdafruitIO_Definitions.h"
18+
#include "AdafruitIO_MQTT.h"
19+
20+
// forward declaration
21+
class AdafruitIO;
22+
23+
typedef void (*AdafruitIOTimeCallbackType)(char *value, uint16_t len);
24+
25+
class AdafruitIO_Time : public AdafruitIO_MQTT {
26+
27+
public:
28+
AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f);
29+
~AdafruitIO_Time();
30+
void onMessage(AdafruitIOTimeCallbackType cb);
31+
void subCallback(char *val, uint16_t len);
32+
char *data;
33+
aio_time_format_t format;
34+
35+
private:
36+
AdafruitIOTimeCallbackType _dataCallback;
37+
void _init();
38+
char *_topic;
39+
Adafruit_MQTT_Subscribe *_sub;
40+
AdafruitIO *_io;
41+
42+
};
43+
44+
#endif // ADAFRUITIO_FEED_H

0 commit comments

Comments
 (0)