I am sending Data from 3 NODEMCU ESP8266 boards to one NODEMCU ESP8266 board.
I am using ESP-now for this and I want to send this recieved data to Aduino uno board via serial communication for further arithematic process. I have written the code for reciever arduino, but i am not sure if it is storing the data recieved or just printing it on the serial monitor. I am stuck, Please help.
This is my code for multiple ESP8266 senders
``
#include <ESP8266WiFi.h>
#include <espnow.h>
// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x30, 0x83, 0x98, 0xA6, 0x60, 0x36};
// Set your Board ID (ESP32 Sender #1 = BOARD_ID 1, ESP32 Sender #2 = BOARD_ID 2, etc)
#define BOARD_ID 2
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id;
int red ;
int blue ;
} struct_message;
// Create a struct_message called test to store variables to be sent
struct_message myData;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("\r\nLast Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
void setup() {
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D3, INPUT);
pinMode(D5, INPUT);
pinMode(D6, INPUT);
pinMode(D7, INPUT);
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set ESP-NOW role
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
// 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_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
// Set values to send
myData.id = 2;
if(digitalRead (D1)==HIGH) {
myData.red = 1 ;
}
else if(digitalRead (D2)==HIGH) {
myData.red = 2 ;
}
else if(digitalRead (D3)==HIGH) {
myData.red = 3 ;
}
else if(digitalRead (D5)==HIGH) {
myData.blue = 1 ;
}
else if(digitalRead (D6)==HIGH) {
myData.blue = 2 ;
}
else if(digitalRead (D7)==HIGH) {
myData.blue = 3 ;
}
// Send message via ESP-NOW
esp_now_send(0, (uint8_t *) &myData, sizeof(myData));
yield () ;
}
This is my code for the ESP board which recieves data from multiple boards and serially sends it to Arduino.
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <SoftwareSerial.h>
// Structure example to receive data
// Must match the sender structure
String str ;
typedef struct struct_message {
int id;
int red;
int blue;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Create a structure to hold the readings from each board
struct_message board2;
// Create an array with all the structures
struct_message boardsStruct[1] = {board2};
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac_addr, uint8_t *incomingData, uint8_t len) {
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(&myData, incomingData, sizeof(myData));
Serial.printf("Board ID %u: %u bytes\n", myData.id, len);
// Update the structures with the new incoming data
boardsStruct[myData.id-2].red = myData.red;
boardsStruct[myData.id-2].blue = myData.blue;
Serial.printf("red value: %d \n", boardsStruct[myData.id-2].red);
Serial.printf("blue value: %d \n", boardsStruct[myData.id-2].blue);
Serial.println();
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
Serial1.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
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_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
}
void loop(){
// Access the variables for each board
/*int board1red = boardsStruct[0].red;
int board1blue = boardsStruct[0].blue;
int board2red = boardsStruct[1].red;
int board2blue = boardsStruct[1].blue;
*/
str = String("coming from ESP8266: ")+String("Red ")+String(myData.red)+String("Blue ")+String(myData.blue);
Serial1.println(str);
}
This is my code for Arduino Reciever.
int sum=0 ;
typedef struct struct_message {
int id;
int red;
int blue;
} struct_message;
// Create a struct_message called myData
struct_message myData;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() { // run over and over
if (Serial.available()) {
Serial.write(Serial.read());
sum = sum + myData.red ;
printf("Red ", sum) ;
}
}
Please help me with the code