Minimalist ESP-NOW broadcast program?

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
Hey all,

I'm trying to begin making ESP-NOW programs for my ESP-32 chips. Problem is that I barely understand it, and all the resources I can find online... well, they suck. What little there is for broadcasting, much less for broadcasting as a form of two-way communication, transmits a lot of complex data that I don't want to use for this simple experiment and is one-way.

I want to make a simple program that allows a chip to broadcast the value of a single integer out, and receive the value of an integer should it be broadcast from any other device. None of these structs and pointers and memcpy and whatnot if at all possible besides what's needed. Just bare-bones transmission. This way I can get a better idea of how this thing fundamentally works. If anyone can help me out in any way, I'd appreciate it greatly.
 
Last edited:

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
Well, from a quick look about (to see what ESP-NOW is all about), they don't all suck but they do require basic programming. It's a skill that's required if you want to use those types of devices.
https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/

I know how to program perfectly well. I've already gotten offline programs working on the things.

I've already checked randomnerdtutorials and seen that article as well as the videos you sent me. It's not helpful for my goal. I specifically am trying to use broadcasts sent to all chips indiscriminately to send single variables. These tutorials do suck for my needs.
 

Papabravo

Joined Feb 24, 2006
22,065
Just because you think such a thing is possible, or meaningful, does not automatically mean that it is. ALL communications protocols have overhead that needs to be dealt with in order to pass even insignificant amounts of data. A prime example would be TCP/IP over Ethernet. To pass a single byte of data over such a link requires a significant amount of overhead to establish the connection and to keep track of what is going on over the link. That single byte of data will have an absolutely astounding amount of overhead. You will not make much progress in your quest if you insist on holding on to your current position. You will be better served by a relentless investigation of the reason(s) behind all of the various overhead items. Progress will be next to impossible otherwise. Not everyone can adapt to this kind of work, and you might be one of those people. OTOH many people have put forth the effort and have reaped substantial rewards. The potential results are entirely in your hands.
 

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
Just because you think such a thing is possible, or meaningful, does not automatically mean that it is. ALL communications protocols have overhead that needs to be dealt with in order to pass even insignificant amounts of data. A prime example would be TCP/IP over Ethernet. To pass a single byte of data over such a link requires a significant amount of overhead to establish the connection and to keep track of what is going on over the link. That single byte of data will have an absolutely astounding amount of overhead. You will not make much progress in your quest if you insist on holding on to your current position. You will be better served by a relentless investigation of the reason(s) behind all of the various overhead items. Progress will be next to impossible otherwise. Not everyone can adapt to this kind of work, and you might be one of those people. OTOH many people have put forth the effort and have reaped substantial rewards. The potential results are entirely in your hands.
If my idea is complicated even being so minimalistic then so be it. I just need to know how to make it. None of the info I can find explains how any of this works, what's going on, and how to actually use it from the fundamentals. They show specific examples and cases that aren't possible to generalize. I just need the simplest possible program for my idea.
 

nsaspook

Joined Aug 27, 2009
16,273
I know how to program perfectly well. I've already gotten offline programs working on the things.

I've already checked randomnerdtutorials and seen that article as well as the videos you sent me. It's not helpful for my goal. I specifically am trying to use broadcasts sent to all chips indiscriminately to send single variables. These tutorials do suck for my needs.
If you could program perfectly well then you would not be asking this question and wanting someone else to do the work for you of finding examples.

https://developer.espressif.com/blog/arduino-esp-now-lib/
https://developer.espressif.com/blog/arduino-esp-now-lib/#broadcasting-data
Broadcasting Data

To broadcast data to multiple devices, you can use the broadcast address FF:FF:FF:FF:FF:FF. This address will send data to all devices in the network. This address is available as ESP_NOW.BROADCAST_ADDR in the ESP-NOW library to simplify its use.
I've never used ESP-NOW or a espressif device but the examples given are perfectly clear to me as a starting point for something like you want.
 
Last edited:

be80be

Joined Jul 5, 2008
2,394
These tutorials why use them these are really clear

