For a project we are trying to use an ESP32 with flex sensors, to control the servo motors attached to another ESP32 (connected via ESP NOW). The transmission sequence works and the data gets received, however the servo motors do nothing. We used the codes and library below, any hints on why it isn't working would be much appreciated.
TRANSMITTER CODE:
#include <esp_now.h>
#include <WiFi.h>
//Naampie van de flex sensor input pins AANPASSEN WEGENS VERANDERINGEN
int flexsensor_5 = 35; //Ring D35
int flexsensor_4 = 34; // Pink D34
int flexsensor_3 = 33; // WIjs D33
int flexsensor_2 = 32; //Middelfinger D32
int flexsensor_1 = 25;//Duim D25
//Naampie van de variabelen voor de flex sensor waardes
int flexvariable_5;
int flexvariable_4;
int flexvariable_3;
int flexvariable_2;
int flexvariable_1;
// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x3C, 0x71, 0xBF, 0x52, 0xBF, 0x60};
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int a;
int b;
int c;
int d;
int e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// 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(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
esp_now_peer_info_t peerInfo;
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() {{
flexvariable_5 = analogRead(flexsensor_5);
flexvariable_5 = map(flexvariable_5, 630, 730, 80, 20);
flexvariable_4 = analogRead(flexsensor_4);
flexvariable_4 = map(flexvariable_4, 520, 710, 70, 175);
flexvariable_3 = analogRead(flexsensor_3);
flexvariable_3 = map(flexvariable_3, 510, 680, 140, 10);
flexvariable_2 = analogRead(flexsensor_2);
flexvariable_2 = map(flexvariable_2, 580, 715, 90, 175);
flexvariable_1 = analogRead(flexsensor_1);
flexvariable_1 = map(flexvariable_1, 550, 700, 90, 175);
}
// Set values to send
myData.a = flexvariable_1;
myData.b = flexvariable_2;
myData.c = flexvariable_3;
myData.d = flexvariable_4;
myData.e = flexvariable_5;
// 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(100);
}
RECEIVER CODE:
#include <ESP32_Servo.h>
//Receiver Code (Hand) - Mert Arduino and Tech
#include <esp_now.h>
#include <WiFi.h>
//Servo naampies
Servo Servomotor1;
Servo Servomotor3;
Servo Servomotor4;
Servo Servomotor2;
Servo Servomotor5;
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message {
int a;
int b;
int c;
int d;
int e;
} struct_message;
// Create a struct_message called myData
struct_message myData;
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print("Bytes received: ");
Serial.println(len);
Serial.print("int: ");
Serial.println(myData.a);
Serial.print("int: ");
Serial.println(myData.b);
Serial.print("int: ");
Serial.println(myData.c);
Serial.print("int: ");
Serial.println(myData.d);
Serial.print("int: ");
Serial.println(myData.e);
Serial.println();
}
int flexvariable_1 = myData.a;
int flexvariable_2 = myData.b;
int flexvariable_3 = myData.c;
int flexvariable_4 = myData.d;
int flexvariable_5 = myData.e;
void setup() {
//Naampies servo motor input pins INPUT PINS AANPASSEN VANWEGE VERANDERINGEN
Servomotor1.attach(18); //A1 D18
Servomotor2.attach(4); //A2 D4
Servomotor3.attach(5); //A3 D5
Servomotor4.attach(21); //A4 D21
Servomotor5.attach(19); //A5 D19
// Initialize 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 recv CB to
// get recv packer info
esp_now_register_recv_cb(OnDataRecv);
}
void loop(){
Servomotor1.write(-(flexvariable_1)); //A1 KIJKEN WELKE PINS BIJ DEZE NUMMERS HOREN
Servomotor2.write(-(flexvariable_2)-150); //A2
Servomotor3.write(-(flexvariable_3)-514); //A3
Servomotor4.write(-(flexvariable_4)-350); //A4
Servomotor5.write(-(flexvariable_5)-340); //A5
}
ServoESP32-master.zip (10.9 KB)