I am trying to write/edit a code to send my MPU6050 accelerometer data from Esp32 to Esp8266 using EspNow protocol.
At the sender end, the accelerometer data was not changing, was incorrect and not sending to the receiver.
At the receiver end, I can't see the accelerometer data at all.
I have attached the codes and the serial monitor output.
sender
#include <esp_now.h>
#include <WiFi.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_MPU6050.h>
#include <SPI.h>
// Check I2C device address and correct line below (0x68)
// id, address
Adafruit_MPU6050 mpu;
#define CHANNEL 1
// REPLACE WITH THE RECEIVER'S MAC Address
uint8_t broadcastAddress[] = {0x13, 0x93, 0xC5, 0xB6, 0x69, 0x92};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message
{
int id; // must be unique for each sender board
float acc_x;
float acc_y;
float acc_z;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// Create peer interface
esp_now_peer_info_t peerInfo;
// 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 setup()
{
// Init Serial Monitor
Serial.begin(2000000);
/* Initialise the sensor */
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1);
}
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// 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
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK)
{
Serial.println("Failed to add peer");
return;
}
}
void loop()
{
sensors_event_t accelerometerData;
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
readAccValues(&accelerometerData);
// Set values to send
myData.id = 1;
Serial.print("acc X:");
Serial.println(myData.acc_x);
Serial.print("acc Y:");
Serial.println(myData.acc_y);
Serial.print("acc Z:");
Serial.println(myData.acc_z);
// 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");
}
delay(10000);
}
void readAccValues(sensors_event_t *event)
{
// read acceleration event
myData.acc_x = event->acceleration.x;
myData.acc_y = event->acceleration.y;
myData.acc_z = event->acceleration.z;
}
sender monitor
Last Packet Send Status: Delivery Success
acc X:1.96
acc Y:0.75
acc Z:0.00
Sent with success
Last Packet Send Status: Delivery Success
acc X:1.96
acc Y:0.75
acc Z:0.00
Sent with success
Last Packet Send Status: Delivery Success
acc X:1.96
acc Y:0.75
acc Z:0.00
Sent with success
Last Packet Send Status: Delivery Success
acc X:1.96
acc Y:0.75
acc Z:0.00
Sent with success
Last Packet Send Status: Delivery Success
receiver code below
#include <espnow.h>
#include <ESP8266WiFi.h>
#define CHANNEL 1
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int id;
float acc_x;
float acc_y;
float acc_z;
}struct_message;
// Create a struct_message called myData
struct_message myData;
// Create a structure to hold the readings from each board
struct_message rightGlove;
struct_message leftGlove;
// Create an array with all the structures
struct_message boardsStruct[3] = {rightGlove, leftGlove};
// 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-1].acc_x = myData.acc_x;
boardsStruct[myData.id-1].acc_y = myData.acc_y;
boardsStruct[myData.id-1].acc_x = myData.acc_z;
/*Serial.print("acc_x: %d \n", boardsStruct[myData.id-1].acc_x);
Serial.print("acc_y: %d \n", boardsStruct[myData.id-1].acc_y);
Serial.print("acc_z: %d \n", boardsStruct[myData.id-1].acc_x);*/
Serial.println();
}
void setup() {
//Initialize Serial Monitor
Serial.begin(2000000);
//Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//Init ESP-NOW
if (esp_now_init() != ERR_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(OnDataRecv);
}
void loop() {
delay(10000);
}
receiver monitor
Packet received from: c8:c9:a3:cb:73:48
Board ID 1: 16 bytes
Packet received from: c8:c9:a3:cb:73:48
Board ID 1: 16 bytes
Packet received from: c8:c9:a3:cb:73:48
Board ID 1: 16 bytes
Packet received from: c8:c9:a3:cb:73:48
Board ID 1: 16 bytes
Packet received from: c8:c9:a3:cb:73:48
Board ID 1: 16 bytes