Advertisement
Maderdash

sender

Aug 24th, 2024
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. struct ControllerData {
  3.     int joystick1_x; // for controller1
  4.     int joystick1_y; // for controller1
  5.     int joystick2_x; // for controller2
  6.     int joystick2_y; // for controller2
  7.     bool switch1; // example buttons but can be what ever data.
  8.     bool switch2;
  9.     bool switch3;
  10.     bool switch4;
  11.     bool switch5;
  12. };
  13.  
  14. void setup() {
  15.     // Initialize Serial Monitor
  16.     Serial.begin(115200);
  17.  
  18.     // Set device as a Wi-Fi Station
  19.     WiFi.mode(WIFI_STA);
  20.  
  21.     // Init ESP-NOW
  22.     if (esp_now_init() != ESP_OK) {
  23.         Serial.println("Error initializing ESP-NOW");
  24.         return;
  25.     }
  26.  
  27.     // Register peer
  28.     esp_now_peer_info_t peerInfo;
  29.     memcpy(peerInfo.peer_addr, receiverMACAddress, 6);
  30.     peerInfo.channel = 0;  
  31.     peerInfo.encrypt = false;
  32.  
  33.     // Add peer
  34.     if (esp_now_add_peer(&peerInfo) != ESP_OK) {
  35.         Serial.println("Failed to add peer");
  36.         return;
  37.     }
  38. }
  39.  
  40. void loop() {
  41.     // Update Data with joystikck and switch states
  42.     controllerData.joystick1_x = analogRead(JOYSTICK1_X_PIN);
  43.     controllerData.joystick1_y = analogRead(JOYSTICK1_Y_PIN);
  44.     controllerData.joystick2_x = analogRead(JOYSTICK2_X_PIN);
  45.     controllerData.joystick2_y = analogRead(JOYSTICK2_Y_PIN);
  46.     controllerData.switch1     = digitalRead(SWITCH1_PIN);
  47.     controllerData.switch2     = digitalRead(SWITCH2_PIN);
  48.     controllerData.switch3     = digitalRead(SWITCH3_PIN);
  49.     controllerData.switch4     = digitalRead(SWITCH4_PIN);
  50.     controllerData.switch5     = digitalRead(SWITCH5_PIN);
  51.  
  52.     // Send data
  53.     esp_err_t result = esp_now_send(receiverMACAddress, (uint8_t *) &controllerData, sizeof(controllerData));
  54.  
  55.     if (result == ESP_OK) {
  56.         Serial.println("Sent with success");
  57.     } else {
  58.         Serial.println("Error sending the data");
  59.     }
  60.  
  61.     delay(1000); // Send data every second
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement