Skip to content

Commit a73200a

Browse files
committed
Merge branch 'master' of github.com:adafruit/Adafruit_IO_Arduino into esp32
2 parents 17e5a2a + 68b9c8d commit a73200a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1380
-25
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Adafruit IO Dashboard Setup Example
2+
//
3+
// Adafruit invests time and resources providing this open source code.
4+
// Please support Adafruit and open source hardware by purchasing
5+
// products from Adafruit!
6+
//
7+
// Written by Todd Treece for Adafruit Industries
8+
// Copyright (c) 2016 Adafruit Industries
9+
// Licensed under the MIT license.
10+
//
11+
// All text above must be included in any redistribution.
12+
13+
/************************** Configuration ***********************************/
14+
15+
// edit the config.h tab and enter your Adafruit IO credentials
16+
// and any additional configuration needed for WiFi, cellular,
17+
// or ethernet clients.
18+
#include "config.h"
19+
20+
/************************ Example Starts Here *******************************/
21+
22+
// set up the 'example' feed
23+
AdafruitIO_Feed *feed = io.feed("example");
24+
25+
// set up the 'example' dashboard
26+
AdafruitIO_Dashboard *dashboard = io.dashboard("example");
27+
28+
void setup() {
29+
30+
// start the serial connection
31+
Serial.begin(115200);
32+
33+
// wait for serial monitor to open
34+
while(! Serial);
35+
36+
// connect to io.adafruit.com
37+
Serial.print("Connecting to Adafruit IO");
38+
io.connect();
39+
40+
// wait for a connection
41+
while(io.status() < AIO_CONNECTED) {
42+
Serial.print(".");
43+
delay(500);
44+
}
45+
46+
// we are connected
47+
Serial.println();
48+
Serial.println(io.statusText());
49+
50+
// create the example feed if it doesn't exist
51+
if(feed->exists()) {
52+
Serial.println("Example feed exists.");
53+
} else {
54+
if(feed->create()) {
55+
Serial.println("Example feed created.");
56+
} else {
57+
Serial.println("Example feed creation failed.");
58+
}
59+
}
60+
61+
// create the example dashboard if it doesn't exist
62+
if(dashboard->exists()) {
63+
Serial.println("Example dashboard exists.");
64+
} else {
65+
if(dashboard->create()) {
66+
Serial.println("Example dashboard created.");
67+
// add blocks to the dashboard using the function below
68+
addBlocks();
69+
} else {
70+
Serial.println("Example dashboard creation failed.");
71+
}
72+
}
73+
74+
}
75+
76+
void loop() {
77+
78+
// io.run(); is required for all sketches.
79+
// it should always be present at the top of your loop
80+
// function. it keeps the client connected to
81+
// io.adafruit.com, and processes any incoming data.
82+
io.run();
83+
84+
}
85+
86+
void addBlocks() {
87+
88+
bool added = false;
89+
90+
Serial.print("Adding momentary button block... ");
91+
MomentaryBlock *button = dashboard->addMomentaryBlock(feed);
92+
button->text = "Button";
93+
button->value = "1";
94+
button->release = "0";
95+
added = button->save();
96+
Serial.println(added ? "added" : "failed");
97+
98+
Serial.print("Adding toggle button block... ");
99+
ToggleBlock *toggle = dashboard->addToggleBlock(feed);
100+
toggle->onText = "1";
101+
toggle->offText = "0";
102+
added = toggle->save();
103+
Serial.println(added ? "added" : "failed");
104+
105+
Serial.print("Adding slider block... ");
106+
SliderBlock *slider = dashboard->addSliderBlock(feed);
107+
slider->min = 0;
108+
slider->max = 100;
109+
slider->step = 10;
110+
slider->label = "Value";
111+
added = slider->save();
112+
Serial.println(added ? "added" : "failed");
113+
114+
Serial.print("Adding gauge block... ");
115+
GaugeBlock *gauge = dashboard->addGaugeBlock(feed);
116+
gauge->min = 0;
117+
gauge->max = 100;
118+
gauge->ringWidth = "thin"; // thin or thick
119+
gauge->label = "Value";
120+
added = gauge->save();
121+
Serial.println(added ? "added" : "failed");
122+
123+
Serial.print("Adding line chart block... ");
124+
ChartBlock *chart = dashboard->addChartBlock(feed);
125+
chart->yAxisMin = 0;
126+
chart->yAxisMax = 100;
127+
chart->xAxisLabel = "X";
128+
chart->yAxisLabel = "Y";
129+
added = chart->save();
130+
Serial.println(added ? "added" : "failed");
131+
132+
Serial.print("Adding text block... ");
133+
TextBlock *text = dashboard->addTextBlock(feed);
134+
text->fontSize = "small"; // small, medium, or large
135+
added = text->save();
136+
Serial.println(added ? "added" : "failed");
137+
138+
Serial.print("Adding stream block... ");
139+
StreamBlock *stream = dashboard->addStreamBlock(feed);
140+
stream->fontSize = "small"; // small, medium, or large
141+
stream->fontColor = "green"; // green or white
142+
stream->showErrors = true;
143+
stream->showTimestamp = true;
144+
stream->showName = true;
145+
added = stream->save();
146+
Serial.println(added ? "added" : "failed");
147+
148+
Serial.print("Adding color picker block... ");
149+
ColorBlock *color = dashboard->addColorBlock(feed);
150+
added = color->save();
151+
Serial.println(added ? "added" : "failed");
152+
153+
Serial.print("Adding map block... ");
154+
MapBlock *map = dashboard->addMapBlock(feed);
155+
map->historyHours = 0;
156+
map->tile = "contrast"; // street, sat, or contrast
157+
added = map->save();
158+
Serial.println(added ? "added" : "failed");
159+
160+
Serial.print("Adding image block... ");
161+
ImageBlock *image = dashboard->addImageBlock(feed);
162+
added = image->save();
163+
Serial.println(added ? "added" : "failed");
164+
165+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/************************ Adafruit IO Config *******************************/
2+
3+
// visit io.adafruit.com if you need to create an account,
4+
// or if you need your Adafruit IO key.
5+
#define IO_USERNAME "your_username"
6+
#define IO_KEY "your_key"
7+
8+
/******************************* WIFI **************************************/
9+
10+
// the AdafruitIO_WiFi client will work with the following boards:
11+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
12+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
13+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
14+
// - Feather WICED -> https://www.adafruit.com/products/3056
15+
16+
#define WIFI_SSID "your_ssid"
17+
#define WIFI_PASS "your_pass"
18+
19+
// comment out the following two lines if you are using fona or ethernet
20+
#include "AdafruitIO_WiFi.h"
21+
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
22+
23+
24+
/******************************* FONA **************************************/
25+
26+
// the AdafruitIO_FONA client will work with the following boards:
27+
// - Feather 32u4 FONA -> https://www.adafruit.com/product/3027
28+
29+
// uncomment the following two lines for 32u4 FONA,
30+
// and comment out the AdafruitIO_WiFi client in the WIFI section
31+
// #include "AdafruitIO_FONA.h"
32+
// AdafruitIO_FONA io(IO_USERNAME, IO_KEY);
33+
34+
35+
/**************************** ETHERNET ************************************/
36+
37+
// the AdafruitIO_Ethernet client will work with the following boards:
38+
// - Ethernet FeatherWing -> https://www.adafruit.com/products/3201
39+
40+
// uncomment the following two lines for ethernet,
41+
// and comment out the AdafruitIO_WiFi client in the WIFI section
42+
// #include "AdafruitIO_Ethernet.h"
43+
// AdafruitIO_Ethernet io(IO_USERNAME, IO_KEY);

AdafruitIO.cpp renamed to src/AdafruitIO.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
AdafruitIO::AdafruitIO(const char *user, const char *key)
1515
{
1616
_mqtt = 0;
17+
_http = 0;
1718
_username = user;
1819
_key = key;
1920
_err_topic = 0;
@@ -28,6 +29,7 @@ AdafruitIO::AdafruitIO(const char *user, const char *key)
2829
AdafruitIO::AdafruitIO(const __FlashStringHelper *user, const __FlashStringHelper *key)
2930
{
3031
_mqtt = 0;
32+
_http = 0;
3133
_username = (const char*)user;
3234
_key = (const char*)key;
3335
_err_topic = 0;
@@ -93,6 +95,11 @@ AdafruitIO_Feed* AdafruitIO::feed(const __FlashStringHelper *name)
9395
return new AdafruitIO_Feed(this, name);
9496
}
9597

98+
AdafruitIO_Dashboard* AdafruitIO::dashboard(const char* name)
99+
{
100+
return new AdafruitIO_Dashboard(this, name);
101+
}
102+
96103
void AdafruitIO::_init()
97104
{
98105

AdafruitIO.h renamed to src/AdafruitIO.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
#include "Adafruit_MQTT.h"
1717
#include "AdafruitIO_Definitions.h"
1818
#include "AdafruitIO_Feed.h"
19+
#include "AdafruitIO_Dashboard.h"
1920
#include "AdafruitIO_Data.h"
21+
#include "ArduinoHttpClient.h"
2022

2123
#ifndef ADAFRUIT_MQTT_VERSION_MAJOR
2224
#error "This sketch requires Adafruit MQTT Library v0.16.0 or higher. Please install or upgrade using the Library Manager."
@@ -29,6 +31,8 @@
2931
class AdafruitIO {
3032

3133
friend class AdafruitIO_Feed;
34+
friend class AdafruitIO_Dashboard;
35+
friend class AdafruitIO_Block;
3236

3337
public:
3438
AdafruitIO(const char *user, const char *key);
@@ -42,6 +46,8 @@ class AdafruitIO {
4246
AdafruitIO_Feed* feed(const char *name);
4347
AdafruitIO_Feed* feed(const __FlashStringHelper *name);
4448

49+
AdafruitIO_Dashboard* dashboard(const char *name);
50+
4551
const __FlashStringHelper* statusText();
4652

4753
aio_status_t status();
@@ -54,9 +60,11 @@ class AdafruitIO {
5460
uint32_t _last_ping = 0;
5561

5662
Adafruit_MQTT *_mqtt;
63+
HttpClient *_http;
5764

5865
const char *_host = "io.adafruit.com";
59-
uint16_t _port = 8883;
66+
uint16_t _mqtt_port = 8883;
67+
uint16_t _http_port = 443;
6068

6169
uint16_t _packetread_timeout;
6270

src/AdafruitIO_Dashboard.cpp

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
//
2+
// Adafruit invests time and resources providing this open source code.
3+
// Please support Adafruit and open source hardware by purchasing
4+
// products from Adafruit!
5+
//
6+
// Copyright (c) 2015-2016 Adafruit Industries
7+
// Authors: Tony DiCola, Todd Treece
8+
// Licensed under the MIT license.
9+
//
10+
// All text above must be included in any redistribution.
11+
//
12+
#include "AdafruitIO_Dashboard.h"
13+
#include "AdafruitIO.h"
14+
15+
AdafruitIO_Dashboard::AdafruitIO_Dashboard(AdafruitIO *io, const char *n)
16+
{
17+
_io = io;
18+
name = n;
19+
}
20+
21+
AdafruitIO_Dashboard::~AdafruitIO_Dashboard(){}
22+
23+
bool AdafruitIO_Dashboard::exists()
24+
{
25+
String url = "/api/v2/";
26+
url += _io->_username;
27+
url += "/dashboards/";
28+
url += name;
29+
30+
_io->_http->startRequest(url.c_str(), HTTP_METHOD_GET);
31+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
32+
_io->_http->endRequest();
33+
34+
int status = _io->_http->responseStatusCode();
35+
_io->_http->responseBody(); // needs to be read even if not used
36+
37+
return status == 200;
38+
}
39+
40+
bool AdafruitIO_Dashboard::create()
41+
{
42+
String url = "/api/v2/";
43+
url += _io->_username;
44+
url += "/dashboards";
45+
46+
String body = "name=";
47+
body += name;
48+
49+
_io->_http->startRequest(url.c_str(), HTTP_METHOD_POST);
50+
_io->_http->sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/x-www-form-urlencoded");
51+
_io->_http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, body.length());
52+
_io->_http->sendHeader("X-AIO-Key", _io->_key);
53+
_io->_http->endRequest();
54+
_io->_http->write((const byte*)body.c_str(), body.length());
55+
56+
int status = _io->_http->responseStatusCode();
57+
_io->_http->responseBody(); // needs to be read even if not used
58+
59+
return status == 201;
60+
}
61+
62+
const char* AdafruitIO_Dashboard::user() {
63+
return _io->_username;
64+
}
65+
66+
AdafruitIO* AdafruitIO_Dashboard::io() {
67+
return _io;
68+
}
69+
70+
ToggleBlock* AdafruitIO_Dashboard::addToggleBlock(AdafruitIO_Feed *feed)
71+
{
72+
return new ToggleBlock(this, feed);
73+
}
74+
75+
MomentaryBlock* AdafruitIO_Dashboard::addMomentaryBlock(AdafruitIO_Feed *feed)
76+
{
77+
return new MomentaryBlock(this, feed);
78+
}
79+
80+
SliderBlock* AdafruitIO_Dashboard::addSliderBlock(AdafruitIO_Feed *feed)
81+
{
82+
return new SliderBlock(this, feed);
83+
}
84+
85+
GaugeBlock* AdafruitIO_Dashboard::addGaugeBlock(AdafruitIO_Feed *feed)
86+
{
87+
return new GaugeBlock(this, feed);
88+
}
89+
90+
TextBlock* AdafruitIO_Dashboard::addTextBlock(AdafruitIO_Feed *feed)
91+
{
92+
return new TextBlock(this, feed);
93+
}
94+
95+
ChartBlock* AdafruitIO_Dashboard::addChartBlock(AdafruitIO_Feed *feed)
96+
{
97+
return new ChartBlock(this, feed);
98+
}
99+
100+
ColorBlock* AdafruitIO_Dashboard::addColorBlock(AdafruitIO_Feed *feed)
101+
{
102+
return new ColorBlock(this, feed);
103+
}
104+
105+
MapBlock* AdafruitIO_Dashboard::addMapBlock(AdafruitIO_Feed *feed)
106+
{
107+
return new MapBlock(this, feed);
108+
}
109+
110+
StreamBlock* AdafruitIO_Dashboard::addStreamBlock(AdafruitIO_Feed *feed)
111+
{
112+
return new StreamBlock(this, feed);
113+
}
114+
115+
ImageBlock* AdafruitIO_Dashboard::addImageBlock(AdafruitIO_Feed *feed)
116+
{
117+
return new ImageBlock(this, feed);
118+
}

0 commit comments

Comments
 (0)