-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from adafruit/41-time-topics
Time helper class and examples added.
- Loading branch information
Showing
7 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
examples/adafruitio_17_time_subscribe/adafruitio_17_time_subscribe.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Adafruit IO Time Topic Subscription Example | ||
// | ||
// Adafruit invests time and resources providing this open source code. | ||
// Please support Adafruit and open source hardware by purchasing | ||
// products from Adafruit! | ||
// | ||
// Written by Adam Bachman, Brent Rubell for Adafruit Industries | ||
// Copyright (c) 2018 Adafruit Industries | ||
// Licensed under the MIT license. | ||
// | ||
// All text above must be included in any redistribution. | ||
|
||
/************************** Configuration ***********************************/ | ||
|
||
// edit the config.h tab and enter your Adafruit IO credentials | ||
// and any additional configuration needed for WiFi, cellular, | ||
// or ethernet clients. | ||
#include "config.h" | ||
|
||
/************************ Example Starts Here *******************************/ | ||
|
||
// set up the 'time/seconds' topic | ||
AdafruitIO_Time *seconds = io.time(AIO_TIME_SECONDS); | ||
|
||
// set up the 'time/milliseconds' topic | ||
AdafruitIO_Time *msecs = io.time(AIO_TIME_MILLIS); | ||
|
||
// set up the 'time/ISO-8601' topic | ||
AdafruitIO_Time *iso = io.time(AIO_TIME_ISO); | ||
|
||
void setup() { | ||
|
||
// start the serial connection | ||
Serial.begin(115200); | ||
|
||
// wait for serial monitor to open | ||
while(! Serial); | ||
|
||
Serial.print("Connecting to Adafruit IO"); | ||
|
||
// start MQTT connection to io.adafruit.com | ||
io.connect(); | ||
|
||
// attach message handler for the seconds feed | ||
seconds->onMessage(handleSecs); | ||
|
||
// attach a message handler for the msecs feed | ||
msecs->onMessage(handleMillis); | ||
|
||
// attach a message handler for the ISO feed | ||
iso->onMessage(handleISO); | ||
|
||
// wait for an MQTT connection | ||
// NOTE: when blending the HTTP and MQTT API, always use the mqttStatus | ||
// method to check on MQTT connection status specifically | ||
while(io.mqttStatus() < AIO_CONNECTED) { | ||
Serial.print("."); | ||
delay(500); | ||
} | ||
|
||
// we are connected | ||
Serial.println(); | ||
Serial.println(io.statusText()); | ||
|
||
} | ||
|
||
void loop() { | ||
|
||
// io.run(); is required for all sketches. | ||
// it should always be present at the top of your loop | ||
// function. it keeps the client connected to | ||
// io.adafruit.com, and processes any incoming data. | ||
io.run(); | ||
|
||
delay(5); | ||
// Because this sketch isn't publishing, we don't need | ||
// a delay() in the main program loop. | ||
|
||
} | ||
|
||
|
||
// message handler for the seconds feed | ||
void handleSecs(char *data, uint16_t len) { | ||
Serial.print("Seconds Feed: "); | ||
Serial.println(data); | ||
} | ||
|
||
|
||
// message handler for the milliseconds feed | ||
void handleMillis(char *data, uint16_t len) { | ||
Serial.print("Millis Feed: "); | ||
Serial.println(data); | ||
} | ||
|
||
|
||
// message handler for the ISO-8601 feed | ||
void handleISO(char *data, uint16_t len) { | ||
Serial.print("ISO Feed: "); | ||
Serial.println(data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/************************ Adafruit IO Config *******************************/ | ||
|
||
// visit io.adafruit.com if you need to create an account, | ||
// or if you need your Adafruit IO key. | ||
#define IO_USERNAME "your_username" | ||
#define IO_KEY "your_key" | ||
|
||
/******************************* WIFI **************************************/ | ||
|
||
// the AdafruitIO_WiFi client will work with the following boards: | ||
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471 | ||
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821 | ||
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010 | ||
// - Feather WICED -> https://www.adafruit.com/products/3056 | ||
|
||
#define WIFI_SSID "your_ssid" | ||
#define WIFI_PASS "your_pass" | ||
|
||
// comment out the following two lines if you are using fona or ethernet | ||
#include "AdafruitIO_WiFi.h" | ||
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS); | ||
|
||
|
||
/******************************* FONA **************************************/ | ||
|
||
// the AdafruitIO_FONA client will work with the following boards: | ||
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027 | ||
|
||
// uncomment the following two lines for 32u4 FONA, | ||
// and comment out the AdafruitIO_WiFi client in the WIFI section | ||
// #include "AdafruitIO_FONA.h" | ||
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY); | ||
|
||
|
||
/**************************** ETHERNET ************************************/ | ||
|
||
// the AdafruitIO_Ethernet client will work with the following boards: | ||
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201 | ||
|
||
// uncomment the following two lines for ethernet, | ||
// and comment out the AdafruitIO_WiFi client in the WIFI section | ||
// #include "AdafruitIO_Ethernet.h" | ||
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// Adafruit invests time and resources providing this open source code. | ||
// Please support Adafruit and open source hardware by purchasing | ||
// products from Adafruit! | ||
// | ||
// Copyright (c) 2015-2016 Adafruit Industries | ||
// Authors: Tony DiCola, Todd Treece, Adam Bachman | ||
// Licensed under the MIT license. | ||
// | ||
// All text above must be included in any redistribution. | ||
// | ||
#include "AdafruitIO_Time.h" | ||
#include "AdafruitIO.h" | ||
|
||
AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f):AdafruitIO_MQTT() | ||
{ | ||
_io = io; | ||
_sub = 0; | ||
_dataCallback = 0; | ||
format = f; | ||
|
||
_init(); | ||
} | ||
|
||
AdafruitIO_Time::~AdafruitIO_Time() | ||
{ | ||
if(_sub) | ||
delete _sub; | ||
|
||
if(data) | ||
delete data; | ||
|
||
if(_topic) | ||
free(_topic); | ||
} | ||
|
||
void AdafruitIO_Time::onMessage(AdafruitIOTimeCallbackType cb) | ||
{ | ||
_dataCallback = cb; | ||
} | ||
|
||
void AdafruitIO_Time::subCallback(char *val, uint16_t len) | ||
{ | ||
data = val; | ||
|
||
// call callback with data | ||
if(_dataCallback) | ||
_dataCallback(data, len); | ||
} | ||
|
||
void AdafruitIO_Time::_init() | ||
{ | ||
|
||
// dynamically allocate memory for mqtt topic and REST URLs | ||
const char *formatString; | ||
switch (format) { | ||
case AIO_TIME_SECONDS: | ||
formatString = "seconds"; | ||
break; | ||
case AIO_TIME_MILLIS: | ||
formatString = "millis"; | ||
break; | ||
case AIO_TIME_ISO: | ||
formatString = "ISO-8601"; | ||
break; | ||
} | ||
|
||
_topic = (char *) malloc(sizeof(char) * (strlen(formatString) + 6)); // 6 extra chars for "time/" and null termination | ||
|
||
if(_topic) { | ||
|
||
// build topic string | ||
strcpy(_topic, "time/"); | ||
strcat(_topic, formatString); | ||
|
||
// setup subscription | ||
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic); | ||
_io->_mqtt->subscribe(_sub); | ||
_sub->setCallback(this, &AdafruitIO_MQTT::subCallback); | ||
|
||
} else { | ||
|
||
// malloc failed | ||
_topic = 0; | ||
_sub = 0; | ||
data = 0; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// Adafruit invests time and resources providing this open source code. | ||
// Please support Adafruit and open source hardware by purchasing | ||
// products from Adafruit! | ||
// | ||
// Copyright (c) 2015-2016 Adafruit Industries | ||
// Authors: Tony DiCola, Todd Treece, Adam Bachman | ||
// Licensed under the MIT license. | ||
// | ||
// All text above must be included in any redistribution. | ||
// | ||
#ifndef ADAFRUITIO_TIME_H | ||
#define ADAFRUITIO_TIME_H | ||
|
||
#include "Arduino.h" | ||
#include "Adafruit_MQTT.h" | ||
#include "AdafruitIO_Definitions.h" | ||
#include "AdafruitIO_MQTT.h" | ||
|
||
// forward declaration | ||
class AdafruitIO; | ||
|
||
typedef void (*AdafruitIOTimeCallbackType)(char *value, uint16_t len); | ||
|
||
class AdafruitIO_Time : public AdafruitIO_MQTT { | ||
|
||
public: | ||
AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f); | ||
~AdafruitIO_Time(); | ||
void onMessage(AdafruitIOTimeCallbackType cb); | ||
void subCallback(char *val, uint16_t len); | ||
char *data; | ||
aio_time_format_t format; | ||
|
||
private: | ||
AdafruitIOTimeCallbackType _dataCallback; | ||
void _init(); | ||
char *_topic; | ||
Adafruit_MQTT_Subscribe *_sub; | ||
AdafruitIO *_io; | ||
|
||
}; | ||
|
||
#endif // ADAFRUITIO_FEED_H |