/*
 * Modder:  @red9030
 * Title:   SERVO ESPNOW (TX)
 * Reference: 
*/
/*
 *****************************************************
 *    LIBRERIAS
 *****************************************************
*/
#include <esp_now.h>
#include <WiFi.h>
/*
 *****************************************************
 *    VARIABLES
 *****************************************************
*/
#define b_yellow 19  //Button for 90º 
#define b_green 18   //Button for 180º
const int LED_onboard=5;

//MAC-ADDRESS
uint8_t receiverMacAddress[] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};  //Here mac for Receptor (RX)

//Structure data for send message
typedef struct struct_message
{
  int data_servo;
}struct_message;
struct_message dataSend;
/*
 *****************************************************
 *    INICIO
 *****************************************************
*/
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(b_green, INPUT_PULLUP);
  pinMode(b_yellow, INPUT_PULLUP);

  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_send_cb(cb_Data_send); //Message Sending and Message Status

 // Register peer
  esp_now_peer_info_t peerInfo;
  memset(&peerInfo, 0, sizeof(esp_now_peer_info_t));
  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");
  } 
}
/*
 *****************************************************
 *    REPETICIÓN
 *****************************************************
*/
void loop() {
if(!digitalRead(b_yellow)){
    dataSend.data_servo = 90;
    esp_now_send(receiverMacAddress, (uint8_t *) &dataSend, sizeof(dataSend));
    Serial.println("Rotate 90");
    delay(200);
  }
  if(!digitalRead(b_green)){
    dataSend.data_servo = 180;
    esp_now_send(receiverMacAddress, (uint8_t *) &dataSend, sizeof(dataSend));
    Serial.println("Rotate 180");
    delay(200);
  }
}
/*
 *****************************************************
 *    FUNCIONES
 *****************************************************
*/
//Callback for send data
void cb_Data_send(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");
  //analogWrite(LED_onboard,);
}
Loading
esp32-devkit-v1