Code:
/*
    ESP-NOW Broadcast Master
    Lucas Saavedra Vaz - 2024

    This sketch demonstrates how to broadcast messages to all devices within the ESP-NOW network.
    This example is intended to be used with the ESP-NOW Broadcast Slave example.

    The master device will broadcast a message every 5 seconds to all devices within the network.
    This will be done using by registering a peer object with the broadcast address.

    The slave devices will receive the broadcasted messages and print them to the Serial Monitor.
*/

#include "ESP32_NOW.h"
#include "WiFi.h"

#include <esp_mac.h>  // For the MAC2STR and MACSTR macros

/* Definitions */

#define ESPNOW_WIFI_CHANNEL 6

/* Classes */

// Creating a new class that inherits from the ESP_NOW_Peer class is required.

class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
public:
  // Constructor of the class using the broadcast address
  ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}

  // Destructor of the class
  ~ESP_NOW_Broadcast_Peer() {
    remove();
  }

  // Function to properly initialize the ESP-NOW and register the broadcast peer
  bool begin() {
    if (!ESP_NOW.begin() || !add()) {
      log_e("Failed to initialize ESP-NOW or register the broadcast peer");
      return false;
    }
    return true;
  }

  // Function to send a message to all devices within the network
  bool send_message(const uint8_t *data, size_t len) {
    if (!send(data, len)) {
      log_e("Failed to broadcast message");
      return false;
    }
    return true;
  }
};

/* Global Variables */

uint32_t msg_count = 0;

// Create a broadcast peer object
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

/* Main */

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // Initialize the Wi-Fi module
  WiFi.mode(WIFI_STA);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
  while (!WiFi.STA.started()) {
    delay(100);
  }

  Serial.println("ESP-NOW Example - Broadcast Master");
  Serial.println("Wi-Fi parameters:");
  Serial.println("  Mode: STA");
  Serial.println("  MAC Address: " + WiFi.macAddress());
  Serial.printf("  Channel: %d\n", ESPNOW_WIFI_CHANNEL);

  // Register the broadcast peer
  if (!broadcast_peer.begin()) {
    Serial.println("Failed to initialize broadcast peer");
    Serial.println("Reebooting in 5 seconds...");
    delay(5000);
    ESP.restart();
  }

  Serial.println("Setup complete. Broadcasting messages every 5 seconds.");
}

void loop() {
  // Broadcast a message to all devices within the network
  char data[32];
  snprintf(data, sizeof(data), "Hello, World! #%lu", msg_count++);

  Serial.printf("Broadcasting message: %s\n", data);

  if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) {
    Serial.println("Failed to broadcast message");
  }

  delay(5000);
}
master above code
slave below code
Code:
/*
    ESP-NOW Broadcast Slave
    Lucas Saavedra Vaz - 2024

    This sketch demonstrates how to receive broadcast messages from a master device using the ESP-NOW protocol.

    The master device will broadcast a message every 5 seconds to all devices within the network.

    The slave devices will receive the broadcasted messages. If they are not from a known master, they will be registered as a new master
    using a callback function.
*/

#include "ESP32_NOW.h"
#include "WiFi.h"

#include <esp_mac.h>  // For the MAC2STR and MACSTR macros

#include <vector>

/* Definitions */

#define ESPNOW_WIFI_CHANNEL 6

/* Classes */

// Creating a new class that inherits from the ESP_NOW_Peer class is required.

class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
  // Constructor of the class
  ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}

  // Destructor of the class
  ~ESP_NOW_Peer_Class() {}

  // Function to register the master peer
  bool add_peer() {
    if (!add()) {
      log_e("Failed to register the broadcast peer");
      return false;
    }
    return true;
  }

  // Function to print the received messages from the master
  void onReceive(const uint8_t *data, size_t len, bool broadcast) {
    Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
    Serial.printf("  Message: %s\n", (char *)data);
  }
};

/* Global Variables */

// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;

/* Callbacks */

// Callback called when an unknown peer sends a message
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
  if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
    Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
    Serial.println("Registering the peer as a master");

    ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

    masters.push_back(new_master);
    if (!masters.back().add_peer()) {
      Serial.println("Failed to register the new master");
      return;
    }
  } else {
    // The slave will only receive broadcast messages
    log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
    log_v("Igorning the message");
  }
}

/* Main */

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // Initialize the Wi-Fi module
  WiFi.mode(WIFI_STA);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
  while (!WiFi.STA.started()) {
    delay(100);
  }

  Serial.println("ESP-NOW Example - Broadcast Slave");
  Serial.println("Wi-Fi parameters:");
  Serial.println("  Mode: STA");
  Serial.println("  MAC Address: " + WiFi.macAddress());
  Serial.printf("  Channel: %d\n", ESPNOW_WIFI_CHANNEL);

  // Initialize the ESP-NOW protocol
  if (!ESP_NOW.begin()) {
    Serial.println("Failed to initialize ESP-NOW");
    Serial.println("Reeboting in 5 seconds...");
    delay(5000);
    ESP.restart();
  }

  // Register the new peer callback
  ESP_NOW.onNewPeer(register_new_master, NULL);

  Serial.println("Setup complete. Waiting for a master to broadcast a message...");
}

void loop() {
  delay(1000);
}
 

Papabravo

Joined Feb 24, 2006
22,065
If my idea is complicated even being so minimalistic then so be it. I just need to know how to make it. None of the info I can find explains how any of this works, what's going on, and how to actually use it from the fundamentals. They show specific examples and cases that aren't possible to generalize. I just need the simplest possible program for my idea.
Well, something is clearly missing. 40 years ago, when I first encountered TCP/IP over Ethernet I was in a similar position. My solution at the time was to put my nose to the grindstone and dissect the code for putting packets together and pulling them apart. I did not have the luxury of access to people who could answer my questions. I just had to work it out or resign. Fortunately, I've never had to resign because I couldn't decipher somebody else's code. In fact, I made a career of doing disaster recovery after prima donnas abandoned their companies after throwing massive hissy fits.
 

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
If you could program perfectly well then you would not be asking this question and wanting someone else to do the work for you of finding examples.

https://developer.espressif.com/blog/arduino-esp-now-lib/
https://developer.espressif.com/blog/arduino-esp-now-lib/#broadcasting-data
Broadcasting Data

To broadcast data to multiple devices, you can use the broadcast address FF:FF:FF:FF:FF:FF. This address will send data to all devices in the network. This address is available as ESP_NOW.BROADCAST_ADDR in the ESP-NOW library to simplify its use.
Considering that you have said nothing I am not already aware of so far, I'd say you have an interesting way of saying you're just as clueless as me on this topic.

There's plenty that isn't laid out clearly in any tutorial, on the callback function for receiving data, such as:

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)

Nearly every tutorial uses these pointer values. Why is that? Why are they needed? Why can't it just receive a single int as a parameter? Can it? Enlighten me!

Your tutorial you provided says that a class inheriting from ESP_NOW_Peer is required for a receiver program to work. Yet nerdtutorials says no such thing:

https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/

The way that peers are added and tracked by the broadcaster (and in your tutorial by the receiver as well) are completely different. So who's right?

And, for the record, viewing a simple program is how everyone learns programming. Surely such a wise individual as yourself is familiar with hello world programs. Why do we use those? Because they are a simple way of illustrating the basics of language. Likewise, there are ways to show the basics of esp now.
 

be80be

Joined Jul 5, 2008
2,394
The len() function is used to return an integer value which indicates the number of items in an object

Code:
#pragma once

#include "esp_wifi_types.h"
#include "Print.h"
#include "esp_now.h"
#include "esp32-hal-log.h"
#include "esp_mac.h"

class ESP_NOW_Peer;  //forward declaration for friend function

