Skip to content

Commit 3eb8d15

Browse files
committed
add basic_subscribe example
1 parent 0c1ac69 commit 3eb8d15

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
//
2+
// Adafruit IO Basic Subscription Example
3+
//
4+
// ▄████
5+
// ▄███████
6+
// █████████▌
7+
// ███████████
8+
// ████████████▌
9+
// ███████████████▄ ████████████▌
10+
// █████████████████████▀▀█████ ▄▄▄▄▄▄▄
11+
// ▐██████████████████ █████████████████▄▄
12+
// ▀█████████ ▀▀███ ██████████████████████
13+
// █████████▄▄ ▐████▀ ▐█████████████▀
14+
// ▀▀███████████████▄▄█████████████▀
15+
// ▄███████ ██ ▀████████████▀
16+
// ███████▀ ▄████ ▐█████▄
17+
// █████████████████▄▄██████▄
18+
// ███████████████████████████
19+
// ██████████████ ▐████████████▌
20+
// ▐██████████▀▀ ▀███████████▌
21+
// █████▀▀ ▀█████████▌
22+
// ▀██████
23+
// ▀███
24+
//
25+
//
26+
// Written by Todd Treece for Adafruit Industries
27+
// Copyright (c) 2016 Adafruit Industries
28+
// Licensed under the MIT license.
29+
//
30+
// All text above must be included in any redistribution.
31+
//
32+
33+
/************************* WiFi Access Point *********************************/
34+
35+
#define WIFI_SSID "your_ssid"
36+
#define WIFI_PASS "your_pass"
37+
38+
/************************* Adafruit IO Setup ********************************/
39+
40+
#define IO_USERNAME "your_username"
41+
#define IO_KEY "your_key"
42+
43+
/*************************** Client Setup ***********************************/
44+
45+
// The Adafruit IO WiFi client will work with the following boards:
46+
// - HUZZAH ESP8266 Breakout -> https://www.adafruit.com/products/2471
47+
// - Feather HUZZAH ESP8266 -> https://www.adafruit.com/products/2821
48+
// - Feather M0 WiFi -> https://www.adafruit.com/products/3010
49+
// - Feather WICED -> https://www.adafruit.com/products/3056
50+
51+
// set up the wifi client using the supplied ssid & pass:
52+
#include "AdafruitIO_WiFi.h"
53+
AdafruitIO_WiFi io(WIFI_SSID, WIFI_PASS);
54+
55+
/************************ Example Starts Here *******************************/
56+
57+
// set up the 'counter' feed
58+
AdafruitIO_Feed *counter = io.feed("counter");
59+
60+
void setup() {
61+
62+
// start the serial connection
63+
Serial.begin(115200);
64+
65+
// wait for serial monitor to open
66+
while(! Serial);
67+
68+
Serial.print("Connecting to Adafruit IO");
69+
70+
// connect to io.adafruit.com
71+
io.connect(IO_USERNAME, IO_KEY);
72+
73+
// set up a message handler for the count feed.
74+
// the handleMessage function (defined below)
75+
// will be called whenever a message is
76+
// received from adafruit io.
77+
counter->onMessage(handleMessage);
78+
79+
// wait for a connection
80+
while(io.status() < AIO_CONNECTED) {
81+
Serial.print(".");
82+
delay(500);
83+
}
84+
85+
// we are connected
86+
Serial.println();
87+
Serial.println(io.statusText());
88+
89+
}
90+
91+
void loop() {
92+
93+
// io.run(); is required for all sketches.
94+
// it should always be present at the top of your loop
95+
// function. it keeps the client connected to
96+
// io.adafruit.com, and processes any incoming data.
97+
io.run();
98+
99+
}
100+
101+
// this function is called whenever a 'counter' message
102+
// is received from Adafruit IO. it was attached to
103+
// the counter feed in the setup() function above.
104+
void handleMessage(AdafruitIO_Data *data) {
105+
106+
Serial.print("received <- ");
107+
Serial.println(data->value());
108+
109+
}

0 commit comments

Comments
 (0)