Advertisement
Maderdash

reciver

Aug 24th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. // Structure to receive data
  5. struct ControllerData {
  6.     int joystick1_x;
  7.     int joystick1_y;
  8.     int joystick2_x;
  9.     int joystick2_y;
  10.     bool switch1;
  11.     bool switch2;
  12.     bool switch3;
  13.     bool switch4;
  14.     bool switch5;
  15. };
  16.  
  17. // Create a struct to hold the incoming data
  18. ControllerData controllerData;
  19.  
  20. // Callback function that will be executed when data is received
  21. void onDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
  22.     memcpy(&controllerData, incomingData, sizeof(controllerData));
  23. }
  24.  
  25. void setup() {
  26.     // Initialize Serial Monitor
  27.     Serial.begin(115200);
  28.  
  29.     // Set device as a Wi-Fi Station
  30.     WiFi.mode(WIFI_STA);
  31.  
  32.     // Init ESP-NOW
  33.     if (esp_now_init() != ESP_OK) {
  34.         Serial.println("Error initializing ESP-NOW");
  35.         return;
  36.     }
  37.  
  38.     // Register for a callback function that will be called when data is received
  39.     esp_now_register_recv_cb(onDataRecv);
  40. }
  41.  
  42. void loop() {
  43.     // The loop function is empty because all the work is done in the callback function
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement