Go Down

Topic: DHT22 Initial Startup Error (Read 322 times) previous topic - next topic

1CM69

Hi all,

I am very new to Arduino and i have been trying to implement some code for ESP Now on a Master & Slave board (Wemos D1 Mini).

It is almost working exactly as I require it except for an error in reading from my DHT22 on initial power up, Temperature shows nan & Humidity shows a string of numbers.

After this initial error the program then runs perfectly correctly, currently restarting the ESP every 10s for testing purposes.

It is only if the power supply to the Master is interrupted that the first reading following this shows the error.

I understand that there is a minimum start up time for the sensor and to try and get around this issue I have attempted to place a delay in the code but having tried placing the delay in numerous places, all I achieved was a longer uptime for the sendreading function.

The code I am using will be very familiar to many folks on here as many internet searches kept bringing me back to the authors GitHub.

Here is the Master:

Code: [Select]
/*
 Testing ESP-Now
 See https://espressif.com/en/products/software/esp-now/overview
 ESP-Now enables fast lightwieght connectionless communication between ESP8266's.
 So for example a remote sensor node could send a reading using ESP-Now in just a few
 hundred milliseconds and deepSleep the rest of the time and so get increased battery
 life compared to the node using a regular WiFi connection which could take 10 times
 as long to connect to WiFi and send a sensor reading.
 
 ESP-Now has the concept of controllers and slaves. AFAICT the controller is the remote
 sensor node and the slave is the always on "gateway" node that listens for sensor node readings.
 **** This sketch is the controller/sensor node ****
 *** Note: to compile ESP-Now (with arduino/esp8266 release 2.3.0) need to edit
 * ~/Library/Arduino15/packages/esp8266/hardware/esp8266/2.1.0/platform.txt
 * Search "compiler.c.elf.libs", and append "-lespnow" at the end of the line.
 * See: http://www.esp8266.com/viewtopic.php?p=44161#p44161
 ***
 Ant Elder
 License: Apache License v2
*/
#include <ESP8266WiFi.h>
#include "DHT.h"
extern "C" {
  #include <espnow.h>
}

// this is the MAC Address of the remote ESP which this ESP sends its data too
uint8_t remoteMac[] = {0x86, 0xF3, 0xEB, 0X62, 0x71, 0xEC};

#define WIFI_CHANNEL 1
#define SLEEP_TIME 15e6
#define SEND_TIMEOUT 10000

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

uint8_t DHTPin = D2;
               
DHT dht(DHTPin, DHTTYPE);

// keep in sync with slave struct
struct SENSOR_DATA {
    float temp;
    int hum;
    float t;
};

volatile boolean readingSent;

void setup() {
  pinMode(DHTPin, INPUT);
  dht.begin();

  Serial.begin(115200); Serial.println();

  WiFi.mode(WIFI_STA); // Station mode for sensor/controller node
  WiFi.begin();
  Serial.print("This node mac: "); Serial.println(WiFi.macAddress());

 

  if (esp_now_init()!=0) {
    Serial.println("*** ESP_Now init failed");
    ESP.restart();
  }

  delay(1); // This delay seems to make it work more reliably???

  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);

  esp_now_register_send_cb([](uint8_t* mac, uint8_t status) {
    Serial.print("send_cb, status = "); Serial.print(status);
    Serial.print(", to mac: ");
    char macString[50] = {0};
    sprintf(macString,"%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
    Serial.println(macString);

    readingSent = true;
  });

  sendReading();
}

void loop() {
  if (readingSent || (millis() > SEND_TIMEOUT)) {
    Serial.print("Going to sleep, uptime: "); Serial.println(millis()); 
    delay(10000);//ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
    ESP.restart();
  }
}

void sendReading() {
    readingSent = false;
   
    SENSOR_DATA sd;
    sd.temp = dht.readTemperature();
    sd.hum = dht.readHumidity();
    sd.t = millis();
    Serial.print("sendReading, t="); Serial.println(sd.t);
    Serial.print("Temperature = "); Serial.println(sd.temp);
    Serial.print("Humidity = "); Serial.println(sd.hum);

    u8 bs[sizeof(sd)];
    memcpy(bs, &sd, sizeof(sd));
    esp_now_send(NULL, bs, sizeof(bs)); // NULL means send to all peers
}


Any ideas would be gratefully accepted

I don't think that posting the Slave code would help but please let me know if it is required.

Regards..,

Kirk

pylon

Have you tried inserting the delay() call above the dht.begin() call?

1CM69

Have you tried inserting the delay() call above the dht.begin() call?
Hi, thanks for replying. Yes I have tried placing the delay in various places throughout the code as well as where you have suggested.

Eventually through trial and error placing the delay as below has solved the errors at power up that I was getting, albeit with some conditions.

Code: [Select]

void sendReading() {
    delay(206);
    readingSent = false;
.......


It took me a while to narrow the needed delay down to 206ms, this is the absolute minimum time I can get away with without getting the errors from the DHT22 sensor.

Without this delay and forgetting about the initial errors after power up the data is being sent via ESP Now to my other ESP8266 in in average time of 79ms

If I disconnect power and reconnect the error is sent in an average of 120ms, so around 40ms longer.

Adding in this delay of 206ms I get an average uptime of 285ms sending the readings across, very consistent (79 + 206 = 285)

Oddly If I disconnect power on the Master & reconnect now with this delay in place, the total uptime drops to an average of 279ms, 6ms less than the programmatical restart.

I think that I can easily live with the 285ms uptime though as my sensor is going to be remotely located & battery powered only to be woken 1 per hour to send the readings and then go back to deep sleep.

Regards..,

Kirk


1CM69

Just as a quick follow up, I have now tested my code using DS18B20 sensors and they all run fine without the delay, so it looks like the DHT22 has a longer startup time.

Go Up