|
| 1 | +// Adafruit IO Group Subscribe 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 group |
| 23 | +AdafruitIO_Group *group = io.group("example"); |
| 24 | + |
| 25 | +void setup() { |
| 26 | + |
| 27 | + // start the serial connection |
| 28 | + Serial.begin(115200); |
| 29 | + |
| 30 | + // wait for serial monitor to open |
| 31 | + while(! Serial); |
| 32 | + |
| 33 | + // connect to io.adafruit.com |
| 34 | + Serial.print("Connecting to Adafruit IO"); |
| 35 | + io.connect(); |
| 36 | + |
| 37 | + group->onMessage("count-1", one); |
| 38 | + group->onMessage("count-2", two); |
| 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 | +} |
| 51 | + |
| 52 | +void loop() { |
| 53 | + |
| 54 | + // io.run(); is required for all sketches. |
| 55 | + // it should always be present at the top of your loop |
| 56 | + // function. it keeps the client connected to |
| 57 | + // io.adafruit.com, and processes any incoming data. |
| 58 | + io.run(); |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +// this function is called whenever a 'counter-1' message |
| 64 | +// is received from Adafruit IO. it was attached to |
| 65 | +// the counter-1 feed in the setup() function above. |
| 66 | +void one(AdafruitIO_Data *data) { |
| 67 | + Serial.print("received example.count-1 <- "); |
| 68 | + Serial.println(data->value()); |
| 69 | +} |
| 70 | + |
| 71 | +// this function is called whenever a 'counter-2' message |
| 72 | +// is received from Adafruit IO. it was attached to |
| 73 | +// the counter-2 feed in the setup() function above. |
| 74 | +void two(AdafruitIO_Data *data) { |
| 75 | + Serial.print("received example.count-2 <- "); |
| 76 | + Serial.println(data->value()); |
| 77 | +} |
0 commit comments