Skip to content

Commit 452259b

Browse files
committed
add dashboard creation example
1 parent c40913b commit 452259b

File tree

5 files changed

+210
-8
lines changed

5 files changed

+210
-8
lines changed
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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+
87+
void addBlocks() {
88+
89+
bool added = false;
90+
91+
Serial.print("Adding momentary button block... ");
92+
MomentaryBlock *button = dashboard->addMomentaryBlock(feed);
93+
button->text = "Button";
94+
button->value = "1";
95+
button->release = "0";
96+
added = button->save();
97+
Serial.println(added ? "added" : "failed");
98+
99+
Serial.print("Adding toggle button block... ");
100+
ToggleBlock *toggle = dashboard->addToggleBlock(feed);
101+
toggle->onText = "1";
102+
toggle->offText = "0";
103+
added = toggle->save();
104+
Serial.println(added ? "added" : "failed");
105+
106+
Serial.print("Adding slider block... ");
107+
SliderBlock *slider = dashboard->addSliderBlock(feed);
108+
slider->min = 0;
109+
slider->max = 100;
110+
slider->step = 10;
111+
slider->label = "Value";
112+
added = slider->save();
113+
Serial.println(added ? "added" : "failed");
114+
115+
Serial.print("Adding gauge block... ");
116+
GaugeBlock *gauge = dashboard->addGaugeBlock(feed);
117+
gauge->min = 0;
118+
gauge->max = 100;
119+
gauge->ringWidth = "thin"; // thin or thick
120+
gauge->label = "Value";
121+
added = gauge->save();
122+
Serial.println(added ? "added" : "failed");
123+
124+
Serial.print("Adding line chart block... ");
125+
ChartBlock *chart = dashboard->addChartBlock(feed);
126+
chart->yAxisMin = 0;
127+
chart->yAxisMax = 100;
128+
chart->xAxisLabel = "X";
129+
chart->yAxisLabel = "Y";
130+
added = chart->save();
131+
Serial.println(added ? "added" : "failed");
132+
133+
Serial.print("Adding text block... ");
134+
TextBlock *text = dashboard->addTextBlock(feed);
135+
text->fontSize = "small"; // small, medium, or large
136+
added = text->save();
137+
Serial.println(added ? "added" : "failed");
138+
139+
Serial.print("Adding stream block... ");
140+
StreamBlock *stream = dashboard->addStreamBlock(feed);
141+
stream->fontSize = "small"; // small, medium, or large
142+
stream->fontColor = "green"; // green or white
143+
stream->showErrors = true;
144+
stream->showTimestamp = true;
145+
stream->showName = true;
146+
added = stream->save();
147+
Serial.println(added ? "added" : "failed");
148+
149+
Serial.print("Adding color picker block... ");
150+
ColorBlock *color = dashboard->addColorBlock(feed);
151+
added = color->save();
152+
Serial.println(added ? "added" : "failed");
153+
154+
Serial.print("Adding map block... ");
155+
MapBlock *map = dashboard->addMapBlock(feed);
156+
map->historyHours = 0;
157+
map->tile = "contrast"; // street, sat, or contrast
158+
added = map->save();
159+
Serial.println(added ? "added" : "failed");
160+
161+
Serial.print("Adding image block... ");
162+
ImageBlock *image = dashboard->addImageBlock(feed);
163+
added = image->save();
164+
Serial.println(added ? "added" : "failed");
165+
166+
}
167+
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);

src/AdafruitIO_Dashboard.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ bool AdafruitIO_Dashboard::exists()
3939

4040
bool AdafruitIO_Dashboard::create()
4141
{
42-
if(exists())
43-
return true;
44-
4542
String url = "/api/v2/";
4643
url += _io->_username;
4744
url += "/dashboards";

src/AdafruitIO_Feed.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ bool AdafruitIO_Feed::exists()
126126

127127
bool AdafruitIO_Feed::create()
128128
{
129-
if(exists())
130-
return true;
131-
132129
String body = "name=";
133130
body += name;
134131

src/blocks/AdafruitIO_Block.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ bool AdafruitIO_Block::save()
8080
body += block_feeds;
8181
body += "}";
8282

83-
Serial.println(body);
84-
8583
http->startRequest(url.c_str(), HTTP_METHOD_POST);
8684
http->sendHeader(HTTP_HEADER_CONTENT_TYPE, "application/json");
8785
http->sendHeader(HTTP_HEADER_CONTENT_LENGTH, body.length());

0 commit comments

Comments
 (0)