Skip to content

Instantly share code, notes, and snippets.

@mzero
Created August 27, 2020 15:48
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 mzero/ec8cece8af9a05f786a386aa07f306d3 to your computer and use it in GitHub Desktop.
Save mzero/ec8cece8af9a05f786a386aa07f306d3 to your computer and use it in GitHub Desktop.
Example of reading four probes via Adafruit IO, and displaying in rotation on a display.
/**
*** Probes & Data storage
**/
AdafruitIO_Feed *probe1 = io.feed("probe_one");
AdafruitIO_Feed *probe2 = io.feed("probe_two");
AdafruitIO_Feed *probe3 = io.feed("probe_three");
AdafruitIO_Feed *probe4 = io.feed("probe_four");
const int numberOfProbes = 4;
float readings[numberOfProbes];
bool readingValid[numberOfProbes];
uint32_t readingMillis[numberOfProbes];
void handleProbe(int probeNumber, AdafruitIO_Data *data) {
readings[probeNumber] = data->toFloat();
readingsValid[probeNumber] = true;
readingsMillis[probeNumber] = millis();
}
void handleProbe1(AdafruitIO_Data *data) {
handleProbe(0, data); // Note index starting from zero
}
void handleProbe2(AdafruitIO_Data *data) {
handleProbe(1, data);
}
void handleProbe3(AdafruitIO_Data *data) {
handleProbe(2, data);
}
void handleProbe4(AdafruitIO_Data *data) {
handleProbe(3, data);
}
void probesStart() {
Serial.print("Connecting to Adafruit IO");
io.connect();
probe1->onMessage(handleProbe1);
probe2->onMessage(handleProbe2);
probe3->onMessage(handleProbe3);
probe4->onMessage(handleProbe4);
while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println(io.statusText());
// I'm not sure these four lines are needed at all
probe1->get();
probe2->get();
probe3->get();
probe4->get();
}
void probesRun() {
io.run();
}
/**
*** Display of Probe data
**/
const char* probeNames[numberOfProbes] {
"DAY",
"Bott",
"Foo",
"Bar"
};
const uint32_t screenRotationMillis = 2000; // 2 seconds between screens
const uint32_t staleDataMillis = 10000; // data considered old after 10 sec
void display(uint32_t now, int probeNumber) {
matrix.setRotation(2);
matrix.clearScreen();
matrix.setCursor(0, 0);
matrix.println(probeNames[probeNumber]);
if (readingValid[probeNumber]) {
matrix.print(readings[probeNumber]);
matrix.print(" F");
if (now - readingMillis[probeNumber] < staleDataMillis) {
matrix.print(" old");
}
} else {
matrix.print("--"); // no reading yet
}
matrix.writeScreen();
}
void displayRun() {
static uint32_t nextDisplayTime = 0;
static int nextProbeToDisplay = 0;
uint32_t now = millis();
if (now > nextDisplayTime) {
display(now, nextProbeToDisplay);
nextDisplayTime = now + screenRotationMillis;
nextProbeToDisplay = (nextProbeToDisplay + 1) % numberOfProbes;
}
}
/**
*** Main code
**/
void setup() {
Serial.begin(115200);
while(! Serial); // wait for serial monitor to open
probesStart();
}
void loop() {
probesRun();
displayRun();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment