Setting up Triggers

So now that we are sending cap-touch and light data to Adafruit IO and reacting to servo and NeoPixel events from it let's link them up.

Adafruit IO has a really cool feature for adding some logic to your data collection: triggers.  A trigger is a response to an event getting posted, I.e. a new piece of data being added (aka published) to a feed.  When data gets added to a feed, any associated triggers compare the new value against a constant or the current value of another feed.

If the condition is satisfied it can take one of several actions:

  • email you a message,
  • send a webhook message, or
  • publish a specific value to feed.

It's the last of these that is of particular interest.

Let's make the cap-touch move the servo between 0 and 180 degrees. Since we send a 1 and 0 when the cap-touch is touched and released, respectively, we can use that in two triggers. One for each state.

In the main Adafruit IO screen, on the left side, you'll see a menu item Triggers. Click that and create the two Reactive Triggers below. They look similar but have slightly different values.

We can do something similar with the light and NeoPixel feeds. In all we have four triggers:

With these in place, load up the code below in remote_monitor.ino. This program has both the touch input code and the servo/NeoPixel output code.

When the sketch is running, you should be able to touch and release the capacitive touch sensor 1 to control the servo, as well as vary the lighting on the photoresistor to control the color of the NeoPixels.

Multiple Data Sources

Things get interesting when you take into account that multiple devices can connect to Adafruit IO and publish to feeds. If we build another Feather + Crickit and put a input only version of the sketch on it (below) we can have it publish readings that the triggers then use. The original Feather + Crickit consumes the output from the triggers. Conceivably (possibly limited by the data limits on your Adafruit IO account) you could have any number of nodes feeding data to the system.

// Crickit + Adafruit IO Publish 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 Dave Astels for Adafruit Industries
// Copyright (c) 2018 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

#include "config.h"
#include <Adafruit_Crickit.h>

#define CAPTOUCH_THRESH 500

#define IO_LOOP_DELAY (1000)
unsigned long lastUpdate = 0;

// set up the feeds
AdafruitIO_Feed *light;
uint16_t last_reported_light = 0;

AdafruitIO_Feed *touch;
boolean last_touch = false;

// set up the Crickit

Adafruit_Crickit crickit;

void setup_feeds()
{
  light = io.feed("crickit.light");
  touch = io.feed("crickit.touch-0");
}


void setup()
{
  setup_feeds();
  Serial.println("Feeds set up");

  // start the serial connection
  Serial.begin(115200);

  // wait for serial monitor to open
  while(! Serial);

  Serial.println("Connecting to Adafruit IO");

  // connect to io.adafruit.com
  io.connect();

  // wait for a connection
  while(io.status() < AIO_CONNECTED) {
    Serial.print(".");
    //    Serial.println(io.statusText());
    delay(500);
  }

  // we are connected
  Serial.println();
  Serial.println(io.statusText());

  if (!crickit.begin()) {
    Serial.println("Error starting Crickit!");
    while(1);
  } else {
    Serial.println("Crickit started");
  }

  Serial.println("setup complete");
}


void loop()
{

  // io.run(); is required for all sketches.
  // it should always be present at the top of your loop
  // function. it keeps the client connected to
  // io.adafruit.com, and processes any incoming data.
  io.run();

  if (millis() > (lastUpdate + IO_LOOP_DELAY)) {

    uint16_t light_level = crickit.analogRead(CRICKIT_SIGNAL1);
    uint16_t light_delta = abs(light_level - last_reported_light);

    if (light_delta > 10) {
      light->save(light_level);
      last_reported_light = light_level;
      Serial.print("Sending ");
    }
    Serial.print("Light: ");
    Serial.println(light_level);

    uint16_t val = crickit.touchRead(0);

    if (val >= CAPTOUCH_THRESH && !last_touch) {
      touch->save(1);
      last_touch = true;
      Serial.println("CT 0 touched.");
    } else if (val < CAPTOUCH_THRESH && last_touch) {
      touch->save(0);
      last_touch = false;
      Serial.println("CT 0 released.");
    }

    // after publishing, store the current time
    lastUpdate = millis();
  }

}
This guide was first published on Jul 18, 2018. It was last updated on Jul 18, 2018. This page (Setting up Triggers) was last updated on Sep 18, 2019.