Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct ControllerData {
- int joystick1_x; // for controller1
- int joystick1_y; // for controller1
- int joystick2_x; // for controller2
- int joystick2_y; // for controller2
- bool switch1; // example buttons but can be what ever data.
- bool switch2;
- bool switch3;
- bool switch4;
- bool switch5;
- };
- void setup() {
- // Initialize Serial Monitor
- Serial.begin(115200);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- // Init ESP-NOW
- if (esp_now_init() != ESP_OK) {
- Serial.println("Error initializing ESP-NOW");
- return;
- }
- // 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;
- }
- }
- void loop() {
- // Update Data with joystikck and switch states
- controllerData.joystick1_x = analogRead(JOYSTICK1_X_PIN);
- controllerData.joystick1_y = analogRead(JOYSTICK1_Y_PIN);
- controllerData.joystick2_x = analogRead(JOYSTICK2_X_PIN);
- controllerData.joystick2_y = analogRead(JOYSTICK2_Y_PIN);
- controllerData.switch1 = digitalRead(SWITCH1_PIN);
- controllerData.switch2 = digitalRead(SWITCH2_PIN);
- controllerData.switch3 = digitalRead(SWITCH3_PIN);
- controllerData.switch4 = digitalRead(SWITCH4_PIN);
- controllerData.switch5 = digitalRead(SWITCH5_PIN);
- // Send data
- esp_err_t result = esp_now_send(receiverMACAddress, (uint8_t *) &controllerData, sizeof(controllerData));
- if (result == ESP_OK) {
- Serial.println("Sent with success");
- } else {
- Serial.println("Error sending the data");
- }
- delay(1000); // Send data every second
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement