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:
/*
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