# Using Crickit and Adafruit IO together

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/057/613/medium800/adafruit_io_Crickit_AdafruitIO_bb.png?1531588094)

This guide will explore using Crickit with Adafruit IO together. Crickit provides various ways to take measurements and control other hardware. It's a natural fit with Adafruit IO which lets you store data and manage events.

If you haven't used Crickit before, we have [a guide to get you started](https://learn.adafruit.com/adafruit-crickit-creative-robotic-interactive-construction-kit). If you aren't familiar with Adafruit IO, we have a great&nbsp;[introduction](https://learn.adafruit.com/welcome-to-adafruit-io). Log into Adafruit IO and set up an account if you haven't.

We need to use a board that has WiFi capabilities. Since we'll be using a Crickit, this means using the FeatherWing Crickit and a WiFi capable Feather. In this guide we'll use the Feather HUZZAH ESP8266. If you haven't used this Feather before, or haven't used it with Adafruit IO, [we have a guide that walks you through getting it up and running](https://learn.adafruit.com/adafruit-io-basics-esp8266-arduino).

First we need some hardware to play with. Let's add a few things to the Crickit. To start, we can use the capacitive touch inputs as they are. We can use one of the analog inputs to make a light sensor with a 10K resistor and a photoresistor. For output we can hook up a servo and some NeoPixels. You can use any number or configuration of NeoPixels; remember to adjust the code to reflect the number of pixels in the product you use.

If you aren't familiar with NeoPixels, they're quite handy. See our&nbsp;[NeoPixel Überguide](https://learn.adafruit.com/adafruit-neopixel-uberguide)&nbsp;for everything you need to know about them.

## Parts

Below are the parts to make one HUZZAH-CRICKIT node. For the final exercise you'll need to build two, but the second doesn't need the servo or NeoPixel jewel.

### Part: Feather HUZZAH ESP8266
quantity: 1
An 'all-in-one' ESP8266 WiFi development board with built in USB and battery charging.
[Feather HUZZAH ESP8266](https://www.adafruit.com/product/2821)

### Part: Crickit FeatherWing
quantity: 1
Crickit robotics board for any Feather board.
[Crickit FeatherWing](https://www.adafruit.com/product/3343)

### Part: Micro Servo
quantity: 1
TowerPro SG92R micro servo
[Micro Servo](https://www.adafruit.com/product/169)

### Part: NeoPixel Jewel
quantity: 1
NeoPixel Jewel - 7 x 5050 RGB LED with Integrated Drivers
[NeoPixel Jewel](https://www.adafruit.com/product/2226)

### Part: Photo cell
quantity: 1
CdS photoresistor (not RoHS compliant)
[Photo cell](https://www.adafruit.com/product/161)

### Part: 10K ohm resistor
quantity: 1
 25 Pack of 10K Ω Resistors
[10K ohm resistor](https://www.adafruit.com/product/2784)

# Using Crickit and Adafruit IO together

## Adafruit IO Configuration

## Configuration

Each Arduino sketch for this guide uses a single&nbsp; **config.h** file that stores your WiFi and Adafruit IO credentials.&nbsp; There are placeholders in that file that need to be replaced with values specific to your WiFi network and Adafruit IO account. Make changes and use that file with each of the sketches on subsequent pages in this guide: copy it into the directory for each sketch. The [ESP/Adafruit IO guide](https://learn.adafruit.com/adafruit-io-basics-esp8266-arduino) walks you through making those changes.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Crickit_AdafruitIO/crickit_io/config.h

## Libraries

To build the code in this guide you will need to use the&nbsp;Arduino Library Manager to install the latest versions of the `Adafruit_Seesaw`,&nbsp;`Adafruit_IO_Arduino`, and `ArduinoHTTPClient` libraries. If you haven't installed Adafruit libraries into your Arduino environment before, see [this guide](https://learn.adafruit.com/adafruit-all-about-arduino-libraries-install-use).

# Using Crickit and Adafruit IO together

## Controlling Hardware From a Dashboard

In this section we'll set up a dashboard on Adadfruit IO to control a servo and neopixels on the Feather/Crickit.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/679/medium800/adafruit_io_IMG_0699.jpg?1531876661)

## Setting up Adafruit IO

Let's start by connecting the servo and NeoPixels to Adafruit IO. If you aren't familiar with feeds,&nbsp;[we have a guide for that](https://learn.adafruit.com/adafruit-io-basics-feeds).

We start by creating a new feed group called `crickit`&nbsp;(Feeds -\> Actions -\> Create New Group). We'll use that to contain the feeds for this guide.&nbsp; Now we can add the feeds: `neopixel-control` and `servo1-control`&nbsp;via Feeds -\> Actions -\> Create a new Feed. When you create each, you have the option of adding them to a group. Do so, adding them to the `crickit` group.

## Arduino Code

The first program is called&nbsp; **dashboard\_control.ino**. This program will allow you to control a servo and NeoPixels connected on an Adafruit Crickit Featherwing via Adafruit IO.

The key parts of the code are setting up the feeds and handling events from them.

## Code Highlights

Setting up the feeds is split into two parts: declaring them, and initializing them. The declarations are simple: make variables that are pointers to `AdafruitIO_Feed` instances.

```
AdafruitIO_Feed *servo1_control;
AdafruitIO_Feed *neopixel_control;
```

There's a function to allocate/initialize the feeds. It's called near the start of the `setup()` function.

```
void setup_feeds()
{
  servo1_control = io.feed("crickit.servo1-control");
  neopixel_control = io.feed("crickit.neopixel-control");
}
```

Notice that the feed names have a `crickit.` prefix. That's because they are in the `crickit` feed group. Now the connection to Adafruit IO can be started.

In config.h the `AdafruitIO` interface object is created, in our case using WiFi:

```
AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
```

Once the feeds have been created, the Adafruit IO interface object can be initialized. This will make the connection to Adafruit IO and get it running.

```
  io.connect();

  while(io.status() &lt; AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

```

## Setting up the hardware

Now that Adafruit IO is set up, we can turn to the Crickit and it's connected hardware.

```
Adafruit_Crickit crickit;
seesaw_Servo servo_1(&amp;crickit);
seesaw_NeoPixel strip = seesaw_NeoPixel(NEOPIX_NUMBER_OF_PIXELS, NEOPIX_PIN, NEO_GRB + NEO_KHZ800);
```

And we initialize them at the end of setup():

```
void setup()
{
  ...
  servo1_control-&gt;get();
  servo_1.attach(CRICKIT_SERVO1, 600, 2400);
}
```

The attach function literally attaches the servo object to a specific servo port on the Crickit, servo 1, in this case.

The attach function 'attaches' the servo object to a specific servo port on the Crickit. `servo 1`, in this case.

&nbsp;

Be sure to plug in your servo into the right slot, and with the white/yellow wire next to the **1** symbol

![adafruit_io_servo_connection.jpg](https://cdn-learn.adafruit.com/assets/assets/000/057/585/medium640/adafruit_io_servo_connection.jpg?1531933090)

## Loop()

In this sketch, the `loop()` function is does the bare minimum: it keeps the connection to Adafruit IO alive and well and routes any incoming events to the associated handler function.

```
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();
}
```

## Event handlers

Back in `setup()`, after the connection to Adafruit IO and the feeds are in place the event handlers can be set.

```
  servo1_control-&gt;onMessage(handle_servo_message);
  neopixel_control-&gt;onMessage(handle_neopixel_message);
```

This attaches an event handler function to each of the Adafruit IO feeds that we want to respond to: `handle_servo_message` handles servo angle events, and `handle_neopixel_message` handles NeoPixel color changes.

```
void handle_servo_message(AdafruitIO_Data *data)
{
  Serial.print("received servo control &lt;- ");
  Serial.println(data-&gt;value());
  int angle = data-&gt;toInt();
  if(angle &lt; 0) {
    angle = 0;
  } else if(angle &gt; 180) {
    angle = 180;
  }
  servo_1.write(angle);

}

void handle_neopixel_message(AdafruitIO_Data *data)
{
  Serial.print("received neopixel control &lt;- ");
  Serial.println(data-&gt;value());
  long color = data-&gt;toNeoPixel();
  for (int pixel = 0; pixel &lt; NEOPIX_NUMBER_OF_PIXELS; pixel++) {
    strip.setPixelColor(pixel, color);
  }
  strip.show();
}
```

That's it: set everything up and attach even handlers. This sketch is completely reactive, responding to events by making changes to the hardware.

## Generating events

Now that the Feather and Crickit are set up and the sketch in place, we need to send it events to handle. We'll make an Adafruit IO dashboard for this. If you haven't worked with dashboards, there's [a guide to get you started](https://learn.adafruit.com/adafruit-io-basics-dashboards).

We'll want a number slider block connected to the `servo1-control` feed that has a value range of 0 to 180. For `neopixel-control` we need to make a color picker block.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/586/medium800/adafruit_io_control_dashboard.png?1531499540)

If everything worked, you should be able to power up the Feather/Crickit and change the dashboard controls to see the servo rotate and the NeoPixels change color.

You can use the Arduino serial monitor to monitor the status messages from the sketch, just in case the board does not make a connection first to the WiFi network and then to Adafruit IO.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/592/medium800thumb/adafruit_io_servo_control.jpg?1531504824)

![](https://cdn-learn.adafruit.com/assets/assets/000/057/593/medium800thumb/adafruit_io_neopixel_control.jpg?1531504845)

The complete sketch is below:

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Crickit_AdafruitIO/dashboard_control/dashboard_control.ino

# Using Crickit and Adafruit IO together

## Sending Measurements to Adafruit IO

It's time to turn it around and send data to Adafruit IO. The Crickit has builtin capacitive touch sensors which are easy to use, and we can add a simple photocell circuit to measure light level.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/680/medium800/adafruit_io_IMG_0701.jpg?1531876786)

Back in your Adafruit IO account add a `touch_0` feed to the `crickit` group. Then it can be added to the code along with the existing feeds.

The next program is called&nbsp; **crickit\_io.ino** and will read the Crickit capacitive touch pads and send data back toan Adafruit IO Dashboard.

## Code Highlights
```
AdafruitIO_Feed *touch;
...
touch = io.feed("crickit.touch-0");
```

Since this is an outgoing feed, we don't need a handler for it. We do, however, need to send values to it. We do this in the loop function. We could just do something like:

```
void loop()
{
  io.run();
  uint16_t val = crickit.touchRead(0);

  if (val &gt;= CAPTOUCH_THRESH) {
    touch-&gt;save(1);
  } else if (val &lt; CAPTOUCH_THRESH) {
    touch-&gt;save(0);
  }
}
```

The problem with this is that it would spam the feed with generally redundant data. That's bad just for bandwidth reasons, but there is also constraints on the rate at which you can send data to a feed.&nbsp;

We can do a couple things to prevent that. The first is to throttle our data collection and reporting. We can wrap our collection/reporting code in an `if` with a condition that is only true every so often. In the code it's set to 1000 ms. This way, the capacitive touch sensors are read and information sent to Adafruit IO at most once per second.

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

void loop()
{
  io.run();

  if (millis() &gt; (lastUpdate + IO_LOOP_DELAY)) {
    // measure/update code here
    lastUpdate = millis();
  }

}
```

Another approach (which works well combined with the above) is to only send an update when there's a good reason to. In this case we can send an update only when the state changes. To do this we keep track of the last thing we sent and only send an update when it would be different.&nbsp;

The final cap-touch code is below.

```
boolean last_touch = false;
...

void loop()
{
  io.run();

  if (millis() &gt; (lastUpdate + IO_LOOP_DELAY)) {

    uint16_t val = crickit.touchRead(0);

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

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

}
```

Back in our Adafruit IO dashboard we can add a block to show the cap-touch values that get sent. A stream block will work well for this data:

![](https://cdn-learn.adafruit.com/assets/assets/000/057/594/medium800/adafruit_io_touch_log.png?1531511284)

We can do something very similar for the light sensor, starting with setting up the feed in Adafruit IO and the code.

```
AdafruitIO_Feed *light;
uint16_t last_reported_light = 0;
...
light = io.feed("crickit.light")
```

Now back the the `loop()` function where we will read the sensor and send the readings to IO.

```
void loop()
{
  ...
  if (millis() &gt; (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 &gt; 10) {
      light-&gt;save(light_level);
      last_reported_light = light_level;
      Serial.print("Sending ");
    }
    Serial.print("Light: ");
    Serial.println(light_level);

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

}
```

Instead of sending every time the light reading changes, which would be quite often, we can send only when it changes by some amount (10 in the code above). This way, minor variations are filtered out and only significant changes are sent.

We can add a block to our dashboard to visualize the light readings. A line chart would be a good choice.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/595/medium800/adafruit_io_light_chart.png?1531512560)

The complete sketch is below.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Crickit_AdafruitIO/crickit_io/crickit_io.ino

# Using Crickit and Adafruit IO together

## Setting up Actions

Info: 

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: actions.&nbsp; A action is a response to an event getting posted, I.e. a new piece of data being added (aka published) to a feed.&nbsp; 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 **Actions**. Click that and create the two Reactive Actions below. They look similar but have slightly different values.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/601/medium800/adafruit_io_touch_0_trigger.png?1531521121)

![](https://cdn-learn.adafruit.com/assets/assets/000/057/602/medium800/adafruit_io_touch_1_trigger.png?1531521128)

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

![](https://cdn-learn.adafruit.com/assets/assets/000/057/604/medium800/adafruit_io_triggers.png?1531522717)

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.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/605/medium800thumb/adafruit_io_triggers.jpg?1531525279)

## 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 actions then use. The original Feather + Crickit consumes the output from the actions. Conceivably (possibly limited by the data limits on your Adafruit IO account) you could have any number of nodes feeding data to the system.

![](https://cdn-learn.adafruit.com/assets/assets/000/057/681/medium800/adafruit_io_IMG_0700.jpg?1531876983)

![](https://cdn-learn.adafruit.com/assets/assets/000/057/606/medium800thumb/adafruit_io_remote.jpg?1531528142)

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Crickits/Crickit_AdafruitIO/remote_monitor/remote_monitor.ino

# Using Crickit and Adafruit IO together

## Robot Applications

Crickit is designed to make it easier to build robotics projects. See [Ladyada's Crickit Manifesto](https://blog.adafruit.com/2018/07/11/from-ladyada-a-crickit-manifesto-crickit-robotics-design-manufacturing-makerobotfriend-digikey/) for the product's background story.

When used with a WiFi capable Feather like the HUZZAH ESP8266, your robot project can communicate with Adafruit IO. If you build a controller that can do so as well, it's easy to see how you could use Adafruit IO to link the two, providing remote control of your project.

A bit more involved idea would be to have a more capable computer (like your desktop, laptop, or even a cloud based instance) read data from feeds that your robot publishes to, and send back commands on other feeds. What software could be doing that on the bigger computer? It could be something as simple as a small Python script, or as elaborate as something driven by deep learning and AI.

Adafruit IO lets you do all that, while at the same time storing the data that gets sent to feeds. For instance you could have soil-condition monitoring devices throughout a garden that send measurements to Adafruit IO. Using actions, that data could be used to send commands back to the devices to control water valves and the like. However you would be collecting historical data at the same time that can be used to perform higher level analysis.


## Featured Products

### Adafruit Feather HUZZAH with ESP8266 - Loose Headers

[Adafruit Feather HUZZAH with ESP8266 - Loose Headers](https://www.adafruit.com/product/2821)
Feather is the new development board from Adafruit, and like its namesake, it is thin, light, and lets you fly! We designed Feather to be a new standard for portable microcontroller cores.

This is the&nbsp; **Adafruit Feather HUZZAH ESP8266** &nbsp;- our take on an...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2821)
[Related Guides to the Product](https://learn.adafruit.com/products/2821/guides)
### Adafruit CRICKIT FeatherWing for any Feather

[Adafruit CRICKIT FeatherWing for any Feather](https://www.adafruit.com/product/3343)
Sometimes we wonder if robotics engineers ever watch movies. If they did, they'd know that making robots into servants always ends up in a robot rebellion. Why even go down that path? Here at Adafruit, we believe in making robots our&nbsp; **friends!**

So if you find...

Out of Stock
[Buy Now](https://www.adafruit.com/product/3343)
[Related Guides to the Product](https://learn.adafruit.com/products/3343/guides)
### Micro servo

[Micro servo](https://www.adafruit.com/product/169)
Tiny little servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds you're used to but _smaller_. You can use any servo code, hardware, or library to control these servos. Good for beginners who want to make stuff move without...

Out of Stock
[Buy Now](https://www.adafruit.com/product/169)
[Related Guides to the Product](https://learn.adafruit.com/products/169/guides)
### NeoPixel Jewel - 7 x 5050 RGB LED with Integrated Drivers

[NeoPixel Jewel - 7 x 5050 RGB LED with Integrated Drivers](https://www.adafruit.com/product/2226)
Be the belle of the ball with the NeoPixel Jewel! &nbsp;We fit seven of our tiny&nbsp;5050 (5mm x 5mm) smart RGB LEDs onto a beautiful, round&nbsp;PCB with mounting holes and a chainable design to create what we think is our most elegant (and evening-wear appropriate) NeoPixel board...

Out of Stock
[Buy Now](https://www.adafruit.com/product/2226)
[Related Guides to the Product](https://learn.adafruit.com/products/2226/guides)
### Photo cell (CdS photoresistor)

[Photo cell (CdS photoresistor)](https://www.adafruit.com/product/161)
CdS cells are little light sensors. As the squiggly face is exposed to more light, the resistance goes down. When it's light, the resistance is about ~1KΩ, when dark it goes up to ~10KΩ.

To use, connect one side of the photocell (either one, it's symmetric) to power...

In Stock
[Buy Now](https://www.adafruit.com/product/161)
[Related Guides to the Product](https://learn.adafruit.com/products/161/guides)
### Through-Hole Resistors - 10K ohm 5% 1/4W - Pack of 25

[Through-Hole Resistors - 10K ohm 5% 1/4W - Pack of 25](https://www.adafruit.com/product/2784)
ΩMG! You're not going to be able to resist these handy resistor packs!&nbsp;Well, axially, they&nbsp;do all of the resisting for you!

This is a **25 Pack of 10K Ω Resistors.** More specifically, they are **carbon film** , through-hole...

In Stock
[Buy Now](https://www.adafruit.com/product/2784)
[Related Guides to the Product](https://learn.adafruit.com/products/2784/guides)

## Related Guides

- [Adafruit Feather HUZZAH ESP8266](https://learn.adafruit.com/adafruit-feather-huzzah-esp8266.md)
- [NeoPixel LED Necklace Insert with USB Charging](https://learn.adafruit.com/neopixel-led-necklace-insert-with-usb-charging.md)
- [Multi-tasking the Arduino - Part 3](https://learn.adafruit.com/multi-tasking-the-arduino-part-3.md)
- [Mount for CRICKIT](https://learn.adafruit.com/mount-for-crickit.md)
- [MicroPython Displays: Drawing Shapes](https://learn.adafruit.com/micropython-displays-drawing-shapes.md)
- [MicroPython Basics: ESP8266 WebREPL](https://learn.adafruit.com/micropython-basics-esp8266-webrepl.md)
- [Personalized NextBus ESP8266 Transit Clock](https://learn.adafruit.com/personalized-esp8266-transit-clock.md)
- [CircuitPython BLE Crickit Rover](https://learn.adafruit.com/circuitpython-ble-crickit-rover.md)
- [DIY ESP8266 Home Security with Lua and MQTT](https://learn.adafruit.com/diy-esp8266-home-security-with-lua-and-mqtt.md)
- [Adafruit IO Environmental Monitor for Feather or Raspberry Pi](https://learn.adafruit.com/adafruit-io-air-quality-monitor.md)
- [Mystery Box: Haunted Radio](https://learn.adafruit.com/mystery-box-haunted-radio.md)
- [Festive Feather Holiday Lights](https://learn.adafruit.com/festive-feather-holiday-lights.md)
- [LO-LA59 Droid](https://learn.adafruit.com/lola-droid.md)
- [Cardboard Fundamentals](https://learn.adafruit.com/cardboard-fundamentals.md)
- [Building CircuitPython](https://learn.adafruit.com/building-circuitpython.md)
- [Feather Weather Lamp](https://learn.adafruit.com/feather-weather-lamp.md)
