first commit

This commit is contained in:
Jérôme Delacotte
2025-03-06 11:15:32 +01:00
commit 7b30d6e298
5276 changed files with 2108927 additions and 0 deletions

View File

@@ -0,0 +1,466 @@
/*!
* @file AdafruitIO.cpp
*
* @mainpage Adafruit IO Arduino Client Library
*
* @section intro_sec Introduction
*
* This is the documentation for the Adafruit IO Arduino library. This library
* provides a simple device independent interface for interacting with Adafruit
* IO using Arduino. It allows you to switch between WiFi (ESP8266, ESP32,
* AirLift, WINC1500, & WICED), Cellular (32u4 FONA), and Ethernet (Ethernet
* FeatherWing)
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section dependencies Dependencies
*
* This library depends on <a
* href="https://github.com/arduino-libraries/ArduinoHttpClient">
* ArduinoHTTPClient</a> and <a
* href="https://github.com/adafruit/Adafruit_MQTT_Library"> Adafruit MQTT
* Library</a> being present on your system. Please make sure you have installed
* the latest version before using this library.
*
* @section author Author
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* @section license License
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Instantiate the AIO object.
@param user
A pointer to a constant AIO user name.
@param key
A pointer to a constant key for the user name.
*/
/**************************************************************************/
AdafruitIO::AdafruitIO(const char *user, const char *key) {
_mqtt = 0;
_http = 0;
_username = user;
_key = key;
_err_topic = 0;
_throttle_topic = 0;
_err_sub = 0;
_throttle_sub = 0;
_packetread_timeout = 100;
_user_agent = 0;
_init();
}
/**************************************************************************/
/*!
@brief Initialize the AIO object.
*/
/**************************************************************************/
void AdafruitIO::_init() {
// we have never pinged, so set last ping to now
_last_ping = millis();
// dynamically allocate memory for err topic
_err_topic = (char *)malloc(
sizeof(char) * (strlen(_username) + strlen(AIO_ERROR_TOPIC) + 1));
if (_err_topic) {
// build error topic
strcpy(_err_topic, _username);
strcat(_err_topic, AIO_ERROR_TOPIC);
} else {
// malloc failed
_err_topic = 0;
}
// dynamically allocate memory for throttle topic
_throttle_topic = (char *)malloc(
sizeof(char) * (strlen(_username) + strlen(AIO_THROTTLE_TOPIC) + 1));
if (_throttle_topic) {
// build throttle topic
strcpy(_throttle_topic, _username);
strcat(_throttle_topic, AIO_THROTTLE_TOPIC);
} else {
// malloc failed
_throttle_topic = 0;
}
}
/**************************************************************************/
/*!
@brief Destructor to end the AIO object.
*/
/**************************************************************************/
AdafruitIO::~AdafruitIO() {
if (_err_topic)
free(_err_topic);
if (_throttle_topic)
free(_throttle_topic);
if (_err_sub)
delete _err_sub;
if (_throttle_sub)
delete _throttle_sub;
}
/**************************************************************************/
/*!
@brief Prints errors
@param err
An error string to print.
@param len
The length of the error string.
*/
/**************************************************************************/
void errorCallback(char *err, uint16_t len) {
AIO_ERROR_PRINTLN();
AIO_ERROR_PRINT("ERROR: ");
AIO_ERROR_PRINTLN(err);
AIO_ERROR_PRINTLN();
}
/**************************************************************************/
/*!
@brief Connects to AIO, setting up using parameters set when the
class is instantiated.
*/
/**************************************************************************/
void AdafruitIO::connect() {
AIO_DEBUG_PRINTLN("AdafruitIO::connect()");
_last_mqtt_connect = 0; // need to start over fresh
_status = AIO_IDLE;
_last_ping = 0;
if (_err_sub) {
// setup error sub
_err_sub = new Adafruit_MQTT_Subscribe(_mqtt, _err_topic);
_mqtt->subscribe(_err_sub);
_err_sub->setCallback(errorCallback);
}
if (_throttle_sub) {
// setup throttle sub
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
_mqtt->subscribe(_throttle_sub);
_throttle_sub->setCallback(errorCallback);
}
_connect();
}
/**************************************************************************/
/*!
@brief Disconnects from WiFi.
*/
/**************************************************************************/
void AdafruitIO::wifi_disconnect() {
AIO_DEBUG_PRINTLN("AdafruitIO::wifi_disconnect()");
_disconnect();
}
/**************************************************************************/
/*!
@brief Create a new AIO feed.
@param name
The AIO name of the feed.
@return A pointer to the feed.
*/
/**************************************************************************/
AdafruitIO_Feed *AdafruitIO::feed(const char *name) {
return new AdafruitIO_Feed(this, name);
}
/**************************************************************************/
/*!
@brief Create a new AIO feed.
@param name
The AIO name of the feed.
@param owner
The AIO name of the user that owns the feed, if not the current
user.
@return A pointer to the feed.
*/
/**************************************************************************/
AdafruitIO_Feed *AdafruitIO::feed(const char *name, const char *owner) {
return new AdafruitIO_Feed(this, name, owner);
}
/**************************************************************************/
/*!
@brief Create a new AIO time.
@param format
A format specifier.
@return A pointer to the time.
*/
/**************************************************************************/
AdafruitIO_Time *AdafruitIO::time(aio_time_format_t format) {
return new AdafruitIO_Time(this, format);
}
/**************************************************************************/
/*!
@brief Create a new AIO group.
@param name
The AIO name of the group.
@return A pointer to the group.
*/
/**************************************************************************/
AdafruitIO_Group *AdafruitIO::group(const char *name) {
return new AdafruitIO_Group(this, name);
}
/**************************************************************************/
/*!
@brief Create a new AIO dashboard.
@param name
The AIO name of the dashboard.
@return A pointer to the dashboard.
*/
/**************************************************************************/
AdafruitIO_Dashboard *AdafruitIO::dashboard(const char *name) {
return new AdafruitIO_Dashboard(this, name);
}
/**************************************************************************/
/*!
@brief Provide status explanation strings.
@return A pointer to the status string, _status. _status is the AIO status
value
*/
/**************************************************************************/
const __FlashStringHelper *AdafruitIO::statusText() {
switch (_status) {
// CONNECTING
case AIO_IDLE:
return F("Idle. Waiting for connect to be called...");
case AIO_NET_DISCONNECTED:
return F("Network disconnected.");
case AIO_DISCONNECTED:
return F("Disconnected from Adafruit IO.");
// FAILURE
case AIO_NET_CONNECT_FAILED:
return F("Network connection failed.");
case AIO_CONNECT_FAILED:
return F("Adafruit IO connection failed.");
case AIO_FINGERPRINT_INVALID:
return F("Adafruit IO SSL fingerprint verification failed.");
case AIO_AUTH_FAILED:
return F("Adafruit IO authentication failed.");
// SUCCESS
case AIO_NET_CONNECTED:
return F("Network connected.");
case AIO_CONNECTED:
return F("Adafruit IO connected.");
case AIO_CONNECTED_INSECURE:
return F("Adafruit IO connected. **THIS CONNECTION IS INSECURE** SSL/TLS "
"not supported for this platform.");
case AIO_FINGERPRINT_UNSUPPORTED:
return F("Adafruit IO connected over SSL/TLS. Fingerprint verification "
"unsupported.");
case AIO_FINGERPRINT_VALID:
return F("Adafruit IO connected over SSL/TLS. Fingerprint valid.");
default:
return F("Unknown status code");
}
}
/**************************************************************************/
/*!
@brief Must be called frequently to keep AIO connections alive. When
called with no arguments run() will try to repair MQTT and WiFi
connections before returning. To avoid potentially long timeout
delays, sketches can use the busywait_ms and fail_fast arguments
to return an imperfect status quickly. The calling sketch will
then need to respond appropriately to that status.
@param busywait_ms
The packet read timeout, optional.
@param fail_fast
Set true to skip retries and return with status immediately,
optional.
@return AIO status value
*/
/**************************************************************************/
aio_status_t AdafruitIO::run(uint16_t busywait_ms, bool fail_fast) {
uint32_t timeStart = millis();
if (status() < AIO_NET_CONNECTED) { // If we aren't network connected...
if (fail_fast)
return status(); // return status and fail quickly
else { // or try to reconnect from the start
AIO_ERROR_PRINT("run() connection failed -- retrying");
unsigned long started = millis();
connect();
// wait for a full AIO connection then carry on
while (status() < AIO_CONNECTED) {
// or return an error if the reconnection times out
if (millis() - started > AIO_NET_CONNECTION_TIMEOUT)
return status();
delay(500);
}
}
}
// loop until we have a connection
// mqttStatus() will try to reconnect before returning
while (mqttStatus(fail_fast) != AIO_CONNECTED &&
millis() - timeStart < AIO_MQTT_CONNECTION_TIMEOUT) {
}
if (mqttStatus(fail_fast) != AIO_CONNECTED)
return status();
if (busywait_ms > 0)
_packetread_timeout = busywait_ms;
_mqtt->processPackets(_packetread_timeout);
// ping to keep connection alive if needed
if (millis() > (_last_ping + AIO_PING_INTERVAL)) {
_mqtt->ping();
_last_ping = millis();
}
return status();
}
/**************************************************************************/
/*!
@brief Status check.
@return An AIO status value. Lower values represent poorer connection
status.
*/
/**************************************************************************/
aio_status_t AdafruitIO::status() {
aio_status_t net_status = networkStatus();
// if we aren't connected, return network status
if (net_status != AIO_NET_CONNECTED) {
_status = net_status;
return _status;
}
// check mqtt status and return
_status = mqttStatus();
return _status;
}
/**************************************************************************/
/*!
@brief Identify the board.
@return A board ID
*/
/**************************************************************************/
char *AdafruitIO::boardID() { return AdafruitIO_Board::id(); }
/**************************************************************************/
/*!
@brief Identify the board type.
@return A board type
*/
/**************************************************************************/
const char *AdafruitIO::boardType() { return AdafruitIO_Board::type(); }
/**************************************************************************/
/*!
@brief Identify the software version.
@return A pointer to a version number string.
*/
/**************************************************************************/
char *AdafruitIO::version() {
sprintf(_version, "%d.%d.%d", ADAFRUITIO_VERSION_MAJOR,
ADAFRUITIO_VERSION_MINOR, ADAFRUITIO_VERSION_PATCH);
return _version;
}
/**************************************************************************/
/*!
@brief Identify the user agent.
@return A pointer to a user agent string.
*/
/**************************************************************************/
char *AdafruitIO::userAgent() {
if (!_user_agent) {
_user_agent =
(char *)malloc(sizeof(char) * (strlen(version()) + strlen(boardType()) +
strlen(connectionType()) + 24));
strcpy(_user_agent, "AdafruitIO-Arduino/");
strcat(_user_agent, version());
strcat(_user_agent, " (");
strcat(_user_agent, boardType());
strcat(_user_agent, "-");
strcat(_user_agent, connectionType());
strcat(_user_agent, ")");
}
return _user_agent;
}
/**************************************************************************/
/*!
@brief Checks connection status with Adafruit IO's MQTT broker.
@param fail_fast
Set true to skip retries and return with status immediately.
@return True if connected, otherwise False.
*/
/**************************************************************************/
aio_status_t AdafruitIO::mqttStatus(bool fail_fast) {
// if the connection failed,
// return so we don't hammer IO
if (_status == AIO_CONNECT_FAILED) {
AIO_ERROR_PRINT("mqttStatus() failed to connect");
AIO_ERROR_PRINTLN(_mqtt->connectErrorString(_status));
return _status;
}
if (_mqtt->connected())
return AIO_CONNECTED;
// prevent fast reconnect attempts, except for the first time through
if (_last_mqtt_connect == 0 ||
millis() - _last_mqtt_connect > AIO_THROTTLE_RECONNECT_INTERVAL) {
_last_mqtt_connect = millis();
switch (_mqtt->connect(_username, _key)) {
case 0:
return AIO_CONNECTED;
case 1: // invalid mqtt protocol
case 2: // client id rejected
case 4: // malformed user/pass
case 5: // unauthorized
return AIO_CONNECT_FAILED;
case 3: // mqtt service unavailable
case 6: // throttled
case 7: // banned -> all MQTT bans are temporary, so eventual retry is
// permitted
// delay to prevent fast reconnects and fast returns (backward
// compatibility)
if (!fail_fast)
delay(AIO_THROTTLE_RECONNECT_INTERVAL);
return AIO_DISCONNECTED;
default:
return AIO_DISCONNECTED;
}
}
return AIO_DISCONNECTED;
}

View File

@@ -0,0 +1,174 @@
/*!
* @file AdafruitIO.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_H
#define ADAFRUITIO_H
#include "AdafruitIO_Dashboard.h"
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_Feed.h"
#include "AdafruitIO_Group.h"
#include "AdafruitIO_Time.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
#include "ArduinoHttpClient.h"
#include "util/AdafruitIO_Board.h"
#ifndef ADAFRUIT_MQTT_VERSION_MAJOR
#error \
"This sketch requires Adafruit MQTT Library v1.0.0 or higher. Please install or upgrade using the Library Manager."
#endif
#if ADAFRUIT_MQTT_VERSION_MAJOR == 1 && ADAFRUIT_MQTT_VERSION_MINOR < 0
#error \
"This sketch requires Adafruit MQTT Library v1.0.0 or higher. Please install or upgrade using the Library Manager."
#endif
/**************************************************************************/
/*!
@brief Class for interacting with Adafruit IO
*/
/**************************************************************************/
class AdafruitIO {
/**
* @brief AdafruitIO_Feed addition.
* @relates AdafruitIO_Feed
*/
friend class AdafruitIO_Feed;
/**
* @brief AdafruitIO_Group addition.
* @relates AdafruitIO_Group
*/
friend class AdafruitIO_Group;
/**
* @brief AdafruitIO_Dashboard addition.
* @relates AdafruitIO_Dashboard
*/
friend class AdafruitIO_Dashboard;
/**
* @brief AdafruitIO_Block addition.
* @relates AdafruitIO_Block
*/
friend class AdafruitIO_Block;
/**
* @brief AdafruitIO_Time addition.
* @relates AdafruitIO_Time
*/
friend class AdafruitIO_Time;
public:
AdafruitIO(const char *user, const char *key);
virtual ~AdafruitIO();
void connect();
void wifi_disconnect();
aio_status_t run(uint16_t busywait_ms = 0, bool fail_fast = false);
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);
const __FlashStringHelper *statusText();
aio_status_t status();
/********************************************************************/
/*!
@brief Returns network module status.
@return 0
*/
/*******************************************************************/
virtual aio_status_t networkStatus() = 0;
/********************************************************************/
/*!
@brief Returns MQTT connection status.
*/
/*******************************************************************/
aio_status_t mqttStatus(bool fail_fast = false);
char *boardID();
const char *boardType();
char *version();
char *userAgent();
/********************************************************************/
/*!
@brief Returns the Adafruit IO network module connection type.
@return 0
*/
/*******************************************************************/
virtual const char *connectionType() = 0;
protected:
/********************************************************************/
/*!
@brief Establishes a connection with the Adafruit IO MQTT broker.
@return 0
*/
/*******************************************************************/
virtual void _connect() = 0;
/******************************************************/
/*!
@brief Disconnects from the Adafruit IO MQTT broker.
@return 0
*/
/*****************************************************/
virtual void _disconnect() = 0;
aio_status_t _status = AIO_IDLE; /*!< Adafruit IO Connection Status */
uint32_t _last_ping =
0; /*!< Previous time when client pinged Adafruit IO, in milliseconds */
uint32_t _last_mqtt_connect = 0; /*!< Previous time when client connected to
Adafruit IO, in milliseconds */
Adafruit_MQTT *_mqtt; /*!< Reference to Adafruit_MQTT, _mqtt. */
HttpClient *_http; /*!< Reference to HTTPClient, _http */
char _version[10]; /*!< Adafruit IO Arduino library version */
const char *_host = "io.adafruit.com"; /*!< Adafruit IO URL */
uint16_t _mqtt_port = 8883; /*!< Adafruit IO MQTT SSL port */
uint16_t _mqtt_eth_port =
1883; /*!< Adafruit IO MQTT insecure port, used by ethernet clients. */
uint16_t _http_port = 443; /*!< Adafruit IO HTTP SSL port */
uint16_t _packetread_timeout; /*!< Maximum amount of time to wait before
processing packets. */
const char *_username; /*!< Adafruit IO Username. */
const char *_key; /*!< Adafruit IO Key. */
char *_err_topic; /*!< Adafruit IO MQTT error message topic. */
char *_throttle_topic; /*!< Adafruit IO MQTT throttle message topic. */
char *_user_agent; /*!< Identifies the Adafruit IO client. */
Adafruit_MQTT_Subscribe
*_err_sub; /*!< Subscription to Adafruit IO Error topic. */
Adafruit_MQTT_Subscribe
*_throttle_sub; /*!< Subscription to Adafruit IO Throttle topic. */
private:
void _init();
};
#endif // ADAFRUITIO_H

