Unable to bifurcate readings at receiver esp from multiple sender esp

Our project has 3 senders and one receiver esps.
We are sending data through Struct message from sender side (3 senders) and it is being received from receiver esp but in arbitary manner.
Serial monitor shows the output by those sender boards but is unable to print those simultaneously on an lcd display (TFT ILI9486) connected to receiver esp.

Attaching the code for your reference:

RECEIVER CODE:

/*
Rui Santos
Complete project details at ESP32: ESP-NOW and Wi-Fi Web Server Dashboard (Arduino) | Random Nerd Tutorials

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 <WiFi.h>
#include "ESPAsyncWebServer.h"
#include <Arduino_JSON.h>
#include <HTTPClient.h>
#include <SPI.h>
#include "MCUFRIEND_kbv.h"
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <TFT_eSPI.h>
TFT_eSPI tft = TFT_eSPI();

#define ESP32_PARALLEL
#define ILI9486_DRIVER
String temperature;
String humidity;

// Replace with your network credentials (STATION)
const char* ssid = "realme";
const char* password = "12345678";

//ACCESS POINT credentials
const char* ssidAP = "ESP_E63B4D";
const char* passwordAP = "";

// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int id;
float temperature;
float humidity;
unsigned int readingId;
} struct_message;

struct_message incomingReadings;
//struct_message incomingReadings1;
//struct_message incomingReadings3;

#define TFT_CD D15
#define TFT_RTS D32
#define TFTWIDTH 480
#define TFTHIGHT 320

#define TFT_BLACK 0x0000 /* 0, 0, 0 /
#define TFT_NAVY 0x000F /
0, 0, 128 /
#define TFT_DARKGREEN 0x03E0 /
0, 128, 0 /
#define TFT_DARKCYAN 0x03EF /
0, 128, 128 /
#define TFT_MAROON 0x7800 /
128, 0, 0 /
#define TFT_PURPLE 0x780F /
128, 0, 128 /
#define TFT_OLIVE 0x7BE0 /
128, 128, 0 /
#define TFT_LIGHTGREY 0xD69A /
211, 211, 211 /
#define TFT_DARKGREY 0x7BEF /
128, 128, 128 /
#define TFT_BLUE 0x001F /
0, 0, 255 /
#define TFT_GREEN 0x07E0 /
0, 255, 0 /
#define TFT_CYAN 0x07FF /
0, 255, 255 /
#define TFT_RED 0xF800 /
255, 0, 0 /
#define TFT_MAGENTA 0xF81F /
255, 0, 255 /
#define TFT_YELLOW 0xFFE0 /
255, 255, 0 /
#define TFT_WHITE 0xFFFF /
255, 255, 255 /
#define TFT_ORANGE 0xFDA0 /
255, 180, 0 /
#define TFT_GREENYELLOW 0xB7E0 /
180, 255, 0 /
#define TFT_PINK 0xFE19 /
255, 192, 203 / //Lighter pink, was 0xFC9F
#define TFT_BROWN 0x9A60 /
150, 75, 0 /
#define TFT_GOLD 0xFEA0 /
255, 215, 0 /
#define TFT_SILVER 0xC618 /
192, 192, 192 /
#define TFT_SKYBLUE 0x867D /
135, 206, 235 /
#define TFT_VIOLET 0x915C /
180, 46, 226 */

JSONVar board;
//JSONVar board2;
//JSONVar board3;

unsigned long previousMillis = 0;
const long interval = 5000;

AsyncWebServer server(80);
AsyncEventSource events("/events");

// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *incomingData, int len) {
// Copies the sender mac address to a string
char macStr[18];
Serial.print("Packet received from: ");
snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
Serial.println(macStr);
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));

board["id"] = incomingReadings.id;
board["temperature"] = incomingReadings.temperature;
board["humidity"] = incomingReadings.humidity;
board["readingId"] = String(incomingReadings.readingId);
String jsonString = JSON.stringify(board);
events.send(jsonString.c_str(), "new_readings", millis());

Serial.printf("Board ID %u: %u bytes\n", incomingReadings.id, len);
Serial.printf("t value: %4.2f \n", incomingReadings.temperature);
Serial.printf("h value: %4.2f \n", incomingReadings.humidity);
Serial.printf("readingID value: %d \n", incomingReadings.readingId);
Serial.println();
}
/*void OnDataRecv1(const uint8_t *mac_addr1, const uint8_t *incomingData1, int len1) {
// Copies the sender mac address to a string
char macStr1[18];
Serial.print("Packet received from: ");
snprintf(macStr1, sizeof(macStr1), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr1[0], mac_addr1[1], mac_addr1[2], mac_addr1[3], mac_addr1[4], mac_addr1[5]);
Serial.println(macStr1);
memcpy(&incomingReadings1, incomingData1, sizeof(incomingReadings1));

board2["id"] = incomingReadings1.id;
board2["temperature"] = incomingReadings1.temperature;
board2["humidity"] = incomingReadings1.humidity;
board2["readingId"] = String(incomingReadings1.readingId);
String jsonString1 = JSON.stringify(board2);
events.send(jsonString1.c_str(), "new_readings", millis());

Serial.printf("Board ID %u: %u bytes\n", incomingReadings1.id, len1);
Serial.printf("t value: %4.2f \n", incomingReadings1.temperature);
Serial.printf("h value: %4.2f \n", incomingReadings1.humidity);
Serial.printf("readingID value: %d \n", incomingReadings1.readingId);
Serial.println();
}
void OnDataRecv3(const uint8_t *mac_addr3, const uint8_t *incomingData3, int len3) {
// Copies the sender mac address to a string
char macStr3[18];
Serial.print("Packet received from: ");
snprintf(macStr3, sizeof(macStr3), "%02x:%02x:%02x:%02x:%02x:%02x",
mac_addr3[0], mac_addr3[1], mac_addr3[2], mac_addr3[3], mac_addr3[4], mac_addr3[5]);
Serial.println(macStr3);
memcpy(&incomingReadings3, incomingData3, sizeof(incomingReadings3));

board3["id"] = incomingReadings3.id;
board3["temperature"] = incomingReadings3.temperature;
board3["humidity"] = incomingReadings3.humidity;
board3["readingId"] = String(incomingReadings3.readingId);
String jsonString3 = JSON.stringify(board3);
events.send(jsonString3.c_str(), "new_readings", millis());

Serial.printf("Board ID %u: %u bytes\n", incomingReadings3.id, len3);
Serial.printf("t value: %4.2f \n", incomingReadings3.temperature);
Serial.printf("h value: %4.2f \n", incomingReadings3.humidity);
Serial.printf("readingID value: %d \n", incomingReadings3.readingId);
Serial.println();
}
*/
const char index_html[] PROGMEM = R"rawliteral(

ESP-NOW DASHBOARD html {font-family: Arial; display: inline-block; text-align: center;} p { font-size: 1.2rem;} body { margin: 0;} .topnav { overflow: hidden; background-color: #2f4468; color: white; font-size: 1.7rem; } .content { padding: 20px; } .card { background-color: white; box-shadow: 2px 2px 12px 1px rgba(140,140,140,.5); } .cards { max-width: 700px; margin: 0 auto; display: grid; grid-gap: 2rem; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); } .reading { font-size: 2.8rem; } .packet { color: #bebebe; } .card.temperature { color: #fd7e14; } .card.humidity { color: #1b78e2; }

ESP-NOW DASHBOARD

LECP #1 - TEMPERATURE

°C

Reading ID:

LECP #1 - HUMIDITY

%

Reading ID:

LECP #2 - TEMPERATURE

°C

Reading ID:

LECP #2 - HUMIDITY

%

Reading ID:

LECP #3 - TEMPERATURE

°C

Reading ID:

LECP #3 - HUMIDITY

%

Reading ID:

LECP #4 - TEMPERATURE

°C

Reading ID:

LECP #4 - HUMIDITY

%

Reading ID:

LECP #5 - TEMPERATURE

°C

Reading ID:

LECP #5 - HUMIDITY

%

Reading ID:

)rawliteral";

