Sending data from NodeMcu esp8266 to Arduino

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

the Arduino Uno uses 5V logic and the NODEMCU ESP8266 3.3V logic
if you you are only transmiiting serial data from the NODEMCU ESP8266 to the UNO should be no problem but if you are transmitting from the UNO to NODEMCU ESP8266 you shoud use a level converter.
your code is difficult to read - use code tags for each peice of code

your nodemcu appears to be trasmitting a string

str = String("coming from ESP8266: ")+String("Red ")+String(myData.red)+String("Blue ")+String(myData.blue);
Serial1.println(str);

but your Uno read reading character by character and adding to an integer

Serial.write(Serial.read());
sum = sum + myData.red ;

try reading a complete lines using readString() and printing the complete line to the Serial Monitor

your Uno code uses Serial to receive data which is also used by the serial monitor - not sure if this would work?
perhaps use SoftwareSerial to a different pin - however, you would need to reduce the baud rate
probably better to use an Aduino mega hardware serial port

I have used the logic converter. I am using RX TX pins on both the boards.
How to use hardware serial port.
I am very new with Arduino and ESP.

I am using voltage divider.
Actually i want to send data stored in myData.red and myData.blue from ESP and add all those data in Arduino.
From esp to esp i am doing right.
i am going wrong somewhere in esp to arduino.
I am very new with esp and Arduino

sender esp code

#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 () ;  
    
}


reciever which also sends 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);    
}

Arduino which recieves

int sum1=0 ;
int sum2=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());
sum1 = sum1 + myData.red ;
printf("Red ", sum1) ;
sum2 = sum2 + myData.blue ;
printf("Blue ", sum2) ;
}
}

a schematic showing the components and interconnections would be useful
the Arduino Mega has 4 hardware serial ports which will operate at 115200baud without problems

I would recommend you use a Mega and connect the esp to the Serial1 port using level converters
initially just print the characters received from the esp
you can extract information from the string when you know you are receiving it OK

This is ESP sender board schematic

This is for Arduino

Yes i tried with printing it on serial monitor.
And i am getting nthe recieved data.
Now i just want to add those data in Arduino

it looks as if the string from the esp is along the lines of

coming from ESP8266: Red 3Blue 2

if you save the line of text into a char array or a String you could then extract the values of red 3 and blue 2 and add them to your sums

Can you please guide me with that...i mean how to do that in a code

So you have several 32bit 80+MHz (or whatever esp8266 ticks at) microcontrollers, have them collect data, and to do calculations you send it to an 8-bit 16MHz processor...?
That's like taking a ferrari to drive through your front yard to the curbside where your tricyle is parked so you can paddle your way to the store.

1 Like

using the Serial class you could read a string (assuming you don't timeout)

  if(serial1.available() {
     String text=Serial1.reasStringUnitil('\n');

then use the String class to look for "Red" and "Blue" to xtract the values you require

as @anon35827816 says you need to consider how much data you are transmitting and how often

A few things wrong with that!

One is that you have shown the top supply rails jumpered but not the bottom.

The second is that you are connecting the buttons to the 5 V rail which means you are overloading and potentially damaging the I/O pins. If you must connect them to the supply rail, it must be the 3.3 V rail.

But in fact, it is almost always more sensible to connect the buttons to ground and the pull-up resistors - which may not be necessary because you can activate the internal pull-ups - to the 3.3 V rail. You just write the logic accordingly.

470 Ohm resistors are rather lower than necessary (6 mA) - 2k2 would be plenty.

A somewhat faster level conversion is a diode with cathode to the 5 V logic output and anode to the ESP input, either with a pullup resistor to 3.3 V or just the internal pullup.

I am connecting it to 3.3V rail. There are no issues regarding circuit.
it is working fine

Can i do arithematic calculations in esp8266 ?

I'll try that. I am transmitting a one number every 10-15 seconds.

your esp sender is transmitting continuously in loop() - do you need to do that?
put a delay() at the of loop() or only transmit when the state of the signals change?

I also find it a good idea when using ESP-NOW to print the board mac on startup, e.g.

  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

I would like to say that lit. "every" calculation you can do on an Uno - can be done on a NodeMCU also.

Get rid of the Arduino - and do your "calculations" on the NodeMCU.

Yes i have done that in another separate program

Yes. And a lot faster than on an UNO at that.
The Arduino sounds like an unnecessary addition to your project.