Skip to content

Instantly share code, notes, and snippets.

@Ajak58a
Created August 5, 2024 16:06
Show Gist options
  • Save Ajak58a/2691ecadc9f97f0afb0ca5c45d8b4100 to your computer and use it in GitHub Desktop.
Save Ajak58a/2691ecadc9f97f0afb0ca5c45d8b4100 to your computer and use it in GitHub Desktop.
Communicating over ESP NOW
#include <WiFi.h>
#include <esp_now.h>
bool buttonDown = false;
bool ledOn = false;
#define STATUS_LED 23
#define STATUS_BUTTON 22
void formatMacAddress(const uint8_t *macAddr, char *buffer, int maxLength)
{
snprintf(buffer, maxLength, "%02x:%02x:%02x:%02x:%02x:%02x", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
}
void receiveCallback(const uint8_t *macAddr, const uint8_t *data, int dataLen)
{
char buffer[ESP_NOW_MAX_DATA_LEN + 1];
int msgLen = min(ESP_NOW_MAX_DATA_LEN, dataLen);
strncpy(buffer, (const char *)data, msgLen);
buffer[msgLen] = 0;
char macStr[18];
formatMacAddress(macAddr, macStr, 18);
Serial.printf("Received message from: %s - %s\n", macStr, buffer);
if (strcmp("on", buffer) == 0)
{
ledOn = true;
}
else
{
ledOn = false;
}
digitalWrite(STATUS_LED, ledOn);
}
void sentCallback(const uint8_t *macAddr, esp_now_send_status_t status)
{
char macStr[18];
formatMacAddress(macAddr, macStr, 18);
Serial.print("Last Packet Sent to: ");
Serial.println(macStr);
Serial.print("Last Packet Send Status: ");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
void broadcast(const String &message)
{
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
esp_now_peer_info_t peerInfo = {};
memcpy(&peerInfo.peer_addr, broadcastAddress, 6);
if (!esp_now_is_peer_exist(broadcastAddress))
{
esp_now_add_peer(&peerInfo);
}
esp_err_t result = esp_now_send(broadcastAddress, (const uint8_t *)message.c_str(), message.length());
if (result == ESP_OK)
{
Serial.println("Broadcast message success");
}
else if (result == ESP_ERR_ESPNOW_NOT_INIT)
{
Serial.println("ESP-NOW not Init.");
}
else if (result == ESP_ERR_ESPNOW_ARG)
{
Serial.println("Invalid Argument");
}
else if (result == ESP_ERR_ESPNOW_INTERNAL)
{
Serial.println("Internal Error");
}
else if (result == ESP_ERR_ESPNOW_NO_MEM)
{
Serial.println("ESP_ERR_ESPNOW_NO_MEM");
}
else if (result == ESP_ERR_ESPNOW_NOT_FOUND)
{
Serial.println("Peer not found.");
}
else
{
Serial.println("Unknown error");
}
}
void setup()
{
Serial.begin(115200);
delay(1000);
WiFi.mode(WIFI_STA);
Serial.println("ESP-NOW Broadcast Demo");
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
WiFi.disconnect();
if (esp_now_init() == ESP_OK)
{
Serial.println("ESP-NOW Init Success");
esp_now_register_recv_cb(esp_now_recv_cb_t(receiveCallback));
esp_now_register_send_cb(sentCallback);
}
else
{
Serial.println("ESP-NOW Init Failed");
delay(3000);
ESP.restart();
}
pinMode(STATUS_BUTTON, INPUT_PULLUP);
pinMode(STATUS_LED, OUTPUT);
}
void loop()
{
if (digitalRead(STATUS_BUTTON))
{
if (!buttonDown)
{
buttonDown = true;
ledOn = !ledOn;
digitalWrite(STATUS_LED, ledOn);
if (ledOn)
{
broadcast("on");
}
else
{
broadcast("off");
}
}
delay(500);
}
else
{
buttonDown = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment