Advertisement
pleasedontcode

ESP32 Sender

Jul 28th, 2024
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 0.89 KB | Source Code | 0 0
  1. #include <esp_now.h>
  2. #include <WiFi.h>
  3.  
  4. uint8_t broadcastAddress[] = {0x24, 0x0A, 0xC4, 0x35, 0x3A, 0x90}; // MAC address of the receiver
  5.  
  6. void setup() {
  7.   Serial.begin(115200);
  8.   WiFi.mode(WIFI_STA);
  9.  
  10.   if (esp_now_init() != ESP_OK) {
  11.     Serial.println("Error initializing ESP-NOW");
  12.     return;
  13.   }
  14.  
  15.   esp_now_peer_info_t peerInfo;
  16.   memcpy(peerInfo.peer_addr, broadcastAddress, 6);
  17.   peerInfo.channel = 0;
  18.   peerInfo.encrypt = false;
  19.  
  20.   if (esp_now_add_peer(&peerInfo) != ESP_OK) {
  21.     Serial.println("Failed to add peer");
  22.     return;
  23.   }
  24. }
  25.  
  26. void loop() {
  27.   const char *message = "Hello, ESP-NOW!";
  28.   esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *)message, strlen(message));
  29.  
  30.   if (result == ESP_OK) {
  31.     Serial.println("Message sent successfully");
  32.   } else {
  33.     Serial.println("Error sending message");
  34.   }
  35.  
  36.   delay(1000); // Send message every second
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement