Concept:
Sender will transmit button states values to receiver. receiver received the message and then print on serial monitor to indicate which button has been presssed.
Receiver also would transmit the X Axis data read from the MPU6050 to the Sender. The Sender receive it and display on serial monitor or in this case, the ssd1306 oled display.
receiver's sketch:
sender's sketch:
I managed to transmit the button states values from the sender to receiver, but unable to make sender receive the X Axis value sent from the receiver.
I attached both receiver and sender code below, appreciated for any help
Sender's code:
#include <esp_now.h>
#include <WiFi.h>
#include <ezButton.h>
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x48, 0xE7, 0x29, 0xA2, 0x5A, 0xF4};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
char a[32];
int b;
bool d;
float gyroX ; //mpu6050 data
} struct_message;
// Create a struct_message called myData
struct_message myData;
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");
}
// Callback function to handle received data
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *data, int data_len) {
memcpy(&myData, data, sizeof(myData));
Serial.print("Angle:");
Serial.println(myData.gyroX);
}
//////////////////////////////////////////////////////////
ezButton button1(15);
ezButton button2(5);
ezButton button3(18);
ezButton button4(19);
ezButton button5(23);
////////////////////////////////////////////////////////////
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// 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;
}
//define buttons by ezButton library
button1.setDebounceTime(50);
button2.setDebounceTime(50);
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);
////////////////////////////////////////////////////////////
Serial.println("OK");
}
void loop() {
button1.loop();
button2.loop();
button3.loop();
button4.loop();
button5.loop();
if(button1.isPressed())
{
// Set values to send
myData.b = 0;
// display on Serial Monitor
Serial.println("btn1 pressed");
// 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");
}
}
if(button2.isPressed())
{
// Set values to send
myData.b = 1;
// display on Serial Monitor
Serial.println("btn2 pressed");
// 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");
}
}
if(button3.isPressed())
{
// Set values to send
myData.b = 2;
// display on Serial Monitor
Serial.println("btn3 pressed");
// 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");
}
}
if(button4.isPressed())
{
// Set values to send
myData.b = 3;
// display on Serial Monitor
Serial.println("btn4 pressed");
// 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");
}
}
if(button5.isPressed())
{
// Set values to send
myData.b = 4;
// display on Serial Monitor
Serial.println("btn5 pressed");
// 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");
}
}
}
Receiver's code:
#include <esp_now.h>
#include <WiFi.h>
#include <Adafruit_MPU6050.h>
#include <Wire.h>
//define Adafruit mpu6050
Adafruit_MPU6050 mpu;
// Replace with the same MAC address used in the sender (broadcastAddress)
uint8_t broadcastAddress[] = {0x48, 0xE7, 0x29, 0xA2, 0x5A, 0xF4};
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
char a[32];
int b;
bool d;
float gyroX;// store data from x axis mpu6050
} struct_message;
struct_message myData;
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");
}
// Callback function to handle received data
void OnDataRecv(const uint8_t * mac_addr, const uint8_t *data, int data_len) {
Serial.print("Received from: ");
for (int i = 0; i < 6; i++) {
Serial.print(mac_addr[i], HEX);
if (i < 5) {
Serial.print(":");
}
}
Serial.println("");
// Cast received data to struct_message
struct_message* receivedData = (struct_message*)data;
// Print button pressed based on received value
switch (receivedData->b) {
case 0:
Serial.println("Button 1 pressed on sender");
break;
case 1:
Serial.println("Button 2 pressed on sender");
break;
case 2:
Serial.println("Button 3 pressed on sender");
break;
case 3:
Serial.println("Button 4 pressed on sender");
break;
case 4:
Serial.println("Button 5 pressed on sender");
break;
default:
Serial.println("Unknown button pressed");
}
}
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 5000; // interval at which to blink (milliseconds)
void setup() {
// Init Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station (optional, not required for ESP-NOW)
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register for receive callback
esp_now_register_recv_cb(OnDataRecv);
// Register peer with same settings as sender (assuming same channel, encryption etc.)
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;
}
Serial.println("ESP32 Receiver Ready!");
// setup for MPU6050
if(!mpu.begin()){
Serial.println("failed to find MPU6050");
while(1){
delay (10);
}
}
Serial.println("MPU6050 Found");
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// get new sensor events
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
myData.gyroX= g.gyro.x;
// 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");
}
//print out value
Serial.print("Rotation X:");
Serial.println(g.gyro.x);
}
}
here's my Serial Monitor, as you guys could see, the esp32 receiver could receive message and display which button was pressed + read the X- axis values but it can't transmitted the values to esp32 sender
ESP32 sender could send button states data to the esp32 receiver, but it didnt receive the x-axis values that esp32 receiver sent