I see that inside of your
float getTemperatureDHT22(int DHT22_PIN) {
function, you are aware that the DHT22 requires waiting 2 seconds before taking consecutive reads, and you do account for that at the beginning of the procedure. However, at the bottom of the
if(errorCode==DHT_ERROR_NONE) {
block, you are making two consecutive reads of the DHT22 without any delay:
...
Serial.println(myDHT22.getTemperatureC());
return myDHT22.getTemperatureC();
}
Either make one call, saving the return value in a local variable, and print and return that variable, or just comment out the Serial.println(myDHT22.getTemperatureC());
and see how you make out.
Hopefully that helps.
EDIT:
===============
Some clarification is required because this could be library dependent. In the version of the DHT22 library that I'm using, the only call that actually interrogates the sensor for the above code would be
DHT22_ERROR_t errorCode = myDHT22.readData();
The 2 second delay requirement between consecutive calls relates to calls to that method. In contrast, calls to getTemperatureC()
and getHumidity()
are only returning DHT22 class variables. Those calls are not interrogating the sensor and do not require a delay, again in the library that I am now using.
Check your version of the DHT22.cpp library file to be absolutely sure. If the getTemperatureC()
does nothing except return _lastTemperature;
then consecutive calls to getTemperatureC()
and getHumidity()
do not require additional delays. Just remember that they will not be updated until the next call to readData()
.
===============
Considerations for Ethernet shield and DHT22 pin selection:
The next thing I noticed is that you are using Pin 10 for one of your sensors. You stated you are using a Mega. Although you don't specify, I'm going to assume you are using Rev 3 of the Mega, and the Arduino Ethernet shield (wired) based on the Wiznet W5100 ethernet chip (hopefully this assumption is correct, as the following is applicable to that hardware). Requirements of the Ethernet shield on the Uno and Mega 2560 R3 are:
On both boards, pin 10 is used to select the W5100 and pin 4 for the SD card. These pins cannot be used for general I/O. On the Mega, the hardware SS pin, 53, is not used to select either the W5100 or the SD card, but it must be kept as an output or the SPI interface won't work. http://arduino.cc/en/Main/ArduinoEthernetShield
Therefore, the next things to try next are
- Move the DHT22 off of pin 10
- Set pin 53 as an output
- If using the above Ethernet shield, and you are not using an SD card, set pin 4 to output and write it high (see link above)
Then please report back.