class ESP_NOW_Class : public Print {
public:
  const uint8_t BROADCAST_ADDR[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

  ESP_NOW_Class();
  ~ESP_NOW_Class();

  bool begin(const uint8_t *pmk = NULL /* 16 bytes */);
  bool end();

  int getTotalPeerCount();
  int getEncryptedPeerCount();

  int availableForWrite();
  size_t write(const uint8_t *data, size_t len);
  size_t write(uint8_t data) {
    return write(&data, 1);
  }

  void onNewPeer(void (*cb)(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg), void *arg);
  bool removePeer(ESP_NOW_Peer &peer);
};

class ESP_NOW_Peer {
private:
  uint8_t mac[6];
  uint8_t chan;
  wifi_interface_t ifc;
  bool encrypt;
  uint8_t key[16];

protected:
  bool added;
  bool add();
  bool remove();
  size_t send(const uint8_t *data, int len);

  ESP_NOW_Peer(const uint8_t *mac_addr, uint8_t channel = 0, wifi_interface_t iface = WIFI_IF_AP, const uint8_t *lmk = NULL);

public:
  virtual ~ESP_NOW_Peer() {}

  const uint8_t *addr() const;
  bool addr(const uint8_t *mac_addr);

  uint8_t getChannel() const;
  bool setChannel(uint8_t channel);

  wifi_interface_t getInterface() const;
  bool setInterface(wifi_interface_t iface);

  bool isEncrypted() const;
  bool setKey(const uint8_t *lmk);

  operator bool() const;

  //optional callbacks to be implemented by the upper class
  virtual void onReceive(const uint8_t *data, size_t len, bool broadcast) {
    log_i("Received %d bytes from " MACSTR " %s", len, MAC2STR(mac), broadcast ? "(broadcast)" : "");
  }

  virtual void onSent(bool success) {
    log_i("Message transmission to peer " MACSTR " %s", MAC2STR(mac), success ? "successful" : "failed");
  }

  friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer &);
};

extern ESP_NOW_Class ESP_NOW;
have a look in there
 

nsaspook

Joined Aug 27, 2009
16,273
Considering that you have said nothing I am not already aware of so far, I'd say you have an interesting way of saying you're just as clueless as me on this topic.

There's plenty that isn't laid out clearly in any tutorial, on the callback function for receiving data, such as:

void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len)

Nearly every tutorial uses these pointer values. Why is that? Why are they needed? Why can't it just receive a single int as a parameter? Can it? Enlighten me!

Your tutorial you provided says that a class inheriting from ESP_NOW_Peer is required for a receiver program to work. Yet nerdtutorials says no such thing:

https://randomnerdtutorials.com/esp-now-esp32-arduino-ide/

The way that peers are added and tracked by the broadcaster (and in your tutorial by the receiver as well) are completely different. So who's right?

And, for the record, viewing a simple program is how everyone learns programming. Surely such a wise individual as yourself is familiar with hello world programs. Why do we use those? Because they are a simple way of illustrating the basics of language. Likewise, there are ways to show the basics of esp now.
Any good programmer could take the vendor example (and others) apart and rebuild it to something they can use. Are you telling me you don't understand why pointers are be used in callback that would modify the calling variables?

Show me your work on this problem instead of asking for a set answer in response.
 
Last edited:

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
These tutorials why use them these are really clear

Code:
/*
    ESP-NOW Broadcast Master
    Lucas Saavedra Vaz - 2024

    This sketch demonstrates how to broadcast messages to all devices within the ESP-NOW network.
    This example is intended to be used with the ESP-NOW Broadcast Slave example.

    The master device will broadcast a message every 5 seconds to all devices within the network.
    This will be done using by registering a peer object with the broadcast address.

    The slave devices will receive the broadcasted messages and print them to the Serial Monitor.
*/

#include "ESP32_NOW.h"
#include "WiFi.h"

#include <esp_mac.h>  // For the MAC2STR and MACSTR macros

/* Definitions */

#define ESPNOW_WIFI_CHANNEL 6

/* Classes */

// Creating a new class that inherits from the ESP_NOW_Peer class is required.

class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer {
public:
  // Constructor of the class using the broadcast address
  ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {}

  // Destructor of the class
  ~ESP_NOW_Broadcast_Peer() {
    remove();
  }

