one ESP32 is connected to the wifi as webserver for now
the other ESP32 is a HUB which will collect readings from NodeMCUs then will forward it to the webserver
the nodeMCU will be sender will send the data to the HUB
(This hub is because ESP-NOW and WIFI doesn't work with me direct)
the issue I've now that the packets are inconsistent in sending sometime it sent and sometimes it failed, and I can't see why
could anyone help me with this please?
- attached the serial log (0 is sent, 1 isn't)
here is the code of both hub and NodeMCU
Code: Untitled.c Select all
/**
* ESP-NOW
*
* Sender
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <SPI.h>
uint8_t peer1[] = {0x24, 0x62, 0xAB, 0xFF, 0x2C, 0x5C};
typedef struct struct_message {
int id;
float temp;
float hum;
int readingId;
} struct_message;
struct_message sendData;
// Initialize DHT sensor.
void onSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.println("Status:");
Serial.println(sendStatus);
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
const char* dummy = "abcdefgh";
// SSID pass channel BSSID connect
WiFi.begin(dummy, dummy, 11, false, false);
// Get Mac Add
Serial.print("Mac Address: ");
Serial.print(WiFi.macAddress());
Serial.println("ESP-Now Sender");
// Initializing the ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Problem during ESP-NOW init");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
// Register the peer
Serial.println("Registering a peer");
esp_now_add_peer(peer1, ESP_NOW_ROLE_SLAVE, 11, NULL, 0);
Serial.println("Registering send callback function");
esp_now_register_send_cb(onSent);
}
void loop() {
sendData.temp = random(10, 30);
sendData.hum = random(50, 90);
Serial.println("Send a new message");
esp_now_send(NULL, (uint8_t *) &sendData, sizeof(sendData));
delay(5000);
}Code: Untitled.c Select all
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp-now-wi-fi-web-server/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <esp_wifi.h>
#include <WiFi.h>
// Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 1
// Digital pin connected to the DHT sensor
#define DHTPIN 4
// Uncomment the type of sensor in use:
//#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//MAC Address of the receiver 24:62:AB:FE:3F:A0
//uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xFF, 0x2C, 0x5C};
uint8_t broadcastAddress[] = {0x24, 0x62, 0xAB, 0xFE, 0x3F, 0xA0};
//Structure example to send data
//Must match the receiver structure
typedef struct struct_message {
int id;
float temp;
float hum;
int readingId;
} struct_message;
//Create a struct_message called myData
struct_message myData;
struct_message receivData;
unsigned long previousMillis = 0; // Stores last time temperature was published
const long interval = 10000; // Interval at which to publish sensor readings
unsigned int readingId = 0;
// Insert your SSID
constexpr char WIFI_SSID[] = "SAP_1";
int32_t getWiFiChannel(const char *ssid) {
if (int32_t n = WiFi.scanNetworks()) {
for (uint8_t i=0; i<n; i++) {
if (!strcmp(ssid, WiFi.SSID(i).c_str())) {
return WiFi.channel(i);
}
}
}
return 0;
}
float readDHTTemperature() {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
// Read temperature as Celsius (the default)
float t = 11;
// Read temperature as Fahrenheit (isFahrenheit = true)
//float t = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return 0;
}
else {
Serial.println(t);
return t;
}
}
float readDHTHumidity() {
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = 225;
if (isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return 0;
}
else {
Serial.println(h);
return h;
}
}
// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void onDataReceiver(const uint8_t * mac, const uint8_t *incomingData, int len) {
Serial.println("Message received.");
// We don't use mac to verify the sender
// Let us transform the incomingData into our message structure
memcpy(&receivData, incomingData, sizeof(receivData));
Serial.println("=== Data ===");
Serial.print("Mac address: ");
for (int i = 0; i < 6; i++) {
Serial.print("0x");
Serial.print(mac[i], HEX);
Serial.print(":");
}
Serial.print("\n\nMessage: ");
Serial.println(receivData.id);
Serial.println(receivData.temp);
Serial.println();
//Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
}
void setup() {
//Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station and set channel
WiFi.mode(WIFI_STA);
int32_t channel = getWiFiChannel(WIFI_SSID);
//WiFi.printDiag(Serial); // Uncomment to verify channel number before
esp_wifi_set_promiscuous(true);
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE);
esp_wifi_set_promiscuous(false);
WiFi.printDiag(Serial); // Uncomment to verify channel change after
//Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
esp_now_register_recv_cb(onDataReceiver);
//Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.encrypt = false;
//Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// Save the last time a new reading was published
previousMillis = currentMillis;
//Set values to send
myData.id = BOARD_ID;
myData.temp = readDHTTemperature();
myData.hum = readDHTHumidity();
myData.readingId = readingId++;
}
}