|
| 1 | +// Adafruit IO Time Topic Subscription 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 Adam Bachman, Brent Rubell for Adafruit Industries |
| 8 | +// Copyright (c) 2018 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 'time/seconds' topic |
| 23 | +AdafruitIO_Time *seconds = io.time(AIO_TIME_SECONDS); |
| 24 | + |
| 25 | +// set up the 'time/milliseconds' topic |
| 26 | +AdafruitIO_Time *msecs = io.time(AIO_TIME_MILLIS); |
| 27 | + |
| 28 | +// set up the 'time/ISO-8601' topic |
| 29 | +AdafruitIO_Time *iso = io.time(AIO_TIME_ISO); |
| 30 | + |
| 31 | +void setup() { |
| 32 | + |
| 33 | + // start the serial connection |
| 34 | + Serial.begin(115200); |
| 35 | + |
| 36 | + // wait for serial monitor to open |
| 37 | + while(! Serial); |
| 38 | + |
| 39 | + Serial.print("Connecting to Adafruit IO"); |
| 40 | + |
| 41 | + // start MQTT connection to io.adafruit.com |
| 42 | + io.connect(); |
| 43 | + |
| 44 | + // attach message handler for the seconds feed |
| 45 | + seconds->onMessage(handleSecs); |
| 46 | + |
| 47 | + // attach a message handler for the msecs feed |
| 48 | + msecs->onMessage(handleMillis); |
| 49 | + |
| 50 | + // attach a message handler for the ISO feed |
| 51 | + iso->onMessage(handleISO); |
| 52 | + |
| 53 | + // wait for an MQTT connection |
| 54 | + // NOTE: when blending the HTTP and MQTT API, always use the mqttStatus |
| 55 | + // method to check on MQTT connection status specifically |
| 56 | + while(io.mqttStatus() < AIO_CONNECTED) { |
| 57 | + Serial.print("."); |
| 58 | + delay(500); |
| 59 | + } |
| 60 | + |
| 61 | + // we are connected |
| 62 | + Serial.println(); |
| 63 | + Serial.println(io.statusText()); |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +void loop() { |
| 68 | + |
| 69 | + // io.run(); is required for all sketches. |
| 70 | + // it should always be present at the top of your loop |
| 71 | + // function. it keeps the client connected to |
| 72 | + // io.adafruit.com, and processes any incoming data. |
| 73 | + io.run(); |
| 74 | + |
| 75 | + delay(5); |
| 76 | + // Because this sketch isn't publishing, we don't need |
| 77 | + // a delay() in the main program loop. |
| 78 | + |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// message handler for the seconds feed |
| 83 | +void handleSecs(char *data, uint16_t len) { |
| 84 | + Serial.print("Seconds Feed: "); |
| 85 | + Serial.println(data); |
| 86 | +} |
| 87 | + |
| 88 | + |
| 89 | +// message handler for the milliseconds feed |
| 90 | +void handleMillis(char *data, uint16_t len) { |
| 91 | + Serial.print("Millis Feed: "); |
| 92 | + Serial.println(data); |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +// message handler for the ISO-8601 feed |
| 97 | +void handleISO(char *data, uint16_t len) { |
| 98 | + Serial.print("ISO Feed: "); |
| 99 | + Serial.println(data); |
| 100 | +} |
0 commit comments