  // Function to properly initialize the ESP-NOW and register the broadcast peer
  bool begin() {
    if (!ESP_NOW.begin() || !add()) {
      log_e("Failed to initialize ESP-NOW or register the broadcast peer");
      return false;
    }
    return true;
  }

  // Function to send a message to all devices within the network
  bool send_message(const uint8_t *data, size_t len) {
    if (!send(data, len)) {
      log_e("Failed to broadcast message");
      return false;
    }
    return true;
  }
};

/* Global Variables */

uint32_t msg_count = 0;

// Create a broadcast peer object
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

/* Main */

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // Initialize the Wi-Fi module
  WiFi.mode(WIFI_STA);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
  while (!WiFi.STA.started()) {
    delay(100);
  }

  Serial.println("ESP-NOW Example - Broadcast Master");
  Serial.println("Wi-Fi parameters:");
  Serial.println("  Mode: STA");
  Serial.println("  MAC Address: " + WiFi.macAddress());
  Serial.printf("  Channel: %d\n", ESPNOW_WIFI_CHANNEL);

  // Register the broadcast peer
  if (!broadcast_peer.begin()) {
    Serial.println("Failed to initialize broadcast peer");
    Serial.println("Reebooting in 5 seconds...");
    delay(5000);
    ESP.restart();
  }

  Serial.println("Setup complete. Broadcasting messages every 5 seconds.");
}

void loop() {
  // Broadcast a message to all devices within the network
  char data[32];
  snprintf(data, sizeof(data), "Hello, World! #%lu", msg_count++);

  Serial.printf("Broadcasting message: %s\n", data);

  if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) {
    Serial.println("Failed to broadcast message");
  }

  delay(5000);
}
master above code
slave below code
Code:
/*
    ESP-NOW Broadcast Slave
    Lucas Saavedra Vaz - 2024

    This sketch demonstrates how to receive broadcast messages from a master device using the ESP-NOW protocol.

    The master device will broadcast a message every 5 seconds to all devices within the network.

    The slave devices will receive the broadcasted messages. If they are not from a known master, they will be registered as a new master
    using a callback function.
*/

#include "ESP32_NOW.h"
#include "WiFi.h"

#include <esp_mac.h>  // For the MAC2STR and MACSTR macros

#include <vector>

/* Definitions */

#define ESPNOW_WIFI_CHANNEL 6

/* Classes */

// Creating a new class that inherits from the ESP_NOW_Peer class is required.

class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
  // Constructor of the class
  ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}

  // Destructor of the class
  ~ESP_NOW_Peer_Class() {}

  // Function to register the master peer
  bool add_peer() {
    if (!add()) {
      log_e("Failed to register the broadcast peer");
      return false;
    }
    return true;
  }

  // Function to print the received messages from the master
  void onReceive(const uint8_t *data, size_t len, bool broadcast) {
    Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
    Serial.printf("  Message: %s\n", (char *)data);
  }
};

/* Global Variables */

// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;

/* Callbacks */

// Callback called when an unknown peer sends a message
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
  if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
    Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
    Serial.println("Registering the peer as a master");

    ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);

    masters.push_back(new_master);
    if (!masters.back().add_peer()) {
      Serial.println("Failed to register the new master");
      return;
    }
  } else {
    // The slave will only receive broadcast messages
    log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
    log_v("Igorning the message");
  }
}

/* Main */

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }

  // Initialize the Wi-Fi module
  WiFi.mode(WIFI_STA);
  WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
  while (!WiFi.STA.started()) {
    delay(100);
  }

  Serial.println("ESP-NOW Example - Broadcast Slave");
  Serial.println("Wi-Fi parameters:");
  Serial.println("  Mode: STA");
  Serial.println("  MAC Address: " + WiFi.macAddress());
  Serial.printf("  Channel: %d\n", ESPNOW_WIFI_CHANNEL);

  // Initialize the ESP-NOW protocol
  if (!ESP_NOW.begin()) {
    Serial.println("Failed to initialize ESP-NOW");
    Serial.println("Reeboting in 5 seconds...");
    delay(5000);
    ESP.restart();
  }

  // Register the new peer callback
  ESP_NOW.onNewPeer(register_new_master, NULL);

  Serial.println("Setup complete. Waiting for a master to broadcast a message...");
}

