Project tutorial

Anduino Speaks Adafruit! © Apache-2.0

Get connected to Adafruit IO using MQTT! This project explains how to stream DHT22 temp data to Adafruit IO's dashboard!

  • 1,496 views
  • 0 comments
  • 2 respects

Components and supplies

Apps and online services

About this project

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.

// WiFi parameters 
#define WLAN_SSID       "WIFI_SSID" 
#define WLAN_PASS       "WIFI_PASS" 
// Adafruit IO 
#define AIO_SERVER      "io.adafruit.com" 
#define AIO_SERVERPORT  1883 
#define AIO_USERNAME    "your_IO_Username" 
#define AIO_KEY         "your_AIO_key" 

Also feel free to change the name of the feed, we've named it anduinoTemp for this write up.

Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt,  AIO_USERNAME "/feeds/anduinoTemp"); 

The last part we'll need to edit is where we connect our DHT22 Temp sensor. There are two popular form factors, one which includes a pre-wired pull up resistor and one which requires you to wire in one yourself. Wire up ground, vcc, (3.3v on the Due and Zero) and the signal to a DIO pin.

#define DHTPIN 53       // what digital pin we're connected to 
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321 

That's it! Once you've made these changes, wire up the temp sensor, flash the sketch, and let's head over to Adafruit IO's dashboard and check our data!

Adafruit IO Dashboard

If all is well, and you see in your terminal that:

Connecting to Your_WiFi
WiFi connected! 
Connecting to Adafruit IO... Adafruit IO Connected! 
Temp published: 82 
Temperature: 82 *F	 

You're connected and streaming data, you should start to see data in your feed!

Click Feeds, on the left hand side, and then select 'anduinoTemp'. Now that your feed is working, you can create a dashboard to display your temperature results.

Creating Dashboards in Adafruit IO

To create a new dashboard select 'Dashboards' on the left hand side toolbar, then click the 'Actions' drop down, and select 'Create a new Dashboard'. Give it a name and hit create! Choose which feed you'd like to access in this dashboard:

Select the anduinoTemp feed, next you should get to a blank dashboard. Now you can add in some display blocks. Click the blue '+' button on the top right to bring up this selector:

I chose to create the data 'stream' and 'line chart'. Edit any details you'd like, and voilà!

What's next?

We mentioned the DHT22 can also give you humidity readings so a natural improvement could be updating the sketch/dashboards to include temperature and humidity. I'm planning on expanding on this write up in a new project to give some insight into another one of my favorite Adafruit IO features:

Adafruit IO --> IFTTT!

Now that you have your feed working, leveraging any of the IFTTT services available are made possible thanks to Adafruit's suite of IFTTT applets!

**For now barring a pull request I've just submitted to Adafruit you'll need to make this quick change in order for this to compile on the Due, otherwise with anduinoWiFi and the Zero things should work just fine "out of the box"**.

Code

Anduino_to_AdafruitArduino
Get connected to Adafruit IO using MQTT! This project uses Adafruit's MQTT library, DHT22, and andunoLCD to display temperature info on anduino's LCD display and in the cloud!
No preview (download only).
Anduino
An Arduino library for the Andium(Anduino) shield. Transform your Arduino into an AndiumNode.

Comments

Similar projects you might like

Arduino Indoor Garden

Project tutorial by MEGA DAS

  • 11,754 views
  • 7 comments
  • 37 respects

WiFi To Your Website

Project showcase by Projecter

  • 2,364 views
  • 4 comments
  • 4 respects

SERENA: Alarm Control Panel

Project tutorial by Michael Marinis

  • 3,837 views
  • 0 comments
  • 9 respects

Settlers Of Catan Dice

Project tutorial by joshi

  • 3,514 views
  • 10 comments
  • 16 respects

Digital & Binary Clock In 8 Digits x 7 Segments LED Display

Project tutorial by LAGSILVA

  • 104 views
  • 0 comments
  • 2 respects
Add projectSign up / Login