B4R Tutorial DHT11 & DHT22 temperature and humidity sensors ARDUINO UNO Code

This B4R example/library works with DHT11 and DHT22 temperature and humidity sensors:

This inexpensive device (DHT11) allows for measure temperature and humidity:
  • Very Low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 4 pins with 0.1" spacing
This cheap device (DHT22) allows also for measure temperature and humidity:
  • Low cost
  • 3 to 5V power and I/O
  • 2.5mA max current use during conversion (while requesting data)
  • Good for 0-100% humidity readings with 2-5% accuracy
  • Good for -40 to 125°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1" spacing


DHT11 & DHT22
dht11.jpg


DHT22-PinOut.png

DHT11 & DHT22
DHT11+UNO_bb.jpg

Attached an example that reads DHT11 sensor (on Arduino pin 4) and DHT22 sensor (on pin 3) at same time
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public DHT11sensor As dht                'DHT11 sensor
    Public DHT22sensor As dht                'DHT22 sensor
    Public Timer1 As Timer                    'Timer for reading DHT11 measures at desired interval
    Public DHT11pin,DHT22pin As Pin            'Arduino Pin connected to DHT11 signal
    Public Interval As Int                    'Interval as seconds


    Dim humidity,temperature As Double        'Humidity/Temperature DHT11 readings

End Sub

Private Sub AppStart

    Serial1.Initialize(115200)
    Log("AppStart")

    Interval=2                                        '2 sec Timer Interval (for DHT22 measures)                            
    DHT11pin.Initialize(4,DHT11pin.MODE_INPUT)        'Initialize at Arduino Pin 4
    DHT22pin.Initialize(3,DHT22pin.MODE_INPUT)        'Initialize at Arduino Pin 3
    Timer1.Initialize("Timer1_Tick",Interval*1000)    
    Timer1.Enabled=True                                'Begin Timer

End Sub


Sub Timer1_Tick

    DHT11sensor.Read11(DHT11pin.PinNumber)            'Reading the DHT11 measure
    humidity=DHT11sensor.GetHumidity                'Get humidity from readed measure
    temperature=DHT11sensor.GetTemperature            'Get temperature from readed measure
    Log("DHT11 ","Humidity = ",humidity, " %", "  Temperature =",temperature, " Cº")

    Delay(200)
    DHT22sensor.Read22(DHT22pin.PinNumber)            'Reading the DHT22 measure
    humidity=DHT22sensor.GetHumidity                'Get humidity from readed measure
    temperature=DHT22sensor.GetTemperature        'Get temperature from readed measure
    Log("DHT22 ","Humidity = ",NumberFormat(humidity,2,1), " %", "  Temperature =",NumberFormat(temperature,2,1), " Cº")

End Sub
DHT11-DHT22 logs.jpg


In attached file (rdht.zip), you'll find the B4R library and the Arduino lib (dhtlib.zip)
DHT is not a lib included on Arduino Skecth and it seems there're two ways to get working a library not included in the Arduino default libraries:
1 Option) Download the zip from https://github.com/adafruit/DHT-sensor-library and install on Arduino IDE with Sketch Menu --> Include Library --> Add Zip library or Sketch Menu --> Include Library --> Manage Library and do a Search for it. After, descompress the rDHT.zip on B4R Libraries Folder

2 Option) Modifying the rDht.h and rDht.xml for search the Arduino library (Dht.h and Dht.h) on B4R folder (it won't work on Arduino IDE) --> See posts #8-#13

For now, I'm more comfortable with first option as it permits to test the Arduino with several ino programs before to testing on B4R, but the choice it's yours.

rDhtlib v2.00:
- Included DHT11 and DHT22 sensors (decompress to B4R Libraries folder)
 

Attachments

  • upload_2016-4-22_12-12-25.png
    upload_2016-4-22_12-12-25.png
    11.7 KB · Views: 1,293
  • DHTlib.zip
    10.1 KB · Views: 1,283
  • rDHTv2.zip
    1.2 KB · Views: 1,090
Last edited:

ingo.tw

Member
Licensed User
Longtime User
In file included from D:\B4R\dht\Objects\src\src.ino:1:0:
sketch\B4RDefines.h:17:17: fatal error: dht.h: No such file or directory
#include <dht.h>
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.
 

inakigarm

Well-Known Member
Licensed User
Longtime User
I don't have the computer to test it but I think it' could be because you haven't download the dht lib from the Arduino sketch (if this is the error it's my fault for not described at first post- will be modifies)
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

can't get this example to run. Steps taken:
  • Downloaded the example including the dht library
  • Created an additional Libraries Folder C:\Daten\b4\b4r\libraries
  • Created subfolder C:\Daten\b4\b4r\libraries\rDHT
  • Copied rDHT.xml to C:\Daten\b4\b4r\libraries
  • Copied dht.h, dht.cpp, rDHT.h and rDHT.cpp to C:\Daten\b4\b4r\libraries\rDHT
  • Created sample project based on code in post #1
When compiling received error message below.

Please advice how to setup additional libraries? What is missing in the steps taken as outlined?

Compiler Error Message
B4X:
B4R version: 1.00 BETA 7
Parsing code.    (0.00s)
Compiling code.    (0.02s)
Building project    (0.07s)
Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 (cpu=atmega2560) - COM3)    Error

In file included from C:\Daten\b4\b4r\b4rhowto\B4RHowToDHT11\Objects\src\src.ino:1:0:
sketch\B4RDefines.h:17:17: fatal error: dht.h: No such file or directory
#include <dht.h>
                 ^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Based on replies, tested following:
1) In the Arduino IDE build sample sketch dht11 with an imported DHT library (in folder arduino/sketchbook/libraries/DHT) containg dht.h and dht.cpp. Sketch running OK.
2) B4R: Changed "depends on" in rDHT.h into "include" in the B4R additional libraries folder C:\Daten\b4\b4r\libraries\rDHT.
When compiling, receive bunch of error messages. See below ERROR Step 2.
3) Deleted the Arduino DHT library (see step 1). When compining, received Error Message can not find dht.h --- although in rDHT folder. See ERROR Step 3.
4) Add back in the Arduino DHT library (see step 1) and deleted from B4R rDHT libraries folder the files dht.h and dhp.cpp, so the rDHT.h and cpp remain.
The program is running OK:
B4X:
AppStart
Humidity = 39 %  Temperature =20 Cº
Humidity = 39 %  Temperature =20 Cº

ISSUE: it seems that there is a conflict when the same libraries (h and cpp files) are installed in the Arduino IDE and in the B4R additional libraries folder.

Any advice how to resolve?

ERROR Step 2
B4X:
B4R version: 1.00 BETA 7
Parsing code.    (0.00s)
Compiling code.    (0.11s)
Building project    (0.07s)
Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 (cpu=atmega2560) - COM3)    Error

libraries\DHT\dht.cpp.o: In function `dht::_readSensor(unsigned char, unsigned char, unsigned char)':
C:\Daten\arduino\sketchbook\libraries\DHT/dht.cpp:104: multiple definition of `dht::_readSensor(unsigned char, unsigned char, unsigned char)'
sketch\dht.cpp.o:sketch/dht.cpp:104: first defined here
c:/daten/arduino/ide/hardware/tools/avr/bin/../lib/gcc/avr/4.8.1/../../../../avr/bin/ld.exe: Disabling relaxation: it will not work with multiple definitions
libraries\DHT\dht.cpp.o: In function `dht::_readSensor(unsigned char, unsigned char, unsigned char)':
C:\Daten\arduino\sketchbook\libraries\DHT/dht.cpp:104: multiple definition of `dht::read11(unsigned char)'
sketch\dht.cpp.o:sketch/dht.cpp:104: first defined here
libraries\DHT\dht.cpp.o: In function `dht::_readSensor(unsigned char, unsigned char, unsigned char)':
C:\Daten\arduino\sketchbook\libraries\DHT/dht.cpp:104: multiple definition of `dht::read(unsigned char)'
sketch\dht.cpp.o:sketch/dht.cpp:104: first defined here
collect2.exe: error: ld returned 1 exit status
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.

ERROR Step 3
B4X:
B4R version: 1.00 BETA 7
Parsing code.    (0.00s)
Compiling code.    (0.11s)
Building project    (0.07s)
Compiling & deploying Ino project (Arduino/Genuino Mega or Mega 2560 (cpu=atmega2560) - COM3)    Error

In file included from C:\Daten\b4\b4r\b4rhowto\B4RHowToDHT11\Objects\src\src.ino:1:0:
sketch\B4RDefines.h:17:17: fatal error: dht.h: No such file or directory
#include <dht.h>
                 ^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Mega or Mega 2560.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Thanks - thats it = been a good learning for building B4R additional libraries, means:

Creating a B4R additional library usign an external library and do not want a dependency on the Arduino Library Folder, use "#include" and not "depends on".
Ensure "depends on" is not in the XML file.
Example for this case:
B4X:
#pragma once
#include "B4RDefines.h"
#include "dht.h"
namespace B4R {
...

The B4R additional libraries folder has the same structure as the B4R internal libraries folder.
Example for this case:
...\b4r\libraries\rDHT.xml
...\b4r\libraries\rDHT\rDHT.h, rDHT.cpp and the external library files dht.h and dht.cpp.
 

janderkan

Well-Known Member
Licensed User
Longtime User
I am so sorry for being so bad to read this thread.

I have done like @rwblinn described:
...\b4r\libraries\rDHT.xml
...\b4r\libraries\rDHT\rDHT.h, rDHT.cpp and the external library files dht.h and dht.cpp.
Changed \b4r\libraries\rDHT\rdht.h file from //~dependson: <dht.h> to #include "dht.h"
and
Made sure that I did not have the library included in Arduino

Done like @Erel described
Removed from \b4r\libraries\rDHT.xml the second last line <dependsOn><dht.h></dependsOn>

I created a new projekt and included rDht library.
Added 1 line:
Public DHTsensor As dht

Getting the following error:

B4R version: 1.00
Parsing code. (0.00s)
Compiling code. (0.08s)
Building project (0.08s)
Compiling & deploying Ino project (Arduino/Genuino Uno - COM7) Error

In file included from sketch\B4RDefines.h:20:0,
from sketch\B4RArduino.cpp:1:
sketch\rDht.h:10:4: error: 'dht' does not name a type
dht Dht;
^
exit status 1
Error compiling for board Arduino/Genuino Uno.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Hi,

please find attached a test project including the rDHT library (for several tests used).
Could you test if this is working?
 

Attachments

  • b4rhowtodht11.zip
    131.4 KB · Views: 585

janderkan

Well-Known Member
Licensed User
Longtime User
Hi,

The test from @rwblinn worked fine, however it is a DHT22 sensor I have.

So I replaced rDht.xml, rDHT\rDHT.h and rDHT\rDHT.cpp with the files from the first post from @inakigarm

I replaced the files rDHT\dht.cpp and rDHT\dht.h with files downloaded from Arduino.

Did not work.

Found that files from Arduino was named DHT.cpp and DHT.h
In these files the DHT class is with capital letters, and the files comes from Adafruit

Renamed DHT.cpp and DHT.h to DHT_ada.cpp and DHT_ada.h
Found the latest files from Rob Tillaart (v1.21) and copied them to Addlibraries\rDHT

Did not work.

Found that both the Ada files and the Rob files was in the \objects\src folder of my B4R project.
Removed DHT_ada.cpp and DHT_ada.h from Addlibraries\rDHT.

Wuhuu, It works.

Attached the files I use
 

Attachments

  • rDHT.zip
    4.1 KB · Views: 520

janderkan

Well-Known Member
Licensed User
Longtime User
Now I have tried using the DHT22 with a Wemos Mini.
I replaced the initialization with this:

Dht22Pin.Initialize(EspPins.D5,Dht22Pin.MODE_INPUT)

But when compiling I get this error:

In file included from sketch\dht.h:19:0,
from sketch\dht.cpp:41:
sketch\dht.cpp: In member function 'int8_t dht::_readSensor(uint8_t, uint8_t, uint8_t)':
C:\Users\LapTop\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266/Arduino.h:226:63: error: cannot convert 'volatile uint32_t* {aka volatile unsigned int*}' to 'volatile uint8_t* {aka volatile unsigned char*}' in initialization
#define portInputRegister(port) ((volatile uint32_t*) &GPI)
^
sketch\dht.cpp:121:29: note: in expansion of macro 'portInputRegister'
volatile uint8_t *PIR = portInputRegister(port);
^
exit status 1
Error compiling for board WeMos D1 R2 & mini.
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Below code successfully tested on a NodeMCU 0.9. Attached the rDHT Library v2.

B4X:
#Region Project Notes
' B4R HowTo Project: DHT22 Sensor connected to ESP8266 NodeMCU 0.9
' Libraries: rDHT v2.00
' Connection (looking at the white sensor from right to left)
' DHT22 = NodeMCU (Wirecolor)
' GND = GND (black)
' NC
' Signal = D2 (SDA,DATA) (white)    Note: According DHT22 documentation put a 10K resistor between VCC and the DATA
' VCC = 3.3v (red)
' Example Output:
' humidity = 59.70 %  temperature =21.80 DegC#
' humidity = 65.40 %  temperature =22.10 DegC
#End Region

Sub Process_Globals
    Public Serial1 As Serial
    Public DHT22sensor As dht                       
    Public Timer1 As Timer                             
    Public DHT22pin As Pin                             
    Public Interval As Int                                
    Dim humidity,temperature As Double         'DHT22 readings
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Interval=5                                            '5 sec Timer Interval                                 
    DHT22pin.Initialize(4,DHT22pin.MODE_INPUT)            'Initialize at NodeMCU Pin D2 (SDA,DATA)
    Timer1.Initialize("Timer1_Tick",Interval*1000)         
    Timer1.Enabled=True                                
    Log("AppStart NodeMCU DHT22 Experiment")
End Sub

Sub Timer1_Tick
    DHT22sensor.Read22(DHT22pin.PinNumber)             
    humidity=DHT22sensor.GetHumidity                    
    temperature=DHT22sensor.GetTemperature                
    Log("Humidity = ",NumberFormat(humidity,0,2), " %", "  Temperature =",NumberFormat(temperature,0,2), " DegC")
End Sub
 

Attachments

  • rDHT-v2.zip
    4.1 KB · Views: 616
Top