void loop() {
  delay(1000);
}
I don't want a master slave system though. I want two-way communication where the chips are communicate independently and neither is in control of the other. Additionally, other tutorials show a completely different way of doing this, like below. Notice that the ESP_NOW_Peer class, which your example said was required, isn't used here.

Sender:
/*
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
#include <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR RECEIVER MAC Address
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  int b;
  float c;
  bool d;
} struct_message;

// Create a struct_message called myData
struct_message myData;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  Serial.print("\r\nLast Packet Send Status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  // Init Serial Monitor
  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;
  }

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);
 
  // Register peer
  memcpy(peerInfo.peer_addr, broadcastAddress, 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() {
  // Set values to send
  strcpy(myData.a, "THIS IS A CHAR");
  myData.b = random(1,20);
  myData.c = 1.2;
  myData.d = false;
 
  // Send message via ESP-NOW
  esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
  
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}
 

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
Any good programmer could take the vendor example (and others) apart and rebuild it to something they can use. Are you telling me you don't understand why pointers are be used in callback than would modify the calling variables?

Show me your work on this problem instead of asking for a set answer in response.
I'm sorry to hear that you're not a good programmer, but perhaps we can improve together! Good luck my friend! Anyways, I need to shift my attention to answers on here. Goodbye!

PS: No, I don't know why, "pointers are be used in callback than would modify the calling variables." Not sure anyone here even understands what you mean by that. Perhaps you should increase you knowledge of the English language along with your knowledge of programming languages!
 
Last edited:

be80be

Joined Jul 5, 2008
2,394
You have to tell it how long the data is how would they know where it started and ended

This doesn't have to be text
Code:
snprintf(data, sizeof(data), "Hello, World! #%lu", msg_count++);
This is how you would work it
Code:
size_t send(const uint8_t *data, int len);
Its basically data = what you want to send
 
Last edited:

nsaspook

Joined Aug 27, 2009
16,273
I'm sorry to hear that you're not a good programmer, but perhaps we can improve together! Good luck my friend! Anyways, I need to shift my attention to answers on here. Goodbye!

PS: No, I don't know why pointers are be used in callback than would modify the calling variables.
I hope you have success.
 

be80be

Joined Jul 5, 2008
2,394
I didn't write the code espressif did it included when you add ESP32 to arduino ide

You can auto pair but you sill need one master to do that
 
Last edited:

Papabravo

Joined Feb 24, 2006
22,065
I'm sorry to hear that you're not a good programmer, but perhaps we can improve together! Good luck my friend! Anyways, I need to shift my attention to answers on here. Goodbye!

PS: No, I don't know why, "pointers are be used in callback than would modify the calling variables." Not sure anyone here even understands what you mean by that. Perhaps you should increase you knowledge of the English language along with your knowledge of programming languages!
It seems your attitude is inimical to learning anything useful from reading code or taking the advice of others who have been doing it longer, and more successfully. I guess you're on your own unless someone knows the way to lead you.
 

Thread Starter

LonelyLad

Joined Sep 17, 2024
29
It seems your attitude is inimical to learning anything useful from reading code or taking the advice of others who have been doing it longer, and more successfully. I guess you're on your own unless someone knows the way to lead you.
It seems his attitude was too inimical to helping others learn anything useful. I guess I'm on my own regardless. Neither of you all are of any help.
 

Papabravo

Joined Feb 24, 2006
22,065
It seems his attitude was too inimical to helping others learn anything useful. I guess I'm on my own regardless. Neither of you all are of any help.
I gave you what I thought you needed, but you didn't see it that way. That's OK, and we'll get the chaplain to punch your TS card.
 

PhilTilson

Joined Nov 29, 2009
153
I have read through this thread with increasing incredulity! I have always worked on the principle that, if I need help with something, then it's best not to come across as a petulant brat, but rather to try to explain my predicament sympathetically. I can't really say that I am surprised by the thread-starter's nom-de-plume!
 
Top