void setup() {
// Initialize Serial Monitor
tft.begin(0x9486);
Serial.begin(115200);
tft.setRotation(2);
tft.fillScreen(ILI9486_BLACK); // Black Background
tft.fillRect(0,0,40,480,TFT_CYAN); // Upper GREEN Rectange
tft.setRotation(1);
tft.fillRect(0,40,160,140,TFT_MAGENTA); // Upper GREEN Rectange
tft.fillRect(160,40,160,140,TFT_MAGENTA); // Lower RED Rectange
tft.fillRect(320,40,160,140,TFT_MAGENTA); // Upper BLUE Rectange

tft.drawRect(0,40,160,140,TFT_YELLOW); // White borders to the rectangles
tft.drawRect(160,40,160,140,TFT_YELLOW); // White borders to the rectangles
tft.drawRect(320,40,160,140,TFT_YELLOW); // White borders to the rectangles

tft.fillRect(0,180,160,140,TFT_MAGENTA); // Upper GREEN Rectange
tft.fillRect(160,180,160,140,TFT_MAGENTA); // Lower RED Rectange
tft.fillRect(320,180,160,140,TFT_MAGENTA); // Upper BLUE Rectange

tft.drawRect(0,180,160,140,TFT_YELLOW); // White borders to the rectangles
tft.drawRect(160,180,160,140,TFT_YELLOW); // White borders to the rectangles
tft.drawRect(320,180,160,140,TFT_YELLOW);

// Set the device as a Station and Soft Access Point simultaneously
WiFi.mode(WIFI_AP_STA);

// Set device as a Wi-Fi Station
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Setting as a Wi-Fi Station..");
}
Serial.print("Station IP Address: ");
Serial.println(WiFi.localIP());
Serial.print("Wi-Fi Channel: ");
Serial.println(WiFi.channel());

// 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 recv CB to
// get recv packer info

//esp_now_register_recv_cb(OnDataRecv3);
esp_now_register_recv_cb(OnDataRecv);
//esp_now_register_recv_cb(OnDataRecv1);

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
});

events.onConnect([](AsyncEventSourceClient *client){
if(client->lastId()){
Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId());
}
// send event with message "hello!", id current millis
// and set reconnect delay to 1 second
client->send("hello!", NULL, millis(), 0);
});
server.addHandler(&events);
server.begin();
}

void loop() {
static unsigned long lastEventTime = millis();
static const unsigned long EVENT_INTERVAL_MS = 5000;
if ((millis() - lastEventTime) > EVENT_INTERVAL_MS) {
if(WiFi.status()== WL_CONNECTED ){
events.send("ping",NULL,millis());
lastEventTime = millis();
}

tft.setTextColor(TFT_BLACK); // Set Text Proporties
tft.setTextSize(2);
tft.setCursor(30,10);
tft.println("LOCAL ENGINE CONTROL PANEL (LECP)"); // Write Text on LCD

tft.setTextColor(TFT_BLACK);
tft.setCursor(40,45);
tft.println("LECP 1"); // Write Text on LCD

board["id"] = incomingReadings.id;

Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity );
tft.setTextSize(2);
tft.setTextColor(TFT_MAGENTA);
tft.setTextColor(TFT_BLACK,TFT_MAGENTA);
tft.setCursor(20,70);
tft.print("T: ");
tft.setTextPadding(120);
tft.print(incomingReadings.temperature);
tft.print(" ");
tft.setTextSize(1);
tft.write(248);
tft.setTextSize(2);
tft.print("C");

  // display humidity
  tft.setTextSize(2);
  tft.setCursor(20,90);
  tft.print("H: ");
  tft.print(incomingReadings.humidity);
  tft.print(" %"); 

board["id"] = incomingReadings.id;
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity );
tft.setCursor(200, 45);
tft.println("LECP 2"); // Write Text on LCD
// display temperature
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK,TFT_MAGENTA);
tft.setCursor(190,70);
tft.print("T: ");
tft.setTextPadding(120);
tft.print(incomingReadings.temperature);
tft.print(" ");
tft.setTextSize(1);
tft.write(248);
tft.setTextSize(2);
tft.print("C");

  // display humidity
  tft.setTextSize(2);
  tft.setCursor(190, 90);
  tft.print("H: ");
  tft.print(incomingReadings.humidity);
  tft.print(" %"); 

