Skip to content

Instantly share code, notes, and snippets.

@Fire7ly
Last active May 20, 2024 20:21
Show Gist options
  • Save Fire7ly/5d6e333919355b374b5547aa397f12b7 to your computer and use it in GitHub Desktop.
Save Fire7ly/5d6e333919355b374b5547aa397f12b7 to your computer and use it in GitHub Desktop.
ESPNOW_ESP32 Sender Example from https://www.youtube.com/watch?v=Ydi0M3Xd_vs
#include <esp_now.h>
#include <WiFi.h>
#define CHANNEL 1
/** This is all the data about the peer **/
esp_now_peer_info_t slave;
/** The all importent data **/
uint8_t data = 0;
void setup()
{
Serial.begin(11520);
WiFi.mode(WIFI_STA);
esp_now_init();
esp_now_register_send_cb(onDataSent);
ScanForSlave(); // WiFi.mocAddress()
esp_now_add_peer(&slave);
}
void loop()
{
esp_now_send(slave.peer_addr, &data, sizeof(data));
data++;
delay(3000);
}
/** Scan for slaves in AP mode and ad as peer if found **/
void ScanForSlave()
{
uint8_t scanResults = WiFi.scanNetworks();
for (int i = 0; i < scanResults; ++i)
{
String SSID = WiFi.SSID(i);
String BSSIDstr = WiFi.BSSIDstr(i);
if (SSID.indexOf("RX") == 0)
{
int mac[6];
if (6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]))
{
for (int ii = 0; ii < 6; ++ii)
{
slave.peer_addr[ii] = (uint8_t)mac[ii];
}
}
slave.channel = CHANNEL; // pick a channel
slave.encrypt = 0; // no encryption
break;
}
}
}
/** callback when data is send from Master to Slave **/
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status)
{
Serial.print("I sent my data -> ");
Serial.println(data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment