Skip to content

Instantly share code, notes, and snippets.

@abachman
Last active November 3, 2018 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abachman/f1571715455dc7779fe43f988a4338c7 to your computer and use it in GitHub Desktop.
Save abachman/f1571715455dc7779fe43f988a4338c7 to your computer and use it in GitHub Desktop.
Troubleshooting IO with a very simple publish + subscribe sketch
// Adafruit IO Publish & Subscribe Example
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
/************************** Configuration ***********************************/
#include "config.h"
// limit publishing speed
#define IO_LOOP_DELAY 5000
unsigned long lastUpdate = 0;
// set up the 'counter' feed
AdafruitIO_Feed *counter = io.feed("counter");
int count = 0;
void setup() {
Serial.begin(115200);
// start connecting, delay until connected
io.connect();
counter->onMessage(handleMessage);
while(io.status() < AIO_CONNECTED) {
delay(500);
}
Serial.println("connected");
}
void loop() {
io.run();
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
Serial.println("publishing...");
counter->save(count);
lastUpdate = millis();
}
}
void handleMessage(AdafruitIO_Data *data) {
Serial.print("received <- ");
Serial.println(data->value());
}
// Adafruit IO Publish & Subscribe Example
//
// Adafruit invests time and resources providing this open source code.
// Please support Adafruit and open source hardware by purchasing
// products from Adafruit!
//
// Written by Todd Treece for Adafruit Industries
// Copyright (c) 2016 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.
/************************** Configuration ***********************************/
#include "config.h"
// limit publishing speed
#define IO_LOOP_DELAY 5000
unsigned long lastUpdate = 0;
// set up the 'counter' feed
AdafruitIO_Feed *counter = io.feed("counter");
int count = 0;
void setup() {
Serial.begin(115200);
// start connecting, delay until connected
io.connect();
counter->onMessage(handleMessage);
while(io.status() < AIO_CONNECTED) {
delay(500);
}
Serial.println("connected");
}
void loop() {
io.run();
if (millis() > (lastUpdate + IO_LOOP_DELAY)) {
Serial.println("publishing...");
counter->save(count);
lastUpdate = millis();
}
}
void handleMessage(AdafruitIO_Data *data) {
Serial.print("received <- ");
Serial.println(data->value());
}
#include "AdafruitIO_WiFi.h"
#define IO_USERNAME ""
#define IO_KEY ""
#define WIFI_SSID ""
#define WIFI_PASS ""
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment