Arduino Ethernet + DHT11

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.

2 Likes

Hi great tutorial but how to show this in the dashboard instead of calling the API

I now want to display this in the dashboard but I do not fully get how to do this?
I have to use the stream function and set a end point? Can someone provide an example how to code this to see the stream of the temprature in the dashboard?

Thanks!

Hi!

If you get the sketch working and you can read the value in the API, the good news is that you don’t need to code anything additional to display the value in the dashboard! Just create a widget in the dashboard, and select the device and resource as source of data.

You can see an example of creating a dashboard in the following video. Please, take special attention when configuring the device and resource. If you have your device connected, you will be able to select it and its resource.

hm for some strange reason the dashboard seems down. Because I map the resources and the API is working perfectyl I am using your same code. The dashboard gets no input points. Also I have tried to delete and create a new dashboard.

I see the resources, I select my wemos board, i select dht11 en then I select Celcius and set the interval to 1.

I am I doing something wrong? API overview works perfectly.

Thank you wold love to hear from you. Great platform!

This should be enough for get it working. What browser are you using? Are you using the latest Arduino library version? (starting at 2.0 at least).

1 Like

Great it works now. It was due to the fact that I installed using the Arduino interface and that one installed the library v1.1.0! I read somewhere on the internet that I could install it this way.

Anyway I upgraded the library to the latest version and now the dashboard works.

Great software I hope to contribute to this community by writing some tutorials in the future.

Thanks!

Great @Pieter! I am glad you get it working! The dashboard is a feature starting with the 2.0 libraries, so this was the issue :slight_smile:

It would be really awesome if you contribute creating tutorials or posting sample IoT projects! This is quite important for people starting with the platform. We have created some of them, but it is never enough!

Let me know if you need more help!

1 Like

Thanks I will post some projects I am working on already and trying to integrate it with thinger.io

Love to get more involved.

Hi, my device is connected but dashboard doesn’t show any information… I don’t know what is the problem. You can also answer me to danilchuk.89g@gmail.com

I will be glad for any help

Thank you

Hi. Thanks, but you are the grate tutorial, I have a problem connecting the arduino and the platform, I used the debug output and it showed:

  >   NETWORK] Ethernet Initialization ...
>     [NETWORK] Has IP address: 192.168.1.78
>     [NETWORK] Connected!
>     [_SOCKET] Connecting to iot.thinger.io:25200 ...
>     [_SOCKET] Using secure TLS / SSL connection: no
>     [_SOCKET] Connected!
>     [THINGER] Authentication. User: thinger Device: arduino
>     [THINGER] Type Bytes: 35 [OK]
>     [THINGER] Authentication error! Check the user name, device ID, or device credentials.
>     [_SOCKET] Now it's closed!

I understood that the arduino connected with Thinger but showed an authentication error, checked the parameters and created a new ones having the same results. Any suggestion of why this happens ?, thank you. Greetings.

Hi @TelematicEng, it seems that you are using the user "thinger" to connect your device. Please, use your username in the platform. Also set the device ID you registered in the platform for your device. Now you should have a device with ID arduino. Is it?

Best.

Thanks @alvarolb for your quick response, that’s right, I tried again with another user, I created a new user “telematic” on the platform and added a device (device_id: arduino, credentials: 12345678a), I modified the arduino code and uploaded it , but the results were the same, I attached some details, I am new to thinger and I would greatly appreciate the help.

[NETWORK] Starting connection...
[NETWORK] Initializing Ethernet...
[NETWORK] Starting connection...
[NETWORK] Initializing Ethernet...
[NETWORK] Got IP Address: 192.168.1.78
[NETWORK] Connected!
[_SOCKET] Connecting to iot.thinger.io:25200...
[_SOCKET] Using secure TLS/SSL connection: no
[_SOCKET] Connected!
[THINGER] Authenticating. User: telematic Device: arduino
[THINGER] Writing bytes: 38 [OK]
[THINGER] Auth Failed! Check username, device id, or device credentials.
[_SOCKET] Is now closed!

Already resolved, I just had to set the IP address and connect the arduino, although in the example of the library does not show it necessary, still thanks for the support, regards.

#define THINGER_SERVER "192.168.1.78"

Ah! You should mentioned that you were using a custom deployment! :wink:

Hello sir. The program works fine but i need it to activate a relay at a certain temperature. can you help me? Thanks.

Two Ways you can read the sensor reading into a variable ie

int tempReading = dht.readTemperature();

if (tempReading >= triggerTemperature){
digitalWrite(RELAY, HIGH);
}else{
digitalWrite(RELAY, LOW);
}

I hope this helps

Thanks for responding. I have successfully achieved that part.

Regards.

Hi There, thanks for the tutorial.

I am having an issue with my code however:

Using Uno Board with Duinotech Ethernet Shield and DHT11 sensor, getting this error:

Arduino: 1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board: "Arduino/Genuino Uno"

In file included from C:\Users\user\Documents\Arduino\libraries\libraries\thinger_trial_1.2\thinger_trial_1.2.ino:2:0:

C:\Users\user\Documents\Arduino\libraries\libraries\libraries\DHT_sensor_library/DHT_U.h:25:29: fatal error: Adafruit_Sensor.h: No such file or directory

compilation terminated.

exit status 1

Error compiling for board Arduino/Genuino Uno.

Any Ideas??

You have issues with sensor’s library, I would try to reinstall it.

Thanks, I tried an earlier version of the library and it worked.

Just on case anyone needs this in future with the code the Library is DHT sensor library v 1.2.3 not the latest

:smiley: