Today I spent multiple hours to find out why my ESP8266 doesn't get any temperature and humidity data. I checked the cabeling multiple times, 10k resistor - was OK. I used a powerful power supply - no data. I connected the DHT22 to 5V - no data. I replaced the DHT22 with another one - no data. I found a thread in the Arduino forum which discussed the same issue and there somebody wrote I should use D2 instead of 2 for the pin definition. I thought D2 is GPIO pin 2. When I changed the code to use D2 I got an error D2 is not defined. I used the
generic ESP8266 board model in Arduino until now. Then I switched to
NodeMCU 1.0 (ESP 12E Module)(I use NodeMCU Lua Lolin V3 Modul with ESP8266 12E) and the compile was successfull. Now I was able to retrieve data from the DHT22. Now I also noticed you have to be carefull when you use the devkit names, e.g. D2. It's not identical to the pin numbers. For my dev board D2 is pin 4. So I can use D2 or 4 to define the pin in the code.
Summary: Either use the predefined names for the dev boards D1, D2 ... or look up the exact GPIO pins of the board you use. And make sure you select the correct board module for your board.