initial actions migration

This commit is contained in:
brentru 2020-01-03 18:15:56 -05:00
parent 512dc2eef2
commit 332b730dee
55 changed files with 1692 additions and 1894 deletions

62
.github/workflows/githubci.yml vendored Normal file
View file

@ -0,0 +1,62 @@
name: Arduino Library CI
on: [pull_request, push]
jobs:
build:
strategy:
fail-fast: false
matrix:
arduino-platform: ["esp32", "esp8266",
"pyportal_titano", "metro_m4_airliftlite",
"uno"]
runs-on: ubuntu-latest
steps:
- uses: actions/setup-python@v1
with:
python-version: '3.x'
- uses: actions/checkout@v2
- uses: actions/checkout@v2
with:
repository: adafruit/ci-arduino
path: ci
- name: pre-install
run: bash ci/actions_install.sh
# manually install WiFi
- name: extra libraries
run: |
git clone --quiet https://github.com/adafruit/WiFiNINA.git /home/runner/Arduino/libraries/WiFiNINA
- name: test platforms
run: python3 ci/build_platform.py ${{ matrix.arduino-platform }}
clang_and_doxy:
runs-on: ubuntu-latest
needs: build
steps:
- uses: actions/setup-python@v1
with:
python-version: '3.x'
- uses: actions/checkout@v2
- uses: actions/checkout@v2
with:
repository: adafruit/ci-arduino
path: ci
- name: pre-install
run: bash ci/actions_install.sh
- name: clang
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .
- name: doxygen
env:
GH_REPO_TOKEN: ${{ secrets.GH_REPO_TOKEN }}
PRETTYNAME : "Adafruit IO Arduino Library"
run: bash ci/doxy_gen_and_deploy.sh

View file

@ -1,26 +0,0 @@
language: c
sudo: false
# Blacklist
branches:
except:
- gh-pages
env:
global:
- PRETTYNAME="Adafruit IO Arduino Library"
before_install:
- source <(curl -SLs https://raw.githubusercontent.com/adafruit/travis-ci-arduino/master/install.sh)
- cd $HOME/arduino_ide/libraries/
- wget https://github.com/adafruit/WiFiNINA/archive/master.zip && unzip master.zip
- mv WiFiNINA-master/ WiFiNINA && rm master.zip
- cd /home/travis/build/adafruit/Adafruit_IO_Arduino
install:
- arduino --install-library "Adafruit IO Arduino","Adafruit MQTT Library","ArduinoHttpClient"
- arduino --install-library "Adafruit Unified Sensor","Adafruit NeoPixel","DHT sensor library", "Ethernet"
script:
- build_platform m4wifi
- build_platform esp8266

2
Jenkinsfile vendored
View file

@ -3,4 +3,4 @@ arduino {
platforms = ["esp8266:esp8266", "esp32:esp32", "adafruit:avr", "arduino:samd", "adafruit:samd"]
libraries = ["Adafruit MQTT Library", "Adafruit FONA Library", "Ethernet", "WiFi101"]
boards = ["ESP8266", "M0_WINC1500", "WICED", "MKR1000", "M0_ETHERNETWING", "FONA_32U4", "ESP32"]
}
}

View file

@ -1,6 +1,11 @@
# Adafruit IO Arduino Library
[![Documentation Status](https://readthedocs.org/projects/adafruit-io-arduino/badge/?version=latest)](https://adafruit-io-arduino.readthedocs.io/en/latest/) [![Build Status](https://travis-ci.com/adafruit/Adafruit_IO_Arduino.svg?branch=master)](https://travis-ci.com/adafruit/Adafruit_IO_Arduino) [![Discord](https://img.shields.io/discord/327254708534116352.svg)](https://discord.gg/nBQh6qu)
[![Documentation Status](https://readthedocs.org/projects/adafruit-io-arduino/badge/?version=latest)](https://adafruit-io-arduino.readthedocs.io/en/latest/)
![Build Status](https://github.com/adafruit/Adafruit_IO_Arduino/workflows/Arduino%20Library%20CI/badge.svg)
[![Discord](https://img.shields.io/discord/327254708534116352.svg)](https://discord.gg/nBQh6qu)
![AIOArduino](https://cdn-learn.adafruit.com/assets/assets/000/057/496/original/adafruit_io_AIOA.png?1531335660)

View file

@ -7,3 +7,4 @@ paragraph=Arduino library to access Adafruit IO using the Adafruit AirLift, ESP8
category=Communication
url=https://github.com/adafruit/Adafruit_IO_Arduino
architectures=*
depends=Adafruit MQTT Library, ArduinoHttpClient, Adafruit Unified Sensor, Adafruit NeoPixel, DHT sensor library, Ethernet

View file

@ -25,8 +25,7 @@
@return none
*/
/**************************************************************************/
AdafruitIO::AdafruitIO(const char *user, const char *key)
{
AdafruitIO::AdafruitIO(const char *user, const char *key) {
_mqtt = 0;
_http = 0;
_username = user;
@ -47,16 +46,16 @@ AdafruitIO::AdafruitIO(const char *user, const char *key)
@return none
*/
/**************************************************************************/
void AdafruitIO::_init()
{
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));
_err_topic = (char *)malloc(
sizeof(char) * (strlen(_username) + strlen(AIO_ERROR_TOPIC) + 1));
if(_err_topic) {
if (_err_topic) {
// build error topic
strcpy(_err_topic, _username);
@ -66,13 +65,13 @@ void AdafruitIO::_init()
// malloc failed
_err_topic = 0;
}
// dynamically allocate memory for throttle topic
_throttle_topic = (char *)malloc(sizeof(char) * (strlen(_username) + strlen(AIO_THROTTLE_TOPIC) + 1));
_throttle_topic = (char *)malloc(
sizeof(char) * (strlen(_username) + strlen(AIO_THROTTLE_TOPIC) + 1));
if(_throttle_topic) {
if (_throttle_topic) {
// build throttle topic
strcpy(_throttle_topic, _username);
@ -82,9 +81,7 @@ void AdafruitIO::_init()
// malloc failed
_throttle_topic = 0;
}
}
/**************************************************************************/
@ -93,24 +90,23 @@ void AdafruitIO::_init()
@return none
*/
/**************************************************************************/
AdafruitIO::~AdafruitIO()
{
if(_err_topic)
AdafruitIO::~AdafruitIO() {
if (_err_topic)
free(_err_topic);
if(_throttle_topic)
if (_throttle_topic)
free(_throttle_topic);
if(_err_sub)
if (_err_sub)
delete _err_sub;
if(_throttle_sub)
if (_throttle_sub)
delete _throttle_sub;
}
/**************************************************************************/
/*!
@brief Prints errors
@brief Prints errors
@param err
An error string to print.
@param len
@ -118,8 +114,7 @@ AdafruitIO::~AdafruitIO()
@return none
*/
/**************************************************************************/
void errorCallback(char *err, uint16_t len)
{
void errorCallback(char *err, uint16_t len) {
AIO_ERROR_PRINTLN();
AIO_ERROR_PRINT("ERROR: ");
AIO_ERROR_PRINTLN(err);
@ -128,28 +123,27 @@ void errorCallback(char *err, uint16_t len)
/**************************************************************************/
/*!
@brief Connects to AIO, setting up using parameters set when the
@brief Connects to AIO, setting up using parameters set when the
class is instantiated.
@return none
*/
/**************************************************************************/
void AdafruitIO::connect()
{
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) {
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) {
if (_throttle_sub) {
// setup throttle sub
_throttle_sub = new Adafruit_MQTT_Subscribe(_mqtt, _throttle_topic);
_mqtt->subscribe(_throttle_sub);
@ -157,7 +151,6 @@ void AdafruitIO::connect()
}
_connect();
}
/**************************************************************************/
@ -166,33 +159,29 @@ void AdafruitIO::connect()
@return none
*/
/**************************************************************************/
void AdafruitIO::wifi_disconnect()
{
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.
@param owner
The AIO name of the user that owns the feed, if not the current user.
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)
{
AdafruitIO_Feed *AdafruitIO::feed(const char *name) {
return new AdafruitIO_Feed(this, name);
}
AdafruitIO_Feed* AdafruitIO::feed(const char* name, const char* owner)
{
AdafruitIO_Feed *AdafruitIO::feed(const char *name, const char *owner) {
return new AdafruitIO_Feed(this, name, owner);
}
@ -204,8 +193,7 @@ AdafruitIO_Feed* AdafruitIO::feed(const char* name, const char* owner)
@return A pointer to the time.
*/
/**************************************************************************/
AdafruitIO_Time* AdafruitIO::time(aio_time_format_t format)
{
AdafruitIO_Time *AdafruitIO::time(aio_time_format_t format) {
return new AdafruitIO_Time(this, format);
}
@ -217,8 +205,7 @@ AdafruitIO_Time* AdafruitIO::time(aio_time_format_t format)
@return A pointer to the group.
*/
/**************************************************************************/
AdafruitIO_Group* AdafruitIO::group(const char* name)
{
AdafruitIO_Group *AdafruitIO::group(const char *name) {
return new AdafruitIO_Group(this, name);
}
@ -230,12 +217,10 @@ AdafruitIO_Group* AdafruitIO::group(const char* name)
@return A pointer to the dashboard.
*/
/**************************************************************************/
AdafruitIO_Dashboard* AdafruitIO::dashboard(const char* name)
{
AdafruitIO_Dashboard *AdafruitIO::dashboard(const char *name) {
return new AdafruitIO_Dashboard(this, name);
}
/**************************************************************************/
/*!
@brief Provide status explanation strings.
@ -244,78 +229,96 @@ AdafruitIO_Dashboard* AdafruitIO::dashboard(const char* name)
@return A pointer to the status string.
*/
/**************************************************************************/
const __FlashStringHelper* AdafruitIO::statusText()
{
switch(_status) {
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.");
// 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.");
// 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");
// 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
@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
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
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.
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)
{
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
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) {
while (status() < AIO_CONNECTED) {
// or return an error if the reconnection times out
if(millis() - started > AIO_NET_CONNECTION_TIMEOUT) return status();
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();
while (mqttStatus(fail_fast) != AIO_CONNECTED &&
millis() - timeStart < AIO_MQTT_CONNECTION_TIMEOUT) {
}
if (mqttStatus(fail_fast) != AIO_CONNECTED)
return status();
if(busywait_ms > 0)
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)) {
if (millis() > (_last_ping + AIO_PING_INTERVAL)) {
_mqtt->ping();
_last_ping = millis();
}
@ -325,16 +328,15 @@ aio_status_t AdafruitIO::run(uint16_t busywait_ms, bool fail_fast)
/**************************************************************************/
/*!
@brief Status check.
@return An AIO status value. Lower values represent poorer connection
@return An AIO status value. Lower values represent poorer connection
status.
*/
/**************************************************************************/
aio_status_t AdafruitIO::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) {
if (net_status != AIO_NET_CONNECTED) {
_status = net_status;
return _status;
}
@ -350,10 +352,7 @@ aio_status_t AdafruitIO::status()
@return A board ID
*/
/**************************************************************************/
char* AdafruitIO::boardID()
{
return AdafruitIO_Board::id();
}
char *AdafruitIO::boardID() { return AdafruitIO_Board::id(); }
/**************************************************************************/
/*!
@ -361,20 +360,17 @@ char* AdafruitIO::boardID()
@return A board type
*/
/**************************************************************************/
const char* AdafruitIO::boardType()
{
return AdafruitIO_Board::type();
}
const char *AdafruitIO::boardType() { return AdafruitIO_Board::type(); }
/**************************************************************************/
/*!
@brief Identify the software version.
@return A pointer to a version number string.
@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);
char *AdafruitIO::version() {
sprintf(_version, "%d.%d.%d", ADAFRUITIO_VERSION_MAJOR,
ADAFRUITIO_VERSION_MINOR, ADAFRUITIO_VERSION_PATCH);
return _version;
}
@ -384,10 +380,11 @@ char* AdafruitIO::version()
@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));
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, " (");
@ -403,44 +400,47 @@ char* AdafruitIO::userAgent()
/*!
@brief MQTT status check.
@param fail_fast
Set true to skip retries and return with status immediately, optional.
@return An MQTT status value. Lower values represent poorer connection
Set true to skip retries and return with status immediately,
optional.
@return An MQTT status value. Lower values represent poorer connection
status.
*/
/**************************************************************************/
aio_status_t AdafruitIO::mqttStatus(bool fail_fast)
{
aio_status_t AdafruitIO::mqttStatus(bool fail_fast) {
// if the connection failed,
// return so we don't hammer IO
if(_status == AIO_CONNECT_FAILED)
{
if (_status == AIO_CONNECT_FAILED) {
AIO_ERROR_PRINT("mqttStatus() failed to connect");
AIO_ERROR_PRINTLN(_mqtt->connectErrorString(_status));
return _status;
}
if(_mqtt->connected())
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){
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;
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

@ -11,32 +11,33 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_H
#define ADAFRUITIO_H
#include "Arduino.h"
#include "Adafruit_MQTT.h"
#include "AdafruitIO_Dashboard.h"
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_Feed.h"
#include "AdafruitIO_Group.h"
#include "AdafruitIO_Dashboard.h"
#include "AdafruitIO_Data.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 v0.17.0 or higher. Please install or upgrade using the Library Manager."
#error \
"This sketch requires Adafruit MQTT Library v0.17.0 or higher. Please install or upgrade using the Library Manager."
#endif
#if ADAFRUIT_MQTT_VERSION_MAJOR == 0 && ADAFRUIT_MQTT_VERSION_MINOR < 17
#error "This sketch requires Adafruit MQTT Library v0.17.0 or higher. Please install or upgrade using the Library Manager."
#error \
"This sketch requires Adafruit MQTT Library v0.17.0 or higher. Please install or upgrade using the Library Manager."
#endif
/**************************************************************************/
/*!
/*!
@brief Class for interacting with adafruit.io (AIO)
*/
/**************************************************************************/
@ -48,64 +49,63 @@ class AdafruitIO {
friend class AdafruitIO_Block;
friend class AdafruitIO_Time;
public:
AdafruitIO(const char *user, const char *key);
virtual ~AdafruitIO();
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);
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);
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();
const __FlashStringHelper *statusText();
aio_status_t status();
virtual aio_status_t networkStatus() = 0;
aio_status_t mqttStatus(bool fail_fast = false);
aio_status_t status();
virtual aio_status_t networkStatus() = 0;
aio_status_t mqttStatus(bool fail_fast = false);
char* boardID();
const char* boardType();
char* version();
char* userAgent();
virtual const char* connectionType() = 0;
char *boardID();
const char *boardType();
char *version();
char *userAgent();
virtual const char *connectionType() = 0;
protected:
virtual void _connect() = 0;
virtual void _disconnect() = 0;
aio_status_t _status = AIO_IDLE;
uint32_t _last_ping = 0;
uint32_t _last_mqtt_connect = 0;
protected:
virtual void _connect() = 0;
virtual void _disconnect() = 0;
aio_status_t _status = AIO_IDLE;
uint32_t _last_ping = 0;
uint32_t _last_mqtt_connect = 0;
Adafruit_MQTT *_mqtt;
HttpClient *_http;
Adafruit_MQTT *_mqtt;
HttpClient *_http;
char _version[10];
char _version[10];
const char *_host = "io.adafruit.com";
uint16_t _mqtt_port = 8883;
uint16_t _mqtt_eth_port = 1883;
uint16_t _http_port = 443;
const char *_host = "io.adafruit.com";
uint16_t _mqtt_port = 8883;
uint16_t _mqtt_eth_port = 1883;
uint16_t _http_port = 443;
uint16_t _packetread_timeout;
uint16_t _packetread_timeout;
const char *_username;
const char *_key;
const char *_username;
const char *_key;
char *_err_topic;
char *_throttle_topic;
char *_user_agent;
char *_err_topic;
char *_throttle_topic;
char *_user_agent;
Adafruit_MQTT_Subscribe *_err_sub;
Adafruit_MQTT_Subscribe *_throttle_sub;
private:
void _init();
Adafruit_MQTT_Subscribe *_err_sub;
Adafruit_MQTT_Subscribe *_throttle_sub;
private:
void _init();
};
#endif // ADAFRUITIO_H

View file

@ -12,16 +12,14 @@
#include "AdafruitIO_Dashboard.h"
#include "AdafruitIO.h"
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *n)
{
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *n) {
_io = io;
name = n;
}
AdafruitIO_Dashboard::~AdafruitIO_Dashboard(){}
AdafruitIO_Dashboard::~AdafruitIO_Dashboard() {}
bool AdafruitIO_Dashboard::exists()
{
bool AdafruitIO_Dashboard::exists() {
String url = "/api/v2/";
url += _io->_username;
url += "/dashboards/";
@ -38,8 +36,7 @@ bool AdafruitIO_Dashboard::exists()
return status == 200;
}
bool AdafruitIO_Dashboard::create()
{
bool AdafruitIO_Dashboard::create() {
String url = "/api/v2/";
url += _io->_username;
url += "/dashboards";
@ -69,60 +66,46 @@ bool AdafruitIO_Dashboard::create()
return status == 201;
}
const char* AdafruitIO_Dashboard::user() {
return _io->_username;
}
const char *AdafruitIO_Dashboard::user() { return _io->_username; }
AdafruitIO* AdafruitIO_Dashboard::io() {
return _io;
}
AdafruitIO *AdafruitIO_Dashboard::io() { return _io; }
ToggleBlock* AdafruitIO_Dashboard::addToggleBlock(AdafruitIO_Feed *feed)
{
ToggleBlock *AdafruitIO_Dashboard::addToggleBlock(AdafruitIO_Feed *feed) {
return new ToggleBlock(this, feed);
}
MomentaryBlock* AdafruitIO_Dashboard::addMomentaryBlock(AdafruitIO_Feed *feed)
{
MomentaryBlock *AdafruitIO_Dashboard::addMomentaryBlock(AdafruitIO_Feed *feed) {
return new MomentaryBlock(this, feed);
}
SliderBlock* AdafruitIO_Dashboard::addSliderBlock(AdafruitIO_Feed *feed)
{
SliderBlock *AdafruitIO_Dashboard::addSliderBlock(AdafruitIO_Feed *feed) {
return new SliderBlock(this, feed);
}
GaugeBlock* AdafruitIO_Dashboard::addGaugeBlock(AdafruitIO_Feed *feed)
{
GaugeBlock *AdafruitIO_Dashboard::addGaugeBlock(AdafruitIO_Feed *feed) {
return new GaugeBlock(this, feed);
}
TextBlock* AdafruitIO_Dashboard::addTextBlock(AdafruitIO_Feed *feed)
{
TextBlock *AdafruitIO_Dashboard::addTextBlock(AdafruitIO_Feed *feed) {
return new TextBlock(this, feed);
}
ChartBlock* AdafruitIO_Dashboard::addChartBlock(AdafruitIO_Feed *feed)
{
ChartBlock *AdafruitIO_Dashboard::addChartBlock(AdafruitIO_Feed *feed) {
return new ChartBlock(this, feed);
}
ColorBlock* AdafruitIO_Dashboard::addColorBlock(AdafruitIO_Feed *feed)
{
ColorBlock *AdafruitIO_Dashboard::addColorBlock(AdafruitIO_Feed *feed) {
return new ColorBlock(this, feed);
}
MapBlock* AdafruitIO_Dashboard::addMapBlock(AdafruitIO_Feed *feed)
{
MapBlock *AdafruitIO_Dashboard::addMapBlock(AdafruitIO_Feed *feed) {
return new MapBlock(this, feed);
}
StreamBlock* AdafruitIO_Dashboard::addStreamBlock(AdafruitIO_Feed *feed)
{
StreamBlock *AdafruitIO_Dashboard::addStreamBlock(AdafruitIO_Feed *feed) {
return new StreamBlock(this, feed);
}
ImageBlock* AdafruitIO_Dashboard::addImageBlock(AdafruitIO_Feed *feed)
{
ImageBlock *AdafruitIO_Dashboard::addImageBlock(AdafruitIO_Feed *feed) {
return new ImageBlock(this, feed);
}

View file

@ -12,18 +12,18 @@
#ifndef ADAFRUITIO_DASHBOARD_H
#define ADAFRUITIO_DASHBOARD_H
#include "Arduino.h"
#include "AdafruitIO_Definitions.h"
#include "blocks/ToggleBlock.h"
#include "blocks/MomentaryBlock.h"
#include "blocks/SliderBlock.h"
#include "blocks/GaugeBlock.h"
#include "blocks/TextBlock.h"
#include "Arduino.h"
#include "blocks/ChartBlock.h"
#include "blocks/ColorBlock.h"
#include "blocks/MapBlock.h"
#include "blocks/StreamBlock.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;
@ -31,32 +31,31 @@ class AdafruitIO_Feed;
class AdafruitIO_Dashboard {
public:
AdafruitIO_Dashboard(AdafruitIO *io, const char *name);
~AdafruitIO_Dashboard();
public:
AdafruitIO_Dashboard(AdafruitIO *io, const char *name);
~AdafruitIO_Dashboard();
const char* name;
const char* user();
const char *name;
const char *user();
AdafruitIO* io();
AdafruitIO *io();
bool exists();
bool create();
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;
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;
};
#endif // ADAFRUITIO_DASHBOARD_H

View file

@ -12,8 +12,7 @@
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Feed.h"
AdafruitIO_Data::AdafruitIO_Data()
{
AdafruitIO_Data::AdafruitIO_Data() {
_lat = 0;
_lon = 0;
_ele = 0;
@ -24,8 +23,7 @@ AdafruitIO_Data::AdafruitIO_Data()
memset(_csv, 0, AIO_CSV_LENGTH);
}
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f)
{
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f) {
_lat = 0;
_lon = 0;
_ele = 0;
@ -37,8 +35,7 @@ AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f)
memset(_csv, 0, AIO_CSV_LENGTH);
}
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, char *csv)
{
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
@ -53,8 +50,7 @@ AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, char *csv)
_parseCSV();
}
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, const char *csv)
{
AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, const char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
@ -69,8 +65,7 @@ AdafruitIO_Data::AdafruitIO_Data(AdafruitIO_Feed *f, const char *csv)
_parseCSV();
}
AdafruitIO_Data::AdafruitIO_Data(const char *f)
{
AdafruitIO_Data::AdafruitIO_Data(const char *f) {
_lat = 0;
_lon = 0;
_ele = 0;
@ -81,8 +76,7 @@ AdafruitIO_Data::AdafruitIO_Data(const char *f)
memset(_value, 0, AIO_DATA_LENGTH);
}
AdafruitIO_Data::AdafruitIO_Data(const char *f, char *csv)
{
AdafruitIO_Data::AdafruitIO_Data(const char *f, char *csv) {
_lat = 0;
_lon = 0;
_ele = 0;
@ -97,44 +91,39 @@ AdafruitIO_Data::AdafruitIO_Data(const char *f, char *csv)
_parseCSV();
}
bool AdafruitIO_Data::setCSV(char *csv)
{
return setCSV((const char *)(csv));
}
bool AdafruitIO_Data::setCSV(char *csv) { return setCSV((const char *)(csv)); }
bool AdafruitIO_Data::setCSV(const char *csv)
{
bool AdafruitIO_Data::setCSV(const char *csv) {
memset(_csv, 0, AIO_CSV_LENGTH);
strcpy(_csv, csv);
return _parseCSV();
}
void AdafruitIO_Data::setLocation(double lat, double lon, double ele)
{
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))
if ((abs(0 - lat) < 0.000001) && (abs(0 - lon) < 0.000001) &&
(abs(0 - ele) < 0.000001))
return;
_lat = lat;
_lon = lon;
_ele = ele;
}
void AdafruitIO_Data::setValue(const char *value, double lat, double lon, double ele)
{
void AdafruitIO_Data::setValue(const char *value, double lat, double lon,
double ele) {
strcpy(_value, value);
setLocation(lat, lon, ele);
}
void AdafruitIO_Data::setValue(char *value, double lat, double lon, double ele)
{
void AdafruitIO_Data::setValue(char *value, double lat, double lon,
double ele) {
strcpy(_value, value);
setLocation(lat, lon, ele);
}
void AdafruitIO_Data::setValue(bool value, double lat, double lon, double ele)
{
if(value)
void AdafruitIO_Data::setValue(bool value, double lat, double lon, double ele) {
if (value)
strcpy(_value, "1");
else
strcpy(_value, "0");
@ -142,256 +131,223 @@ void AdafruitIO_Data::setValue(bool value, double lat, double lon, double ele)
setLocation(lat, lon, ele);
}
void AdafruitIO_Data::setValue(String value, double lat, double lon, double ele)
{
void AdafruitIO_Data::setValue(String value, double lat, double lon,
double ele) {
value.toCharArray(_value, value.length() + 1);
setLocation(lat, lon, ele);
}
void AdafruitIO_Data::setValue(int value, double lat, double lon, double ele)
{
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);
}
void AdafruitIO_Data::setValue(unsigned int value, double lat, double lon, double ele)
{
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);
}
void AdafruitIO_Data::setValue(long value, double lat, double lon, double ele)
{
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);
}
void AdafruitIO_Data::setValue(unsigned long value, double lat, double lon, double ele)
{
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);
}
void AdafruitIO_Data::setValue(float value, double lat, double lon, double ele, int precision)
{
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
#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);
}
void AdafruitIO_Data::setValue(double value, double lat, double lon, double ele, int precision)
{
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
#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);
}
char* AdafruitIO_Data::feedName()
{
if(! _feed)
return (char*)"";
char *AdafruitIO_Data::feedName() {
if (!_feed)
return (char *)"";
return _feed;
}
char* AdafruitIO_Data::value()
{
return toChar();
}
char *AdafruitIO_Data::value() { return toChar(); }
char* AdafruitIO_Data::toChar()
{
return _value;
}
char *AdafruitIO_Data::toChar() { return _value; }
String AdafruitIO_Data::toString()
{
if(! _value)
String AdafruitIO_Data::toString() {
if (!_value)
return String();
return String(_value);
}
bool AdafruitIO_Data::toBool()
{
if(! _value)
bool AdafruitIO_Data::toBool() {
if (!_value)
return false;
if(strcmp(_value, "1") == 0 || _value[0] == 't' || _value[0] == 'T')
if (strcmp(_value, "1") == 0 || _value[0] == 't' || _value[0] == 'T')
return true;
else
return false;
}
bool AdafruitIO_Data::isTrue()
{
return toBool();
}
bool AdafruitIO_Data::isTrue() { return toBool(); }
bool AdafruitIO_Data::isFalse()
{
return !toBool();
}
bool AdafruitIO_Data::isFalse() { return !toBool(); }
int AdafruitIO_Data::toInt()
{
if(! _value)
int AdafruitIO_Data::toInt() {
if (!_value)
return 0;
char* endptr;
char *endptr;
return (int)strtol(_value, &endptr, 10);
}
int AdafruitIO_Data::toPinLevel()
{
if(isTrue())
int AdafruitIO_Data::toPinLevel() {
if (isTrue())
return HIGH;
else
return LOW;
}
unsigned int AdafruitIO_Data::toUnsignedInt()
{
if(! _value)
unsigned int AdafruitIO_Data::toUnsignedInt() {
if (!_value)
return 0;
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
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
}
float AdafruitIO_Data::toFloat()
{
if(! _value)
float AdafruitIO_Data::toFloat() {
if (!_value)
return 0;
char* endptr;
char *endptr;
return (float)strtod(_value, &endptr);
}
double AdafruitIO_Data::toDouble()
{
if(! _value)
double AdafruitIO_Data::toDouble() {
if (!_value)
return 0;
char* endptr;
char *endptr;
return strtod(_value, &endptr);
}
long AdafruitIO_Data::toLong()
{
if(! _value)
long AdafruitIO_Data::toLong() {
if (!_value)
return 0;
char* endptr;
char *endptr;
return strtol(_value, &endptr, 10);
}
unsigned long AdafruitIO_Data::toUnsignedLong()
{
if(! _value)
unsigned long AdafruitIO_Data::toUnsignedLong() {
if (!_value)
return 0;
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
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
}
int AdafruitIO_Data::toRed()
{
// Convert 0xRRGGBB to red.
if (! _value)
{
return 0;
}
char r[5];
strcpy(r, "0x");
strncpy(&r[2], toChar() + 1, 2);
r[4] = '\x00';
return (int)strtol(r, NULL, 0);
int AdafruitIO_Data::toRed() {
// Convert 0xRRGGBB to red.
if (!_value) {
return 0;
}
char r[5];
strcpy(r, "0x");
strncpy(&r[2], toChar() + 1, 2);
r[4] = '\x00';
return (int)strtol(r, NULL, 0);
}
int AdafruitIO_Data::toGreen()
{
// Convert 0xRRGGBB to green.
if (! _value)
{
return 0;
}
char g[5];
strcpy(g, "0x");
strncpy(&g[2], toChar() + 3, 2);
g[4] = '\x00';
return (int)strtol(g, NULL, 0);
int AdafruitIO_Data::toGreen() {
// Convert 0xRRGGBB to green.
if (!_value) {
return 0;
}
char g[5];
strcpy(g, "0x");
strncpy(&g[2], toChar() + 3, 2);
g[4] = '\x00';
return (int)strtol(g, NULL, 0);
}
int AdafruitIO_Data::toBlue()
{
// Convert 0xRRGGBB to blue.
if (! _value)
{
return 0;
}
char b[5];
strcpy(b, "0x");
strncpy(&b[2], toChar() + 5, 2);
b[4] = '\x00';
return (int)strtol(b, NULL, 0);
int AdafruitIO_Data::toBlue() {
// Convert 0xRRGGBB to blue.
if (!_value) {
return 0;
}
char b[5];
strcpy(b, "0x");
strncpy(&b[2], toChar() + 5, 2);
b[4] = '\x00';
return (int)strtol(b, NULL, 0);
}
long AdafruitIO_Data::toNeoPixel()
{
if (! _value)
{
return 0;
}
char rgb[9];
strcpy(rgb, "0x");
strncpy(&rgb[2], toChar() + 1, 6);
return strtol(rgb, NULL, 0);
long AdafruitIO_Data::toNeoPixel() {
if (!_value) {
return 0;
}
char rgb[9];
strcpy(rgb, "0x");
strncpy(&rgb[2], toChar() + 1, 6);
return strtol(rgb, NULL, 0);
}
char* AdafruitIO_Data::toCSV()
{
if(! _value)
char *AdafruitIO_Data::toCSV() {
if (!_value)
return _csv;
memset(_csv, 0, AIO_CSV_LENGTH);
@ -408,45 +364,36 @@ char* AdafruitIO_Data::toCSV()
return _csv;
}
double AdafruitIO_Data::lat()
{
return _lat;
}
double AdafruitIO_Data::lat() { return _lat; }
double AdafruitIO_Data::lon()
{
return _lon;
}
double AdafruitIO_Data::lon() { return _lon; }
double AdafruitIO_Data::ele()
{
return _ele;
}
double AdafruitIO_Data::ele() { return _ele; }
static char _double_buffer[20];
char* AdafruitIO_Data::charFromDouble(double d, int precision)
{
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
#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
* https://github.com/semitrivial/csv_parser/blob/93246cac509f85da12c6bb8c641fa75cd863c34f/csv.c
* - retrieved 2017-11-09
*
* MIT License
*
@ -472,19 +419,14 @@ char* AdafruitIO_Data::charFromDouble(double d, int precision)
*
*/
static int count_fields( const char *line )
{
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] == '\"' )
{
for (cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++) {
if (fQuote) {
if (*ptr == '\"') {
if (ptr[1] == '\"') {
ptr++;
continue;
}
@ -493,21 +435,19 @@ static int count_fields( const char *line )
continue;
}
switch( *ptr )
{
case '\"':
fQuote = 1;
continue;
case ',':
cnt++;
continue;
default:
continue;
switch (*ptr) {
case '\"':
fQuote = 1;
continue;
case ',':
cnt++;
continue;
default:
continue;
}
}
if ( fQuote )
{
if (fQuote) {
return -1;
}
@ -519,121 +459,103 @@ static int count_fields( const char *line )
* which are escaped by "double quotes", extract a NULL-terminated
* array of strings, one for every cell in the row.
*/
char **parse_csv( const char *line )
{
char **parse_csv(const char *line) {
char **buf, **bptr, *tmp, *tptr;
const char *ptr;
int fieldcnt, fQuote, fEnd;
fieldcnt = count_fields( line );
fieldcnt = count_fields(line);
if ( fieldcnt == -1 )
{
if (fieldcnt == -1) {
return NULL;
}
buf = (char **)malloc( sizeof(char*) * (fieldcnt+1) );
buf = (char **)malloc(sizeof(char *) * (fieldcnt + 1));
if ( !buf )
{
if (!buf) {
return NULL;
}
tmp = (char *)malloc( strlen(line) + 1 );
tmp = (char *)malloc(strlen(line) + 1);
if ( !tmp )
{
free( buf );
if (!tmp) {
free(buf);
return NULL;
}
bptr = buf;
for ( ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0; ; ptr++ )
{
if ( fQuote )
{
if ( !*ptr )
{
for (ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0;; ptr++) {
if (fQuote) {
if (!*ptr) {
break;
}
if ( *ptr == '\"' )
{
if ( ptr[1] == '\"' )
{
if (*ptr == '\"') {
if (ptr[1] == '\"') {
*tptr++ = '\"';
ptr++;
continue;
}
fQuote = 0;
}
else {
} else {
*tptr++ = *ptr;
}
continue;
}
switch( *ptr )
{
case '\"':
fQuote = 1;
continue;
case '\0':
fEnd = 1;
case ',':
*tptr = '\0';
*bptr = strdup( tmp );
switch (*ptr) {
case '\"':
fQuote = 1;
continue;
case '\0':
fEnd = 1;
case ',':
*tptr = '\0';
*bptr = strdup(tmp);
if ( !*bptr )
{
for ( bptr--; bptr >= buf; bptr-- )
{
free( *bptr );
}
free( buf );
free( tmp );
return NULL;
if (!*bptr) {
for (bptr--; bptr >= buf; bptr--) {
free(*bptr);
}
free(buf);
free(tmp);
bptr++;
tptr = tmp;
return NULL;
}
if ( fEnd )
{
break;
} else
{
continue;
}
bptr++;
tptr = tmp;
default:
*tptr++ = *ptr;
if (fEnd) {
break;
} else {
continue;
}
default:
*tptr++ = *ptr;
continue;
}
if ( fEnd )
{
if (fEnd) {
break;
}
}
*bptr = NULL;
free( tmp );
free(tmp);
return buf;
}
//// END simple_csv SECTION
bool AdafruitIO_Data::_parseCSV()
{
bool AdafruitIO_Data::_parseCSV() {
int field_count = count_fields(_csv);
if (field_count > 0)
{
if (field_count > 0) {
// this is normal IO data in `value,lat,lon,ele` format
char **fields = parse_csv(_csv);
@ -642,38 +564,32 @@ bool AdafruitIO_Data::_parseCSV()
field_count--;
// locations fields are handled with char * to float conversion
if (field_count > 0)
{
if (field_count > 0) {
_lat = atof(fields[1]);
field_count--;
}
if (field_count > 0)
{
if (field_count > 0) {
_lon = atof(fields[1]);
field_count--;
}
if (field_count > 0)
{
if (field_count > 0) {
_ele = atof(fields[1]);
field_count--;
}
// cleanup to avoid leaks
int i = 0;
while (fields[i] != NULL){
while (fields[i] != NULL) {
free(fields[i++]);
}
free(fields);
return field_count == 0;
}
else
{
} else {
return false;
}
return true;
}

View file

@ -12,85 +12,86 @@
#ifndef ADAFRUITIO_DATA_H
#define ADAFRUITIO_DATA_H
#include "Arduino.h"
#include "AdafruitIO_Definitions.h"
#include "Arduino.h"
// forward decl
class AdafruitIO_Feed;
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);
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);
bool setCSV(char *csv);
bool setCSV(const char *csv);
void setLocation(double lat, double lon, double ele=0);
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);
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 *feedName();
char* value();
char* toChar();
String toString();
char *value();
char *toChar();
String toString();
bool toBool();
bool isTrue();
bool isFalse();
bool toBool();
bool isTrue();
bool isFalse();
int toInt();
int toPinLevel();
unsigned int toUnsignedInt();
int toInt();
int toPinLevel();
unsigned int toUnsignedInt();
double toDouble();
float toFloat();
double toDouble();
float toFloat();
long toLong();
unsigned long toUnsignedLong();
long toLong();
unsigned long toUnsignedLong();
int toRed();
int toGreen();
int toBlue();
long toNeoPixel();
int toRed();
int toGreen();
int toBlue();
long toNeoPixel();
char* toCSV();
char* charFromDouble(double d, int precision=6);
char *toCSV();
char *charFromDouble(double d, int precision = 6);
double lat();
double lon();
double ele();
double lat();
double lon();
double ele();
AdafruitIO_Data *next_data;
AdafruitIO_Data *next_data;
private:
private:
char _feed[AIO_FEED_NAME_LENGTH];
char _feed[AIO_FEED_NAME_LENGTH];
char _csv[AIO_CSV_LENGTH];
char _value[AIO_DATA_LENGTH];
char _csv[AIO_CSV_LENGTH];
char _value[AIO_DATA_LENGTH];
double _lat,
_lon,
_ele;
bool _parseCSV();
double _lat, _lon, _ele;
bool _parseCSV();
};
#endif // ADAFRUITIO_DATA_H

View file

@ -22,23 +22,22 @@ class AdafruitIO_Data;
typedef void (*AdafruitIODataCallbackType)(AdafruitIO_Data *data);
class AdafruitIOGroupCallback {
public:
AdafruitIOGroupCallback(const char *f, AdafruitIODataCallbackType cb) {
feed = f;
dataCallback = cb;
next_cb = 0;
}
public:
AdafruitIOGroupCallback(const char *f, AdafruitIODataCallbackType cb) {
feed = f;
dataCallback = cb;
next_cb = 0;
}
AdafruitIOGroupCallback(AdafruitIODataCallbackType cb) {
feed = 0;
dataCallback = cb;
next_cb = 0;
}
const char *feed;
AdafruitIODataCallbackType dataCallback;
AdafruitIOGroupCallback *next_cb;
AdafruitIOGroupCallback(AdafruitIODataCallbackType cb) {
feed = 0;
dataCallback = cb;
next_cb = 0;
}
const char *feed;
AdafruitIODataCallbackType dataCallback;
AdafruitIOGroupCallback *next_cb;
};
// Uncomment/comment to turn on/off debug output messages.
@ -47,25 +46,34 @@ class AdafruitIOGroupCallback {
// #define AIO_ERROR
// Where debug messages will be printed
// note: if you're using something like Zero or Due, change the below to SerialUSB
// note: if you're using something like Zero or Due, change the below to
// SerialUSB
#define AIO_PRINTER Serial
// Define actual debug output functions when necessary.
#ifdef AIO_DEBUG
#define AIO_DEBUG_PRINT(...) { AIO_PRINTER.print(__VA_ARGS__); }
#define AIO_DEBUG_PRINTLN(...) { AIO_PRINTER.println(__VA_ARGS__); }
#define AIO_DEBUG_PRINT(...) \
{ AIO_PRINTER.print(__VA_ARGS__); }
#define AIO_DEBUG_PRINTLN(...) \
{ AIO_PRINTER.println(__VA_ARGS__); }
#else
#define AIO_DEBUG_PRINT(...) {}
#define AIO_DEBUG_PRINTLN(...) {}
#define AIO_DEBUG_PRINT(...) \
{}
#define AIO_DEBUG_PRINTLN(...) \
{}
#endif
// Define actual error output functions when necessary.
#ifdef AIO_ERROR
#define AIO_ERROR_PRINT(...) { AIO_PRINTER.print(__VA_ARGS__); }
#define AIO_ERROR_PRINTLN(...) { AIO_PRINTER.println(__VA_ARGS__); }
#define AIO_ERROR_PRINT(...) \
{ AIO_PRINTER.print(__VA_ARGS__); }
#define AIO_ERROR_PRINTLN(...) \
{ AIO_PRINTER.println(__VA_ARGS__); }
#else
#define AIO_ERROR_PRINT(...) {}
#define AIO_ERROR_PRINTLN(...) {}
#define AIO_ERROR_PRINT(...) \
{}
#define AIO_ERROR_PRINTLN(...) \
{}
#endif
// Adafruit IO Ping Interval, in milliseconds
@ -73,18 +81,20 @@ class AdafruitIOGroupCallback {
// Time to wait between re-connecting to Adafruit IO after throttled
#define AIO_THROTTLE_RECONNECT_INTERVAL 60000
// Time to wait for a successful reconnection after MQTT disconnect
#define AIO_MQTT_CONNECTION_TIMEOUT 60000
#define AIO_MQTT_CONNECTION_TIMEOUT 60000
// Time to wait for a successful reconnection after network disconnect
#define AIO_NET_CONNECTION_TIMEOUT 60000
#define AIO_NET_CONNECTION_TIMEOUT 60000
// Time to wait for a net disconnect to take effect
#define AIO_NET_DISCONNECT_WAIT 300
#define AIO_NET_DISCONNECT_WAIT 300
#define AIO_ERROR_TOPIC "/errors"
#define AIO_THROTTLE_TOPIC "/throttle"
// latest fingerprint can be generated with
// echo | openssl s_client -connect io.adafruit.com:443 | openssl x509 -fingerprint -noout
#define AIO_SSL_FINGERPRINT "77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18"
// echo | openssl s_client -connect io.adafruit.com:443 | openssl x509
// -fingerprint -noout
#define AIO_SSL_FINGERPRINT \
"77 00 54 2D DA E7 D8 03 27 31 23 99 EB 27 DB CB A5 4C 57 18"
// Maximum length of an Adafruit IO Feed name
#define AIO_FEED_NAME_LENGTH 20
@ -96,25 +106,26 @@ class AdafruitIOGroupCallback {
/** aio_status_t offers 13 status states */
typedef enum {
// CONNECTING
AIO_IDLE = 0,
AIO_NET_DISCONNECTED = 1, // Network disconnected
AIO_DISCONNECTED = 2, // Disconnected from Adafruit IO
AIO_FINGERPRINT_UNKOWN = 3, // Unknown AIO_SSL_FINGERPRINT
// CONNECTING
AIO_IDLE = 0,
AIO_NET_DISCONNECTED = 1, // Network disconnected
AIO_DISCONNECTED = 2, // Disconnected from Adafruit IO
AIO_FINGERPRINT_UNKOWN = 3, // Unknown AIO_SSL_FINGERPRINT
// FAILURE
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
// FAILURE
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
// SUCCESS
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
// SUCCESS
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;
@ -122,10 +133,9 @@ typedef enum {
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_MILLIS = 1, // Milisecond MQTT feed
AIO_TIME_ISO = 2 // ISO8601 MQTT Feed
} aio_time_format_t;
#endif /* ADAFRUITIO_DEFINITIONS_H_ */

View file

@ -12,32 +12,31 @@
#ifndef ADAFRUITIO_ETHERNET_H
#define ADAFRUITIO_ETHERNET_H
#include "Arduino.h"
#include <SPI.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 <Dns.h>
#include <Dhcp.h>
#include "AdafruitIO.h"
// all logic in .h to avoid auto compile
class AdafruitIO_Ethernet : public AdafruitIO
{
class AdafruitIO_Ethernet : public AdafruitIO {
public:
AdafruitIO_Ethernet(const char *user, const char *key) : AdafruitIO(user, 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);
_mqtt = new Adafruit_MQTT_Client(_client, _host, _mqtt_eth_port, _username,
_key);
_http = new HttpClient(*_client, _host, _http_port);
}
aio_status_t networkStatus()
{
aio_status_t networkStatus() {
if (_status == AIO_NET_CONNECTED)
return _status;
@ -45,30 +44,23 @@ public:
return _status;
}
const char *connectionType()
{
return "ethernet_wing";
}
const char *connectionType() { return "ethernet_wing"; }
protected:
byte _mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient *_client;
void _connect()
{
if (Ethernet.begin(_mac) == 0)
{
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.");
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
{
} else {
_status = AIO_NET_CONNECTED;
}
}

View file

@ -12,82 +12,75 @@
#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>
#include "Adafruit_MQTT.h"
#include "Adafruit_FONA.h"
#include "AdafruitIO.h"
#include "Adafruit_MQTT_FONA.h"
#define FONA_RX 9
#define FONA_TX 8
#define FONA_RX 9
#define FONA_TX 8
#define FONA_RST 4
#define FONA_RI 7
#define FONA_RI 7
#define FONA_BAUD 4800
// all logic in .h to avoid auto compile
class AdafruitIO_FONA : public AdafruitIO {
public:
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;
}
public:
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;
}
void setAPN(FONAFlashStringPtr apn, FONAFlashStringPtr username=0, FONAFlashStringPtr password=0)
{
_fona->setGPRSNetworkSettings(apn, username, password);
}
void setAPN(FONAFlashStringPtr apn, FONAFlashStringPtr username = 0,
FONAFlashStringPtr password = 0) {
_fona->setGPRSNetworkSettings(apn, username, password);
}
aio_status_t AdafruitIO_FONA::networkStatus()
{
// return if in a failed state
if(_status == AIO_NET_CONNECT_FAILED)
return _status;
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);
// 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;
}
const char *connectionType() { return "fona"; }
protected:
uint16_t _mqtt_port = 1883;
SoftwareSerial *_serial;
Adafruit_FONA *_fona;
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;
}
const char* connectionType()
{
return "fona";
}
protected:
uint16_t _mqtt_port = 1883;
SoftwareSerial *_serial;
Adafruit_FONA *_fona;
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;
}
// disable cme error reporting
_serial->println("AT+CMEE=2");
_status = AIO_NET_DISCONNECTED;
}
};
#endif // ADAFRUITIO_FONA_H

View file

@ -12,8 +12,8 @@
#include "AdafruitIO_Feed.h"
#include "AdafruitIO.h"
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n):AdafruitIO_MQTT()
{
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = _io->_username;
@ -21,8 +21,8 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n):AdafruitIO_MQTT(
_init();
}
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n, const char *un):AdafruitIO_MQTT()
{
AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n, const char *un)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = un;
@ -30,99 +30,88 @@ AdafruitIO_Feed::AdafruitIO_Feed(AdafruitIO *io, const char *n, const char *un):
_init();
}
AdafruitIO_Feed::~AdafruitIO_Feed()
{
if(_sub)
AdafruitIO_Feed::~AdafruitIO_Feed() {
if (_sub)
delete _sub;
if(_pub)
if (_pub)
delete _pub;
if(_get_pub)
if (_get_pub)
delete _get_pub;
if(data)
if (data)
delete data;
if(_topic)
if (_topic)
free(_topic);
if (_get_topic)
free(_get_topic);
if(_feed_url)
if (_feed_url)
free(_feed_url);
if(_create_url)
if (_create_url)
free(_create_url);
}
void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb)
{
void AdafruitIO_Feed::onMessage(AdafruitIODataCallbackType cb) {
_dataCallback = cb;
}
bool AdafruitIO_Feed::save(char *value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(char *value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(bool value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(bool value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(String value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(String value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(int value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(int value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(unsigned int value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(unsigned int value, double lat, double lon,
double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(long value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(long value, double lat, double lon, double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(unsigned long value, double lat, double lon, double ele)
{
bool AdafruitIO_Feed::save(unsigned long value, double lat, double lon,
double ele) {
data->setValue(value, lat, lon, ele);
return _pub->publish(data->toCSV());
}
bool AdafruitIO_Feed::save(float value, double lat, double lon, double ele, int precision)
{
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());
}
bool AdafruitIO_Feed::save(double value, double lat, double lon, double ele, int precision)
{
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());
}
bool AdafruitIO_Feed::get()
{
return _get_pub->publish("\0");
}
bool AdafruitIO_Feed::get() { return _get_pub->publish("\0"); }
bool AdafruitIO_Feed::exists()
{
bool AdafruitIO_Feed::exists() {
_io->_http->beginRequest();
_io->_http->get(_feed_url);
_io->_http->sendHeader("X-AIO-Key", _io->_key);
@ -134,8 +123,7 @@ bool AdafruitIO_Feed::exists()
return status == 200;
}
bool AdafruitIO_Feed::create()
{
bool AdafruitIO_Feed::create() {
String body = "name=";
body += name;
@ -161,8 +149,7 @@ bool AdafruitIO_Feed::create()
return status == 201;
}
AdafruitIO_Data* AdafruitIO_Feed::lastValue()
{
AdafruitIO_Data *AdafruitIO_Feed::lastValue() {
// 15 extra for api path, 12 for /data/retain, 1 for null
String url = "/api/v2/";
url += owner;
@ -197,41 +184,45 @@ AdafruitIO_Data* AdafruitIO_Feed::lastValue()
AIO_ERROR_PRINTLN(_io->_http->responseBody());
return NULL;
}
}
void AdafruitIO_Feed::setLocation(double lat, double lon, double ele)
{
void AdafruitIO_Feed::setLocation(double lat, double lon, double ele) {
data->setLocation(lat, lon, ele);
}
void AdafruitIO_Feed::subCallback(char *val, uint16_t len)
{
void AdafruitIO_Feed::subCallback(char *val, uint16_t len) {
data->setCSV(val);
// call callback with data
if(_dataCallback)
if (_dataCallback)
_dataCallback(data);
}
void AdafruitIO_Feed::_init()
{
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
_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) {
if (_topic && _create_url && _feed_url) {
// build topic string
strcpy(_topic, owner);
@ -275,7 +266,5 @@ void AdafruitIO_Feed::_init()
_pub = 0;
_get_pub = 0;
data = 0;
}
}

View file

@ -12,66 +12,68 @@
#ifndef ADAFRUITIO_FEED_H
#define ADAFRUITIO_FEED_H
#include "Arduino.h"
#include "Adafruit_MQTT.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
class AdafruitIO_Feed : public AdafruitIO_MQTT {
public:
AdafruitIO_Feed(AdafruitIO *io, const char *name);
AdafruitIO_Feed(AdafruitIO *io, const char *name, const char *owner);
public:
AdafruitIO_Feed(AdafruitIO *io, const char *name);
AdafruitIO_Feed(AdafruitIO *io, const char *name, const char *owner);
~AdafruitIO_Feed();
~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 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 get();
bool exists();
bool create();
bool exists();
bool create();
void setLocation(double lat, double lon, double ele=0);
void setLocation(double lat, double lon, double ele = 0);
void onMessage(AdafruitIODataCallbackType cb);
void subCallback(char *val, uint16_t len);
void onMessage(AdafruitIODataCallbackType cb);
void subCallback(char *val, uint16_t len);
const char *name;
const char *owner;
const char *name;
const char *owner;
AdafruitIO_Data *lastValue();
AdafruitIO_Data *data;
AdafruitIO_Data *lastValue();
AdafruitIO_Data *data;
private:
AdafruitIODataCallbackType _dataCallback;
private:
AdafruitIODataCallbackType _dataCallback;
void _init();
void _init();
char *_topic;
char *_get_topic;
char *_create_url;
char *_feed_url;
char *_topic;
char *_get_topic;
char *_create_url;
char *_feed_url;
Adafruit_MQTT_Subscribe *_sub;
Adafruit_MQTT_Publish *_pub;
Adafruit_MQTT_Publish *_get_pub;
AdafruitIO *_io;
AdafruitIO_Data *_data;
Adafruit_MQTT_Subscribe *_sub;
Adafruit_MQTT_Publish *_pub;
Adafruit_MQTT_Publish *_get_pub;
AdafruitIO *_io;
AdafruitIO_Data *_data;
};
#endif // ADAFRUITIO_FEED_H

View file

@ -12,8 +12,8 @@
#include "AdafruitIO_Group.h"
#include "AdafruitIO.h"
AdafruitIO_Group::AdafruitIO_Group(AdafruitIO *io, const char *n):AdafruitIO_MQTT()
{
AdafruitIO_Group::AdafruitIO_Group(AdafruitIO *io, const char *n)
: AdafruitIO_MQTT() {
_io = io;
name = n;
owner = _io->_username;
@ -21,91 +21,80 @@ AdafruitIO_Group::AdafruitIO_Group(AdafruitIO *io, const char *n):AdafruitIO_MQT
_init();
}
AdafruitIO_Group::~AdafruitIO_Group()
{
if(_sub)
AdafruitIO_Group::~AdafruitIO_Group() {
if (_sub)
delete _sub;
if(_pub)
if (_pub)
delete _pub;
if(_get_pub)
if (_get_pub)
delete _get_pub;
if(data)
if (data)
delete data;
if(_topic)
if (_topic)
free(_topic);
if (_get_topic)
free(_get_topic);
if(_group_url)
if (_group_url)
free(_group_url);
if(_create_url)
if (_create_url)
free(_create_url);
}
void AdafruitIO_Group::set(const char *feed, char *value)
{
void AdafruitIO_Group::set(const char *feed, char *value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, bool value)
{
void AdafruitIO_Group::set(const char *feed, bool value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, String value)
{
void AdafruitIO_Group::set(const char *feed, String value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, int value)
{
void AdafruitIO_Group::set(const char *feed, int value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, unsigned int value)
{
void AdafruitIO_Group::set(const char *feed, unsigned int value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, long value)
{
void AdafruitIO_Group::set(const char *feed, long value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, unsigned long value)
{
void AdafruitIO_Group::set(const char *feed, unsigned long value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, float value)
{
void AdafruitIO_Group::set(const char *feed, float value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
void AdafruitIO_Group::set(const char *feed, double value)
{
void AdafruitIO_Group::set(const char *feed, double value) {
AdafruitIO_Data *f = getFeed(feed);
f->setValue(value);
}
bool AdafruitIO_Group::save()
{
bool AdafruitIO_Group::save() {
if(data == NULL)
if (data == NULL)
return false;
char csv[150];
@ -113,7 +102,7 @@ bool AdafruitIO_Group::save()
strcpy(csv, "");
while(cur_data != NULL) {
while (cur_data != NULL) {
strcat(csv, cur_data->feedName());
strcat(csv, ",");
@ -121,182 +110,160 @@ bool AdafruitIO_Group::save()
strcat(csv, "\n");
cur_data = cur_data->next_data;
}
return _pub->publish(csv);
}
bool AdafruitIO_Group::get()
{
return _get_pub->publish("\0");
}
bool AdafruitIO_Group::get() { return _get_pub->publish("\0"); }
AdafruitIO_Data* AdafruitIO_Group::getFeed(const char *feed)
{
AdafruitIO_Data *AdafruitIO_Group::getFeed(const char *feed) {
// uint8_t i;
if(data == NULL) {
if (data == NULL) {
data = new AdafruitIO_Data(feed);
return data;
}
AdafruitIO_Data *cur_data = data;
while(cur_data != NULL) {
while (cur_data != NULL) {
if(strcmp(cur_data->feedName(), feed) == 0) {
if (strcmp(cur_data->feedName(), feed) == 0) {
return cur_data;
}
if(! cur_data->next_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;
}
void AdafruitIO_Group::onMessage(AdafruitIODataCallbackType cb)
{
void AdafruitIO_Group::onMessage(AdafruitIODataCallbackType cb) {
// uint8_t i;
if(_groupCallback == NULL) {
if (_groupCallback == NULL) {
_groupCallback = new AdafruitIOGroupCallback(cb);
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while(cur_cb != NULL) {
while (cur_cb != NULL) {
if(! cur_cb->next_cb) {
if (!cur_cb->next_cb) {
cur_cb->next_cb = new AdafruitIOGroupCallback(cb);
return;
}
cur_cb = cur_cb->next_cb;
}
}
void AdafruitIO_Group::onMessage(const char *feed, AdafruitIODataCallbackType cb)
{
void AdafruitIO_Group::onMessage(const char *feed,
AdafruitIODataCallbackType cb) {
// uint8_t i;
if(_groupCallback == NULL) {
if (_groupCallback == NULL) {
_groupCallback = new AdafruitIOGroupCallback(feed, cb);
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while(cur_cb != NULL) {
while (cur_cb != NULL) {
if(strcmp(cur_cb->feed, feed) == 0) {
if (strcmp(cur_cb->feed, feed) == 0) {
return;
}
if(! cur_cb->next_cb) {
if (!cur_cb->next_cb) {
cur_cb->next_cb = new AdafruitIOGroupCallback(feed, cb);
return;
}
cur_cb = cur_cb->next_cb;
}
}
void AdafruitIO_Group::call(AdafruitIO_Data *d)
{
void AdafruitIO_Group::call(AdafruitIO_Data *d) {
// uint8_t i;
if(_groupCallback == NULL) {
if (_groupCallback == NULL) {
return;
}
AdafruitIOGroupCallback *cur_cb = _groupCallback;
while(cur_cb) {
while (cur_cb) {
if(strcmp(cur_cb->feed, d->feedName()) == 0 || cur_cb->feed == NULL) {
if (strcmp(cur_cb->feed, d->feedName()) == 0 || cur_cb->feed == NULL) {
cur_cb->dataCallback(d);
}
cur_cb = cur_cb->next_cb;
}
}
void AdafruitIO_Group::subCallback(char *val, uint16_t len)
{
void AdafruitIO_Group::subCallback(char *val, uint16_t len) {
char *line;
char *name;
char *value;
if(_groupCallback == NULL)
if (_groupCallback == NULL)
return;
while((line = strtok_r(val, "\n", &val)) != NULL) {
while ((line = strtok_r(val, "\n", &val)) != NULL) {
name = strtok_r(line, ",", &line);
// couldn't grab name from line, move on
if(! name)
if (!name)
continue;
// don't handle location for now
if(strcmp(name, "location") == 0)
if (strcmp(name, "location") == 0)
continue;
value = strtok_r(line, ",", &line);
// no value? move on
if(! value)
if (!value)
continue;
AdafruitIO_Data *feed = getFeed(name);
// we couldn't get the data, move on
if(! feed)
if (!feed)
continue;
feed->setValue(value);
call(feed);
}
}
void AdafruitIO_Group::setLocation(double lat, double lon, double ele)
{
void AdafruitIO_Group::setLocation(double lat, double lon, double ele) {
// uint8_t i;
if(data == NULL) {
if (data == NULL) {
return;
}
AdafruitIO_Data *cur_data = data;
while(cur_data) {
while (cur_data) {
cur_data->setLocation(lat, lon, ele);
cur_data = cur_data->next_data;
}
}
bool AdafruitIO_Group::exists()
{
bool AdafruitIO_Group::exists() {
_io->_http->beginRequest();
_io->_http->get(_group_url);
_io->_http->sendHeader("X-AIO-Key", _io->_key);
@ -307,8 +274,7 @@ bool AdafruitIO_Group::exists()
return status == 200;
}
bool AdafruitIO_Group::create()
{
bool AdafruitIO_Group::create() {
String body = "name=";
body += name;
@ -333,18 +299,25 @@ bool AdafruitIO_Group::create()
return status == 201;
}
void AdafruitIO_Group::_init()
{
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
_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) {
if (_topic && _create_url && _group_url) {
// build topic string
strcpy(_topic, owner);
@ -387,7 +360,5 @@ void AdafruitIO_Group::_init()
_sub = 0;
_pub = 0;
_get_pub = 0;
}
}

View file

@ -12,70 +12,67 @@
#ifndef ADAFRUITIO_GROUP_H
#define ADAFRUITIO_GROUP_H
#include "Arduino.h"
#include "Adafruit_MQTT.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_Data.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
class AdafruitIO_Group : public AdafruitIO_MQTT {
public:
AdafruitIO_Group(AdafruitIO *io, const char *name);
~AdafruitIO_Group();
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);
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();
bool save();
bool get();
void setLocation(double lat=0, double lon=0, double ele=0);
void setLocation(double lat = 0, double lon = 0, double ele = 0);
bool exists();
bool create();
bool exists();
bool create();
void onMessage(AdafruitIODataCallbackType cb);
void onMessage(const char *feed, AdafruitIODataCallbackType cb);
void onMessage(AdafruitIODataCallbackType cb);
void onMessage(const char *feed, AdafruitIODataCallbackType cb);
void subCallback(char *val, uint16_t len);
void call(AdafruitIO_Data *d);
void subCallback(char *val, uint16_t len);
void call(AdafruitIO_Data *d);
const char *name;
const char *owner;
const char *name;
const char *owner;
AdafruitIO_Data *data;
AdafruitIO_Data* getFeed(const char *feed);
AdafruitIO_Data *data;
AdafruitIO_Data *getFeed(const char *feed);
private:
void _init();
private:
void _init();
char *_topic;
char *_get_topic;
char *_create_url;
char *_group_url;
char *_topic;
char *_get_topic;
char *_create_url;
char *_group_url;
Adafruit_MQTT_Subscribe *_sub;
Adafruit_MQTT_Publish *_pub;
Adafruit_MQTT_Publish *_get_pub;
Adafruit_MQTT_Subscribe *_sub;
Adafruit_MQTT_Publish *_pub;
Adafruit_MQTT_Publish *_get_pub;
AdafruitIO *_io;
AdafruitIOGroupCallback *_groupCallback;
double _lat,
_lon,
_ele;
AdafruitIO *_io;
AdafruitIOGroupCallback *_groupCallback;
double _lat, _lon, _ele;
};
#endif // ADAFRUITIO_GROUP_H

View file

@ -16,10 +16,9 @@
class AdafruitIO_MQTT {
public:
AdafruitIO_MQTT() {}
virtual void subCallback(char *val, uint16_t len) = 0;
public:
AdafruitIO_MQTT() {}
virtual void subCallback(char *val, uint16_t len) = 0;
};
#endif // ADAFRUITIO_MQTT_H

View file

@ -12,8 +12,8 @@
#include "AdafruitIO_Time.h"
#include "AdafruitIO.h"
AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f):AdafruitIO_MQTT()
{
AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f)
: AdafruitIO_MQTT() {
_io = io;
_sub = 0;
_dataCallback = 0;
@ -22,56 +22,54 @@ AdafruitIO_Time::AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f):AdafruitIO
_init();
}
AdafruitIO_Time::~AdafruitIO_Time()
{
if(_sub)
AdafruitIO_Time::~AdafruitIO_Time() {
if (_sub)
delete _sub;
if(data)
if (data)
delete data;
if(_topic)
if (_topic)
free(_topic);
}
void AdafruitIO_Time::onMessage(AdafruitIOTimeCallbackType cb)
{
void AdafruitIO_Time::onMessage(AdafruitIOTimeCallbackType cb) {
_dataCallback = cb;
}
void AdafruitIO_Time::subCallback(char *val, uint16_t len)
{
void AdafruitIO_Time::subCallback(char *val, uint16_t len) {
data = val;
// call callback with data
if(_dataCallback)
if (_dataCallback)
_dataCallback(data, len);
}
void AdafruitIO_Time::_init()
{
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;
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
_topic = (char *)malloc(
sizeof(char) * (strlen(formatString) +
6)); // 6 extra chars for "time/" and null termination
if(_topic) {
if (_topic) {
// build topic string
strcpy(_topic, "time/");
@ -88,7 +86,5 @@ void AdafruitIO_Time::_init()
_topic = 0;
_sub = 0;
data = 0;
}
}

View file

@ -12,10 +12,10 @@
#ifndef ADAFRUITIO_TIME_H
#define ADAFRUITIO_TIME_H
#include "Arduino.h"
#include "Adafruit_MQTT.h"
#include "AdafruitIO_Definitions.h"
#include "AdafruitIO_MQTT.h"
#include "Adafruit_MQTT.h"
#include "Arduino.h"
// forward declaration
class AdafruitIO;
@ -24,21 +24,20 @@ typedef void (*AdafruitIOTimeCallbackType)(char *value, uint16_t len);
class AdafruitIO_Time : public AdafruitIO_MQTT {
public:
AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f);
~AdafruitIO_Time();
void onMessage(AdafruitIOTimeCallbackType cb);
void subCallback(char *val, uint16_t len);
char *data;
aio_time_format_t format;
private:
AdafruitIOTimeCallbackType _dataCallback;
void _init();
char *_topic;
Adafruit_MQTT_Subscribe *_sub;
AdafruitIO *_io;
public:
AdafruitIO_Time(AdafruitIO *io, aio_time_format_t f);
~AdafruitIO_Time();
void onMessage(AdafruitIOTimeCallbackType cb);
void subCallback(char *val, uint16_t len);
char *data;
aio_time_format_t format;
private:
AdafruitIOTimeCallbackType _dataCallback;
void _init();
char *_topic;
Adafruit_MQTT_Subscribe *_sub;
AdafruitIO *_io;
};
#endif // ADAFRUITIO_FEED_H

View file

@ -14,37 +14,38 @@
#if defined(ARDUINO_SAMD_MKR1000)
#include "wifi/AdafruitIO_MKR1000.h"
typedef AdafruitIO_MKR1000 AdafruitIO_WiFi;
#include "wifi/AdafruitIO_MKR1000.h"
typedef AdafruitIO_MKR1000 AdafruitIO_WiFi;
#elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || defined(USE_AIRLIFT)
#elif defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) || defined(ADAFRUIT_PYPORTAL) || \
defined(USE_AIRLIFT)
#include "wifi/AdafruitIO_AIRLIFT.h"
typedef AdafruitIO_AIRLIFT AdafruitIO_WiFi;
#include "wifi/AdafruitIO_AIRLIFT.h"
typedef AdafruitIO_AIRLIFT AdafruitIO_WiFi;
#elif defined(USE_WINC1500)
#elif defined(USE_WINC1500)
#include "wifi/AdafruitIO_WINC1500.h"
typedef AdafruitIO_WINC1500 AdafruitIO_WiFi;
#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;
#include "wifi/AdafruitIO_ESP32.h"
typedef AdafruitIO_ESP32 AdafruitIO_WiFi;
#elif defined(ESP8266)
#include "wifi/AdafruitIO_ESP8266.h"
typedef AdafruitIO_ESP8266 AdafruitIO_WiFi;
#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;
#include "wifi/AdafruitIO_WICED.h"
typedef AdafruitIO_WICED AdafruitIO_WiFi;
#else
#error "Must define USE_AIRLIFT or USE_WINC1500 before including this file."
#error "Must define USE_AIRLIFT or USE_WINC1500 before including this file."
#endif

View file

@ -13,33 +13,31 @@
#include "AdafruitIO.h"
#include "AdafruitIO_Dashboard.h"
AdafruitIO_Block::AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
{
AdafruitIO_Block::AdafruitIO_Block(AdafruitIO_Dashboard *d,
AdafruitIO_Feed *f) {
_dashboard = d;
_feed = f;
}
AdafruitIO_Block::~AdafruitIO_Block(){}
AdafruitIO_Block::~AdafruitIO_Block() {}
String AdafruitIO_Block::properties()
{
String AdafruitIO_Block::properties() {
String props = "{}";
return props;
}
String AdafruitIO_Block::dimensions()
{
String AdafruitIO_Block::dimensions() {
String dim = "\",\"size_x\":\"";
dim += _width();
dim += "\",\"size_y\":\"";
dim += _height();
if(_row() > 0) {
if (_row() > 0) {
dim += "\",\"row\":\"";
dim += _row();
}
if(_column() > 0) {
if (_column() > 0) {
dim += "\",\"column\":\"";
dim += _column();
}
@ -47,13 +45,9 @@ String AdafruitIO_Block::dimensions()
return dim;
}
const char* AdafruitIO_Block::type()
{
return _visual_type;
}
const char *AdafruitIO_Block::type() { return _visual_type; }
bool AdafruitIO_Block::save()
{
bool AdafruitIO_Block::save() {
HttpClient *http = _dashboard->io()->_http;
String url = "/api/v2/";

View file

@ -12,41 +12,40 @@
#ifndef ADAFRUITIO_BLOCK_H
#define ADAFRUITIO_BLOCK_H
#include "Arduino.h"
#include "AdafruitIO_Definitions.h"
#include "Arduino.h"
class AdafruitIO_Dashboard;
class AdafruitIO_Feed;
class AdafruitIO_Block {
public:
AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~AdafruitIO_Block();
public:
AdafruitIO_Block(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~AdafruitIO_Block();
int width = 2;
int height = 2;
int row = 0;
int column = 0;
int width = 2;
int height = 2;
int row = 0;
int column = 0;
virtual String properties();
String dimensions();
virtual String properties();
String dimensions();
virtual const char* type();
virtual const char *type();
bool save();
bool save();
protected:
AdafruitIO_Dashboard *_dashboard;
AdafruitIO_Feed *_feed;
protected:
AdafruitIO_Dashboard *_dashboard;
AdafruitIO_Feed *_feed;
const char *_visual_type;
virtual int _width() { return width; }
virtual int _height() { return height; }
virtual int _row() { return row; }
virtual int _column() { return column; }
const char *_visual_type;
virtual int _width() { return width; }
virtual int _height() { return height; }
virtual int _row() { return row; }
virtual int _column() { return column; }
};
#endif // ADAFRUITIO_BLOCK_H

View file

@ -11,8 +11,8 @@
//
#include "ChartBlock.h"
ChartBlock::ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
ChartBlock::ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
historyHours = 0;
xAxisLabel = "X";
yAxisLabel = "Y";
@ -20,10 +20,9 @@ ChartBlock::ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO
yAxisMax = 100;
}
ChartBlock::~ChartBlock(){}
ChartBlock::~ChartBlock() {}
String ChartBlock::properties()
{
String ChartBlock::properties() {
String props = "{\"historyHours\":\"";
props += historyHours;

View file

@ -16,31 +16,30 @@
class ChartBlock : public AdafruitIO_Block {
public:
ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ChartBlock();
public:
ChartBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ChartBlock();
const char* type() { return _visual_type; }
const char *type() { return _visual_type; }
int historyHours;
const char *xAxisLabel;
const char *yAxisLabel;
int yAxisMin;
int yAxisMax;
int historyHours;
const char *xAxisLabel;
const char *yAxisLabel;
int yAxisMin;
int yAxisMax;
int width = 6;
int height = 4;
int width = 6;
int height = 4;
String properties();
String properties();
protected:
const char *_visual_type = "line_chart";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
protected:
const char *_visual_type = "line_chart";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_CHARTBLOCK_H

View file

@ -16,25 +16,23 @@
class ColorBlock : public AdafruitIO_Block {
public:
ColorBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f) {}
~ColorBlock() {}
public:
ColorBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {}
~ColorBlock() {}
int width = 4;
int height = 4;
int width = 4;
int height = 4;
const char* type() { return _visual_type; }
protected:
const char *_visual_type = "color_picker";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "color_picker";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_COLORBLOCK_H

View file

@ -11,20 +11,19 @@
//
#include "GaugeBlock.h"
GaugeBlock::GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
GaugeBlock::GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
min = 0;
max = 100;
ringWidth = "thin";
label = "Value";
}
GaugeBlock::~GaugeBlock(){}
GaugeBlock::~GaugeBlock() {}
String GaugeBlock::properties()
{
String GaugeBlock::properties() {
int w = 0;
if (strcmp(ringWidth, "thin")) {
w = 25;
} else {

View file

@ -16,29 +16,29 @@
class GaugeBlock : public AdafruitIO_Block {
public:
GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~GaugeBlock();
public:
GaugeBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~GaugeBlock();
int min;
int max;
int min;
int max;
const char *ringWidth;
const char *label;
const char *ringWidth;
const char *label;
int width = 4;
int height = 4;
int width = 4;
int height = 4;
String properties();
const char* type() { return _visual_type; }
protected:
const char *_visual_type = "gauge";
String properties();
const char *type() { return _visual_type; }
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
protected:
const char *_visual_type = "gauge";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_GAUGEBLOCK_H

View file

@ -16,23 +16,23 @@
class ImageBlock : public AdafruitIO_Block {
public:
ImageBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f) {}
~ImageBlock() {}
public:
ImageBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {}
~ImageBlock() {}
int height = 6;
int width = 4;
int height = 6;
int width = 4;
const char* type() { return _visual_type; }
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "image";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
protected:
const char *_visual_type = "image";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_IMAGEBLOCK_H

View file

@ -11,27 +11,26 @@
//
#include "MapBlock.h"
MapBlock::MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
historyHours = 0;
tile = "contrast";
MapBlock::MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
historyHours = 0;
tile = "contrast";
}
MapBlock::~MapBlock() {}
String MapBlock::properties()
{
String MapBlock::properties() {
if ((strcmp(tile, "contrast") != 0) && (strcmp(tile, "street") != 0) && (strcmp(tile, "sat") != 0))
{
tile = "contrast";
}
if ((strcmp(tile, "contrast") != 0) && (strcmp(tile, "street") != 0) &&
(strcmp(tile, "sat") != 0)) {
tile = "contrast";
}
props = "{\"historyHours\":\"";
props += historyHours;
props += "\",\"tile\":\"";
props += tile;
props += "\"}";
props = "{\"historyHours\":\"";
props += historyHours;
props += "\",\"tile\":\"";
props += tile;
props += "\"}";
return props;
return props;
}

View file

@ -16,29 +16,27 @@
class MapBlock : public AdafruitIO_Block {
public:
MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MapBlock();
String props;
int historyHours;
const char *tile;
public:
MapBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MapBlock();
int width = 4;
int height = 4;
String props;
int historyHours;
const char *tile;
String properties();
const char* type() { return _visual_type; }
int width = 4;
int height = 4;
protected:
const char *_visual_type = "map";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "map";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_MAPBLOCK_H

View file

@ -11,17 +11,16 @@
//
#include "MomentaryBlock.h"
MomentaryBlock::MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
MomentaryBlock::MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
text = "RESET";
value = "1";
release = "0";
}
MomentaryBlock::~MomentaryBlock(){}
MomentaryBlock::~MomentaryBlock() {}
String MomentaryBlock::properties()
{
String MomentaryBlock::properties() {
String props = "{\"text\":\"";
props += text;
props += "\",\"value\":\"";

View file

@ -16,29 +16,27 @@
class MomentaryBlock : public AdafruitIO_Block {
public:
MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MomentaryBlock();
public:
MomentaryBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~MomentaryBlock();
const char *text;
const char *value;
const char *release;
const char *text;
const char *value;
const char *release;
int width = 2;
int height = 2;
int width = 2;
int height = 2;
String properties();
const char* type() { return _visual_type; }
protected:
const char *_visual_type = "momentary_button";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "momentary_button";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_MOMENTARYBLOCK_H

View file

@ -11,18 +11,17 @@
//
#include "SliderBlock.h"
SliderBlock::SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
SliderBlock::SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
min = 0;
max = 100;
step = 10;
label = "Value";
}
SliderBlock::~SliderBlock(){}
SliderBlock::~SliderBlock() {}
String SliderBlock::properties()
{
String SliderBlock::properties() {
String props = "{\"min\":\"";
props += min;
props += "\",\"max\":\"";

View file

@ -16,30 +16,28 @@
class SliderBlock : public AdafruitIO_Block {
public:
SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~SliderBlock();
public:
SliderBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~SliderBlock();
int min;
int max;
int step;
const char *label;
int min;
int max;
int step;
const char *label;
int width = 4;
int height = 2;
int width = 4;
int height = 2;
String properties();
const char* type() { return _visual_type; }
protected:
const char *_visual_type = "slider";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "slider";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_SLIDERBLOCK_H

View file

@ -11,42 +11,40 @@
//
#include "StreamBlock.h"
StreamBlock::StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
fontSize = "small";
fontColor = "green";
showErrors = true;
showTimestamp = true;
showName = true;
StreamBlock::StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
fontSize = "small";
fontColor = "green";
showErrors = true;
showTimestamp = true;
showName = true;
}
StreamBlock::~StreamBlock() {}
String StreamBlock::properties()
{
int s = 0;
String StreamBlock::properties() {
int s = 0;
if (strcmp(fontSize, "small") == 0) {
s = 12;
}
if (strcmp(fontSize, "medium") == 0) {
s = 18;
}
else {
s = 24;
}
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 += "\"}";
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;
return props;
}

View file

@ -16,30 +16,29 @@
class StreamBlock : public AdafruitIO_Block {
public:
StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~StreamBlock();
public:
StreamBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~StreamBlock();
const char *fontSize;
const char *fontColor;
bool showErrors;
bool showTimestamp;
bool showName;
const char *fontSize;
const char *fontColor;
bool showErrors;
bool showTimestamp;
bool showName;
int width = 6;
int height = 4;
int width = 6;
int height = 4;
String properties();
const char* type() { return _visual_type; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "stream";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
protected:
const char *_visual_type = "stream";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_STREAMBLOCK_H

View file

@ -11,33 +11,27 @@
//
#include "TextBlock.h"
TextBlock::TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
fontSize = "small";
TextBlock::TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
fontSize = "small";
}
TextBlock::~TextBlock() {}
String TextBlock::properties()
{
int s = 0;
String TextBlock::properties() {
int s = 0;
if ((strcmp(fontSize, "small") == 0))
{
s = 12;
}
else if ((strcmp(fontSize, "medium") == 0))
{
s = 18;
}
else
{
s = 24;
}
if ((strcmp(fontSize, "small") == 0)) {
s = 12;
} else if ((strcmp(fontSize, "medium") == 0)) {
s = 18;
} else {
s = 24;
}
String props = "{\"fontSize\":\"";
props += s;
props += "\"}";
String props = "{\"fontSize\":\"";
props += s;
props += "\"}";
return props;
return props;
}

View file

@ -16,27 +16,25 @@
class TextBlock : public AdafruitIO_Block {
public:
TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~TextBlock();
public:
TextBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~TextBlock();
const char *fontSize;
const char *fontSize;
int width = 2;
int height = 1;
int width = 2;
int height = 1;
String properties();
const char* type() { return _visual_type; }
protected:
const char *_visual_type = "text";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "text";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_TEXTBLOCK_H

View file

@ -11,16 +11,15 @@
//
#include "ToggleBlock.h"
ToggleBlock::ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f) : AdafruitIO_Block(d, f)
{
ToggleBlock::ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f)
: AdafruitIO_Block(d, f) {
onText = "1";
offText = "0";
}
ToggleBlock::~ToggleBlock(){}
ToggleBlock::~ToggleBlock() {}
String ToggleBlock::properties()
{
String ToggleBlock::properties() {
String props = "{\"onText\":\"";
props += onText;
props += "\",\"offText\":\"";

View file

@ -16,27 +16,26 @@
class ToggleBlock : public AdafruitIO_Block {
public:
ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ToggleBlock();
public:
ToggleBlock(AdafruitIO_Dashboard *d, AdafruitIO_Feed *f);
~ToggleBlock();
const char *onText;
const char *offText;
const char *onText;
const char *offText;
int width = 4;
int height = 2;
int width = 4;
int height = 2;
String properties();
const char* type() { return _visual_type; }
String properties();
const char *type() { return _visual_type; }
protected:
const char *_visual_type = "toggle_button";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
protected:
const char *_visual_type = "toggle_button";
int _width() { return width; }
int _height() { return height; }
int _row() { return row; }
int _column() { return column; }
};
#endif // ADAFRUITIO_TOGGLEBLOCK_H

View file

@ -14,91 +14,83 @@
char AdafruitIO_Board::_id[64] = "";
#if defined(ARDUINO_SAMD_MKR1000)
const char AdafruitIO_Board::_type[] = "mkr1000";
const char AdafruitIO_Board::_type[] = "mkr1000";
#elif defined(ARDUINO_SAMD_FEATHER_M0)
const char AdafruitIO_Board::_type[] = "feather_m0";
const char AdafruitIO_Board::_type[] = "feather_m0";
#elif defined(ARDUINO_AVR_FEATHER32U4)
const char AdafruitIO_Board::_type[] = "feather_32u4";
const char AdafruitIO_Board::_type[] = "feather_32u4";
#elif defined(ARDUINO_STM32_FEATHER)
const char AdafruitIO_Board::_type[] = "feather_wiced";
const char AdafruitIO_Board::_type[] = "feather_wiced";
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
const char AdafruitIO_Board::_type[] = "esp32";
#include <WiFi.h>
const char AdafruitIO_Board::_type[] = "esp32";
#elif defined(ESP8266)
const char AdafruitIO_Board::_type[] = "esp8266";
const char AdafruitIO_Board::_type[] = "esp8266";
#else
const char AdafruitIO_Board::_type[] = "unknown";
const char AdafruitIO_Board::_type[] = "unknown";
#endif
const char* AdafruitIO_Board::type()
{
return AdafruitIO_Board::_type;
}
const char *AdafruitIO_Board::type() { return AdafruitIO_Board::_type; }
#if defined(ARDUINO_ARCH_SAMD)
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;
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);
sprintf(AdafruitIO_Board::_id, "%8x%8x%8x%8x", val1, val2, val3, val4);
return AdafruitIO_Board::_id;
}
return AdafruitIO_Board::_id;
}
#elif defined(ARDUINO_ARCH_AVR)
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;
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)
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;
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)
char* AdafruitIO_Board::id()
{
sprintf(AdafruitIO_Board::_id, "%06x", ESP.getChipId());
return AdafruitIO_Board::_id;
}
char *AdafruitIO_Board::id() {
sprintf(AdafruitIO_Board::_id, "%06x", ESP.getChipId());
return AdafruitIO_Board::_id;
}
#elif defined(ARDUINO_STM32_FEATHER)
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;
}
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
char* AdafruitIO_Board::id()
{
strcpy(AdafruitIO_Board::_id, "unknown");
return AdafruitIO_Board::_id;
}
char *AdafruitIO_Board::id() {
strcpy(AdafruitIO_Board::_id, "unknown");
return AdafruitIO_Board::_id;
}
#endif

View file

@ -15,19 +15,17 @@
#include "Arduino.h"
#if defined(ARDUINO_ARCH_AVR)
#include <avr/boot.h>
#include <avr/boot.h>
#endif
class AdafruitIO_Board {
public:
static char _id[64];
static char* id();
static const char _type[];
static const char* type();
public:
static char _id[64];
static char *id();
static const char _type[];
static const char *type();
};
#endif // ADAFRUITIO_BOARD_H

View file

@ -19,181 +19,171 @@
#ifndef ADAFRUITIO_AIRLIFT_H
#define ADAFRUITIO_AIRLIFT_H
#include "Arduino.h"
#include "AdafruitIO.h"
#include "WiFiNINA.h"
#include "SPI.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFiNINA.h"
#define NINAFWVER "1.0.0"
/****************************************************************************/
/*!
/*!
@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 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 {
_disconnect();
// setup ESP32 pins
if (_ssPin != -1) {
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
}
// check esp32 module version against NINAFWVER
firmwareCheck();
// 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;
}
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 Disconnect the wifi network.
@return none
*/
/**************************************************************************/
void _disconnect()
{
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
/**************************************************************************/
/*!
@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 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 {
_disconnect();
// setup ESP32 pins
if (_ssPin != -1) {
WiFi.setPins(_ssPin, _ackPin, _rstPin, _gpio0Pin, _wifi);
}
// check esp32 module version against NINAFWVER
firmwareCheck();
// 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.
@return none
*/
/**************************************************************************/
void _disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
};
#endif // ADAFRUITIO_AIRLIFT_H

View file

@ -15,8 +15,9 @@
#include "AdafruitIO_ESP32.h"
AdafruitIO_ESP32::AdafruitIO_ESP32(const char *user, const char *key, const char *ssid, const char *pass):AdafruitIO(user, key)
{
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;
@ -24,17 +25,15 @@ AdafruitIO_ESP32::AdafruitIO_ESP32(const char *user, const char *key, const char
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_ESP32::~AdafruitIO_ESP32()
{
if(_client)
AdafruitIO_ESP32::~AdafruitIO_ESP32() {
if (_client)
delete _client;
if(_mqtt)
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_ESP32::_connect()
{
if(strlen(_ssid) == 0) {
void AdafruitIO_ESP32::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
@ -43,7 +42,6 @@ void AdafruitIO_ESP32::_connect()
delay(100);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
@ -52,31 +50,25 @@ void AdafruitIO_ESP32::_connect()
@return none
*/
/**************************************************************************/
void AdafruitIO_ESP32::_disconnect()
{
void AdafruitIO_ESP32::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_ESP32::networkStatus()
{
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;
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";
}
const char *AdafruitIO_ESP32::connectionType() { return "wifi"; }
#endif // ESP32

View file

@ -11,38 +11,38 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_ESP32_H
#define ADAFRUITIO_ESP32_H
#ifdef ARDUINO_ARCH_ESP32
#include "Arduino.h"
#include "AdafruitIO.h"
#include <WiFi.h>
#include "WiFiClientSecure.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();
public:
AdafruitIO_ESP32(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_ESP32();
aio_status_t networkStatus();
const char* connectionType();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiClientSecure *_client;
const char *_ssid;
const char *_pass;
WiFiClientSecure *_client;
};
#endif //ESP32
#endif // ESP32
#endif // ADAFRUITIO_ESP32_H

View file

@ -11,13 +11,14 @@
*
* 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)
{
AdafruitIO_ESP8266::AdafruitIO_ESP8266(const char *user, const char *key,
const char *ssid, const char *pass)
: AdafruitIO(user, key) {
_ssid = ssid;
_pass = pass;
_client = new WiFiClientSecure;
@ -26,17 +27,15 @@ AdafruitIO_ESP8266::AdafruitIO_ESP8266(const char *user, const char *key, const
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_ESP8266::~AdafruitIO_ESP8266()
{
if(_client)
AdafruitIO_ESP8266::~AdafruitIO_ESP8266() {
if (_client)
delete _client;
if(_mqtt)
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_ESP8266::_connect()
{
if(strlen(_ssid) == 0) {
void AdafruitIO_ESP8266::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
@ -45,7 +44,6 @@ void AdafruitIO_ESP8266::_connect()
delay(100);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
@ -54,31 +52,25 @@ void AdafruitIO_ESP8266::_connect()
@return none
*/
/**************************************************************************/
void AdafruitIO_ESP8266::_disconnect()
{
void AdafruitIO_ESP8266::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_ESP8266::networkStatus()
{
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;
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";
}
const char *AdafruitIO_ESP8266::connectionType() { return "wifi"; }
#endif // ESP8266

View file

@ -11,37 +11,37 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_ESP8266_H
#define ADAFRUITIO_ESP8266_H
#ifdef ESP8266
#include "Arduino.h"
#include "AdafruitIO.h"
#include "ESP8266WiFi.h"
#include "WiFiClientSecure.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "ESP8266WiFi.h"
#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();
public:
AdafruitIO_ESP8266(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_ESP8266();
aio_status_t networkStatus();
const char* connectionType();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiClientSecure *_client;
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiClientSecure *_client;
};
#endif //ESP8266
#endif // ESP8266
#endif // ADAFRUITIO_ESP8266_H

View file

@ -11,13 +11,14 @@
*
* 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)
{
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;
@ -25,21 +26,19 @@ AdafruitIO_MKR1000::AdafruitIO_MKR1000(const char *user, const char *key, const
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_MKR1000::~AdafruitIO_MKR1000()
{
if(_client)
AdafruitIO_MKR1000::~AdafruitIO_MKR1000() {
if (_client)
delete _client;
if(_mqtt)
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_MKR1000::_connect()
{
if(strlen(_ssid) == 0) {
void AdafruitIO_MKR1000::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
// no shield? bail
if(WiFi.status() == WL_NO_SHIELD)
if (WiFi.status() == WL_NO_SHIELD)
return;
_disconnect();
@ -47,7 +46,6 @@ void AdafruitIO_MKR1000::_connect()
WiFi.begin(_ssid, _pass);
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
@ -56,31 +54,25 @@ void AdafruitIO_MKR1000::_connect()
@return none
*/
/**************************************************************************/
void AdafruitIO_MKR1000::_disconnect()
{
void AdafruitIO_MKR1000::_disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_MKR1000::networkStatus()
{
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;
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";
}
const char *AdafruitIO_MKR1000::connectionType() { return "wifi"; }
#endif // ARDUINO_ARCH_SAMD

View file

@ -11,38 +11,38 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_MKR1000_H
#define ADAFRUITIO_MKR1000_H
#if defined(ARDUINO_SAMD_MKR1000)
#include "Arduino.h"
#include "AdafruitIO.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "Arduino.h"
#include "SPI.h"
#include "WiFi101.h"
#include "WiFiSSLClient.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
class AdafruitIO_MKR1000 : public AdafruitIO {
public:
AdafruitIO_MKR1000(const char *user, const char *key, const char *ssid, const char *pass);
~AdafruitIO_MKR1000();
public:
AdafruitIO_MKR1000(const char *user, const char *key, const char *ssid,
const char *pass);
~AdafruitIO_MKR1000();
aio_status_t networkStatus();
const char* connectionType();
aio_status_t networkStatus();
const char *connectionType();
protected:
void _connect();
void _disconnect();
protected:
void _connect();
void _disconnect();
const char *_ssid;
const char *_pass;
WiFiSSLClient *_client;
const char *_ssid;
const char *_pass;
WiFiSSLClient *_client;
};
#endif // ARDUINO_ARCH_SAMD

View file

@ -11,13 +11,14 @@
*
* 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)
{
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;
@ -25,17 +26,15 @@ AdafruitIO_WICED::AdafruitIO_WICED(const char *user, const char *key, const char
_http = new HttpClient(*_client, _host, _http_port);
}
AdafruitIO_WICED::~AdafruitIO_WICED()
{
if(_client)
AdafruitIO_WICED::~AdafruitIO_WICED() {
if (_client)
delete _client;
if(_mqtt)
if (_mqtt)
delete _mqtt;
}
void AdafruitIO_WICED::_connect()
{
if(strlen(_ssid) == 0) {
void AdafruitIO_WICED::_connect() {
if (strlen(_ssid) == 0) {
_status = AIO_SSID_INVALID;
} else {
_disconnect();
@ -43,22 +42,20 @@ void AdafruitIO_WICED::_connect()
_status = AIO_NET_DISCONNECTED;
}
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
@return none
*/
/**************************************************************************/
void AdafruitIO_WICED::_disconnect()
{
void AdafruitIO_WICED::_disconnect() {
Feather.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
aio_status_t AdafruitIO_WICED::networkStatus()
{
if(Feather.connected())
aio_status_t AdafruitIO_WICED::networkStatus() {
if (Feather.connected())
return AIO_NET_CONNECTED;
// if granular status is needed, we can
@ -69,9 +66,6 @@ aio_status_t AdafruitIO_WICED::networkStatus()
return AIO_NET_DISCONNECTED;
}
const char* AdafruitIO_WICED::connectionType()
{
return "wifi";
}
const char *AdafruitIO_WICED::connectionType() { return "wifi"; }
#endif // ARDUINO_STM32_FEATHER

View file

@ -11,35 +11,35 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_WICED_H
#define ADAFRUITIO_WICED_H
#ifdef ARDUINO_STM32_FEATHER
#include "Arduino.h"
#include "AdafruitIO.h"
#include <adafruit_feather.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();
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;
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

View file

@ -11,7 +11,7 @@
*
* All text above must be included in any redistribution.
*/
#ifndef ADAFRUITIO_WICED_SSL_H
#define ADAFRUITIO_WICED_SSL_H
@ -21,15 +21,12 @@
class AdafruitIO_WICED_SSL : public AdafruitTCP {
public:
AdafruitIO_WICED_SSL() : AdafruitTCP() {}
int connect(const char *host, uint16_t port)
{
return connectSSL(host, port);
}
public:
AdafruitIO_WICED_SSL() : AdafruitTCP() {}
int connect(const char *host, uint16_t port) {
return connectSSL(host, port);
}
};
#endif // ARDUINO_STM32_FEATHER

View file

@ -11,152 +11,147 @@
*
* 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)
#if !defined(ARDUINO_SAMD_MKR1000) && defined(ARDUINO_ARCH_SAMD) && \
!defined(ADAFRUIT_METRO_M4_AIRLIFT_LITE) && !defined(ADAFRUIT_PYPORTAL)
#include "Arduino.h"
#include "AdafruitIO.h"
#include "SPI.h"
#include "WiFi101.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);
}
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 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;
/**************************************************************************/
/*!
@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;
}
}
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);
}
/**************************************************************************/
/*!
@brief Disconnect the wifi network.
@return none
*/
/**************************************************************************/
void _disconnect() {
WiFi.disconnect();
delay(AIO_NET_DISCONNECT_WAIT);
}
};
#endif // ARDUINO_ARCH_SAMD