Upload files to "espnow_sender_code"

This commit is contained in:
Adema 2025-03-19 20:39:55 +01:00
commit e8bb0a36f3

View File

@ -0,0 +1,61 @@
#include <esp_now.h>
#include <WiFi.h>
// Structure to send data
typedef struct struct_message {
int a0;
int a1;
int a2;
} struct_message;
// Create a struct_message instance
struct_message myData;
// Replace with your receiver's MAC Address
uint8_t receiverMAC[] = {0x64, 0xe8, 0x33, 0x86, 0x41, 0xa8}; // Change XX to match receiver MAC
// Callback function when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Success" : "Fail");
}
void setup() {
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 send callback
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverMAC, 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() {
// Read analog values from A0 to A2
myData.a0 = analogRead(A0);
myData.a1 = analogRead(A1);
myData.a2 = analogRead(A2);
// Send data to receiver
esp_now_send(receiverMAC, (uint8_t *) &myData, sizeof(myData));
//delay(1000); // Send every second
}