I am trying to build a data collector for my garden. Currently, I am struggling to get data out of a DHT11 and using an ESP8266 ESP-01S to eventually send data to ThingSpeak.
I broke each step apart and was able to accomplish each task individually.
I was able to connect the ESP8266 ESP-01 to the Arduino Uno (pulling off the microcontroller) and write data to ThingSpeak with the board changed to “Generic ESP8266 Module”. I was also able to collect data from the DHT11 connected to my Uno with the microcontroller back on the Uno and the board set to “Arduino Uno” in my IDE.
Now when I add both the peripherals together and merge my code on the board “Generic ESP8266 Module”, I cannot read from the DHT11. My humidity and temperature both respond back “nan”. I have tried keeping the microcontroller attached to the Uno and have pulled it off as well with the same results. (If I understand correctly, the ESP8266 is doing all the work and the Uno is now the middle man for connections?)
Below is my code. I'm trying to just get a reading on the DHT to print on the serial monitor through the ESP8266.
#include <ESP8266WiFi.h>
#include "DHT.h"
int status = WL_IDLE_STATUS;
WiFiClient client;
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
dht.begin();
delay(3000);
}
void loop() {
delay(5000);
float h = dht.readHumidity();
float f = dht.readTemperature(true);
Serial.print("This is Humidity and Temp: ");
Serial.print(h);
Serial.print(" and ");
Serial.println(f);
delay(5000);
}
My ESP8266 has the RX and TX connected to the RX and TX of the Uno. The 3v and EN of the ESP8266 is connected to my breadboard that is connected to the 3v of the Uno. And the ground is connected to the ground.
My DHT has the data pin connected to A0 and VCC is connected to the 5v of my Uno. And ground is connected to ground.
Does anyone know why I cannot read from the DHT11? Thanks.
Yes, the pieces will work separately but not when combined. Why?
When you remove the ATmega328 and connect the ESP8266-01 rx/tx is connected to pins 0/1 of the Uno board, the ESP8266 is communicating through the USB to serial converter ic. That’s fine, but...
When you re-install the ATmega328, you have a conflict. You cannot have the ATmega328 serial port on pins 0/1 talking to both the 8266 and the USB converter chip. Somebody wins, somebody loses.
You need to move the ESP8266 serial tx/rx pins to different I/O pins and use software serial to communicate with ESP8266-01. That’s how you make the Uno processor, the ATmega328, communicate with both the serial to USB converter and the ESP8266-01.
A follow-up thought is that the combination of the ESP8266-01 and an Uno is a rather weak and problematic lash-up with a heap of learning curve thrown in for free.
A Nano 33 IoT board would be a far better solution. If bottom line cost is the issue, a Wemos D1 is an okay compromise with less I/O and only one analog input.
If temp/humidity only is required, this can be achieved with esp-01 alone. You can use the Uno (with chip removed) to upload code to the esp-01 and not include the Uno in the final project. The esp-01 has 2 extra gpio pins which could drive the dht11.
However, dht11 is not a great sensor, not very accurate or robust for outdoor use. If you replace it, choose a senor with i2c interface. This means that as well as better sensor, you can add more sensors and more digital or analog i/o pins, displays etc. later to expand your project.
I have found when using an esp8266 the standard "dht.h" library is very unreliable, giving lots of false or error readings but with the "DHT_sensor_library_for_ESPx" library they work great
I have done this with every bare ESP8266 module I've programmed (quite a few), and had no problems.
Could be that "Serial.begin()" sets those MCU pins. Don't know.
Leo..
Perhaps you're right. I'm pretty sure Serial.begin() sets TX as OUTPUT, but with a blank sketch running on the atmega, it will remain an INPUT.
Normally when you upload code, the Arduino gets reset, and then the bootloader would set TX as an output. But perhaps if the target board is set to "Generic ESP8266", no reset is done, it relies on the user setting the esp into program upload mode.