I am trying to build a door sensor that, as soon as the door opens, will activate a reed switch and send the value to an ESP-NOW SLAVE which will then connect to WiFi, and then MQTT.
So my sender device can send the value continuously with an interval of 2 seconds, but most of the time my messages fail, even when the devices are kept next to each other.
When I do WiFi.disconnect();
it works very well, very few packets fail and the range is also good.
Here is what my code looks like right now for the receiver side:
Receiver.ino
#include "WifiSetup.h"
#include <espnow.h>
//##################################################
//##################################################
// Change this!!
char* ssid = "RANDOM";
char* password = "123455";
char* deviceAP_name = "RANDOM"; //
char* deviceAP_password = "RANDOM";
char mqttServer[40] = "192.168.1.20";
int mqttPort = 1883;
char mqttUser[40] = "username";
char mqttPassword[40] = "password";
//##################################################
//##################################################
boolean wifiConnected = false;
boolean clientConnected = false;
typedef struct struct_message {
char dt[50]; // device topic
char dstate[5];
char bt[50]; // battery topic
float bvolt;
} struct_message;
// Init ESP Now with fallback
void InitESPNow() {
// WiFi.disconnect();
if (esp_now_init() == 0) {
Serial.println("ESPNow Init Success");
} else {
Serial.println("ESPNow Init Failed");
// Retry InitESPNow, add a counte and then restart?
// InitESPNow();
// or Simply Restart
ESP.restart();
}
}
// Create a struct_message called myData
struct_message myData;
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("char: ");
Serial.println(myData.dt);
Serial.print("char: ");
Serial.println(myData.dstate);
Serial.println();
}
void setup() {
Serial.begin(115200);
wifiConnected = connectWifi();
if (!wifiConnected) {
reconnectWifiSetup();
}
InitESPNow();
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
wifiReconnectLoop();
yield();
}
Here is what my WifiConnectivity program looks like:
WifiSetup.h
#include <ESP8266WiFi.h>
//##################################################
//##################################################
// Change this!!
extern char* ssid;
extern char* password;
extern char* deviceAP_name;
extern char* deviceAP_password;
//##################################################
//##################################################
boolean connectWifi() {
Serial.println("############ Connecting to Wifi #################");
boolean state = true;
int i = 0;
// WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
wifiConnected = false;
// Wait for connection
Serial.print("Connecting...");
while (WiFi.status() != WL_CONNECTED) {
delay(50);
wifiConnected = false;
Serial.print(".");
if (i > 150) {
state = false;
break;
}
i++;
}
Serial.println("");
if (state) {
digitalWrite(2, HIGH);
wifiConnected = true;
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("ESP8266 Board MAC Address: ");
Serial.println(WiFi.macAddress());
Serial.println("############ Connected to Wifi #################");
} else {
Serial.println("Connection failed.");
Serial.println("############ Failed Wifi Connection #################");
}
return state;
}
// check wifi connection if not connected then set the timmer and start the connection after every 2 minutes (120 sec):
int total_wifi_check_count;
unsigned long overallStartTime_wifi;
unsigned long overallPeriod_wifi = 120000;
unsigned long currentTime_wifi;
void reconnectWifiSetup() {
Serial.println("Cannot connect to WiFi. Please check data and reset the ESP.");
currentTime_wifi = millis();
overallStartTime_wifi = currentTime_wifi;
total_wifi_check_count++;
}
void wifiReconnectLoop() {
if (WiFi.status() != WL_CONNECTED) {
currentTime_wifi = millis();
if (currentTime_wifi - overallStartTime_wifi >= overallPeriod_wifi) {
if (total_wifi_check_count >= 15) {
ESP.reset();
}
if (!connectWifi()) {
total_wifi_check_count++;
Serial.println(total_wifi_check_count);
overallStartTime_wifi = currentTime_wifi;
}
}
} else {
currentTime_wifi = millis();
overallStartTime_wifi = currentTime_wifi;
}
}
To improve the battery life and the response time I am using ESP-NOW, but due to less range and non-consistent message delivery in near range I am also not sure how I can get this done.
Any suggestions will be helpful.
Thank you!
WiFi.disconnect()
to make sure that the WiFi is not connected. – hcheung Jan 28 at 13:36