View File

@@ -0,0 +1,234 @@
/*!
* @file AdafruitIO_Dashboard.cpp
*
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*
*/
#include "AdafruitIO_Dashboard.h"
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Sets Adafruit IO Dashboard instance.
@param *io
Reference to Adafruit IO class.
@param *n
Valid username string.
*/
/**************************************************************************/
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *n) {
_io = io;
name = n;
}
AdafruitIO_Dashboard::~AdafruitIO_Dashboard() {}
/**************************************************************************/
/*!
@brief Checks if Adafruit IO Dashboard exists.
https://io.adafruit.com/api/docs/#return-dashboard
@return True if successful, otherwise False.
*/
/**************************************************************************/
bool AdafruitIO_Dashboard::exists() {
String url = "/api/v2/";
url += _io->_username;
url += "/dashboards/";
url += name;
_io->_http->beginRequest();
_io->_http->get(url.c_str());
_io->_http->sendHeader("X-AIO-Key", _io->_key);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 200;
}
/**************************************************************************/
/*!
@brief Creates a new dashboard.
https://io.adafruit.com/api/docs/#create-a-dashboard
@return True if successful, otherwise False.
*/
/**************************************************************************/
bool AdafruitIO_Dashboard::create() {
String url = "/api/v2/";
url += _io->_username;
url += "/dashboards";
String body = "name=";
body += name;
_io->_http->beginRequest();
_io->_http->post(url.c_str());
_io->_http->sendHeader("Content-Type", "application/x-www-form-urlencoded");
_io->_http->sendHeader("Content-Length", body.length());
_io->_http->sendHeader("X-AIO-Key", _io->_key);
// the following call to endRequest
// should be replaced by beginBody once the
// Arduino HTTP Client Library is updated
// _io->_http->beginBody();
_io->_http->endRequest();
_io->_http->print(body);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 201;
}
/**************************************************************************/
/*!
@brief Returns the dashboard owner.
@return Adafruit IO username.
*/
/**************************************************************************/
const char *AdafruitIO_Dashboard::user() { return _io->_username; }
AdafruitIO *AdafruitIO_Dashboard::io() { return _io; }
/**************************************************************************/
/*!
@brief Creates a new toggle block element on a dashboard connected
to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Toggle block dashboard element.
*/
/**************************************************************************/
ToggleBlock *AdafruitIO_Dashboard::addToggleBlock(AdafruitIO_Feed *feed) {
return new ToggleBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new momentary block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Momentary block dashboard element.
*/
/**************************************************************************/
MomentaryBlock *AdafruitIO_Dashboard::addMomentaryBlock(AdafruitIO_Feed *feed) {
return new MomentaryBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new slider block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Slider block dashboard element.
*/
/**************************************************************************/
SliderBlock *AdafruitIO_Dashboard::addSliderBlock(AdafruitIO_Feed *feed) {
return new SliderBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new gauge block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Gauge block dashboard element.
*/
/**************************************************************************/
GaugeBlock *AdafruitIO_Dashboard::addGaugeBlock(AdafruitIO_Feed *feed) {
return new GaugeBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new momentary block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Text block dashboard element.
*/
/**************************************************************************/
TextBlock *AdafruitIO_Dashboard::addTextBlock(AdafruitIO_Feed *feed) {
return new TextBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new chart block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Chart block dashboard element.
*/
/**************************************************************************/
ChartBlock *AdafruitIO_Dashboard::addChartBlock(AdafruitIO_Feed *feed) {
return new ChartBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new color block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Color block dashboard element.
*/
/**************************************************************************/
ColorBlock *AdafruitIO_Dashboard::addColorBlock(AdafruitIO_Feed *feed) {
return new ColorBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new map block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Map block dashboard element.
*/
/**************************************************************************/
MapBlock *AdafruitIO_Dashboard::addMapBlock(AdafruitIO_Feed *feed) {
return new MapBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new stream block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Stream block dashboard element.
*/
/**************************************************************************/
StreamBlock *AdafruitIO_Dashboard::addStreamBlock(AdafruitIO_Feed *feed) {
return new StreamBlock(this, feed);
}
/**************************************************************************/
/*!
@brief Creates a new image block element on a dashboard
connected to provided feed.
@param *feed
Reference to an Adafruit IO feed.
@return Image block dashboard element.
*/
/**************************************************************************/
ImageBlock *AdafruitIO_Dashboard::addImageBlock(AdafruitIO_Feed *feed) {
return new ImageBlock(this, feed);
}

View File

@@ -0,0 +1,76 @@
/*!
* @file AdafruitIO_Dashboard.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_DASHBOARD_H
#define ADAFRUITIO_DASHBOARD_H
#include "AdafruitIO_Definitions.h"
#include "Arduino.h"
#include "blocks/ChartBlock.h"
#include "blocks/ColorBlock.h"
#include "blocks/GaugeBlock.h"
#include "blocks/ImageBlock.h"
#include "blocks/MapBlock.h"
#include "blocks/MomentaryBlock.h"
#include "blocks/SliderBlock.h"
#include "blocks/StreamBlock.h"
#include "blocks/TextBlock.h"
#include "blocks/ToggleBlock.h"
// forward declaration
class AdafruitIO;
class AdafruitIO_Feed;
/**************************************************************************/
/*!
@brief Class for interacting with Adafruit IO Dashboards.
https://io.adafruit.com/api/docs/#dashboards
*/
/**************************************************************************/
class AdafruitIO_Dashboard {
public:
AdafruitIO_Dashboard(AdafruitIO *io, const char *name);
~AdafruitIO_Dashboard();
const char *name; /*!< Dashboard name. */
const char *user(); /*!< Dashboard owner's Adafruit IO username. */
/**************************************************************************/
/*!
@brief Creates an instance of AdafruitIO.
@return True
*/
/**************************************************************************/
AdafruitIO *io();
bool exists();
bool create();
ToggleBlock *addToggleBlock(AdafruitIO_Feed *feed);
MomentaryBlock *addMomentaryBlock(AdafruitIO_Feed *feed);
SliderBlock *addSliderBlock(AdafruitIO_Feed *feed);
GaugeBlock *addGaugeBlock(AdafruitIO_Feed *feed);
TextBlock *addTextBlock(AdafruitIO_Feed *feed);
ChartBlock *addChartBlock(AdafruitIO_Feed *feed);
ColorBlock *addColorBlock(AdafruitIO_Feed *feed);
MapBlock *addMapBlock(AdafruitIO_Feed *feed);
StreamBlock *addStreamBlock(AdafruitIO_Feed *feed);
ImageBlock *addImageBlock(AdafruitIO_Feed *feed);
private:
AdafruitIO *_io; /*!< Reference to Adafruit IO client */
};
#endif // ADAFRUITIO_DASHBOARD_H

View File

@@ -0,0 +1,915 @@
/*!
* @file AdafruitIO_Data.cpp
*
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*
*/
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Feed.h"
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record.
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data() {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
memset(_value, 0, AIO_DATA_LENGTH);
memset(_csv, 0, AIO_CSV_LENGTH);
}
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record
@param *f
A reference to an Adafruit IO Feed name.
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f) {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
strcpy(_feed, f->name);
memset(_value, 0, AIO_DATA_LENGTH);
memset(_csv, 0, AIO_CSV_LENGTH);
}
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record
@param *f
A reference to an Adafruit IO Feed name.
@param *csv
A reference to a comma-separated-value list.
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
strcpy(_feed, f->name);
memset(_value, 0, AIO_DATA_LENGTH);
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, csv);
_parseCSV();
}
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record
@param *f
A reference to an Adafruit IO Feed name.
@param *csv
A reference to a fixed comma-separated-value list
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, const char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
strcpy(_feed, f->name);
memset(_value, 0, AIO_DATA_LENGTH);
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, csv);
_parseCSV();
}
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record
@param *f
A reference to a fixed Adafruit IO Feed name.
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data(const char *f) {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
strcpy(_feed, f);
memset(_value, 0, AIO_DATA_LENGTH);
}
/**************************************************************************/
/*!
@brief Sets up an Adafruit IO Data Record
@param *f
A reference to a fixed Adafruit IO Feed name.
@param *csv
A reference to a comma-separated-value list
*/
/**************************************************************************/
AdafruitIO_Data::AdafruitIO_Data(const char *f, char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
next_data = 0;
memset(_feed, 0, AIO_FEED_NAME_LENGTH);
strcpy(_feed, f);
memset(_value, 0, AIO_DATA_LENGTH);
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, csv);
_parseCSV();
}
/**************************************************************************/
/*!
@brief Sets data to be returned by AdafruitIO_Feed subCallback.
@param *csv
Data to be appended to csv
@return csv
*/
/**************************************************************************/
bool AdafruitIO_Data::setCSV(char *csv) { return setCSV((const char *)(csv)); }
/**************************************************************************/
/*!
@brief Sets data to be returned by AdafruitIO_Feed subCallback.
@param *csv
Data to be appended to csv
@return True if the CSV was parsed successfully, False if not
*/
/**************************************************************************/
bool AdafruitIO_Data::setCSV(const char *csv) {
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, csv);
return _parseCSV();
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed location metadata.
@param lat
Latitude value for feed datum.
@param lon
Longitude value for feed datum.
@param ele
Elevation value for datum.
*/
/**************************************************************************/
void AdafruitIO_Data::setLocation(double lat, double lon, double ele) {
// if lat, lon, ele == 0, don't set them
if ((abs(0 - lat) < 0.000001) && (abs(0 - lon) < 0.000001) &&
(abs(0 - ele) < 0.000001))
return;
_lat = lat;
_lon = lon;
_ele = ele;
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field as a char.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(const char *value, double lat, double lon,
double ele) {
strcpy(_value, value);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field as a char.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(char *value, double lat, double lon,
double ele) {
strcpy(_value, value);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field, as a boolean.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(bool value, double lat, double lon, double ele) {
if (value)
strcpy(_value, "1");
else
strcpy(_value, "0");
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field as a String.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(String value, double lat, double lon,
double ele) {
value.toCharArray(_value, value.length() + 1);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(int value, double lat, double lon, double ele) {
memset(_value, 0, AIO_DATA_LENGTH);
itoa(value, _value, 10);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(unsigned int value, double lat, double lon,
double ele) {
memset(_value, 0, AIO_DATA_LENGTH);
utoa(value, _value, 10);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(long value, double lat, double lon, double ele) {
memset(_value, 0, AIO_DATA_LENGTH);
ltoa(value, _value, 10);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(unsigned long value, double lat, double lon,
double ele) {
memset(_value, 0, AIO_DATA_LENGTH);
ultoa(value, _value, 10);
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
@param precision
Desired decimals of precision for value.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(float value, double lat, double lon, double ele,
int precision) {
memset(_value, 0, AIO_DATA_LENGTH);
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// Use avrlibc dtostre function on AVR platforms.
dtostre(value, _value, precision, 0);
#elif defined(ESP8266)
// ESP8266 Arduino only implements dtostrf and not dtostre. Use dtostrf
// but accept a hint as to how many decimals of precision are desired.
dtostrf(value, 0, precision, _value);
#else
// Otherwise fall back to snprintf on other platforms.
snprintf(_value, sizeof(_value) - 1, "%0.*f", precision, value);
#endif
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO Feed value field.
@param value
Data record's value field.
@param lat
Data record's latitude field.
@param lon
Data record's longitude field.
@param ele
Data record's elevation field.
@param precision
Desired decimals of precision for value.
*/
/**************************************************************************/
void AdafruitIO_Data::setValue(double value, double lat, double lon, double ele,
int precision) {
memset(_value, 0, AIO_DATA_LENGTH);
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// Use avrlibc dtostre function on AVR platforms.
dtostre(value, _value, precision, 0);
#elif defined(ESP8266)
// ESP8266 Arduino only implements dtostrf and not dtostre. Use dtostrf
// but accept a hint as to how many decimals of precision are desired.
dtostrf(value, 0, precision, _value);
#else
// Otherwise fall back to snprintf on other platforms.
snprintf(_value, sizeof(_value) - 1, "%0.*f", precision, value);
#endif
setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Returns existing Adafruit IO feed name.
@return Feed's name if feed exists.
*/
/**************************************************************************/
char *AdafruitIO_Data::feedName() { return _feed; }
/**************************************************************************/
/*!
@brief Converts provided value to char.
@return Char value.
*/
/**************************************************************************/
char *AdafruitIO_Data::value() { return toChar(); }
/**************************************************************************/
/*!
@brief Converts value to char.
@return value.
*/
/**************************************************************************/
char *AdafruitIO_Data::toChar() { return _value; }
/**************************************************************************/
/*!
@brief Converts value to string
@return String of value.
*/
/**************************************************************************/
String AdafruitIO_Data::toString() { return String(_value); }
/**************************************************************************/
/*!
@brief Converts value to Boolean.
@return True if value string equals Boolean True, False if not.
*/
/**************************************************************************/
bool AdafruitIO_Data::toBool() {
if (strcmp(_value, "1") == 0 || _value[0] == 't' || _value[0] == 'T')
return true;
return false;
}
/**************************************************************************/
/*!
@brief Checks if value is True
@return True if value string equals Boolean True, False if not.
*/
/**************************************************************************/
bool AdafruitIO_Data::isTrue() { return toBool(); }
/**************************************************************************/
/*!
@brief Checks if value is False
@return True if value string equals Boolean False, False if not.
*/
/**************************************************************************/
bool AdafruitIO_Data::isFalse() { return !toBool(); }
/**************************************************************************/
/*!
@brief Converts provided String value to Integer.
@return Integer of value, False if no value provided.
*/
/**************************************************************************/
int AdafruitIO_Data::toInt() {
char *endptr;
return (int)strtol(_value, &endptr, 10);
}
/**************************************************************************/
/*!
@brief Checks digital pin level.
@return HIGH if digital pin level is Boolean True, LOW if digital
pin level is False.
*/
/**************************************************************************/
int AdafruitIO_Data::toPinLevel() {
if (isTrue())
return HIGH;
else
return LOW;
}
/**************************************************************************/
/*!
@brief Converts value to to an Unsigned Integer value.
@return Unsigned Integer, False if no value provided.
*/
/**************************************************************************/
unsigned int AdafruitIO_Data::toUnsignedInt() {
char *endptr;
#ifdef ESP8266
// For some reason strtoul is not defined on the ESP8266 platform right now.
// Just use a strtol function and hope for the best.
return (unsigned int)strtol(_value, &endptr, 10);
#else
return (unsigned int)strtoul(_value, &endptr, 10);
#endif
}
/**************************************************************************/
/*!
@brief Converts value to a floating point value.
@return Float value, False if no value provided.
*/
/**************************************************************************/
float AdafruitIO_Data::toFloat() {
char *endptr;
return (float)strtod(_value, &endptr);
}
/**************************************************************************/
/*!
@brief Converts value to to a double value.
@return Double value, False if no value provided.
*/
/**************************************************************************/
double AdafruitIO_Data::toDouble() {
char *endptr;
return strtod(_value, &endptr);
}
/**************************************************************************/
/*!
@brief Converts value to to a long value.
@return long value, False if no value provided.
*/
/**************************************************************************/
long AdafruitIO_Data::toLong() {
char *endptr;
return strtol(_value, &endptr, 10);
}
/**************************************************************************/
/*!
@brief Converts value to to an unsigned long value.
@return Unsigned long value, False if no value provided.
*/
/**************************************************************************/
unsigned long AdafruitIO_Data::toUnsignedLong() {
char *endptr;
#ifdef ESP8266
// For some reason strtoul is not defined on the ESP8266 platform right now.
// Just use a strtol function and hope for the best.
return (unsigned long)strtol(_value, &endptr, 10);
#else
return strtoul(_value, &endptr, 10);
#endif
}
/**************************************************************************/
/*!
@brief Returns RR from 0xRRGGBB value
@return Red hexadecimal value.
*/
/**************************************************************************/
int AdafruitIO_Data::toRed() {
// Convert 0xRRGGBB to red.
char r[5];
strcpy(r, "0x");
strncpy(&r[2], toChar() + 1, 2);
r[4] = '\x00';
return (int)strtol(r, NULL, 0);
}
/**************************************************************************/
/*!
@brief Returns GG from 0xRRGGBB value
@return Green hexadecimal value.
*/
/**************************************************************************/
int AdafruitIO_Data::toGreen() {
// Convert 0xRRGGBB to green.
char g[5];
strcpy(g, "0x");
strncpy(&g[2], toChar() + 3, 2);
g[4] = '\x00';
return (int)strtol(g, NULL, 0);
}
/**************************************************************************/
/*!
@brief Returns BB from 0xRRGGBB value
@return Blue hexadecimal value.
*/
/**************************************************************************/
int AdafruitIO_Data::toBlue() {
// Convert 0xRRGGBB to blue.
char b[5];
strcpy(b, "0x");
strncpy(&b[2], toChar() + 5, 2);
b[4] = '\x00';
return (int)strtol(b, NULL, 0);
}
/**************************************************************************/
/*!
@brief Creates a RGB Tuple usable by Adafruit_NeoPixel's
setPixelColor method
@return RGB tuple as a hexadecimal value.
*/
/**************************************************************************/
long AdafruitIO_Data::toNeoPixel() {
char rgb[9];
strcpy(rgb, "0x");
strncpy(&rgb[2], toChar() + 1, 6);
return strtol(rgb, NULL, 0);
}
/**************************************************************************/
/*!
@brief Creates a list of comma-separated-values for an Adafruit IO
data record.
@return _csv
*/
/**************************************************************************/
char *AdafruitIO_Data::toCSV() {
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, "\"");
strcat(_csv, _value);
strcat(_csv, "\",");
strcat(_csv, charFromDouble(_lat));
strcat(_csv, ",");
strcat(_csv, charFromDouble(_lon));
strcat(_csv, ",");
strcat(_csv, charFromDouble(_ele, 2));
return _csv;
}
/**************************************************************************/
/*!
@brief Returns a data record's latitude value
@return _lat
*/
/**************************************************************************/
double AdafruitIO_Data::lat() { return _lat; }
/**************************************************************************/
/*!
@brief Returns a data record's longitude value
@return _lon
*/
/**************************************************************************/
double AdafruitIO_Data::lon() { return _lon; }
/**************************************************************************/
/*!
@brief Returns a data record's elevation value
@return _ele
*/
/**************************************************************************/
double AdafruitIO_Data::ele() { return _ele; }
static char _double_buffer[20];
/**************************************************************************/
/*!
@brief Converts from a character to a double.
@param d
Double value.
@param precision
Desired level of decimal precision.
@return _double_buffer
*/
/**************************************************************************/
char *AdafruitIO_Data::charFromDouble(double d, int precision) {
memset(_double_buffer, 0, sizeof(_double_buffer));
#if defined(ARDUINO_ARCH_AVR) || defined(ARDUINO_ARCH_MEGAAVR)
// Use avrlibc dtostre function on AVR platforms.
dtostre(d, _double_buffer, 10, 0);
#elif defined(ESP8266)
// ESP8266 Arduino only implements dtostrf and not dtostre. Use dtostrf
// but accept a hint as to how many decimals of precision are desired.
dtostrf(d, 0, precision, _double_buffer);
#else
// Otherwise fall back to snprintf on other platforms.
snprintf(_double_buffer, sizeof(_double_buffer) - 1, "%f", d);
#endif
return _double_buffer;
}
/*
* From the csv_parser project by semitrivial
* https://github.com/semitrivial/csv_parser/blob/93246cac509f85da12c6bb8c641fa75cd863c34f/csv.c
* - retrieved 2017-11-09
*
* MIT License
*
* Copyright 2016 Samuel Alexander
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
static int count_fields(const char *line) {
const char *ptr;
int cnt, fQuote;
for (cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++) {
if (fQuote) {
if (*ptr == '\"') {
if (ptr[1] == '\"') {
ptr++;
continue;
}
fQuote = 0;
}
continue;
}
switch (*ptr) {
case '\"':
fQuote = 1;
continue;
case ',':
cnt++;
continue;
default:
continue;
}
}
if (fQuote) {
return -1;
}
return cnt;
}
/**************************************************************************/
/*!
@brief Extracts a NULL-terminated array of strings, one for every
cell in the row.
@param line
String containing linebreaks or no linebreaks, escaped by
"double quotes".
@return CSV buffer, buf, NULL otherwise.
*/
/**************************************************************************/
char **parse_csv(const char *line) {
char **buf, **bptr, *tmp, *tptr;
const char *ptr;
int fieldcnt, fQuote, fEnd;
fieldcnt = count_fields(line);
if (fieldcnt == -1) {
return NULL;
}
buf = (char **)malloc(sizeof(char *) * (fieldcnt + 1));
if (!buf) {
return NULL;
}
tmp = (char *)malloc(strlen(line) + 1);
if (!tmp) {
free(buf);
return NULL;
}
bptr = buf;
for (ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0;; ptr++) {
if (fQuote) {
if (!*ptr) {
break;
}
if (*ptr == '\"') {
if (ptr[1] == '\"') {
*tptr++ = '\"';
ptr++;
continue;
}
fQuote = 0;
} else {
*tptr++ = *ptr;
}
continue;
}
switch (*ptr) {
case '\"':
fQuote = 1;
continue;
case '\0':
fEnd = 1;
break;
case ',':
*tptr = '\0';
*bptr = strdup(tmp);
if (!*bptr) {
for (bptr--; bptr >= buf; bptr--) {
free(*bptr);
}
free(buf);
free(tmp);
return NULL;
}
bptr++;
tptr = tmp;
if (fEnd) {
break;
} else {
continue;
}
default:
*tptr++ = *ptr;
continue;
}
if (fEnd) {
break;
}
}
*bptr = NULL;
free(tmp);
return buf;
}
//// END simple_csv SECTION
/**************************************************************************/
/*!
@brief Checks if command-separated-value (CSV) is parsable
@return True if CSV is parsable, False if not.
*/
/**************************************************************************/
bool AdafruitIO_Data::_parseCSV() {
int field_count = count_fields(_csv);
if (field_count > 0) {
// this is normal IO data in `value,lat,lon,ele` format
char **fields = parse_csv(_csv);
// first field is handled as string
strcpy(_value, fields[0]);
field_count--;
// locations fields are handled with char * to float conversion
if (field_count > 0) {
_lat = atof(fields[1]);
field_count--;
}
if (field_count > 0) {
_lon = atof(fields[1]);
field_count--;
}
if (field_count > 0) {
_ele = atof(fields[1]);
field_count--;
}
// cleanup to avoid leaks
int i = 0;
while (fields[i] != NULL) {
free(fields[i++]);
}
free(fields);
return field_count == 0;
} else {
return false;
}
return true;
}

View File

@@ -0,0 +1,106 @@
/*!
* @file AdafruitIO_Data.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_DATA_H
#define ADAFRUITIO_DATA_H
#include "AdafruitIO_Definitions.h"
#include "Arduino.h"
// forward decl
class AdafruitIO_Feed;
/**************************************************************************/
/*!
@brief Class for interacting with Adafruit IO Data Records.
https://io.adafruit.com/api/docs/#data
*/
/**************************************************************************/
class AdafruitIO_Data {
public:
AdafruitIO_Data();
AdafruitIO_Data(AdafruitIO_Feed *f);
AdafruitIO_Data(AdafruitIO_Feed *f, char *csv);
AdafruitIO_Data(AdafruitIO_Feed *f, const char *csv);
AdafruitIO_Data(const char *f);
AdafruitIO_Data(const char *f, char *csv);
bool setCSV(char *csv);
bool setCSV(const char *csv);
void setLocation(double lat, double lon, double ele = 0);
void setValue(const char *value, double lat = 0, double lon = 0,
double ele = 0);
void setValue(char *value, double lat = 0, double lon = 0, double ele = 0);
void setValue(bool value, double lat = 0, double lon = 0, double ele = 0);
void setValue(String value, double lat = 0, double lon = 0, double ele = 0);
void setValue(int value, double lat = 0, double lon = 0, double ele = 0);
void setValue(unsigned int value, double lat = 0, double lon = 0,
double ele = 0);
void setValue(long value, double lat = 0, double lon = 0, double ele = 0);
void setValue(unsigned long value, double lat = 0, double lon = 0,
double ele = 0);
void setValue(float value, double lat = 0, double lon = 0, double ele = 0,
int precision = 6);
void setValue(double value, double lat = 0, double lon = 0, double ele = 0,
int precision = 6);
char *feedName();
char *value();
char *toChar();
String toString();
bool toBool();
bool isTrue();
bool isFalse();
int toInt();
int toPinLevel();
unsigned int toUnsignedInt();
double toDouble();
float toFloat();
long toLong();
unsigned long toUnsignedLong();
int toRed();
int toGreen();
int toBlue();
long toNeoPixel();
char *toCSV();
char *charFromDouble(double d, int precision = 6);
double lat();
double lon();
double ele();
AdafruitIO_Data *next_data; /*!< next data value in Adafruit IO data record */
private:
char _feed[AIO_FEED_NAME_LENGTH];
char _csv[AIO_CSV_LENGTH];
char _value[AIO_DATA_LENGTH];
double _lat, _lon, _ele;
bool _parseCSV();
};
#endif // ADAFRUITIO_DATA_H

View File

@@ -0,0 +1,169 @@
/*!
* @file AdafruitIO_Definitions.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_DEFINITIONS_H_
#define ADAFRUITIO_DEFINITIONS_H_
#define ADAFRUITIO_VERSION_MAJOR 4 ///< Adafruit IO Arduino Major Semvar
#define ADAFRUITIO_VERSION_MINOR 2 ///< Adafruit IO Arduino Minor Semvar
#define ADAFRUITIO_VERSION_PATCH 1 ///< Adafruit IO Arduino Patch Semvar
// forward declaration
class AdafruitIO_Data;
typedef void (*AdafruitIODataCallbackType)(
AdafruitIO_Data *data); /*!< Data callback type */
/**************************************************************************/
/*!
@brief Class that contains methods for Adafruit IO MQTT callbacks.
*/
/**************************************************************************/
class AdafruitIOGroupCallback {
public:
/**************************************************************************/
/*!
@brief Sets up MQTT Group callbacks.
@param f
Valid Adafruit IO feed key.
@param cb
Adafruit IO MQTT callback.
*/
/**************************************************************************/
AdafruitIOGroupCallback(const char *f, AdafruitIODataCallbackType cb) {
feed = f;
dataCallback = cb;
next_cb = 0;
}
/**************************************************************************/
/*!
@brief Sets up MQTT Group callbacks.
@param cb
Adafruit IO MQTT callback.
*/
/**************************************************************************/
AdafruitIOGroupCallback(AdafruitIODataCallbackType cb) {
feed = 0;
dataCallback = cb;
next_cb = 0;
}
const char *feed; /*!< Adafruit IO feed name. */
AdafruitIODataCallbackType
dataCallback; /*!< data carried by an AdafruitIOGroupCallback. */
AdafruitIOGroupCallback *next_cb; /*!< Next callback number. */
};
// Uncomment/comment to turn on/off debug output messages.
// #define AIO_DEBUG
// Uncomment/comment to turn on/off error output
// #define AIO_ERROR
// note: if you're using something like Zero or Due, change the below to
// SerialUSB
#define AIO_PRINTER Serial ///< Where debug messages will be printed
// Define actual debug output functions when necessary.
#ifdef AIO_DEBUG
#define AIO_DEBUG_PRINT(...) \
{ AIO_PRINTER.print(__VA_ARGS__); } ///< Prints debug output.
#define AIO_DEBUG_PRINTLN(...) \
{ AIO_PRINTER.println(__VA_ARGS__); } ///< Prints line from debug output.
#else
#define AIO_DEBUG_PRINT(...) \
{} ///< Prints debug output
#define AIO_DEBUG_PRINTLN(...) \
{} ///< Prints line from debug output.
#endif
// Define actual error output functions when necessary.
#ifdef AIO_ERROR
#define AIO_ERROR_PRINT(...) \
{ AIO_PRINTER.print(__VA_ARGS__); } ///< Prints error output
#define AIO_ERROR_PRINTLN(...) \
{ AIO_PRINTER.println(__VA_ARGS__); } ///< Prints line from error output
#else
#define AIO_ERROR_PRINT(...) \
{} ///< Prints error output.
#define AIO_ERROR_PRINTLN(...) \
{} ///< Prints line from error output.
#endif
#define AIO_PING_INTERVAL 60000 ///< Adafruit IO Ping Interval, in milliseconds
#define AIO_THROTTLE_RECONNECT_INTERVAL \
60000 ///< Time to wait between re-connecting to Adafruit IO after throttled
#define AIO_MQTT_CONNECTION_TIMEOUT \
60000 ///< Time to wait for a successful reconnection after MQTT disconnect
#define AIO_NET_CONNECTION_TIMEOUT \
60000 ///< Time to wait for a successful reconnection after network disconnect
#define AIO_NET_DISCONNECT_WAIT \
300 ///< Time to wait for a net disconnect to take effect
#define AIO_ERROR_TOPIC "/errors" ///< Adafruit IO Error MQTT Topic
#define AIO_THROTTLE_TOPIC "/throttle" ///< Adafruit IO Throttle MQTT Topic
// latest fingerprint can be generated with
// echo | openssl s_client -connect io.adafruit.com:443 | openssl x509
// -fingerprint -noout
#define AIO_SSL_FINGERPRINT \
"4E C1 52 73 24 A8 36 D6 7A 4C 67 C7 91 0C 0A 22 B9 2D 5B CA" ///< Latest
///< Adafruit IO
///< SSL
///< Fingerprint
#define AIO_FEED_NAME_LENGTH \
258 ///< Maximum length of an Adafruit IO Feed: Name; 128 + 1 + 128 for the
///< group, a dot, and actual feed name.
#define AIO_DATA_LENGTH \
45 ///< Maximum length of data sent/recieved from Adafruit IO
#define AIO_CSV_LENGTH \
AIO_FEED_NAME_LENGTH + \
4 ///< Maximum comma-separated-value length from Adafruit IO
/** aio_status_t offers 13 status states */
typedef enum {
AIO_IDLE = 0, // Waiting for connection establishement
AIO_NET_DISCONNECTED = 1, // Network disconnected
AIO_DISCONNECTED = 2, // Disconnected from Adafruit IO
AIO_FINGERPRINT_UNKOWN = 3, // Unknown AIO_SSL_FINGERPRINT
AIO_NET_CONNECT_FAILED = 10, // Failed to connect to network
AIO_CONNECT_FAILED = 11, // Failed to connect to Adafruit IO
AIO_FINGERPRINT_INVALID = 12, // Unknown AIO_SSL_FINGERPRINT
AIO_AUTH_FAILED = 13, // Invalid Adafruit IO login credentials provided.
AIO_SSID_INVALID =
14, // SSID is "" or otherwise invalid, connection not attempted
AIO_NET_CONNECTED = 20, // Connected to Adafruit IO
AIO_CONNECTED = 21, // Connected to network
AIO_CONNECTED_INSECURE = 22, // Insecurely (non-SSL) connected to network
AIO_FINGERPRINT_UNSUPPORTED = 23, // Unsupported AIO_SSL_FINGERPRINT
AIO_FINGERPRINT_VALID = 24 // Valid AIO_SSL_FINGERPRINT
} aio_status_t;
/** Three different types of MQTT time feeds from IO */
typedef enum {
AIO_TIME_SECONDS = 0, // Seconds MQTT feed
AIO_TIME_MILLIS = 1, // Milisecond MQTT feed
AIO_TIME_ISO = 2 // ISO8601 MQTT Feed
} aio_time_format_t;
#endif /* ADAFRUITIO_DEFINITIONS_H_ */

View File

@@ -0,0 +1,114 @@
/*!
* @file AdafruitIO_Ethernet.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_ETHERNET_H
#define ADAFRUITIO_ETHERNET_H
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include <SPI.h>
#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Class for interfacing with the Adafruit Ethernet FeatherWing
*/
/**************************************************************************/
class AdafruitIO_Ethernet : public AdafruitIO {
public:
/**************************************************************************/
/*!
@brief Instanciates an Adafruit Ethernet FeatherWing.
@param *user
Reference to a valid Adafruit IO Username.
@param *key
Reference to a valid Adafruit IO Key.
*/
/**************************************************************************/
AdafruitIO_Ethernet(const char *user, const char *key)
: AdafruitIO(user, key) {
_client = new EthernetClient();
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_eth_port, _username,
_key);
_http = new HttpClient(*_client, _host, _http_port);
}
/**************************************************************************/
/*!
@brief Checks the connection status between the Ethernet
FeatherWing and Adafruit IO
@return True if connected to Adafruit IO, otherwise False.
*/
/**************************************************************************/
aio_status_t networkStatus() {
if (_status == AIO_NET_CONNECTED)
return _status;
_connect();
return _status;
}
/**************************************************************************/
/*!
@brief Defines network module type.
@return String "ethernet_wing"
*/
/**************************************************************************/
const char *connectionType() { return "ethernet_wing"; }
protected:
byte _mac[6] = {0xDE, 0xAD, 0xBE,
0xEF, 0xFE, 0xED}; /*!< Ethernet FeatherWing MAC Address */
EthernetClient *_client; /*!< Reference to EthernetClient, _client */
/**************************************************************************/
/*!
@brief Attempts to connect Ethernet FeatherWing to Adafruit IO
*/
/**************************************************************************/
void _connect() {
if (Ethernet.begin(_mac) == 0) {
_status = AIO_NET_DISCONNECTED;
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
AIO_DEBUG_PRINTLN("Ethernet FeatherWing not found! Please recheck "
"wiring connections.");
while (true)
delay(1); // do nothing, no point running without Ethernet hardware
}
} else {
_status = AIO_NET_CONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the ethernet connection.
*/
/**************************************************************************/
void _disconnect() {
_client->stop();
delay(AIO_NET_DISCONNECT_WAIT);
}
};
#endif // ADAFRUITIO_ETHERNET_H

View File

@@ -0,0 +1,140 @@
//
// 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
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
//
#ifndef ADAFRUITIO_FONA_H
#define ADAFRUITIO_FONA_H
#include "AdafruitIO.h"
#include "Adafruit_FONA.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_FONA.h"
#include "Arduino.h"
#include <SoftwareSerial.h>
#define FONA_RX 9
#define FONA_TX 8
#define FONA_RST 4
#define FONA_RI 7
#define FONA_BAUD 4800
/**************************************************************************/
/*!
@brief Class for interfacing with an Adafruit FONA Ceullar Module
*/
/**************************************************************************/
class AdafruitIO_FONA : public AdafruitIO {
public:
/**************************************************************************/
/*!
@brief Initializes a new AdafruitIO_FONA instance.
@param user
GRPS APN username
@param key
GPRS APN password
*/
/**************************************************************************/
AdafruitIO_FONA(const char *user, const char *key) : AdafruitIO(user, key) {
_serial = new SoftwareSerial(FONA_TX, FONA_RX);
_fona = new Adafruit_FONA(FONA_RST);
_mqtt = new Adafruit_MQTT_FONA(_fona, _host, _mqtt_port);
_packetread_timeout = 500;
}
/**************************************************************************/
/*!
@brief Sets Adafruit Fona APN name
@param apn
GPRS APN name.
@param username
GPRS APN username.
@param password
GRPS APN password.
*/
/**************************************************************************/
void setAPN(FONAFlashStringPtr apn, FONAFlashStringPtr username = 0,
FONAFlashStringPtr password = 0) {
_fona->setGPRSNetworkSettings(apn, username, password);
}
/**************************************************************************/
/*!
@brief Returns network connection status.
@return Adafruit IO Network status, aio_status_t
*/
/**************************************************************************/
aio_status_t AdafruitIO_FONA::networkStatus() {
// return if in a failed state
if (_status == AIO_NET_CONNECT_FAILED)
return _status;
// if we are connected, return
if (_fona->GPRSstate())
return AIO_NET_CONNECTED;
// wait for connection to network
if (_fona->getNetworkStatus() != 1)
return AIO_NET_DISCONNECTED;
_fona->enableGPRS(true);
return AIO_NET_CONNECTED;
}
/**************************************************************************/
/*!
@brief Returns network module type.
@return Network module name, "fona"
*/
/**************************************************************************/
const char *connectionType() { return "fona"; }
protected:
uint16_t _mqtt_port = 1883; /*!< Adafruit IO insecure MQTT port. */
SoftwareSerial *_serial; /*!< an instance of SoftwareSerial. */
Adafruit_FONA *_fona; /*!< an instance of Adafruit_FONA. */
/**************************************************************************/
/*!
@brief Establishes a connection to Adafruit IO.
*/
/**************************************************************************/
void _connect() {
// set software serial baud rate
_serial->begin(FONA_BAUD);
// if fona can't be found, bail
if (!_fona->begin(*_serial)) {
_status = AIO_NET_CONNECT_FAILED;
return;
}
// disable cme error reporting
_serial->println("AT+CMEE=2");
_status = AIO_NET_DISCONNECTED;
}
/**************************************************************************/
/*!
@brief Disconnects from Adafruit IO and the cellular network.
*/
/**************************************************************************/
void _disconnect() {
if (!_fona->enableGPRS(false)) {
AIO_DEBUG_PRINTLN("Failed to turn off GPRS.");
}
_status = AIO_NET_DISCONNECTED;
}
};
#endif // ADAFRUITIO_FONA_H

View File

@@ -0,0 +1,489 @@
/*!
* @file AdafruitIO_Feed.cpp
*
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*
*/
#include "AdafruitIO_Feed.h"
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Creates a new instance of an Adafruit IO Feed.
@param *io
Reference to AdafruitIO.
@param *n
Valid feed name.
*/
/**************************************************************************/
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = _io->_username;
_init();
}
/**************************************************************************/
/*!
@brief Creates a new instance of an Adafruit IO Feed.
@param *io
Reference to AdafruitIO.
@param *n
Valid feed name.
@param *un
Feed owner's Adafruit IO username.
*/
/**************************************************************************/
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n, const char *un)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = un;
_init();
}
/**************************************************************************/
/*!
@brief Adafruit IO Feed destructor.
*/
/**************************************************************************/
AdafruitIO_Feed::~AdafruitIO_Feed() {
if (_sub)
delete _sub;
if (_pub)
delete _pub;
if (_get_pub)
delete _get_pub;
if (data)
delete data;
if (_topic)
free(_topic);
if (_get_topic)
free(_get_topic);
if (_feed_url)
free(_feed_url);
if (_create_url)
free(_create_url);
}
/**************************************************************************/
/*!
@brief Creates a new onMessage callback.
@param cb
Adafruit IO callback.
*/
/**************************************************************************/
void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb) {
_dataCallback = cb;
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(char *value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(bool value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(String value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(int value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(unsigned int value, double lat, double lon,
double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(long value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(unsigned long value, double lat, double lon,
double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@param precision
Desired amount of decimal precision.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(float value, double lat, double lon, double ele,
int precision) {
data->setValue(value, lat, lon, ele, precision);
return _pub->publish(data->toCSV());
}
/**************************************************************************/
/*!
@brief Updates Adafruit IO Feed.
@param value
Value to publish to feed.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
@param precision
Desired amount of decimal precision.
@return True if data successfully published to feed, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele,
int precision) {
data->setValue(value, lat, lon, ele, precision);
return _pub->publish(data->toCSV());
}
/****************************************************************************/
/*!
@brief Publishes a null character to an Adafruit IO /get topic.
https://io.adafruit.com/api/docs/mqtt.html#using-the-get-topic
@return True if successful, otherwise False.
*/
/****************************************************************************/
bool AdafruitIO_Feed::get() { return _get_pub->publish("\0"); }
/****************************************************************************/
/*!
@brief Checks if Adafruit IO Feed exists and belongs to username.
https://io.adafruit.com/api/docs/#get-feed
@return True if successful, otherwise False.
*/
/****************************************************************************/
bool AdafruitIO_Feed::exists() {
_io->_http->beginRequest();
_io->_http->get(_feed_url);
_io->_http->sendHeader("X-AIO-Key", _io->_key);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 200;
}
/**************************************************************/
/*!
@brief Creates a new Adafruit IO Feed
https://io.adafruit.com/api/docs/#create-feed
@return True if successful, otherwise False.
*/
/*************************************************************/
bool AdafruitIO_Feed::create() {
String body = "name=";
body += name;
_io->_http->beginRequest();
_io->_http->post(_create_url);
_io->_http->sendHeader("Content-Type", "application/x-www-form-urlencoded");
_io->_http->sendHeader("Content-Length", body.length());
_io->_http->sendHeader("X-AIO-Key", _io->_key);
// the following call to endRequest
// should be replaced by beginBody once the
// Arduino HTTP Client Library is updated
// _io->_http->beginBody();
_io->_http->endRequest();
_io->_http->print(body);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 201;
}
/*****************************************************************************/
/*!
@brief Retrieves the most recent value published to a feed.
https://io.adafruit.com/api/docs/mqtt.html#using-the-get-topic
@return NULL
*/
/*****************************************************************************/
AdafruitIO_Data *AdafruitIO_Feed::lastValue() {
// 15 extra for api path, 12 for /data/retain, 1 for null
String url = "/api/v2/";
url += owner;
url += "/feeds/";
url += name;
url += "/data/retain";
AIO_DEBUG_PRINT("lastValue get ");
AIO_DEBUG_PRINTLN(url);
_io->_http->beginRequest();
_io->_http->get(url.c_str());
_io->_http->sendHeader("X-AIO-Key", _io->_key);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
String body = _io->_http->responseBody();
if (status >= 200 && status <= 299) {
if (body.length() > 0) {
return new AdafruitIO_Data(this, body.c_str());
}
return NULL;
} else {
AIO_ERROR_PRINT("error retrieving lastValue, status: ");
AIO_ERROR_PRINTLN(status);
AIO_ERROR_PRINT("response body: ");
AIO_ERROR_PRINTLN(_io->_http->responseBody());
return NULL;
}
}
/**************************************************************************/
/*!
@brief Sets Adafruit IO feed location metadata.
@param lat
Latitude metadata.
@param lon
Longitudinal metadata.
@param ele
Elevation metadata.
*/
/**************************************************************************/
void AdafruitIO_Feed::setLocation(double lat, double lon, double ele) {
data->setLocation(lat, lon, ele);
}
/**************************************************************************/
/*!
@brief Calls _datacallback if new data is avaliable on feed.
@param val
Value to publish to Adafruit IO feed.
@param len
Feed length.
*/
/**************************************************************************/
void AdafruitIO_Feed::subCallback(char *val, uint16_t len) {
data->setCSV(val);
// call callback with data
if (_dataCallback)
_dataCallback(data);
}
/**************************************************************************/
/*!
@brief Initialize MQTT topics and REST URLs for Adafruit IO feeds.
*/
/**************************************************************************/
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(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, owner);
strcat(_topic, "/f/");
strcat(_topic, name);
strcat(_topic, "/csv");
// build feed url string
strcpy(_feed_url, "/api/v2/");
strcat(_feed_url, owner);
strcat(_feed_url, "/feeds/");
strcat(_feed_url, name);
// build create url string
strcpy(_create_url, "/api/v2/");
strcat(_create_url, owner);
strcat(_create_url, "/feeds");
// build /get topic string
strcpy(_get_topic, owner);
strcat(_get_topic, "/f/");
strcat(_get_topic, name);
strcat(_get_topic, "/csv/get");
// setup subscription
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic);
_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _topic);
_get_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _get_topic);
_io->_mqtt->subscribe(_sub);
_sub->setCallback(this, &AdafruitIO_MQTT::subCallback);
} else {
// malloc failed
_topic = 0;
_get_topic = 0;
_create_url = 0;
_feed_url = 0;
_sub = 0;
_pub = 0;
_get_pub = 0;
data = 0;
}
}

View File

@@ -0,0 +1,90 @@
/*!
* @file AdafruitIO_Feed.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_FEED_H
#define ADAFRUITIO_FEED_H
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
/**************************************************************************/
/*!
@brief Class that provides methods for interfacing with
Adafruit IO feed topics.
https://io.adafruit.com/api/docs/mqtt.html#mqtt-topics
*/
/**************************************************************************/
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);
bool save(bool value, double lat = 0, double lon = 0, double ele = 0);
bool save(String value, double lat = 0, double lon = 0, double ele = 0);
bool save(int value, double lat = 0, double lon = 0, double ele = 0);
bool save(unsigned int value, double lat = 0, double lon = 0, double ele = 0);
bool save(long value, double lat = 0, double lon = 0, double ele = 0);
bool save(unsigned long value, double lat = 0, double lon = 0,
double ele = 0);
bool save(float value, double lat = 0, double lon = 0, double ele = 0,
int precision = 6);
bool save(double value, double lat = 0, double lon = 0, double ele = 0,
int precision = 6);
bool get();
bool exists();
bool create();
void setLocation(double lat, double lon, double ele = 0);
void onMessage(AdafruitIODataCallbackType cb);
void subCallback(char *val, uint16_t len);
const char *name; /*!< Adafruit IO feed name. */
const char *owner; /*!< Adafruit IO feed owner. */
AdafruitIO_Data *lastValue(); /*!< Last value sent to Adafruit IO feed. */
AdafruitIO_Data *data; /*!< Adafruit IO feed data record. */
private:
AdafruitIODataCallbackType
_dataCallback; /*!< Callback from onMessage containing data. */
void _init();
char *_topic; /*!< MQTT Topic URL */
char *_get_topic; /*!< /get topic string */
char *_create_url; /*!< create URL string */
char *_feed_url; /*!< feed URL string */
Adafruit_MQTT_Subscribe *_sub; /*!< MQTT subscription for _topic. */
Adafruit_MQTT_Publish *_pub; /*!< MQTT publish for _topic. */
Adafruit_MQTT_Publish *_get_pub; /*!< MQTT publish to _get_topic. */
AdafruitIO *_io; /*!< An instance of AdafruitIO. */
AdafruitIO_Data *_data; /*!< An instance of AdafruitIO_Data. */
};
#endif // ADAFRUITIO_FEED_H

View File

@@ -0,0 +1,546 @@
/*!
* @file AdafruitIO_Group.cpp
*
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*
*/
#include "AdafruitIO_Group.h"
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Creates a new instance of an Adafruit IO Group.
@param *io
Reference to AdafruitIO.
@param *n
Valid group name.
*/
/**************************************************************************/
AdafruitIO_Group::AdafruitIO_Group(AdafruitIO *io, const char *n)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = _io->_username;
_init();
}
/**************************************************************************/
/*!
@brief Adafruit IO Group destructor.
*/
/**************************************************************************/
AdafruitIO_Group::~AdafruitIO_Group() {
if (_sub)
delete _sub;
if (_pub)
delete _pub;
if (_get_pub)
delete _get_pub;
if (data)
delete data;
if (_topic)
free(_topic);
if (_get_topic)
free(_get_topic);
if (_group_url)
free(_group_url);
if (_create_url)
free(_create_url);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, char *value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, bool value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, String value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, int value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, unsigned int value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, long value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, unsigned long value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, float value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Sets value of Adafruit IO Group.
@param feed
Adafruit IO feed name.
@param value
Adafruit IO feed value.
*/
/**************************************************************************/
void AdafruitIO_Group::set(const char *feed, double value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
/**************************************************************************/
/*!
@brief Updates value of Adafruit IO Group.
@return True if successfully published to group, False if data is
NULL or if unable to successfully publish data to group.
*/
/**************************************************************************/
bool AdafruitIO_Group::save() {
if (data == NULL)
return false;
char csv[150];
AdafruitIO_Data *cur_data = data;
strcpy(csv, "");
while (cur_data != NULL) {
strcat(csv, cur_data->feedName());
strcat(csv, ",");
strcat(csv, cur_data->toChar());
strcat(csv, "\n");
cur_data = cur_data->next_data;
}
return _pub->publish(csv);
}
/**************************************************************************/
/*!
@brief Publishes null value ("\0") to Adafruit IO Group
https://io.adafruit.com/api/docs/mqtt.html#retained-values
@return True if successfully published to group, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Group::get() { return _get_pub->publish("\0"); }
/**************************************************************************/
/*!
@brief Obtains data from feed within group.
@param feed
Existing Adafruit IO Feed.
@return cur_data Data from feed within group
data Adafruit IO Feed does not exist in group. Data is
the value of a generated feed, feed, within group.
NULL If unable to return data.
*/
/**************************************************************************/
AdafruitIO_Data *AdafruitIO_Group::getFeed(const char *feed) {
if (data == NULL) {
data = new AdafruitIO_Data(feed);
return data;
}
AdafruitIO_Data *cur_data = data;
while (cur_data != NULL) {
if (strcmp(cur_data->feedName(), feed) == 0) {
return cur_data;
}
if (!cur_data->next_data) {
cur_data->next_data = new AdafruitIO_Data(feed);
return cur_data->next_data;
}
cur_data = cur_data->next_data;
}
return NULL;
}
/**************************************************************************/
/*!
@brief Sets up Adafruit IO callback to monitor incoming
new data in group.
@param cb
An function to be called if group receives new data.
*/
/**************************************************************************/
void AdafruitIO_Group::onMessage(AdafruitIODataCallbackType cb) {
if (_groupCallback == NULL) {
_groupCallback = new AdafruitIOGroupCallback(cb);
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while (cur_cb != NULL) {
if (!cur_cb->next_cb) {
cur_cb->next_cb = new AdafruitIOGroupCallback(cb);
return;
}
cur_cb = cur_cb->next_cb;
}
}
/**************************************************************************/
/*!
@brief Sets up Adafruit IO callback to monitor incoming
new data in group's feed, feed.
@param feed
An Adafruit IO Feed within Group.
@param cb
An function to be called if group receives new data.
*/
/**************************************************************************/
void AdafruitIO_Group::onMessage(const char *feed,
AdafruitIODataCallbackType cb) {
if (_groupCallback == NULL) {
_groupCallback = new AdafruitIOGroupCallback(feed, cb);
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while (cur_cb != NULL) {
if (strcmp(cur_cb->feed, feed) == 0) {
return;
}
if (!cur_cb->next_cb) {
cur_cb->next_cb = new AdafruitIOGroupCallback(feed, cb);
return;
}
cur_cb = cur_cb->next_cb;
}
}
/**************************************************************************/
/*!
@brief Adafruit IO Group subscription function callback.
@param d
Name of feed within group.
*/
/**************************************************************************/
void AdafruitIO_Group::call(AdafruitIO_Data *d) {
// uint8_t i;
if (_groupCallback == NULL) {
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while (cur_cb) {
if (cur_cb->feed == NULL || strcmp(cur_cb->feed, d->feedName()) == 0) {
cur_cb->dataCallback(d);
}
cur_cb = cur_cb->next_cb;
}
}
/**************************************************************************/
/*!
@brief Checks for new value within Adafruit IO group.
@param val
Value to send to Adafruit IO group.
@param len
Length of Adafruit IO value.
*/
/**************************************************************************/
void AdafruitIO_Group::subCallback(char *val, uint16_t len) {
char *line;
char *name;
char *value;
if (_groupCallback == NULL)
return;
while ((line = strtok_r(val, "\n", &val)) != NULL) {
name = strtok_r(line, ",", &line);
// couldn't grab name from line, move on
if (!name)
continue;
// don't handle location for now
if (strcmp(name, "location") == 0)
continue;
value = strtok_r(line, ",", &line);
// no value? move on
if (!value)
continue;
AdafruitIO_Data *feed = getFeed(name);
// we couldn't get the data, move on
if (!feed)
continue;
feed->setValue(value);
call(feed);
}
}
/**************************************************************************/
/*!
@brief Sets up locational metadata.
@param lat
Desired latitude.
@param lon
Desired longitude.
@param ele
Desired elevation.
*/
/**************************************************************************/
void AdafruitIO_Group::setLocation(double lat, double lon, double ele) {
// uint8_t i;
if (data == NULL) {
return;
}
AdafruitIO_Data *cur_data = data;
while (cur_data) {
cur_data->setLocation(lat, lon, ele);
cur_data = cur_data->next_data;
}
}
/**************************************************************************/
/*!
@brief Checks if Adafruit IO Group exists.
https://io.adafruit.com/api/docs/#get-group
@return True if successful, otherwise False.
*/
/**************************************************************************/
bool AdafruitIO_Group::exists() {
_io->_http->beginRequest();
_io->_http->get(_group_url);
_io->_http->sendHeader("X-AIO-Key", _io->_key);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 200;
}
/**************************************************************************/
/*!
@brief Creates new Adafruit IO Group.
https://io.adafruit.com/api/docs/#create-group
@return True if successful, otherwise False.
*/
/**************************************************************************/
bool AdafruitIO_Group::create() {
String body = "name=";
body += name;
_io->_http->beginRequest();
_io->_http->post(_create_url);
_io->_http->sendHeader("Content-Type", "application/x-www-form-urlencoded");
_io->_http->sendHeader("Content-Length", body.length());
_io->_http->sendHeader("X-AIO-Key", _io->_key);
// the following call to endRequest
// should be replaced by beginBody once the
// Arduino HTTP Client Library is updated
// _io->_http->beginBody();
_io->_http->endRequest();
_io->_http->print(body);
_io->_http->endRequest();
int status = _io->_http->responseStatusCode();
_io->_http->responseBody(); // needs to be read even if not used
return status == 201;
}
/**************************************************************************/
/*!
@brief Initialize MQTT topics and REST URLs for Adafruit IO groups.
*/
/**************************************************************************/
void AdafruitIO_Group::_init() {
// dynamically allocate memory for mqtt topic and REST URLs
_topic = (char *)malloc(
sizeof(char) * (strlen(owner) + strlen(name) +
8)); // 8 extra chars for /g/, /csv & null termination
_get_topic = (char *)malloc(
sizeof(char) *
(strlen(owner) + strlen(name) +
12)); // 12 extra chars for /f/, /csv/get & null termination
_group_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
data = 0;
if (_topic && _create_url && _group_url) {
// build topic string
strcpy(_topic, owner);
strcat(_topic, "/g/");
strcat(_topic, name);
strcat(_topic, "/csv");
// build feed url string
strcpy(_group_url, "/api/v2/");
strcat(_group_url, owner);
strcat(_group_url, "/groups/");
strcat(_group_url, name);
// build create url string
strcpy(_create_url, "/api/v2/");
strcat(_create_url, owner);
strcat(_create_url, "/groups");
// build /get topic string
strcpy(_get_topic, owner);
strcat(_get_topic, "/g/");
strcat(_get_topic, name);
strcat(_get_topic, "/csv/get");
// setup subscription
_sub = new Adafruit_MQTT_Subscribe(_io->_mqtt, _topic);
_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _topic);
_get_pub = new Adafruit_MQTT_Publish(_io->_mqtt, _get_topic);
_io->_mqtt->subscribe(_sub);
_sub->setCallback(this, &AdafruitIO_MQTT::subCallback);
} else {
// malloc failed
_topic = 0;
_get_topic = 0;
_create_url = 0;
_group_url = 0;
_sub = 0;
_pub = 0;
_get_pub = 0;
}
}

View File

@@ -0,0 +1,85 @@
//
// 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
// Author: Todd Treece
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
//
#ifndef ADAFRUITIO_GROUP_H
#define ADAFRUITIO_GROUP_H
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
/**************************************************************************/
/*!
@brief Class for interacting with Adafruit IO Grouped Feeds
https://io.adafruit.com/api/docs/mqtt.html#group-topics
*/
/**************************************************************************/
class AdafruitIO_Group : public AdafruitIO_MQTT {
public:
AdafruitIO_Group(AdafruitIO *io, const char *name);
~AdafruitIO_Group();
void set(const char *feed, char *value);
void set(const char *feed, bool value);
void set(const char *feed, String value);
void set(const char *feed, int value);
void set(const char *feed, unsigned int value);
void set(const char *feed, long value);
void set(const char *feed, unsigned long value);
void set(const char *feed, float value);
void set(const char *feed, double value);
bool save();
bool get();
void setLocation(double lat = 0, double lon = 0, double ele = 0);
bool exists();
bool create();
void onMessage(AdafruitIODataCallbackType cb);
void onMessage(const char *feed, AdafruitIODataCallbackType cb);
void subCallback(char *val, uint16_t len);
void call(AdafruitIO_Data *d);
const char *name; /*!< Adafruit IO group name. */
const char *owner; /*!< Adafruit IO username of group owner. */
AdafruitIO_Data *data; /*!< Adafruit IO data record. */
AdafruitIO_Data *getFeed(const char *feed);
private:
void _init();
char *_topic; /*!< MQTT topic URL.. */
char *_get_topic; /*!< /get topic string. */
char *_create_url; /*!< Create URL string. */
char *_group_url; /*!< Group URL string. */
Adafruit_MQTT_Subscribe *_sub; /*!< MQTT subscription for _topic. */
Adafruit_MQTT_Publish *_pub; /*!< MQTT publish for _topic. */
Adafruit_MQTT_Publish *_get_pub; /*!< MQTT publish to _get_topic. */
AdafruitIO *_io; /*!< An instance of AdafruitIO. */
AdafruitIOGroupCallback *_groupCallback =
NULL; /*!< An instance of AdafruitIOGroupCallback */
double _lat, _lon, _ele; /*!< latitude, longitude, elevation metadata. */
};
#endif // ADAFRUITIO_GROUP_H

View File

@@ -0,0 +1,42 @@
/*!
* @file AdafruitIO_MQTT.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_MQTT_H
#define ADAFRUITIO_MQTT_H
#include "Arduino.h"
/**************************************************************************/
/*!
@brief Class that contains MQTT subscription callbacks.
*/
/**************************************************************************/
class AdafruitIO_MQTT {
public:
AdafruitIO_MQTT() {}
/**************************************************************************/
/*!
@brief Creates an instance of a MQTT subscription callback.
@param val
Value from the MQTT subscription callback.
@param len
Length of returned value.
@return True
*/
/**************************************************************************/
virtual void subCallback(char *val, uint16_t len) = 0;
};
#endif // ADAFRUITIO_MQTT_H

View File

@@ -0,0 +1,131 @@
/*!
* @file AdafruitIO_Time.cpp
*
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*
*/
#include "AdafruitIO_Time.h"
#include "AdafruitIO.h"
/**************************************************************************/
/*!
@brief Sets up a Adafruit IO Time Service helper.
@param io
Reference to AdafruitIO.
@param f
Adafruit IO time format, either AIO_TIME_SECONDS,
AIO_TIME_MILLIS, or AIO_TIME_ISO.
*/
/**************************************************************************/
AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f)
: AdafruitIO_MQTT() {
_io = io;
_sub = 0;
_dataCallback = 0;
format = f;
_init();
}
/**************************************************************************/
/*!
@brief Deconstructor for Adafruit IO time service.
*/
/**************************************************************************/
AdafruitIO_Time::~AdafruitIO_Time() {
if (_sub)
delete _sub;
if (data)
delete data;
if (_topic)
free(_topic);
}
/**************************************************************************/
/*!
@brief Sets up a MQTT message callback.
@param cb
MQTT callback type.
*/
/**************************************************************************/
void AdafruitIO_Time::onMessage(AdafruitIOTimeCallbackType cb) {
_dataCallback = cb;
}
/**************************************************************************/
/*!
@brief Sets up a MQTT subscription callback. Calls data callback
with data.
@param val
Data from MQTT topic.
@param len
Length of MQTT topic data.
*/
/**************************************************************************/
void AdafruitIO_Time::subCallback(char *val, uint16_t len) {
data = val;
// call callback with data
if (_dataCallback)
_dataCallback(data, len);
}
/**************************************************************************/
/*!
@brief Initializes AdafruitIO Time MQTT topic and REST URLs.
*/
/**************************************************************************/
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;
default:
formatString = "seconds";
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;
}
}

View File

@@ -0,0 +1,55 @@
/*!
* @file AdafruitIO_Time.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_TIME_H
#define ADAFRUITIO_TIME_H
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
typedef void (*AdafruitIOTimeCallbackType)(
char *value,
uint16_t len); /*!< an instance of Adafruit IO's time callback. */
/**************************************************************************/
/*!
@brief Class that contains functions for interacting with
the Adafruit IO Time Service.
*/
/**************************************************************************/
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; /*!< Data sent by Adafruit IO's time service. */
aio_time_format_t format; /*!< Adafruit IO time format,
TIME_SECONDS/TIME_MILLIS/TIME_ISO. */
private:
AdafruitIOTimeCallbackType _dataCallback;
void _init();
char *_topic;
Adafruit_MQTT_Subscribe *_sub;
AdafruitIO *_io;
};
#endif // ADAFRUITIO_FEED_H

View File

@@ -0,0 +1,60 @@
/*!
* @file AdafruitIO_WiFi.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_WIFI_H
#define ADAFRUITIO_WIFI_H
#if defined(ARDUINO_SAMD_MKR1000)
#include "wifi/AdafruitIO_MKR1000.h"
typedef AdafruitIO_MKR1000 AdafruitIO_WiFi;
#elif defined(ARDUINO_SAMD_MKR1010)
#include "wifi/AdafruitIO_MKR1010.h"
typedef AdafruitIO_MKR1010 AdafruitIO_WiFi;
#elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
defined(USE_AIRLIFT)
#include "wifi/AdafruitIO_AIRLIFT.h"
typedef AdafruitIO_AIRLIFT AdafruitIO_WiFi;
#elif defined(USE_WINC1500)
#include "wifi/AdafruitIO_WINC1500.h"
typedef AdafruitIO_WINC1500 AdafruitIO_WiFi;
#elif defined(ARDUINO_ARCH_ESP32)
#include "wifi/AdafruitIO_ESP32.h"
typedef AdafruitIO_ESP32 AdafruitIO_WiFi;
#elif defined(ESP8266)
#include "wifi/AdafruitIO_ESP8266.h"
typedef AdafruitIO_ESP8266 AdafruitIO_WiFi;
#elif defined(ARDUINO_STM32_FEATHER)
#include "wifi/AdafruitIO_WICED.h"
typedef AdafruitIO_WICED AdafruitIO_WiFi;
#else
#warning "Must define USE_AIRLIFT or USE_WINC1500 before including this file."
#endif
#endif // ADAFRUITIO_WIFI_H

View File

@@ -0,0 +1,129 @@
/*!
* @file AdafruitIO_Block.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "AdafruitIO_Block.h"
#include "AdafruitIO.h"
#include "AdafruitIO_Dashboard.h"
/**************************************************************************/
/*!
@brief Creates a new Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the block.
*/
/**************************************************************************/
AdafruitIO_Block::AdafruitIO_Block(AdafruitIO_Dashboard *d,
AdafruitIO_Feed *f) {
_dashboard = d;
_feed = f;
}
AdafruitIO_Block::~AdafruitIO_Block() {}
/**************************************************************************/
/*!
@brief Sets block properties.
@return String containing block's properties.
*/
/**************************************************************************/
String AdafruitIO_Block::properties() {
String props = "{}";
return props;
}
/**************************************************************************/
/*!
@brief Sets block dimensions, provided block size (width, height)
and block location on dashboard (row, column).
@return String containing block's dimensions.
*/
/**************************************************************************/
String AdafruitIO_Block::dimensions() {
String dim = "\",\"size_x\":\"";
dim += _width();
dim += "\",\"size_y\":\"";
dim += _height();
if (_row() > 0) {
dim += "\",\"row\":\"";
dim += _row();
}
if (_column() > 0) {
dim += "\",\"column\":\"";
dim += _column();
}
return dim;
}
/**************************************************************************/
/*!
@brief Returns type of Adafruit IO Block.
@return Block type
*/
/**************************************************************************/
const char *AdafruitIO_Block::type() { return _visual_type; }
/**************************************************************************/
/*!
@brief Creates a new block on an Adafruit IO dashboard.
@return True if successful, False otherwise.
*/
/**************************************************************************/
bool AdafruitIO_Block::save() {
HttpClient *http = _dashboard->io()->_http;
String url = "/api/v2/";
url += _dashboard->user();
url += "/dashboards/";
url += _dashboard->name;
url += "/blocks";
String block_feeds = "[{\"feed_id\":\"";
block_feeds += _feed->name;
block_feeds += "\"}]";
String body = "{\"visual_type\":\"";
body += type();
body += dimensions();
body += "\",\"properties\":";
body += properties();
body += ",\"block_feeds\":";
body += block_feeds;
body += "}";
http->beginRequest();
http->post(url.c_str());
http->sendHeader("Content-Type", "application/json");
http->sendHeader("Content-Length", body.length());
http->sendHeader("X-AIO-Key", _dashboard->io()->_key);
// the following call to endRequest
// should be replaced by beginBody once the
// Arduino HTTP Client Library is updated
// http->beginBody();
http->endRequest();
http->print(body);
http->endRequest();
int status = http->responseStatusCode();
http->responseBody(); // needs to be read even if not used
return status == 200;
}

View File

@@ -0,0 +1,90 @@
/*!
* @file AdafruitIO_Block.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_BLOCK_H
#define ADAFRUITIO_BLOCK_H
#include "AdafruitIO_Definitions.h"
#include "Arduino.h"
class AdafruitIO_Dashboard;
class AdafruitIO_Feed;
/**************************************************************************/
/*!
@brief Class for interacting with and creating Adafruit IO Dashboard
blocks.
*/
/**************************************************************************/
class AdafruitIO_Block {
public:
AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~AdafruitIO_Block();
int width = 2; /*!< Dashboard block width. */
int height = 2; /*!< Dashboard block height. */
int row = 0; /*!< Row location of block on dashboard. */
int column = 0; /*!< Column location of block on dashboard. */
virtual String properties();
String dimensions();
virtual const char *type();
bool save();
protected:
AdafruitIO_Dashboard
*_dashboard; /*!< Instance of an Adafruit IO Dashboard. */
AdafruitIO_Feed *_feed; /*!< Instance of an Adafruit IO Feed. */
const char *_visual_type; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
virtual int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
virtual int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
virtual int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
virtual int _column() { return column; }
};
#endif // ADAFRUITIO_BLOCK_H

View File

@@ -0,0 +1,58 @@
/*!
* @file ChartBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "ChartBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Chart Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the chart.
*/
/**************************************************************************/
ChartBlock::ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
historyHours = 0;
xAxisLabel = "X";
yAxisLabel = "Y";
yAxisMin = 0;
yAxisMax = 100;
}
ChartBlock::~ChartBlock() {}
/**************************************************************************/
/*!
@brief Sets chart block properties.
@return String containing properties of the chart block.
*/
/**************************************************************************/
String ChartBlock::properties() {
String props = "{\"historyHours\":\"";
props += historyHours;
props += "\",\"xAxisLabel\":\"";
props += xAxisLabel;
props += "\",\"yAxisLabel\":\"";
props += yAxisLabel;
props += "\",\"yAxisMin\":\"";
props += yAxisMin;
props += "\",\"yAxisMax\":\"";
props += yAxisMax;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,89 @@
/*!
* @file ChartBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_CHARTBLOCK_H
#define ADAFRUITIO_CHARTBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Chart Block.
*/
/**************************************************************************/
class ChartBlock : public AdafruitIO_Block {
public:
ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ChartBlock();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
int historyHours; /*!< Amount of hours to store the chart's history for. */
const char *xAxisLabel; /*!< Chart's x-axis label. */
const char *yAxisLabel; /*!< Chart's y-axis label. */
int yAxisMin; /*!< Chart's y-axis minimum. */
int yAxisMax; /*!< Chart's y-axis maximum. */
int width = 6; /*!< Dashboard block width. */
int height = 4; /*!< Dashboard block height. */
String properties();
protected:
const char *_visual_type = "line_chart"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_CHARTBLOCK_H

View File

@@ -0,0 +1,91 @@
/*!
* @file ColorBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_COLORBLOCK_H
#define ADAFRUITIO_COLORBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Color Block.
*/
/**************************************************************************/
class ColorBlock : public AdafruitIO_Block {
public:
/**************************************************************************/
/*!
@brief Creates a new color block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the block.
*/
/**************************************************************************/
ColorBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {}
~ColorBlock() {}
int width = 4; /*!< Dashboard block width. */
int height = 4; /*!< Dashboard block height. */
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "color_picker"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_COLORBLOCK_H

View File

@@ -0,0 +1,62 @@
/*!
* @file GaugeBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "GaugeBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Gauge Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the Gauge.
*/
/**************************************************************************/
GaugeBlock::GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
min = 0;
max = 100;
ringWidth = "thin";
label = "Value";
}
GaugeBlock::~GaugeBlock() {}
/**************************************************************************/
/*!
@brief Sets Gauge block properties.
@return String containing properties of the Gauge block.
*/
/**************************************************************************/
String GaugeBlock::properties() {
int w = 0;
if (strcmp(ringWidth, "thin")) {
w = 25;
} else {
w = 50;
}
String props = "{\"minValue\":\"";
props += min;
props += "\",\"maxValue\":\"";
props += max;
props += "\",\"ringWidth\":\"";
props += w;
props += "\",\"label\":\"";
props += label;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,89 @@
/*!
* @file GaugeBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_GAUGEBLOCK_H
#define ADAFRUITIO_GAUGEBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Gauge Block.
*/
/**************************************************************************/
class GaugeBlock : public AdafruitIO_Block {
public:
GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~GaugeBlock();
int min; /*!< Min. value displayed on gauge. */
int max; /*!< Max. value displayed on gauge. */
const char *ringWidth; /*!< Width of gauge's ring. */
const char *label; /*!< Gauge text label. */
int width = 4; /*!< Dashboard block width. */
int height = 4; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "gauge"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_GAUGEBLOCK_H

View File

@@ -0,0 +1,91 @@
/*!
* @file ImageBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_IMAGEBLOCK_H
#define ADAFRUITIO_IMAGEBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Image Block.
*/
/**************************************************************************/
class ImageBlock : public AdafruitIO_Block {
public:
/**************************************************************************/
/*!
@brief Creates a new Image Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the image block.
*/
/**************************************************************************/
ImageBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {}
~ImageBlock() {}
int height = 6; /*!< Dashboard block height. */
int width = 4; /*!< Dashboard block width. */
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "image"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_IMAGEBLOCK_H

View File

@@ -0,0 +1,54 @@
/*!
* @file MapBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "MapBlock.h"
/**************************************************************************/
/*!
@brief Creates a new map Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the map.
*/
/**************************************************************************/
MapBlock::MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
historyHours = 0;
tile = "contrast";
}
MapBlock::~MapBlock() {}
/**************************************************************************/
/*!
@brief Sets map block properties.
@return String containing properties of the map block.
*/
/**************************************************************************/
String MapBlock::properties() {
if ((strcmp(tile, "contrast") != 0) && (strcmp(tile, "street") != 0) &&
(strcmp(tile, "sat") != 0)) {
tile = "contrast";
}
props = "{\"historyHours\":\"";
props += historyHours;
props += "\",\"tile\":\"";
props += tile;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,87 @@
/*!
* @file MapBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_MAPBLOCK_H
#define ADAFRUITIO_MAPBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Map Block.
*/
/**************************************************************************/
class MapBlock : public AdafruitIO_Block {
public:
MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MapBlock();
String props; /*!< Map block properties. */
int historyHours; /*!< Time displayed by map block in hours. */
const char *tile; /*!< Map block title. */
int width = 4; /*!< Dashboard block width. */
int height = 4; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "map"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_MAPBLOCK_H

View File

@@ -0,0 +1,51 @@
/*!
* @file MomentaryBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "MomentaryBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Momentary Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the momentary.
*/
/**************************************************************************/
MomentaryBlock::MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
text = "RESET";
value = "1";
release = "0";
}
MomentaryBlock::~MomentaryBlock() {}
/**************************************************************************/
/*!
@brief Sets momentary block properties.
@return String containing properties of the momentary block.
*/
/**************************************************************************/
String MomentaryBlock::properties() {
String props = "{\"text\":\"";
props += text;
props += "\",\"value\":\"";
props += value;
props += "\",\"release\":\"";
props += release;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,87 @@
/*!
* @file MomentaryBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_MOMENTARYBLOCK_H
#define ADAFRUITIO_MOMENTARYBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Momentary Block.
*/
/**************************************************************************/
class MomentaryBlock : public AdafruitIO_Block {
public:
MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MomentaryBlock();
const char *text; /*!< Text displayed by block. */
const char *value; /*!< Value displayed by block. */
const char *release; /*!< Release value text. */
int width = 2; /*!< Dashboard block width. */
int height = 2; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "momentary_button"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_MOMENTARYBLOCK_H

View File

@@ -0,0 +1,54 @@
/*!
* @file SliderBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "SliderBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Slider Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the slider.
*/
/**************************************************************************/
SliderBlock::SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
min = 0;
max = 100;
step = 10;
label = "Value";
}
SliderBlock::~SliderBlock() {}
/**************************************************************************/
/*!
@brief Sets slider block properties.
@return String containing properties of the slider block.
*/
/**************************************************************************/
String SliderBlock::properties() {
String props = "{\"min\":\"";
props += min;
props += "\",\"max\":\"";
props += max;
props += "\",\"step\":\"";
props += step;
props += "\",\"label\":\"";
props += label;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,88 @@
/*!
* @file SliderBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_SLIDERBLOCK_H
#define ADAFRUITIO_SLIDERBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Slider Block.
*/
/**************************************************************************/
class SliderBlock : public AdafruitIO_Block {
public:
SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~SliderBlock();
int min; /*!< Minimum slider data value. */
int max; /*!< Maximum slider data value. */
int step; /*!< Slider data step value. */
const char *label; /*!< Slider title. */
int width = 4; /*!< Dashboard block width. */
int height = 2; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "slider"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_SLIDERBLOCK_H

View File

@@ -0,0 +1,68 @@
/*!
* @file StreamBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "StreamBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Stream Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the stream block.
*/
/**************************************************************************/
StreamBlock::StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
fontSize = "small";
fontColor = "green";
showErrors = true;
showTimestamp = true;
showName = true;
}
StreamBlock::~StreamBlock() {}
/**************************************************************************/
/*!
@brief Sets stream block properties.
@return String containing properties of the stream block.
*/
/**************************************************************************/
String StreamBlock::properties() {
int s = 0;
if (strcmp(fontSize, "small") == 0) {
s = 12;
}
if (strcmp(fontSize, "medium") == 0) {
s = 18;
} else {
s = 24;
}
String props = "{\"fontSize\":\"";
props += s;
props += "\",\"fontColor\":\"";
props += (strcmp(fontColor, "white") == 0) ? "#ffffff" : "#63de00";
props += "\",\"errors\":\"";
props += showErrors ? "yes" : "no";
props += "\",\"showTimestamp\":\"";
props += showTimestamp ? "yes" : "no";
props += "\",\"showName\":\"";
props += showName ? "yes" : "no";
props += "\"}";
return props;
}

View File

@@ -0,0 +1,89 @@
/*!
* @file StreamBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_STREAMBLOCK_H
#define ADAFRUITIO_STREAMBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Stream Block.
*/
/**************************************************************************/
class StreamBlock : public AdafruitIO_Block {
public:
StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~StreamBlock();
const char *fontSize; /*!< Block's text font size. */
const char *fontColor; /*!< Block's text font color. */
bool showErrors; /*!< Display Adafruit IO errors .*/
bool showTimestamp; /*!< Display timestamp metadata. */
bool showName; /*!< Display value name.. */
int width = 6; /*!< Dashboard block width. */
int height = 4; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "stream"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_STREAMBLOCK_H

View File

@@ -0,0 +1,55 @@
/*!
* @file TextBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "TextBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Text Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the Text.
*/
/**************************************************************************/
TextBlock::TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
fontSize = "small";
}
TextBlock::~TextBlock() {}
/**************************************************************************/
/*!
@brief Sets Text block properties.
@return String containing properties of the Text block.
*/
/**************************************************************************/
String TextBlock::properties() {
int s = 0;
if ((strcmp(fontSize, "small") == 0)) {
s = 12;
} else if ((strcmp(fontSize, "medium") == 0)) {
s = 18;
} else {
s = 24;
}
String props = "{\"fontSize\":\"";
props += s;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,85 @@
/*!
* @file TextBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_TEXTBLOCK_H
#define ADAFRUITIO_TEXTBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for interacting with the Adafruit IO Dashboard
Slider Block.
*/
/**************************************************************************/
class TextBlock : public AdafruitIO_Block {
public:
TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~TextBlock();
const char *fontSize; /*!< Dashboard block text font size. */
int width = 2; /*!< Dashboard block width. */
int height = 1; /*!< Dashboard block height. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "text"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_TEXTBLOCK_H

View File

@@ -0,0 +1,48 @@
/*!
* @file ToggleBlock.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "ToggleBlock.h"
/**************************************************************************/
/*!
@brief Creates a new Toggle Block on an Adafruit IO Dashboard.
@param d
Adafruit IO Dashboard name.
@param f
Adafruit IO Feed to display on the toggle block.
*/
/**************************************************************************/
ToggleBlock::ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
onText = "1";
offText = "0";
}
ToggleBlock::~ToggleBlock() {}
/**************************************************************************/
/*!
@brief Sets toggle block properties.
@return String containing properties of the toggle block.
*/
/**************************************************************************/
String ToggleBlock::properties() {
String props = "{\"onText\":\"";
props += onText;
props += "\",\"offText\":\"";
props += offText;
props += "\"}";
return props;
}

View File

@@ -0,0 +1,86 @@
/*!
* @file ToggleBlock.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_TOGGLEBLOCK_H
#define ADAFRUITIO_TOGGLEBLOCK_H
#include "AdafruitIO_Block.h"
/**************************************************************************/
/*!
@brief Class for creating an Adafruit IO Dashboard Toggle Block.
*/
/**************************************************************************/
class ToggleBlock : public AdafruitIO_Block {
public:
ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ToggleBlock();
const char *onText; /*!< Text to display if the switch is in the on state. */
const char
*offText; /*!< Text to display if the switch is in the off state. */
int height = 2; /*!< Dashboard block height. */
int width = 4; /*!< Dashboard block width. */
String properties();
/******************************************/
/*!
@brief Returns block type
@return Block type.
*/
/******************************************/
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "toggle_button"; /*!< Block type. */
/******************************************/
/*!
@brief Returns width of block.
@return Block width.
*/
/******************************************/
int _width() { return width; }
/******************************************/
/*!
@brief Returns height of block.
@return Block height.
*/
/******************************************/
int _height() { return height; }
/******************************************/
/*!
@brief Returns block's row location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard row.
*/
/******************************************/
int _row() { return row; }
/******************************************/
/*!
@brief Returns block's column location
on an Adafruit IO dashboard.
@return Adafruit IO dashboard column
*/
/******************************************/
int _column() { return column; }
};
#endif // ADAFRUITIO_TOGGLEBLOCK_H

View File

@@ -0,0 +1,144 @@
/*!
* @file AdafruitIO_Board.cpp
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#include "AdafruitIO_Board.h"
char AdafruitIO_Board::_id[64] = "";
#if defined(ARDUINO_SAMD_MKR1000)
const char AdafruitIO_Board::_type[] = "mkr1000";
#elif defined(ARDUINO_SAMD_FEATHER_M0)
const char AdafruitIO_Board::_type[] = "feather_m0";
#elif defined(ARDUINO_AVR_FEATHER32U4)
const char AdafruitIO_Board::_type[] = "feather_32u4";
#elif defined(ARDUINO_STM32_FEATHER)
const char AdafruitIO_Board::_type[] = "feather_wiced";
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
const char AdafruitIO_Board::_type[] = "esp32";
#elif defined(ESP8266)
const char AdafruitIO_Board::_type[] = "esp8266";
#else
const char AdafruitIO_Board::_type[] = "unknown";
#endif
/**************************************************************************/
/*!
@brief Returns a unique board identifier based on the
CPU characteristics.
@return Unique board identifier.
*/
/**************************************************************************/
const char *AdafruitIO_Board::type() { return AdafruitIO_Board::_type; }
#if defined(ARDUINO_ARCH_SAMD)
/*******************************************************/
/*!
@brief Assigns an identifier to a SAMD-based board.
@return board identifier, id.
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
volatile uint32_t val1, val2, val3, val4;
volatile uint32_t *ptr1 = (volatile uint32_t *)0x0080A00C;
val1 = *ptr1;
volatile uint32_t *ptr = (volatile uint32_t *)0x0080A040;
val2 = *ptr;
ptr++;
val3 = *ptr;
ptr++;
val4 = *ptr;
sprintf(AdafruitIO_Board::_id, "%8x%8x%8x%8x", val1, val2, val3, val4);
return AdafruitIO_Board::_id;
}
#elif defined(ARDUINO_ARCH_AVR)
/*******************************************************/
/*!
@brief Assigns an identifier to an AVR-based board
from boot_signature.
@return board identifier, id.
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
for (int i = 0; i < 32; i++) {
sprintf(&AdafruitIO_Board::_id[i * 2], "%02x", boot_signature_byte_get(i));
}
return AdafruitIO_Board::_id;
}
#elif defined(ARDUINO_ARCH_ESP32)
/*******************************************************/
/*!
@brief Assigns an identifier to an ESP32-based
board from the MAC address.
@return board identifier, id.
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
byte mac[6];
WiFi.macAddress(mac);
for (int i = 0; i < 6; i++) {
sprintf(&AdafruitIO_Board::_id[i * 2], "%02x", mac[i]);
}
return AdafruitIO_Board::_id;
}
#elif defined(ESP8266)
/*******************************************************/
/*!
@brief Assigns an identifier to an ESP8266 from
the chip identifier.
@return board identifier, id.
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
sprintf(AdafruitIO_Board::_id, "%06x", ESP.getChipId());
return AdafruitIO_Board::_id;
}
#elif defined(ARDUINO_STM32_FEATHER)
/*******************************************************/
/*!
@brief Assigns an identifier to a STM32 Feather.
@return board identifier, id.
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
uint32_t *p_unique_id = (uint32_t *)(0x1FFF7A10);
sprintf(AdafruitIO_Board::_id, "%08lX%08lX%08lX", p_unique_id[2],
p_unique_id[1], p_unique_id[0]);
return AdafruitIO_Board::_id;
}
#else
/*******************************************************/
/*!
@brief Identifies an unknown board type.
@return "unknown"
*/
/*******************************************************/
char *AdafruitIO_Board::id() {
strcpy(AdafruitIO_Board::_id, "unknown");
return AdafruitIO_Board::_id;
}
#endif

View File

@@ -0,0 +1,40 @@
/*!
* @file AdafruitIO_Board.h
*
* This is part of the Adafruit IO library for the Arduino platform.
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Written by Tony DiCola, Todd Treece for Adafruit Industries
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_BOARD_H
#define ADAFRUITIO_BOARD_H
#include "Arduino.h"
#if defined(ARDUINO_ARCH_AVR)
#include <avr/boot.h>
#endif
/**************************************************************************/
/*!
@brief Class for dynamically assigning an identifier for the
development board used with this library.
*/
/**************************************************************************/
class AdafruitIO_Board {
public:
static char _id[64]; /*!< Board Identifier. */
static char *id();
static const char _type[]; /*!< Board name. */
static const char *type(); /*!< Board name. */
};
#endif // ADAFRUITIO_BOARD_H

View File

@@ -0,0 +1,207 @@
/*!
* @file AdafruitIO_AIRLIFT.h
*
* This is part of Adafruit IO Arduino. It is designed specifically to work
* with Adafruit's AirLift ESP32 Co-Processor.
*
* The ESP32 uses SPI to communicate. Three lines (CS, ACK, RST) are required
* to communicate with the ESP32.
*
* 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.
*
* MIT license, all text here must be included in any redistribution.
*
*/
#ifndef ADAFRUITIO_AIRLIFT_H
#define ADAFRUITIO_AIRLIFT_H
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFiNINA.h"
#define NINAFWVER \
"1.0.0" /*!< nina-fw version compatible with this library. \
*/
/****************************************************************************/
/*!
@brief Class that stores functions for interacting with AirLift Devices
*/
/****************************************************************************/
class AdafruitIO_AIRLIFT : public AdafruitIO {
public:
/**************************************************************************/
/*!
@brief Initializes the Adafruit IO class for AirLift devices.
@param user
A reference to the Adafruit IO user, shared by AdafruitIO.
@param key
A reference to the Adafruit IO Key, shared by AdafruitIO.
@param ssid
A reference to the WiFi network SSID.
@param pass
A reference to the WiFi network password.
@param ssPin
A reference to the ESP32_SS Pin.
@param ackPin
A reference to the ESP32_ACK Pin.
@param rstPin
A reference to the ESP32_RST Pin.
@param gpio0Pin
A reference to the gpio0Pin Pin.
@param wifi
A reference to a SPIClass
*/
/**************************************************************************/
AdafruitIO_AIRLIFT(const char *user, const char *key, const char *ssid,
const char *pass, int ssPin, int ackPin, int rstPin,
int gpio0Pin, SPIClass *wifi)
: AdafruitIO(user, key) {
_wifi = wifi;
_ssPin = ssPin;
_ackPin = ackPin;
_rstPin = rstPin;
_gpio0Pin = gpio0Pin;
_ssid = ssid;
_pass = pass;
_mqtt_client = new WiFiSSLClient;
_mqtt = new Adafruit_MQTT_Client(_mqtt_client, _host, _mqtt_port);
_http_client = new WiFiSSLClient;
_http = new HttpClient(*_http_client, _host, _http_port);
}
/**************************************************************************/
/*!
@brief Destructor for the Adafruit IO AirLift class.
*/
/**************************************************************************/
~AdafruitIO_AIRLIFT() {
if (_mqtt_client)
delete _http_client;
if (_http_client)
delete _mqtt_client;
if (_mqtt)
delete _mqtt;
if (_http)
delete _http;
}
/**************************************************************************/
/*!
@brief Allows setting of the AirLift RGB led from the
Adafruit IO AirLift Class.
@param r
Red value, unsigned 8 bit value (0->255)
@param g
Green value, unsigned 8 bit value (0->255)
@param b
Blue value, unsigned 8 bit value (0->255)
*/
/**************************************************************************/
void setLEDs(uint8_t r, uint8_t g, uint8_t b) { WiFi.setLEDs(r, g, b); }
/********************************************************/
/*!
@brief Checks the version of an ESP32 module against
NINAFWVER. Raises an error if the firmware needs to be
upgraded.
*/
/********************************************************/
void firmwareCheck() {
_fv = WiFi.firmwareVersion();
if (_fv < NINAFWVER) {
AIO_DEBUG_PRINTLN("Please upgrade the firmware on the ESP module");
}
}
/********************************************************/
/*!
@brief Returns the network status of an ESP32 module.
@return aio_status_t
*/
/********************************************************/
aio_status_t networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
/*****************************************************************/
/*!
@brief Returns the type of network connection used by AdafruitIO.
@return AIRLIFT
*/
/*****************************************************************/
const char *connectionType() { return "AIRLIFT"; }
protected:
const char *_ssid;
const char *_pass;
String _fv = "0.0.0";
int _ssPin, _ackPin, _rstPin, _gpio0Pin = -1;
WiFiSSLClient *_http_client;
WiFiSSLClient *_mqtt_client;
SPIClass *_wifi;
/**************************************************************************/
/*!
@brief Attempts to establish a WiFi connection with the wireless network,
given _ssid and _pass from the AdafruitIO_AIRLIFT constructor.
*/
/**************************************************************************/
void _connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
// setup ESP32 pins
if (_ssPin != -1) {
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
}
// check esp32 module version against NINAFWVER
firmwareCheck();
// disconnect from possible previous connection
_disconnect();
// check for esp32 module
if (WiFi.status() == WL_NO_MODULE) {
AIO_DEBUG_PRINTLN("No ESP32 module detected!");
return;
}
WiFi.begin(_ssid, _pass);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
*/
/**************************************************************************/
void _disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
};
#endif // ADAFRUITIO_AIRLIFT_H

View File

@@ -0,0 +1,73 @@
/*!
* @file AdafruitIO_ESP32.cpp
*
* 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-2021 Adafruit Industries
* Authors: Tony DiCola, Todd Treece, Brent Rubell
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifdef ARDUINO_ARCH_ESP32
#include "AdafruitIO_ESP32.h"
AdafruitIO_ESP32::AdafruitIO_ESP32(const char *user, const char *key,
const char *ssid, const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
_client = new WiFiClientSecure;
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port);
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_ESP32::~AdafruitIO_ESP32() {
if (_client)
delete _client;
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_ESP32::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
delay(100);
WiFi.begin(_ssid, _pass);
delay(100);
_status = AIO_NET_DISCONNECTED;
}
_client->setCACert(_aio_root_ca);
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
*/
/**************************************************************************/
void AdafruitIO_ESP32::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_ESP32::networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
const char *AdafruitIO_ESP32::connectionType() { return "wifi"; }
#endif // ESP32

View File

@@ -0,0 +1,78 @@
/*!
* @file AdafruitIO_ESP32.h
*
* 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-2021 Adafruit Industries
* Authors: Tony DiCola, Todd Treece, Brent Rubell
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_ESP32_H
#define ADAFRUITIO_ESP32_H
#ifdef ARDUINO_ARCH_ESP32
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "WiFiClientSecure.h"
#include <WiFi.h>
class AdafruitIO_ESP32 : public AdafruitIO {
public:
AdafruitIO_ESP32(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_ESP32();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiClientSecure *_client;
// io.adafruit.com root CA
const char *_aio_root_ca =
"-----BEGIN CERTIFICATE-----\n"
"MIIEjTCCA3WgAwIBAgIQDQd4KhM/xvmlcpbhMf/ReTANBgkqhkiG9w0BAQsFADBh\n"
"MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3\n"
"d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBH\n"
"MjAeFw0xNzExMDIxMjIzMzdaFw0yNzExMDIxMjIzMzdaMGAxCzAJBgNVBAYTAlVT\n"
"MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5j\n"
"b20xHzAdBgNVBAMTFkdlb1RydXN0IFRMUyBSU0EgQ0EgRzEwggEiMA0GCSqGSIb3\n"
"DQEBAQUAA4IBDwAwggEKAoIBAQC+F+jsvikKy/65LWEx/TMkCDIuWegh1Ngwvm4Q\n"
"yISgP7oU5d79eoySG3vOhC3w/3jEMuipoH1fBtp7m0tTpsYbAhch4XA7rfuD6whU\n"
"gajeErLVxoiWMPkC/DnUvbgi74BJmdBiuGHQSd7LwsuXpTEGG9fYXcbTVN5SATYq\n"
"DfbexbYxTMwVJWoVb6lrBEgM3gBBqiiAiy800xu1Nq07JdCIQkBsNpFtZbIZhsDS\n"
"fzlGWP4wEmBQ3O67c+ZXkFr2DcrXBEtHam80Gp2SNhou2U5U7UesDL/xgLK6/0d7\n"
"6TnEVMSUVJkZ8VeZr+IUIlvoLrtjLbqugb0T3OYXW+CQU0kBAgMBAAGjggFAMIIB\n"
"PDAdBgNVHQ4EFgQUlE/UXYvkpOKmgP792PkA76O+AlcwHwYDVR0jBBgwFoAUTiJU\n"
"IBiV5uNu5g/6+rkS7QYXjzkwDgYDVR0PAQH/BAQDAgGGMB0GA1UdJQQWMBQGCCsG\n"
"AQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAGAQH/AgEAMDQGCCsGAQUFBwEB\n"
"BCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3AuZGlnaWNlcnQuY29tMEIGA1Ud\n"
"HwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2ljZXJ0LmNvbS9EaWdpQ2VydEds\n"
"b2JhbFJvb3RHMi5jcmwwPQYDVR0gBDYwNDAyBgRVHSAAMCowKAYIKwYBBQUHAgEW\n"
"HGh0dHBzOi8vd3d3LmRpZ2ljZXJ0LmNvbS9DUFMwDQYJKoZIhvcNAQELBQADggEB\n"
"AIIcBDqC6cWpyGUSXAjjAcYwsK4iiGF7KweG97i1RJz1kwZhRoo6orU1JtBYnjzB\n"
"c4+/sXmnHJk3mlPyL1xuIAt9sMeC7+vreRIF5wFBC0MCN5sbHwhNN1JzKbifNeP5\n"
"ozpZdQFmkCo+neBiKR6HqIA+LMTMCMMuv2khGGuPHmtDze4GmEGZtYLyF8EQpa5Y\n"
"jPuV6k2Cr/N3XxFpT3hRpt/3usU/Zb9wfKPtWpoznZ4/44c1p9rzFcZYrWkj3A+7\n"
"TNBJE0GmP2fhXhP1D/XVfIW/h0yCJGEiV9Glm/uGOa3DXHlmbAcxSyCRraG+ZBkA\n"
"7h4SeM6Y8l/7MBRpPCz6l8Y=\n"
"-----END CERTIFICATE-----\n";
};
#endif // ESP32
#endif // ADAFRUITIO_ESP32_H

View File

@@ -0,0 +1,79 @@
/*!
* @file AdafruitIO_ESP8266.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifdef ESP8266
#include "AdafruitIO_ESP8266.h"
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const char *user, const char *key,
const char *ssid, const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
// Uncomment the following lines and remove the existing WiFiClient and MQTT
// client constructors to use Secure MQTT with ESP8266.
// _client = new WiFiClientSecure;
// _client->setFingerprint(AIO_SSL_FINGERPRINT);
// _mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port);
_client = new WiFiClient;
_mqtt = new Adafruit_MQTT_Client(_client, _host, 1883);
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_ESP8266::~AdafruitIO_ESP8266() {
if (_client)
delete _client;
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_ESP8266::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
delay(100);
WiFi.begin(_ssid, _pass);
delay(100);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
*/
/**************************************************************************/
void AdafruitIO_ESP8266::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_ESP8266::networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
const char *AdafruitIO_ESP8266::connectionType() { return "wifi"; }
#endif // ESP8266

View File

@@ -0,0 +1,60 @@
/*!
* @file AdafruitIO_ESP8266.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_ESP8266_H
#define ADAFRUITIO_ESP8266_H
#ifdef ESP8266
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "ESP8266WiFi.h"
/* NOTE - Projects that require "Secure MQTT" (TLS/SSL) also require a new
* SSL certificate every year. If adding Secure MQTT to your ESP8266 project is
* important - please switch to using the modern ESP32 (and related models)
* instead of the ESP8266 to avoid updating the SSL fingerprint every year.
*
* If you've read through this and still want to use "Secure MQTT" with your
* ESP8266 project, we've left the "WiFiClientSecure" lines commented out. To
* use them, uncomment the commented out lines within `AdafruitIO_ESP8266.h` and
* `AdafruitIO_ESP8266.cpp` and recompile the library.
*/
// #include "WiFiClientSecure.h"
class AdafruitIO_ESP8266 : public AdafruitIO {
public:
AdafruitIO_ESP8266(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_ESP8266();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiClient *_client;
// Uncomment the following line, and remove the line above, to use
// secure MQTT with ESP8266.
// WiFiClientSecure *_client;
};
#endif // ESP8266
#endif // ADAFRUITIO_ESP8266_H

View File

@@ -0,0 +1,77 @@
/*!
* @file AdafruitIO_MKR1000.cpp
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#if defined(ARDUINO_SAMD_MKR1000)
#include "AdafruitIO_MKR1000.h"
AdafruitIO_MKR1000::AdafruitIO_MKR1000(const char *user, const char *key,
const char *ssid, const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
_client = new WiFiSSLClient;
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port);
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_MKR1000::~AdafruitIO_MKR1000() {
if (_client)
delete _client;
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_MKR1000::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
// no shield? bail
if (WiFi.status() == WL_NO_SHIELD)
return;
_disconnect();
WiFi.begin(_ssid, _pass);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
*/
/**************************************************************************/
void AdafruitIO_MKR1000::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_MKR1000::networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
const char *AdafruitIO_MKR1000::connectionType() { return "wifi"; }
#endif // ARDUINO_ARCH_SAMD

View File

@@ -0,0 +1,50 @@
/*!
* @file AdafruitIO_MKR1000.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_MKR1000_H
#define ADAFRUITIO_MKR1000_H
#if defined(ARDUINO_SAMD_MKR1000)
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFi101.h"
#include "WiFiSSLClient.h"
class AdafruitIO_MKR1000 : public AdafruitIO {
public:
AdafruitIO_MKR1000(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_MKR1000();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiSSLClient *_client;
};
#endif // ARDUINO_ARCH_SAMD
#endif // ADAFRUITIO_MKR1000_H

View File

@@ -0,0 +1,83 @@
/*!
* @file AdafruitIO_MKR1010.h
*
* 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: David Goldstein, Morgan Winters
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_MKR1010_H
#define ADAFRUITIO_MKR1010_H
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFiNINA.h"
#include "WiFiSSLClient.h"
class AdafruitIO_MKR1010 : public AdafruitIO {
public:
AdafruitIO_MKR1010(const char *user, const char *key, const char *ssid,
const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
_client = new WiFiSSLClient;
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port);
_http = new HttpClient(*_client, _host, _http_port);
}
~AdafruitIO_MKR1010() {
if (_client)
delete _client;
if (_mqtt)
delete _mqtt;
}
aio_status_t networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
const char *connectionType() { return "wifi"; }
protected:
void _connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
WiFi.begin(_ssid, _pass);
_status = networkStatus();
}
}
void _disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
const char *_ssid;
const char *_pass;
WiFiSSLClient *_client;
};
#endif ADAFRUITIO_MKR1010_H

View File

@@ -0,0 +1,70 @@
/*!
* @file AdafruitIO_WICED.cpp
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifdef ARDUINO_STM32_FEATHER
#include "AdafruitIO_WICED.h"
AdafruitIO_WICED::AdafruitIO_WICED(const char *user, const char *key,
const char *ssid, const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
_client = new AdafruitIO_WICED_SSL;
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_port);
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_WICED::~AdafruitIO_WICED() {
if (_client)
delete _client;
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_WICED::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
Feather.connect(_ssid, _pass);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
*/
/**************************************************************************/
void AdafruitIO_WICED::_disconnect() {
Feather.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_WICED::networkStatus() {
if (Feather.connected())
return AIO_NET_CONNECTED;
// if granular status is needed, we can
// check Feather.errno() codes:
// https://learn.adafruit.com/introducing-the-adafruit-wiced-feather-wifi/constants#err-t
// for now we will try connecting again and return disconnected status
Feather.connect(_ssid, _pass);
return AIO_NET_DISCONNECTED;
}
const char *AdafruitIO_WICED::connectionType() { return "wifi"; }
#endif // ARDUINO_STM32_FEATHER

View File

@@ -0,0 +1,47 @@
/*!
* @file AdafruitIO_WICED.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_WICED_H
#define ADAFRUITIO_WICED_H
#ifdef ARDUINO_STM32_FEATHER
#include "AdafruitIO.h"
#include "AdafruitIO_WICED_SSL.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include <adafruit_feather.h>
class AdafruitIO_WICED : public AdafruitIO {
public:
AdafruitIO_WICED(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_WICED();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
AdafruitIO_WICED_SSL *_client;
};
#endif // ARDUINO_STM32_FEATHER
#endif // ADAFRUITIO_WICED_H

View File

@@ -0,0 +1,34 @@
/*!
* @file AdafruitIO_WICED_SSL.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_WICED_SSL_H
#define ADAFRUITIO_WICED_SSL_H
#ifdef ARDUINO_STM32_FEATHER
#include <adafruit_feather.h>
class AdafruitIO_WICED_SSL : public AdafruitTCP {
public:
AdafruitIO_WICED_SSL() : AdafruitTCP() {}
int connect(const char *host, uint16_t port) {
return connectSSL(host, port);
}
};
#endif // ARDUINO_STM32_FEATHER
#endif // ADAFRUITIO_WICED_H

View File

@@ -0,0 +1,159 @@
/*!
* @file AdafruitIO_WINC1500.h
*
* 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
* Licensed under the MIT license.
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_WINC1500_H
#define ADAFRUITIO_WINC1500_H
#if !defined(ARDUINO_SAMD_MKR1000) && defined(ARDUINO_ARCH_SAMD) && \
!defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) && !defined(ADAFRUIT_PYPORTAL)
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFi101.h"
/**************************************************************************/
/*!
@brief Class for interacting with adafruit.io (AIO) using WINC1500
*/
/**************************************************************************/
class AdafruitIO_WINC1500 : public AdafruitIO {
public:
/**************************************************************************/
/*!
@brief Instantiate the object.
@param user
A pointer to the AIO user name.
@param key
A pointer to the AIO key for the user.
@param ssid
A pointer to the SSID for the wifi.
@param pass
A pointer to the password for the wifi.
@param winc_cs
The cs pin number.
@param winc_irq
The irq pin number.
@param winc_rst
The rst pin number.
@param winc_en
The en pin number.
@return none
*/
/**************************************************************************/
AdafruitIO_WINC1500(const char *user, const char *key, const char *ssid,
const char *pass, int winc_cs = 8, int winc_irq = 7,
int winc_rst = 4, int winc_en = 2)
: AdafruitIO(user, key) {
_winc_cs = winc_cs;
_winc_irq = winc_irq;
_winc_rst = winc_rst;
_winc_en = winc_en;
_ssid = ssid;
_pass = pass;
_mqtt_client = new WiFiSSLClient;
_mqtt = new Adafruit_MQTT_Client(_mqtt_client, _host, _mqtt_port);
_http_client = new WiFiSSLClient;
_http = new HttpClient(*_http_client, _host, _http_port);
}
/**************************************************************************/
/*!
@brief Destructor to end the object.
@return none
*/
/**************************************************************************/
~AdafruitIO_WINC1500() {
if (_mqtt_client)
delete _http_client;
if (_http_client)
delete _mqtt_client;
if (_mqtt)
delete _mqtt;
if (_http)
delete _http;
}
/**************************************************************************/
/*!
@brief Network status check.
@return An AIO network status value. Lower values represent poorer
connection status.
*/
/**************************************************************************/
aio_status_t networkStatus() {
switch (WiFi.status()) {
case WL_CONNECTED:
return AIO_NET_CONNECTED;
case WL_CONNECT_FAILED:
return AIO_NET_CONNECT_FAILED;
case WL_IDLE_STATUS:
return AIO_IDLE;
default:
return AIO_NET_DISCONNECTED;
}
}
const char *connectionType() { return "winc1500"; }
protected:
const char *_ssid;
const char *_pass;
int _winc_cs, _winc_irq, _winc_rst, _winc_en = 0;
WiFiSSLClient *_http_client;
WiFiSSLClient *_mqtt_client;
/**************************************************************************/
/*!
@brief Connect the wifi network.
@return none
*/
/**************************************************************************/
void _connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
WiFi.setPins(_winc_cs, _winc_irq, _winc_rst, _winc_en); //
// no shield? bail
if (WiFi.status() == WL_NO_SHIELD) {
AIO_DEBUG_PRINTLN("No WINC1500 Module Detected!");
return;
}
WiFi.begin(_ssid, _pass);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
@return none
*/
/**************************************************************************/
void _disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
};
#endif // ARDUINO_ARCH_SAMD
#endif // ADAFRUITIO_WINC1500_H