board["id"] = incomingReadings.id;
Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity );
tft.setCursor(360,45);
tft.println("LECP 3"); // Write Text on LCD
// display temperature
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK,TFT_MAGENTA);
tft.setCursor(350,70);
tft.print("T: ");
tft.setTextPadding(120);
tft.print(incomingReadings.temperature);
tft.print(" ");
tft.setTextSize(1);
tft.write(248);
tft.setTextSize(2);
tft.print("C");

  // display humidity
  tft.setTextSize(2);
  tft.setCursor(350, 90);
  tft.print("H: ");
  tft.print(incomingReadings.humidity);
  tft.print(" %");  



 Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity );

tft.setCursor(40,185);
tft.println("LECP 4"); // Write Text on LCD
// display temperature
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK,TFT_MAGENTA);
tft.setCursor(30,215);
tft.print("T: ");
tft.setTextPadding(120);
tft.print(incomingReadings.temperature);
tft.print(" ");
tft.setTextSize(1);
// tft._cp437(true);
tft.write(248);
tft.setTextSize(2);
tft.print("C");

  // display humidity
  tft.setTextSize(2);
  tft.setCursor(30, 235);
  tft.print("H: ");
  tft.print(incomingReadings.humidity);
  tft.print(" %"); 


  Serial.println("Temperature: " + temperature + " *C - Humidity: " + humidity );

tft.setCursor(200,185);
tft.println("LECP 5"); // Write Text on LCD
// display temperature
tft.setTextSize(2);
tft.setTextColor(TFT_BLACK,TFT_MAGENTA);
tft.setCursor(190,215);
tft.print("T: ");
tft.setTextPadding(120);
tft.print(incomingReadings.temperature);
tft.print(" ");
tft.setTextSize(1);
// tft._cp437(true);
tft.write(248);
tft.setTextSize(2);
tft.print("C");

  // display humidity
  tft.setTextSize(2);
  tft.setCursor(190, 235);
  tft.print("H: ");
  tft.print(incomingReadings.humidity);
  tft.print(" %"); 

delay(5000);

               // Write Text on LCD
    // display temperature


  
  // save the last HTTP GET Request
  //previousMillis = currentMillis;

// else {
// Serial.println("WiFi Disconnected");
}

}

/*

String httpGETRequest(const char* serverName)
{
WiFiClient client;
HTTPClient http;

// Your Domain name with URL path or IP address with path
http.begin(client, serverName);

// Send HTTP POST request
int httpResponseCode = http.GET();

String payload = "--";

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();

return payload;
}*/

SENDER CODE:

/*
Rui Santos
Complete project details at ESP32: ESP-NOW and Wi-Fi Web Server Dashboard (Arduino) | Random Nerd Tutorials

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>
#include <Adafruit_Sensor.h>
#include <DHT.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 DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

//MAC Address of the receiver
uint8_t broadcastAddress[] = {0x94, 0xB9, 0x7E, 0xE6, 0x3B, 0x4C};

//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;

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[] = "ESP_E63B4D";

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 = dht.readTemperature();
// 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 = dht.readHumidity();
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");
}
esp_now_peer_info_t peerInfo;
void setup() {
//Init Serial Monitor
Serial.begin(115200);

dht.begin();

// 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);

// 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++;

//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");
}

}
}

Please edit your post to add code tags. For instructions, see the "How to get the best out of the forum" post, linked at the head of every forum category.

Better, re-post the code after formatting it properly in the Arduino IDE, by pressing CTRL-T.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.