Connecting Your Race Car to Adafruit IO

One of the great things about using the Adafruit Feather system is that you can easily swap out Feathers and FeatherWings to make your projects do different things. In this case we are going to use all of the upgrades from the previous steps, but we are going to swap out the 32u4 Bluefruit LE Feather for a HUZZAH ESP8266 Feather. This will allow us to connect our race car to Adafruit IO and log the temperature and humidity, as well as control the race car's speed from afar.

Sign up for Adafruit IO and create your first dashboard.

The first thing to do is head on over to io.adafruit.com and sign up. Then click on dashboards in the left sidebar and under the actions dropdown, click create new dashboard. Name the dashboard whatever you want (like My Race Car), add a description if you want, and click the create button.

Send data to Adafruit IO

Before we start controlling the race car from Adafruit IO, lets just make sure everything is working by streaming some temperature and humidity data and monitoring it on a graph.

If you haven't already, you will need to swap out the Bluefruit Feather for a HUZZAH Feather. Don't have a HUZZAH Feather? Get one here:

Assembled Feather HUZZAH w/ ESP8266 WiFi With Stacking Headers

PRODUCT ID: 3213
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....
$19.95
IN STOCK

Just gently lift off the motor FeatherWing with the wires still attached, and then gently remove the Bluefruit Feather from the breadboard (be sure to remember where the pins were located. Install the Feather HUZZAH in the exact same spot and attach the Motor FeatherWing.

Next, download the following file and open it in the Arduino IDE.

If this is your first time using Adafruit IO, on that config page, you will need to enter your secret key and username into the config tab. On your newly created dashboard, click on the yellow key icon in the upper right:

On the next page you will find your username and unique key. Copy and paste those into the corresponding spots on the Config tab. Then, enter your wifi SSID and password.

Go ahead and upload this to your HUZZAH. Open the Arduino serial terminal and make sure you connected to AIO, and that data is streaming.

Now, lets add in a graph and view the data in real time. Click the blue '+' icon and then select the line graph option. On the next page, choose the temperature and humidity feeds that we just created:

On the next page, name your graph and click save. Feel free to click the green 'Edit this dashboard' button and drag around your graph, resize, and maybe change the graph from live to 1 hour. Click 'DONE EDITING' to save your changes. You should now see data slowly streaming on your graph. Now it's time to control your race car.

There are some minor pin changes between the HUZZAH and Bluefruit Feathers. Because of this, we need to move the pins for our autonomous sensors. Move the left sensor to HUZZAH pin 12, and the right sensor to pin 14:

Next, add a slider to your dashboard in the same way that you created the graph. This time instead of choosing a feed, we are going to create one. In the upper right, name your feed 'racecarspeed'

Click create, and then name your slider 'Race Car Speed', leave the minimum value at 0, and set the max to 255. Save and add the slider to your dashboard.

We also need to create a button. We are going to use the button to manually ask for the temperature and humidity instead of constantly streaming. Create a monetary button and name it 'racecartakereading'

Next, we will upload some modified code and give this all a test. Copy and paste in the below code over the Temperature and Humidity test code:

// Adafruit IO Temperature & Humidity Example
// Tutorial Link: https://learn.adafruit.com/adafruit-io-basics-temperature-and-humidity
//
// 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-2017 Adafruit Industries
// Licensed under the MIT license.
//
// All text above must be included in any redistribution.

/************************** Configuration ***********************************/

// edit the config.h tab and enter your Adafruit IO credentials
// and any additional configuration needed for WiFi, cellular,
// or ethernet clients.
#include "config.h"

/************************ Example Starts Here *******************************/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
#include <Adafruit_MotorShield.h>

// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 

// And connect 2 DC motors to port M3 & M4 !
Adafruit_DCMotor *L_MOTOR = AFMS.getMotor(3);
Adafruit_DCMotor *R_MOTOR = AFMS.getMotor(4);

// set up the 'temperature' and 'humidity' feeds
AdafruitIO_Feed *temperature = io.feed("temperature");
AdafruitIO_Feed *humidity = io.feed("humidity");
AdafruitIO_Feed *racecarspeed = io.feed("racecarspeed");
AdafruitIO_Feed *racecartakereading = io.feed("racecartakereading");

Adafruit_HTU21DF htu = Adafruit_HTU21DF();

int RaceCar_Speed = 10;

// And connect the Sharp distance sensors
int leftSensor = 12;
int rightSensor = 14;

void setup() {

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

  AFMS.begin();  // create with the default frequency 1.6KHz

  // turn on motors
  L_MOTOR->setSpeed(0);
  L_MOTOR->run(RELEASE);

  R_MOTOR->setSpeed(0);
  R_MOTOR->run(RELEASE);

  pinMode(leftSensor, INPUT); // set up distance sensor pins
  pinMode(rightSensor, INPUT);

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

  Serial.println("HTU21D-F test");

  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }

  // connect to io.adafruit.com
  Serial.print("Connecting to Adafruit IO");
  io.connect();

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

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

  racecarspeed->onMessage(handleSpeed);
  racecartakereading->onMessage(handleReading);

  //Set your motor speed (255 Max)
  L_MOTOR->setSpeed(RaceCar_Speed); 
  R_MOTOR->setSpeed(RaceCar_Speed);

}

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

  L_MOTOR->setSpeed(RaceCar_Speed); 
  R_MOTOR->setSpeed(RaceCar_Speed);
  L_MOTOR->run(FORWARD);
  R_MOTOR->run(FORWARD);

  if (digitalRead(rightSensor) == LOW){
    Serial.print("Right Sensor Triggered");
  }

  while (digitalRead(rightSensor) == LOW){
    L_MOTOR->setSpeed(RaceCar_Speed/2); 
    R_MOTOR->setSpeed(RaceCar_Speed/2);
    L_MOTOR->run(BACKWARD);
    R_MOTOR->run(RELEASE);
  }

  while (digitalRead(leftSensor) == LOW){
    L_MOTOR->setSpeed(RaceCar_Speed/2); 
    R_MOTOR->setSpeed(RaceCar_Speed/2);
    L_MOTOR->run(RELEASE);
    R_MOTOR->run(BACKWARD);
  }
}

void handleSpeed(AdafruitIO_Data *data) {

  // convert the data to integer
  RaceCar_Speed = data->toInt();

  Serial.print("speed: ");
  Serial.println(RaceCar_Speed);

}

void handleReading(AdafruitIO_Data *data) {

  float celsius = htu.readTemperature();
  float fahrenheit = (celsius * 1.8) + 32;

  Serial.print("celsius: ");
  Serial.print(celsius);
  Serial.println("C");
  
  Serial.print("fahrenheit: ");
  Serial.print(fahrenheit);
  Serial.println("F");

  // save fahrenheit (or celsius) to Adafruit IO
  temperature->save(fahrenheit);

  Serial.print("humidity: ");
  Serial.print(htu.readHumidity());
  Serial.println("%");

  // save humidity to Adafruit IO
  humidity->save(htu.readHumidity());
}

Upload the above code (make sure you have your config file the same as before). Prop your race car up off the ground and try sliding the speed slider. The wheel speed should go from off to full speed.

You should also be able to press the Take Reading button and see data show up on your graph. Feel free to add some other dashboard blocks like the gauge to show the current temperature and humidity. Organize your dashboard the way you want. Here is how I organized mine:

What else can you control with AIO on your race car? How about adding in a piezo buzzer and creating a horn? Maybe a button to put your race car in reverse? Tell us what you did to your race car in the Adafruit Forums.

Last updated on 2017-07-12 at 03.48.00 PM Published on 2017-06-01 at 12.22.38 PM