This tutorial will cover how to use a DHT11 sensor with the thigner.io platform to allow reading the sensor temperature and humidity from the Internet.
Prerequisites
- Register a new device in the cloud console. We will be using for this example a device with the id (arduino), and with credentials “your_arduino_credentials”. If you need help with this step, please take a look to this post.
- Arduino IDE with the thinger.io library installed. Also described in this post.
Programming the device
For this first project we will be using an Arduino
+ Ethernet Shield
+ DHT11
sensor for reading temperature and humidity. But you can use any other device like an Adafruit CC3000, Texas Instrument CC3200, Arduino Wifi, Arduino Yun, or even a simple ESP8266. There are some basic examples for all those devices.
For this project we can use the EthernetShield example provided in the thinger.io library. You can open this example at File
> Examples
> thinger.io
> ArduinoEthernet
. You should see a very basic example like this shown below (may vary depending on the library installed):
#include <SPI.h>
#include <Ethernet.h>
#include <ThingerEthernet.h>
#define USERNAME "your_username"
#define DEVICE_ID "your_device_id"
#define DEVICE_CREDENTIAL "your_device_credential"
ThingerEthernet thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
// resource input example (i.e. turning on/off a light, a relay, configuring a parameter, etc)
thing["led"] << [](pson& in){ bool on = in; };
// resource output example (i.e. reading a sensor value)
thing["millis"] >> [](pson& out){ out = millis(); };
// resource input/output example (i.e. passing input values and do some calculations)
thing["in_out"] = [](pson& in, pson& out){
out["sum"] = (long)in["value1"] + (long)in["value2"];
out["mult"] = (long)in["value1"] * (long)in["value2"];
};
}
void loop() {
thing.handle();
}
This sketch is a very basic example that allows reading the millis
function of our Arduino (very similar as we will be reading the temperature and humidity from DHT11) and providing some other input and output examples. Please note that there is a variable called thing
of ThingerEthernet
class. This instance takes some parameters, that are the USERNAME, DEVICE_ID, and DEVICE_CREDENTIAL. You must change this default values with the username you used at login, the device identifier you choose in the device register process, and its device credential.
We are going to try the sketch and read the millis
function from the Internet. So it is time to burn the code in the Arduino and test it in the server! In my case I am using an Arduino Uno, and this is how it looks the Arduino IDE with the sketch.
After you burn the sketch in your device, and you wait around 30 seconds or less, you can go to your devices list, and you should see now your new device as connected (you may need to refresh the page). Please, click the device from the list to open its dashboard.
Each device has its own dashboard that currently allows monitoring its connection state, the IP Address, and the session data transmission in real time (more features will be appearing soon). So if you click the device from the list, you should see a screen like the following.
Thats nice, but we want to start interacting with the device, so please click the button below that is named DEVICE_ID API.
One of the most interesting features of thinger.io is that it has an API discovery protocol that allows interacting with your defined resources directly from the web interface. So any function programmed in the device can be called directly from the console. So, if you already have clicked on the Device API button, you should the current device API like in this picture:
In this case you can see that inside the device API there is a resource called “millis” (the same we set on code thing["millis"]
), and you can click on this resource that will look similar to the following picture.
As you can see, our resource has a number output that contains the value returned by the millis()
function. You can try clicking in the Run
button for getting new reads. Each time you click the button, it is called a REST api with a url like the following (with a proper authentication header):
https://api.thinger.io/v1/users/alvarolb/devices/arduino/millis
This allows building third party services or mobile applications that interacts with the devices. This interaction and the OAuth2 authorization used in the the thinger.io platform will be covered in other tutorials.
So at this moment we have been able to register a new device in the platform, and write some code in the Arduino for its connection to the platform. Now we are going to connect a DHT11 sensor for reading temperature and humidity. There are thousands of tutorials about connecting a DHT11 sensor to the Arduino, so this part will be missed in this tutorial. As a reference, this is my Arduino
+ Ethernet
+ DHT11
Now we are going to update the sketch to integrate the DHT11 sensor and remove the example resources. In this case it is configured a new resource called dht11
that will be available from the thinger.io platform. The new sketch now looks as the following:
#include <SPI.h>
#include <Ethernet.h>
#include <ThingerEthernet.h>
#include "DHT.h"
// dht config
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// thinger.io config
ThingerEthernet thing("alvarolb", "arduino", "your_device_credentials");
void setup() {
dht.begin();
thing["dht11"] >> [](pson& out){
out["humidity"] = dht.readHumidity();
out["celsius"] = dht.readTemperature();
out["fahrenheit"] = dht.readTemperature(true);
};
}
void loop() {
thing.handle();
}
The key differences with the previous sketch is the header inclusion for the DHT sensor (I am using a DHT sensor library from Adafruit installed with the Library Manager), the DHT config (pin, and type), and a new resource for accessing the values from the sensor. If we program this sketch and get back to the device API in the thinger.io console (after letting the device connect to the platform), we can see that a new resource has appeared, that is called dht11
.
This resource contains the three values we have configured in the sketch that are filled with the current sensor readings. Notice that the sensor is not read constantly, instead it is read just when you request the reading, so it can save battery while this information is not required. Also this new resource can be read by a REST API call that will return a JSON content like the following:
{"humidity":31,"celsius":30,"fahrenheit":86}
The DHT11 resource configured in the sketch is currently reading both temperature and humidity at the same time. But we can define the resource in many different forms according to our needs or our desired device API design. For example:
Fine grained resources (will allow reading a single value. i.e. /users/alvarolb/devices/arduino/tempC)
thing["tempC"] >> [](pson& out){ out = dht.readTemperature(); };
thing["tempF"] >> [](pson& out){ out = dht.readTemperature(true); };
thing["humidity"] >> [](pson& out){ out = dht.readHumidity(); };
Hierarchical resources (will change the path on the API. i.e. /users/alvarolb/devices/arduino/dht/tempC)
thing["dht"]["tempC"] >> [](pson& out){ out = dht.readTemperature(); };
thing["dht"]["tempF"] >> [](pson& out){ out = dht.readTemperature(true); };
thing["dht"]["humidity"] >> [](pson& out){ out = dht.readHumidity(); };
You can try if you want this different approaches and see how the API changes.