Using a pair ESP-MROOM-32 controller board with a standard 5V servo communicating across ESPnow.
I have confirmed I am able to send data from the Broadcast to the Listener (master to slave) modules.
I would like to command the servo to change states upon a broadcast being received. Command boolean is always recieved as 1. This is likely similar to a button debounce, but my brain is struggling how to get the logic gates to work out when the boolean is always 1.
Attached is the Reciever code.
#include <esp_now.h>
#include <WiFi.h>
#include <ESP32Servo.h>
// Structure example to receive data
// Must match the sender structure
typedef struct test_struct {
bool command;
} test_struct;
// Create a struct_message called myData
test_struct myData;
//define Servo parameters
Servo myServo;
bool pos = 1;
bool oldpos;
#define SERVO_PIN 13
// callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
/// identifies button pressed
if (len == sizeof(myData)) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.println(myData.command); //We got a command
if (pos = 1){
myServo.write(90); //move servo down
pos = 0;
}else{
myServo.write(0); //move servo up
pos = 1;
}
} else {
Serial.println("Error: Incorrect data length");
}
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
//setup Servo
// Allow allocation of all timers
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myServo.setPeriodHertz(50); // standard 50 hz servo
myServo.attach(SERVO_PIN, 500, 2400); // attaches the servo on pin 18 to the servo object
// using default min/max of 1000us and 2000us
// different servos may require different min/max settings
// for an accurate 0 to 180 sweep
// 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(esp_now_recv_cb_t(OnDataRecv));
}
void loop() {
// consistently ping for command from button
esp_now_register_recv_cb(esp_now_recv_cb_t(OnDataRecv));
}
#include <esp_now.h>
#include <WiFi.h>
#include <ESP32Servo.h>
// Structure to receive data
typedef struct test_struct {
bool command;
} test_struct;
// Create a struct_message called myData
test_struct myData;
// Define Servo parameters
Servo myServo;
#define SERVO_PIN 13
int lastPos = 0; // Track the last position of the servo
// Callback function that will be executed when data is received
void OnDataRecv(const esp_now_recv_info* recvInfo, const uint8_t* incomingData, int len) {
// Check if the incoming data is the expected size
if (len == sizeof(myData)) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.println(myData.command); // Print the received command
// Check and move the servo based on the last position
if (lastPos == 0) {
myServo.write(90); // Move servo to 90 degrees
lastPos = 90; // Update last position
} else {
myServo.write(0); // Move servo to 0 degrees
lastPos = 0; // Update last position
}
// Add a delay to allow servo movement
delay(1000);
} else {
Serial.println("Error: Incorrect data length");
}
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initialize Servo
ESP32PWM::allocateTimer(0);
ESP32PWM::allocateTimer(1);
ESP32PWM::allocateTimer(2);
ESP32PWM::allocateTimer(3);
myServo.setPeriodHertz(50); // Standard 50 Hz for servos
myServo.attach(SERVO_PIN, 500, 2400); // Attach the servo
myServo.write(0); // Set initial position
lastPos = 0; // Initialize position tracker
// Test Servo Movement (Initial sweep from 0 to 90, then back to 0)
for (int pos = 0; pos <= 90; pos++) {
myServo.write(pos);
delay(15);
}
for (int pos = 90; pos >= 0; pos--) {
myServo.write(pos);
delay(15);
}
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Register callback for received data
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
// Nothing is required here since everything is handled in the callback
}
I moved your topic to a more appropriate forum category @irishknots .
The Nano ESP32 category you chose is only used for discussions directly related to the Arduino Nano ESP32 board.
In the future, please take the time to pick the forum category that best suits the subject of your question. There is an "About the _____ category" topic at the top of each category that explains its purpose.