/*
* Modder: @red9030
* Title: SERVO ESPNOW (RX)
* Reference:
*/
/*
*****************************************************
* LIBRERIAS
*****************************************************
*/
#include <ESP32Servo.h>
#include <esp_now.h>
#include <WiFi.h>
/*
*****************************************************
* VARIABLES
*****************************************************
*/
const int LED_onboard=5;
const int servoPin = 18;
Servo servo;
//Structure data for recived message
typedef struct struct_message
{
int data_servo;
}struct_message;
struct_message dataRecived;
int Servo_pos;
/*
*****************************************************
* INICIO
*****************************************************
*/
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo.attach(servoPin);
WiFi.mode(WIFI_STA); //We place the module in station mode
// 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_recv_cb(cb_Data_recived);
}
/*
*****************************************************
* REPETICIÓN
*****************************************************
*/
void loop() {
if(dataRecived.data_servo != Servo_pos){
servo.write(dataRecived.data_servo);
delay(15);
Servo_pos = dataRecived.data_servo;
}
}
/*
*****************************************************
* FUNCIONES
*****************************************************
*/
//Callback for recived data
void cb_Data_recived(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&dataRecived, incomingData, sizeof(dataRecived));
Serial.print("Data Recived: ");
Serial.println(dataRecived.data_servo);
analogWrite(LED_onboard,dataRecived.data_servo);
}