Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <esp_now.h>
- #include <WiFi.h>
- // Structure to receive data
- struct ControllerData {
- int joystick1_x;
- int joystick1_y;
- int joystick2_x;
- int joystick2_y;
- bool switch1;
- bool switch2;
- bool switch3;
- bool switch4;
- bool switch5;
- };
- // Create a struct to hold the incoming data
- ControllerData controllerData;
- // Callback function that will be executed when data is received
- void onDataRecv(const uint8_t *mac, const uint8_t *incomingData, int len) {
- memcpy(&controllerData, incomingData, sizeof(controllerData));
- }
- 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 for a callback function that will be called when data is received
- esp_now_register_recv_cb(onDataRecv);
- }
- void loop() {
- // The loop function is empty because all the work is done in the callback function
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement