ESP-01S with DHT11 Temperature reading inaccurate by +23 Deg C

I’ve put together a flashing circuit and sketch (it’s seemed to have worked). The purpose of which is to read the ambient temperature and send it on to a MQTT server within my local network via WiFi. The temperature reading my MQTT server receives is over by about 23 C. The circuit reads about 33 C, and my house is about 17 C at most. I appreciate the DHT11 is only accurate to +/- 2 C but this reading is well over specification.

I’m using a DHT11 sensor module, coupled to an ESP-01S micro-controller. The sketch is something I’ve written myself to publish MQTT messages to a server. The connection to the Wifi network and Publishing of messages works as I’d expect. Power is a from a cheap variable bench PSU reading about 5.14V.

The source code for my sketch is as follows

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>

#define DELAY_TIME 10000
#define DELAY_TIME_SHORT 500

// Wifi Config
const char* ssid = "********";
const char* password = "********";

// MQTT Server config
const char* mqttServer = "********";
const int mqttPort = ****;
const char* mqttUser = "********";
const char* mqttPassword = "********";

// DHT config.
#define DHTTYPE     DHT11
#define DHTPIN      2

// DHT Setup
DHT dht(DHTPIN, DHTTYPE);
float humidity, temp;     // Values read from sensor
String bufString = "";    // String to display
char buf[128];            // Char array for MQTT

// Classes for Comms
WiFiClient espClient;
PubSubClient client(espClient);

void setup()
{
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(DELAY_TIME_SHORT);
  }

  client.setServer(mqttServer, mqttPort);

  // Connect to MQTT server
  while (!client.connected()) {
    if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
      client.publish("esp/temp", "Init ESP8266");
    }
  }
  delay(DELAY_TIME_SHORT);

  // Initialize DHT11 device.
  dht.begin();
}

void loop()
{
  delay(DELAY_TIME);
  humidity = dht.readHumidity();        // Read humidity (percent)
  temp = dht.readTemperature(false);    // Read temperature as Celcius
  bufString = "Temp: " + String((int)temp) + " C, Humidity: " + String((int)humidity) + "%";

  bufString.toCharArray(buf, 128);
  client.publish("esp/temp", buf);
}

or can be seen here - https://pastebin.com/9YmXsEsb

Images of the Sensor/Module, my writing environment, and the MQTT messages can be seen here - ESP-01S with DHT11 Temperature reading inaccurate by +23 Deg C - Album on Imgur

Expected readings would have been around 17 C - 20 C. Using Arduino 1.8.9 on Arch Linux, DHT sensor library (by Adafruit) is at 1.3.0

I’m quite inexperienced with Arduino, MQTT, and these kind of projects (although I did a bit of Digital Electronics nearly 20 years ago) so forgive me if I’ve missed the obvious.

Thanks for your help

What did it show when you printed your 'temp' and 'humidity' variables directly on the Serial Monitor?

gfvalvo: What did it show when you printed your 'temp' and 'humidity' variables directly on the Serial Monitor?

I could never get serial monitor to work with those ESP-01S. I tried every combination of baud rate and option I could think of. Also I'm not sure how I would get the serial communication connected with the temp module installed - https://imgur.com/a/Bi0Xd5E

Do you suspect the conversion from float to string (fair point, I was unsure)? So as a test (just now) I amended the main loop as follows;

void loop()
{
 delay(DELAY_TIME);
 humidity = dht.readHumidity();        // Read humidity (percent)
 temp = dht.readTemperature(false);    // Read temperature as Celcius
 temp = 17.23;                         // Test hard code value

 bufString = "Temp: " + String((int)temp) + " C, Humidity: " + String((int)humidity) + "%";

 bufString.toCharArray(buf, 128);
 client.publish("esp/temp", buf);
}

Note that I've added a line to set temp to 17.23 prior to sending the MQTT message. The message was received as "Temp: 17 C, Humidity: 56%". (https://imgur.com/a/s4Q6DjT). It would seem that the value of the temp variable is being reported correctly.

OK, that's the other test I was going to suggest. It would be nice to confirm it by also printing the DHT data. But, it's pretty good evidence.

Next step would be to provide a GitHub link to the exact DHT library you are using as there are several with that name.

I appreciate your help. The library is Adafruit DHT-Sensor-library, this is the details of the library.properties file

name=DHT sensor library
version=1.3.0
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
paragraph=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
category=Sensors
url=https://github.com/adafruit/DHT-sensor-library
architectures=*

One thing I noticed to get you the above is that the timestamps are 2016, and that the current version on https://github.com/adafruit/DHT-sensor-library/releases is a lot more recent. I’m going to try getting the latest version with git rather than a download zip (which I did this afternoon).

I’m now using the latest version of the library, and seeing temperatures started at 26 C, after two minutes it seems to be at 29 C. Looks like its passing 30 C as I write this. This is the library.properties file

name=DHT sensor library
version=1.3.8
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
paragraph=Arduino library for DHT11, DHT22, etc Temp & Humidity Sensors
category=Sensors
url=https://github.com/adafruit/DHT-sensor-library
architectures=*
depends=Adafruit Unified Sensor

The other libraries I’ve installed are;

4.0K drwxr-xr-x 6 jenkin5 users 4.0K Jan  4 23:16 .
4.0K drwxr-xr-x 4 jenkin5 users 4.0K Jan  4 10:42 ..
4.0K drwxr-xr-x 4 jenkin5 users 4.0K Dec 29 07:13 Adafruit_Sensor
4.0K drwxr-xr-x 3 jenkin5 users 4.0K Jan  4 20:04 Adafruit_ST7735_and_ST7789_Library
4.0K drwxr-xr-x 5 jenkin5 users 4.0K Jan  4 23:15 DHT
4.0K drwxr-xr-x 5 jenkin5 users 4.0K Dec  8 20:12 PubSubClient
4.0K -rw-r--r-- 1 jenkin5 users   87 Nov  7 13:06 readme.txt

This is something interesting. I moved the ESP-01s board away from the DHT11 module using a load of wires. It's been running now for about 15 minutes, and has been reporting a consistent 26 C for the most of that time.

|500x375 https://imgur.com/a/WR8Igd3

I'm measuring the room at 19 C using one thermometer, and 20 C on my 3D printer.

So there is an element of the ESP-01s sitting on top of the DHT11 that is throwing the reading off significantly. This style of packaging is obviously a weakness. However unless I'm mistaken, the fact its reading 26 C while true ambient is 20 C is still outside the +/- 1% tolerance the DHT11 should be within.

Is there a calibration process I can do to rectify this?

Without the ability to do Serial debug prints you're working blind. Guess I'd try the sensor on a different processor where you can print intermediate results. Maybe try a couple sensors with different processor boards. After that I'd probably dig into the datasheet and instrument the library code with debug prints until I determined whether the problem is the hardware or the code.

I haven't had any issues with the Adafruit library using a DHT22.