Now that the dashboard is ready to receive & display some data, it's time to write our Arduino sketch, so the board can actually send data to the dashboard.

You will first need to get your AIO key, which is a unique key that identify your account on Adafruit IO. You can for example get it from your dashboard:

This tutorial is a bit out of date due to it using the PubSub library. Please use our Adafruit MQTT library found on Github.

Let's now build your Arduino sketch. You will need three libraries for this project:

To install a library, simply place the downloaded library folder inside your Arduino 'libraries' folder.

As the code is quite long, I will only cover the important parts here. Of course, you can find all the code inside the GitHub repository of the project. The sketch starts by including the required libraries:

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <PubSubClient.h>
#include "DHT.h"

After that, we define the pin & type of the DHT sensor, and also create an instance of this sensor:

#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT11   // DHT 11 

Then, that's where you will need to enter your WiFi network name and password:

#define WLAN_SSID       "your_wifi_network"
#define WLAN_PASS       "your_wifi_password"

You also need to enter your user name and AIO key:

#define ADAFRUIT_USERNAME  "your_adafruit_username"
#define AIO_KEY  "your_aio_key"

After that, you will need to enter the different paths for your data feeds, that you created while building the dashboard. You might need to modify this part if you are using different names in the dashboard:

#define TEMPERATURE_PUBLISH_PATH "api/feeds/temperature/data/send.json"
#define HUMIDITY_PUBLISH_PATH "api/feeds/humidity/data/send.json"
#define LIGHT_PUBLISH_PATH "api/feeds/light/data/send.json"

We also need to create instances of the CC3000 client, and also of the PubSubClient that we will use to communicate with Adafruit IO:

Adafruit_CC3000_Client client = Adafruit_CC3000_Client();
PubSubClient mqttclient("", 1883, callback, client);

In the loop() function of the sketch, we first start by measuring data from the light level sensor & from the DHT11 sensor:

// Measure ambient light
float light_level_reading = analogRead(LIGHT_SENSOR_PIN);
int light_level = (int)(light_level_reading/1024.*100.);
// Measure temperature & humidity
int h = dht.readHumidity();
int t = dht.readTemperature();

Then, we send the data to the respective data feeds, using the mqttclient.publish() function. We also wait a bit during each publish, so the client has the time to receive the data:

mqttclient.publish(TEMPERATURE_PUBLISH_PATH, (char *) String(t).c_str());
mqttclient.publish(HUMIDITY_PUBLISH_PATH, (char *) String(h).c_str());
mqttclient.publish(LIGHT_PUBLISH_PATH, (char *) String(light_level).c_str());

Finally, we keep the connection with Adafruit IO alive using the mqttclient.loop() function:


Note that you can find all the code for this project inside the corresponding GitHub repository:

This guide was first published on Apr 20, 2015. It was last updated on Apr 20, 2015.

This page (Build the Arduino Sketch) was last updated on Apr 17, 2015.

Text editor powered by tinymce.