Strange ESP8266 and DHT problem, But found work around?!

Hi Everyone,

I am learning how to get the ESP8266 to talk to the DHT22. I am using the Arduino IDE with the "Adafruit Unified Sensor" library v. 1.0.3, the "DHT sensor library" v. 1.3.4, and the ESP8266 stable package 2.4.1 running on Windows 7.

The example code File->Examples->DHT sensor library->DHTtester.ino

I compiled it and loaded it just fine. But, to my surprise, I had to send the DHT data to D4 instead of D2 as on the Arduino Uno R3. I found that by trial and error on the ESP8266 ESP-12E MakerFocus boards found on AMazon. I also got the LoLin ESP-12E boards that are not exactly the same pinout as the MakerFocus board and thus the motivation for mapping out the pins on both boards with the DHTtester code...

Both boards used D4 for "DHTPIN 2". Next, I set "DHTPIN 3" and did not immediately find that one but when I set "DHTPIN 4", I found it went to D2 on both boards. So far so good.

Then I set "DHTPIN 5" and got D1 on the LoLin board. Then I set "DHTPIN 6" and the LoLin kept rebooting or something. Then I set "DHTPIN 7" and got a "WDT Reset" (had to change serial monitor to 115200 baud).

"DHTPIN 8"... It flashed. But nothing at all, if memory serves. I tried reflashing back to the original "DHTPIN 2" but it would not longer flash!! I was starting to get a bit worried at this point.

So I tried holding down the flash button and then pressing trhe reset button on the LoLin ESP-12E and then retried flashing but kept getting the could not connect error. So I thought I had bricked the board! So I rebooted the laptop and ran arduino IDE by itself and tried again but no cigar!

FINALLY I unplugged the LoLin from the USB port and THEN held down the flash button on the board and held it down while I plugged it into the USB port and ran the upload one more time and PRAISE THE LORD!!! It flashed!

So I thought I would pass this tidbit along as it was not at all obvious to me how to bring this board back to life!!

If you can figure out which is the correct one to select from the Tools > Board menu for your board, you can refer to the pins using the Dn notation, as written on the silkscreen of the board. This was quite confusing to me when I first started using one of these boards, as I am used to only using the number to refer to pins with the classic Arduino boards, even when the pins are marked on the silkscreen as Dn (e.g. Arduino Nano).

When you use straight numbers to refer to pins in your ESP8266 code, those are referring to the GPIO numbers on the ESP8266. Some board manufacturers decided to number the pins on their boards in a way that doesn't match the GPIO numbers. The ESP8266 core for Arduino handles this by defining a variable named according to the Dn notation on the silkscreen to the GPIO pin number of that pin in the variant file. You can find the variant files in the variants subfolder of the ESP8266 hardware package, or on its repository:

For example, this is the pin mapping of the "d1_mini" variant:

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t RX   = 3;
static const uint8_t TX   = 1;

The variant used by each board is defined in boards.txt, which is in the root folder of the hardware package, or here:

For example, you can see that the "WeMos D1 R2 & mini" board uses the d1_mini variant:

d1_mini.name=WeMos D1 R2 & mini
d1_mini.build.board=ESP8266_WEMOS_D1MINI
d1_mini.build.variant=d1_mini