Get connected to Adafruit IO using MQTT! This project explains how to stream DHT22 temp data to Adafruit IO’s dashboard!
Motivation
Have you heard? Adafruit offers this great free tool to visualize your Arduino’s data in the cloud! Not only is it super easy to use, but it’s free! Although Adafruit refers to it’s current release as beta, we’ve had great success pairing it with anduino to create some interesting IoT devices. In this guide we’ll walk you through getting your anduinoWiFi connected to Adafruit IO and displaying sensor readings! In this case we’re going to use the DHT22 temperature sensor. Visualizing your apartment’s temperature data in the cloud could be your first step in creating your own DIY Nest thermostat!
Getting Started
In order to upload to the cloud we’re going to need to create an account on Adafruit IO! This way we’ll have the necessary credentials for authenticating our anduino shield to speak to Adafruit’s dashboard. Navigate to Adafruit IO and sign up to join the beta!
Once you’re profile is created you’re going to want to jot down a couple important things about your profile. Navigate to settings and then click the button that says, ‘View AIO Key’.
A dialog will pop up revealing your AIO Key, copy, paste, keep this in a safe place, we’re going to copy it over to our sketch in a moment. Also take note of your username, it should be displayed at the top of the screen adjacent to the /settings title.
Let’s check out the sketch!
Anduino -> Adafruit IO
Clone, download the source, or copy and paste the Arduino sketch below into your IDE. There are a few required libraries we’ll need to make sure you have downloaded and imported into your IDE before moving forward.
You can either grab these from the Github links above our include them via the Sketch>>Include Library>>Manage Libraries option in the Arduino IDE. Here’s our sketch…
#include <WiFi101.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"
#include "AnduinoLCD.h"
// WiFi parameters
#define WLAN_SSID "YOUR_SSID"
#define WLAN_PASS "YOUR_PASSWD"
// Adafruit IO
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "YOUR_AIO_USERNAME"
#define AIO_KEY "YOUR_AIO_KEY"
#define DHTPIN 53 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
/****************************** Feeds ***************************************/
// Setup feed for temperature
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/anduinoTemp");
/*Create an instance of the AnduinoLCD */
AnduinoLCD LCD = AnduinoLCD(ST7735_CS_PIN, ST7735_DC_PIN, PERIPH_RST_PIN);
static int temp = 0;
static int tempPrev = 0;
void setup() {
Serial.begin(115200);
delay(3000);
//Connect to WiFi & Adafruit.IO
connectToWiFi();
connectToAdafruit();
//connect to DHT22 Temp Sensor
dht.begin();
//Initialize LCD
LCD.begin();
LCDinit();
}
void loop() {
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
connect();
}
// Grab the current state of the sensor
temp = dht.readTemperature(true);
//convert int temp to char array
char b[4];
String str;
str=String(temp);
for(int i=0; i<str.length(); i++)
{
b[i]=str.charAt(i);
}
b[(str.length())+1]=0;
// Publish data
if (!temperature.publish((char*)b)) {
Serial.println(F("Failed to publish temp"));
} else {
Serial.print(F("Temp published: "));
Serial.println(temp);
displayTemp(temp, tempPrev);
}
//float h = dht.readHumidity();
// Check if any reads failed and exit early (to try again).
if (isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//Serial.print("Humidity: ");
//Serial.print(h);
//Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" *F\t");
//prev temp stored for LCD
tempPrev = temp;
//repeat every 1min
delay(60000);
}
// connect to adafruit io via MQTT
void connect() {
Serial.print(F("Connecting to Adafruit IO... "));
int8_t ret;
while ((ret = mqtt.connect()) != 0) {
switch (ret) {
case 1: Serial.println(F("Wrong protocol")); break;
case 2: Serial.println(F("ID rejected")); break;
case 3: Serial.println(F("Server unavail")); break;
case 4: Serial.println(F("Bad user/pass")); break;
case 5: Serial.println(F("Not authed")); break;
case 6: Serial.println(F("Failed to subscribe")); break;
default: Serial.println(F("Connection failed")); break;
}
if(ret >= 0)
mqtt.disconnect();
Serial.println(F("Retrying connection..."));
delay(5000);
}
Serial.println(F("Adafruit IO Connected!"));
}
void displayTemp(int temp, int tempPrev)
{
//clear the stale value
LCD.setTextColor(ST7735_BLACK);
LCD.setTextSize(2);
LCD.setTextWrap(true);
LCD.setCursor(40,60);
LCD.setTextSize(5);
LCD.print(tempPrev);
LCD.setTextSize(1);
LCD.print("F");
//Print new value
LCD.setTextColor(ST7735_WHITE);
LCD.setTextSize(2);
LCD.setTextWrap(true);
LCD.setCursor(40,60);
LCD.setTextSize(5);
LCD.print(temp);
LCD.setTextSize(1);
LCD.print("F");
}
void connectToWiFi()
{
// Connect to WiFi access point.
delay(10);
Serial.print(F("Connecting to "));
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F("WiFi connected!"));
}
void connectToAdafruit()
{
// connect to adafruit io
connect();
}
void LCDinit()
{
LCD.setBacklight(ON);
LCD.fillScreen(ST7735_BLACK); //clear the screen
LCD.showBanner(); //load Andium Banner
LCD.setTextColor(ST7735_WHITE);
LCD.setTextSize(2);
LCD.setTextWrap(true);
LCD.setCursor(0,40);
LCD.print("Temperature: ");
}
In this case we’re using Adafruit’s MQTT library to speak to the dashboard. Every minute:
//repeat every 1min
delay(60000);
We loop through the sketch reading the temperature from the sensor:
temp = dht.readTemperature(true);
Publish that reading to our feed.
temperature.publish((char*)b)
We also update anduino’s LCD display with the latest reading each time around.
displayTemp(temp, tempPrev);
Note this sensor is also capable of reading humidity! Once you get this working it’ll be fairly straight forward to uncomment some lines of code and create a new feed for your humidity readings as well!
Before you jump ahead and flash the sketch don’t forget to fill in your specific WIFI_SSID, WIFI_PASS, your_IO_Username, and your_AIO_key.
Read more: Anduino Speaks Adafruit!