i can't get the code below to work, the servos are active, when i move them by hand and then put power to the esp32 they move to the initial position.
I have determined that the input signals are received but nothing happens.
The codes compiles without errors.
if more info is needed please let me know.
thanks in advance.
#include <esp_now.h>
#include <WiFi.h>
//#include <ESP32Servo.h>
#include <ServoEasing.hpp>
ServoEasing Servo1;
ServoEasing Servo2;
#define SIGNAL_TIMEOUT 1000 // This is signal timeout in milli seconds. We will reset the data if no signal
#define SERVO1_PIN 5
#define SERVO2_PIN 18
#define DISABLE_COMPLEX_FUNCTIONS
#define START_DEFREE_VALUE 1500
unsigned long lastRecvTime = 0;
struct PacketData {
byte switch1Value;
byte switch2Value;
byte switch3Value;
byte switch4Value;
};
PacketData receiverData;
//Assign default input received values
void setInputDefaultValues() {
receiverData.switch1Value = 0;
receiverData.switch2Value = 0;
receiverData.switch3Value = 0;
receiverData.switch4Value = 0;
}
// callback function that will be executed when data is received
void OnDataRecv(const esp_now_recv_info* mac, const uint8_t* incomingData, int len) {
if (len == 0) {
return;
}
memcpy(&receiverData, incomingData, sizeof(receiverData));
/*char inputValuesString[100];
sprintf(inputValuesString,
"%3d,%3d,%3d,%3d",
receiverData.switch1Value,
receiverData.switch2Value,
receiverData.switch3Value,
receiverData.switch4Value);
Serial.println(inputValuesString);
*/
lastRecvTime = millis();
}
void setUpPinModes() {
setInputDefaultValues();
}
void setup() {
setUpPinModes();
Servo1.attach(SERVO1_PIN, 1500);
Servo2.attach(SERVO2_PIN, 1500);
Servo1.setSpeed(30);
Servo2.setSpeed(30);
Servo1.setEasingType(EASE_LINEAR);
Servo2.setEasingType(EASE_LINEAR);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
//Check Signal lost.
unsigned long now = millis();
if (now - lastRecvTime > SIGNAL_TIMEOUT) {
if (receiverData.switch1Value == 1) {
Servo1.startEaseTo(2000, 40, START_UPDATE_BY_INTERRUPT);
} else if (receiverData.switch2Value == 1) {
Servo1.startEaseTo(1500, 40, START_UPDATE_BY_INTERRUPT);
} else {
Servo1.startEaseTo(1000, 40, START_UPDATE_BY_INTERRUPT);
}
if (receiverData.switch3Value == 1) {
Servo2.startEaseTo(1000, 40, START_UPDATE_BY_INTERRUPT);
} else if (receiverData.switch4Value == 1) {
Servo2.startEaseTo(2000, 40, START_UPDATE_BY_INTERRUPT);
} else {
Servo2.startEaseTo(1500, 40, START_UPDATE_BY_INTERRUPT);
}
}
}
i am powering the servos from the esp32 only for test purposes.
i only moved them when the power was off, this happens all the time when working with rc boats
unsigned long now = millis();
if (now - lastRecvTime > SIGNAL_TIMEOUT) {
If I understand your code correctly, you are transmitting data appropriately every 50 milliseconds. You are presumably receiving the data at the same interval. If that is the case then the statement above never becomes true and your servo instructions are never executed.
Save yourself time and start with a sketch provided as library example.
The example MUST work before you try your own code.
If the example sketch is not working it is very likely your wiring is wrong, your power source to weak or either the ESP or the servo has is already a defect.
Notify us which example sketch is working for you.