Code A: ESP32 C3 (Host)
#include
// Pin Definitions
#define BUTTON_PIN 4 // GPIO 3 for the button
// Message Structure
typedef struct message {
bool buttonPressed; // Simple boolean message
} message;
message myMessage;
// Define Receiver MAC Address (Replace with Actual S3 MAC)
uint8_t receiverAddress[] = {0x24, 0x58, 0x7C, 0xE3, 0xFF, 0xB8}; // Example MAC
void setup() {
Serial.begin(115200);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Enable Internal Pull-Up
// Initialize Wi-Fi
WiFi.mode(WIFI_STA);
delay(1000);
Serial.println("test");
// Initialize ESP-NOW
if (esp_now_init() == ESP_OK) {
Serial.println("ESP-NOW Init success");
} else {
Serial.println("ESP-NOW Init fail");
return;
}
// Register the Receiver as a Peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverAddress, 6);
peerInfo.channel = 0; // Default Wi-Fi Channel
peerInfo.encrypt = false; // No Encryption
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
if (digitalRead(BUTTON_PIN) == LOW) { // Button Pressed
myMessage.buttonPressed = true; // Send Button Pressed = true
Serial.println("pressed");
esp_err_t result = esp_now_send(receiverAddress, (uint8_t *)&myMessage, sizeof(myMessage));
if (result == ESP_OK) {
Serial.println("Message Sent: Button Pressed");
} else {
Serial.println("Message Failed to Send");
}
delay(500); // Debounce delay
}
}