Advertisement
pleasedontcode

ESP32 Receiver

Jul 28th, 2024
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.57 KB | Source Code | 0 0
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. void onReceive(const uint8_t *mac_addr, const uint8_t *data, int data_len) {
  5.   char message[data_len + 1];
  6.   memcpy(message, data, data_len);
  7.   message[data_len] = '\0';
  8.   Serial.print("Message received: ");
  9.   Serial.println(message);
  10. }
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14.   WiFi.mode(WIFI_STA);
  15.  
  16.   if (esp_now_init() != ESP_OK) {
  17.     Serial.println("Error initializing ESP-NOW");
  18.     return;
  19.   }
  20.  
  21.   esp_now_register_recv_cb(onReceive);
  22. }
  23.  
  24. void loop() {
  25.   // Nothing to do here, everything is handled in the callback
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement