Skip to content

Commit

Permalink
Shared Feeds (#52)
Browse files Browse the repository at this point in the history
* add owner field to AdafruitIO_Feed

* remove spurious setup methods

* clean up some compiler warnings

* Fixing string comparison compile warnings

add strcmp to property assign.

* add example of feed writing

* adding feed writing example, bump lib version up
  • Loading branch information
brentru authored Aug 2, 2018
1 parent 632db97 commit b4af23a
Show file tree
Hide file tree
Showing 15 changed files with 358 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Adafruit IO Shared Feeds Write Example
// desc: Example of writing a button value to a shared feed.
//
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-feeds/sharing-a-feed
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by 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 *******************************/

// digital pin 5
#define BUTTON_PIN 5

// the Adafruit IO username of whomever owns the feed
#define FEED_OWNER "AIO_FEED_OWNER"

// set up a shared feed between you and the FEED_OWNER
// make sure you have both read AND write access to this feed
AdafruitIO_Feed *sharedFeed = io.feed("FEED-NAME", FEED_OWNER);

// button state
bool current = false;
bool last = false;

void setup() {

// set button pin as an input
pinMode(BUTTON_PIN, INPUT);

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// wait for a connection
while(io.status() < 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();

// grab the current state of the button.
// we have to flip the logic because we are
// using a pullup resistor.
if(digitalRead(BUTTON_PIN) == LOW)
current = true;
else
current = false;

// return if the value hasn't changed
if(current == last)
return;

// save the current state to the 'sharedFeed' feed on adafruit io
Serial.print("sending button -> ");
Serial.println(current);
sharedFeed->save(current);

// store last button state
last = current;

}
44 changes: 44 additions & 0 deletions examples/adafruitio_20_shared_feed_write/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/************************ 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 HUZZAH ESP32 -> https://www.adafruit.com/product/3405
// - 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);
78 changes: 78 additions & 0 deletions examples/adafruitio_21_feed_read/adafruitio_21_feed_read.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Adafruit IO Feed Reading
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-feeds/sharing-a-feed
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by 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 *******************************/

// the Adafruit IO username of whomever owns the feed
#define FEED_OWNER "AIO_FEED_OWNER"

// set up the `sharedFeed`
AdafruitIO_Feed *sharedFeed = io.feed("FEED-NAME", FEED_OWNER);

void setup() {

// start the serial connection
Serial.begin(115200);

// wait for serial monitor to open
while(! Serial);

// connect to io.adafruit.com
Serial.print("Connecting to Adafruit IO");
io.connect();

// set up a message handler for the 'sharedFeed' feed.
// the handleMessage function (defined below)
// will be called whenever a message is
// received from adafruit io.
sharedFeed->onMessage(handleMessage);

// wait for a connection
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}

// we are connected
Serial.println();
Serial.println(io.statusText());
sharedFeed->get();

}

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();

}

// this function is called whenever an 'sharedFeed' feed message
// is received from Adafruit IO. it was attached to
// the 'digital' feed in the setup() function above.
void handleMessage(AdafruitIO_Data *data) {

Serial.print("received <- ");
Serial.println(data->toInt());

}
44 changes: 44 additions & 0 deletions examples/adafruitio_21_feed_read/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/************************ 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 HUZZAH ESP32 -> https://www.adafruit.com/product/3405
// - 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);
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit IO Arduino
version=2.7.9
version=2.7.10
author=Adafruit
maintainer=Adafruit <[email protected]>
sentence=Arduino library to access Adafruit IO.
Expand Down
5 changes: 5 additions & 0 deletions src/AdafruitIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ AdafruitIO_Feed* AdafruitIO::feed(const char* name)
return new AdafruitIO_Feed(this, name);
}

AdafruitIO_Feed* AdafruitIO::feed(const char* name, const char* owner)
{
return new AdafruitIO_Feed(this, name, owner);
}

AdafruitIO_Time* AdafruitIO::time(aio_time_format_t format)
{
return new AdafruitIO_Time(this, format);
Expand Down
1 change: 1 addition & 0 deletions src/AdafruitIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AdafruitIO {
void run(uint16_t busywait_ms = 0);

AdafruitIO_Feed* feed(const char *name);
AdafruitIO_Feed* feed(const char *name, const char *owner);
AdafruitIO_Group* group(const char *name);
AdafruitIO_Dashboard* dashboard(const char *name);
AdafruitIO_Time* time(aio_time_format_t format);
Expand Down
38 changes: 25 additions & 13 deletions src/AdafruitIO_Feed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n):AdafruitIO_MQTT(
{
_io = io;
name = n;
_sub = 0;
_pub = 0;
_get_pub = 0;
_dataCallback = 0;
owner = _io->_username;

_init();
}

AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n, const char *un):AdafruitIO_MQTT()
{
_io = io;
name = n;
owner = un;

_init();
}
Expand Down Expand Up @@ -159,7 +165,7 @@ AdafruitIO_Data* AdafruitIO_Feed::lastValue()
{
// 15 extra for api path, 12 for /data/retain, 1 for null
String url = "/api/v2/";
url += _io->_username;
url += owner;
url += "/feeds/";
url += name;
url += "/data/retain";
Expand All @@ -181,6 +187,8 @@ AdafruitIO_Data* AdafruitIO_Feed::lastValue()
return new AdafruitIO_Data(this, body.c_str());
}

return NULL;

} else {

AIO_ERROR_PRINT("error retrieving lastValue, status: ");
Expand Down Expand Up @@ -209,37 +217,41 @@ void AdafruitIO_Feed::subCallback(char *val, uint16_t len)

void AdafruitIO_Feed::_init()
{
_sub = 0;
_pub = 0;
_get_pub = 0;
_dataCallback = 0;

// dynamically allocate memory for mqtt topic and REST URLs
_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 8)); // 8 extra chars for /f/, /csv & null termination
_get_topic = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 12)); // 12 extra chars for /f/, /csv/get & null termination
_feed_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + strlen(name) + 16)); // 16 extra for api path & null term
_create_url = (char *) malloc(sizeof(char) * (strlen(_io->_username) + 15)); // 15 extra for api path & null term
_topic = (char *) malloc(sizeof(char) * (strlen(owner) + strlen(name) + 8)); // 8 extra chars for /f/, /csv & null termination
_get_topic = (char *) malloc(sizeof(char) * (strlen(owner) + strlen(name) + 12)); // 12 extra chars for /f/, /csv/get & null termination
_feed_url = (char *) malloc(sizeof(char) * (strlen(owner) + strlen(name) + 16)); // 16 extra for api path & null term
_create_url = (char *) malloc(sizeof(char) * (strlen(owner) + 15)); // 15 extra for api path & null term

// init feed data
data = new AdafruitIO_Data(this);

if(_topic && _create_url && _feed_url) {

// build topic string
strcpy(_topic, _io->_username);
strcpy(_topic, owner);
strcat(_topic, "/f/");
strcat(_topic, name);
strcat(_topic, "/csv");

// build feed url string
strcpy(_feed_url, "/api/v2/");
strcat(_feed_url, _io->_username);
strcat(_feed_url, owner);
strcat(_feed_url, "/feeds/");
strcat(_feed_url, name);

// build create url string
strcpy(_create_url, "/api/v2/");
strcat(_create_url, _io->_username);
strcat(_create_url, owner);
strcat(_create_url, "/feeds");

// build /get topic string
strcpy(_get_topic, _io->_username);
strcpy(_get_topic, owner);
strcat(_get_topic, "/f/");
strcat(_get_topic, name);
strcat(_get_topic, "/csv/get");
Expand Down
3 changes: 3 additions & 0 deletions src/AdafruitIO_Feed.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class AdafruitIO_Feed : public AdafruitIO_MQTT {

public:
AdafruitIO_Feed(AdafruitIO *io, const char *name);
AdafruitIO_Feed(AdafruitIO *io, const char *name, const char *owner);

~AdafruitIO_Feed();

bool save(char *value, double lat=0, double lon=0, double ele=0);
Expand All @@ -48,6 +50,7 @@ class AdafruitIO_Feed : public AdafruitIO_MQTT {
void subCallback(char *val, uint16_t len);

const char *name;
const char *owner;

AdafruitIO_Data *lastValue();
AdafruitIO_Data *data;
Expand Down
Loading

0 comments on commit b4af23a

Please sign in to comment.