Servos not moving

Hello all,

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);
    }
  }
}

Do not move the servos by hand.

Do not use the ESP32 as a power supply for the servos.

Please, show a legible diagram of your project.

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

This the schematic for the transmitter

and the code

#include <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t receiverMacAddress[] = {}

struct PacketData
{ 
  byte switch1Value;
  byte switch2Value;
  byte switch3Value;
  byte switch4Value;   
};
PacketData data;

// 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);
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Message sent" : "Message failed");
}

void setup() 
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != ESP_OK) 
  {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  else
  {
    Serial.println("Succes: Initialized ESP-NOW");
  }

  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_peer_info_t peerInfo;
  memcpy(peerInfo.peer_addr, receiverMacAddress, 6);
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  
  // Add peer        
  if (esp_now_add_peer(&peerInfo) != ESP_OK)
  {
    Serial.println("Failed to add peer");
    return;
  }
  else
  {
    Serial.println("Succes: Added peer");
  } 

  pinMode(15,INPUT_PULLUP);
  pinMode(16,INPUT_PULLUP);
  pinMode(17,INPUT_PULLUP);
  pinMode(18,INPUT_PULLUP);      
}
 
void loop() 
{
  data.switch1Value   = !digitalRead(15);
  data.switch2Value   = !digitalRead(16);
  data.switch3Value   = !digitalRead(17);
  data.switch4Value   = !digitalRead(18);
  
  esp_err_t result = esp_now_send(receiverMacAddress, (uint8_t *) &data, sizeof(data));
  if (result == ESP_OK) 
  {
    Serial.println("Sent with success");
  }
  else 
  {
    Serial.println("Error sending the data");
  }    
  
  delay(50);
}

Do not do this for any purpose

Do not move the servos by hand.

Read this.


  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.

Ill try something when i have time

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.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.