Problem ESPNOW one way to many: How to make a data structure with millis() that will turn on and off the led of the reciever

Hi. Please help me understand on how to make a data structure with Millis() that will be sent to the receiver( ESPNOW SLAVE1) to switch on and off the led.

Im Using Esp32 Wroom-DA module( ESPNOW One way to Many ).

I attach a copy below.

Any help appreciated. Thanks.

// ESPNOW MASTER
#include <esp_now.h>
#include <WiFi.h>

// REPLACE WITH YOUR ESP RECEIVER'S MAC ADDRESS
uint8_t broadcastAddress1[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04};
uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x94, 0xBB, 0xD0};
uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x78};

typedef struct test_struct {
  int x;
  int y;
} test_struct;

test_struct test;

esp_now_peer_info_t peerInfo;

// callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
  Serial.begin(115200);
 
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }
  
  esp_now_register_send_cb(OnDataSent);
   
  // register peer
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
  // register first peer  
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  // register second peer  
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  /// register third peer
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop() {
  test.x = random(0,20);
  test.y = random(0,20);
 
  esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
   
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}
//ESPNOW SLAVE1
#include <esp_now.h>
#include <WiFi.h>

//Structure example to receive data
//Must match the sender structure
typedef struct test_struct {
  int x;
  int y;
} test_struct;

//Create a struct_message called myData
test_struct myData;

//callback function that will be executed when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&myData, incomingData, sizeof(myData));
  Serial.print("Bytes received: ");
  Serial.println(len);
  Serial.print("x: ");
  Serial.println(myData.x);
  Serial.print("y: ");
  Serial.println(myData.y);
  Serial.println();
}
 
void setup() {
  //Initialize 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 recv CB to
  // get recv packer info
  esp_now_register_recv_cb(OnDataRecv);
}
 
void loop() {

}

if you just wish to blink the receiver LED ON/OFF every two seconds
in the transmiter setup set test.x to 0 and in loop you could

void loop() {
  test.x = !test.x;    // invert x
  esp_err_t result = esp_now_send(0, (uint8_t *) &test, sizeof(test_struct));
  if (result == ESP_OK) {
    Serial.println("Sent with success");
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
}

in the receiver if if test.x is 0 switch LED off if none zero on

if it works update it to use mills() etc etc

This description of the wanted functionality is too short to be clear what you really want.

Do you want the sender-ESP to send a command "ON" and "OFF" at certain time-intervalls?

For activating the "blinking"
Do you want the sender-ESP to send a command to the receiver "receiver start switching on/off with intervall xxxx milliseconds (where "xxxx" is the number of milliseconds)

For de-activating the "blinking"
Do you want the sender-ESP to send a command to the receiver "stop switching

anything else?

regardless of which way you are doing this.

If you don't want to pay for a ready to flash solution

There is no way around:

  • learning the basics of how functions are defined and are used
  • learning how non-blocking timing works
  • learning how any kind of message can be sent by ESP-NOW
  • learning how boolean flag-variables can be used
  • understanding what each part of the code does

and then writing functions where each function does

one thing

a function just for the one thing sending data
a function just for the one thing analysing the sended command
a function just for the one thing switching on / off

and this is well far behind taking some online found code
modify SSID and you are done

As long as you show own effort in learning this, the users here will have fun to support you

best regards Stefan

Hi Stefan,

To be exact its for actviting the blinking code which the reciever have with intervalls. "every 900000 millisecond the ESPMaster send comand to run the code for 60000 millisecond.

I attaced the blink led code of the reciever.

It might be a long code thats why i asked first on how create a data structure with millis to on and off the led.
I've spend two days searching answer to my problem and it lead me here in the furom. maybe I missed something thats why I cant figure it out how.

Hope you can give me some advice.

My learning is only from reading and watching videos.

Thanks in advance stefan.

Regards Evenmerk

// LED 1
int led1 = 32;
unsigned long currentMillis;
unsigned long previoustime = 0;
long led1off = 1000;
long led1on = 500;
int ledState1 = 0;
// LED 2
int led2 = 33;
unsigned long currentMillis2;
unsigned long previoustime2 = 0;
long led2off = 1500;
long led2on = 500;
int ledState2 = 0;
// LED3
int led3 = 27;
unsigned long currentMillis3;
unsigned long previoustime3 = 0;
long led3off = 3000;
long led3on = 3000;
int ledState3 = 0;
// LED 4
int led4 = 14;
unsigned long currentMillis4;
unsigned long previoustime4 = 0;
long led4off = 2000;
long led4on = 2500;
int ledState4 = 0;
// LED 5
int led5 = 26;
unsigned long currentMillis5;
unsigned long previoustime5 = 0;
long led5off = 3000;
long led5on = 3000;
int ledState5 = 0;
// LED 6
int led6 = 34;
unsigned long currentMillis6;
unsigned long previoustime6 = 0;
long led6off = 3000;
long led6on = 3000;
int ledState6 = 0;
// MUSIC 7
int led7 = 19;
unsigned long currentMillis7;
unsigned long previoustime7 = 0;
long led7off = 200000;
long led7on = 3000;
int ledState7 = 0;
// SOUND SENSOR
int MIC = 25;
int sensorMIC = 12;
const int thershold = 50;


void setup(){
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(led6, OUTPUT);
pinMode(led7, OUTPUT); 
pinMode(MIC, OUTPUT);
pinMode(sensorMIC, INPUT);
Serial.begin (9600);
  }
  
void loop(){
currentMillis = millis();
if (((currentMillis - previoustime) >= led1on) && (ledState1 ==1))
{
ledState1 = !ledState1;
digitalWrite(led1, ledState1);
previoustime = currentMillis;
}
else if (((currentMillis - previoustime) >= led1off) && (ledState1 == 0))
{
ledState1 = !ledState1;
digitalWrite(led1, ledState1);
previoustime = currentMillis;
}
// led2
{
currentMillis2 = millis();
if (((currentMillis2 - previoustime2) >= led2on) && (ledState2 == 1))
{
ledState2 = !ledState2;
digitalWrite(led2, ledState2);
previoustime2 = currentMillis2;
}
else if (((currentMillis2 - previoustime2) >= led2off) && (ledState2 == 0))
{
ledState2 = !ledState2;
digitalWrite(led2, ledState2);
previoustime2 = currentMillis2;
  }
}
// LED 3
{
currentMillis3 = millis();
if (((currentMillis3 - previoustime3) >= led3on) && (ledState3 == 1))
{
ledState3 = !ledState3;
digitalWrite(led3, ledState3);
previoustime3 = currentMillis3;
}
else if (((currentMillis3 - previoustime3) >= led3off) && (ledState3 == 0))
{
ledState3 = !ledState3;
digitalWrite(led3, ledState3);
previoustime3 = currentMillis3;
  }
}
// LED 4
{
currentMillis4 = millis();
if (((currentMillis4 - previoustime4) >= led4on) && (ledState4 == 1))
{
ledState4 = !ledState4;
digitalWrite(led4, ledState4);
previoustime4 = currentMillis4;
}
else if (((currentMillis4 - previoustime4) >= led4off) && (ledState4 == 0))
{
ledState4 = !ledState4;
digitalWrite(led4, ledState4);
previoustime4 = currentMillis4;
 }
}
// LED 5
{
currentMillis5 = millis();
if (((currentMillis5 - previoustime5) >= led5on) && (ledState5 == 1))
{
ledState5 = !ledState5;
digitalWrite(led5, ledState5);
previoustime5 = currentMillis5;
}
else if (((currentMillis5 - previoustime5) >= led5off) && (ledState5 == 0))
{
ledState5 = !ledState5;
digitalWrite(led5, ledState5);
previoustime5 = currentMillis5;
  }
}
// LED 6
{
currentMillis6 = millis();
if (((currentMillis6 - previoustime6) >= led6on) && (ledState6 == 1))
{
ledState6 = !ledState6;
digitalWrite(led6, ledState6);
previoustime6 = currentMillis6;
}
else if (((currentMillis6 - previoustime6) >= led6off) && (ledState6 == 0))
{
ledState6 = !ledState6;
digitalWrite(led6, ledState6);
previoustime6 = currentMillis6;
  }
}
// MUSIC 
{
currentMillis7 = millis();
if (((currentMillis7 - previoustime7) >= led7on) && (ledState7 == 1))
{
ledState7 = !ledState7;
digitalWrite(led7, ledState7);
previoustime7 = currentMillis7;
}
else if (((currentMillis7 - previoustime7) >= led7off) && (ledState7 == 0))
{
ledState7 = !ledState7;
digitalWrite(led7, ledState7);
previoustime7 = currentMillis7;
  }
}
// SOUND SENSOR
  int Soundsens=analogRead(sensorMIC);
  if (Soundsens>=thershold) {
    digitalWrite(MIC, LOW);
    delay(100);
  }
  else{
    digitalWrite(MIC,HIGH);
    }    
  }


This means your ESP-Master-code has a timer that get's triggered once evey 90 seconds (the 90.000 milliseconds) to send a "start" command

Your receiver-code is waiting for this "start" command and if received starting a "run-period" of 60 seconds (the 60.000 milliseconds)
starting the 60second-run-period is done very similar on starting this run-period currentMillis = millis()

You have your non-blocking timed blinking code inside its own function
This function is called until the 60-second timer expires

Whenever you start writing code where everything is the same except a increasing number at the end of the variable-names like in your code

long led1off = 1000;
long led1on = 500;
int ledState1 = 0;

you can use array-variables instead of numberised variables

in combination with a loop that runs through all timers.

Your logic for switching on/off can be simplified by setting the "waitperiod" alternatingly to the "on"-time / the "off"-time

if you are pretty new to C++-coding
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

thank you stefan. noted.

Hi Stefan.

Hope you can help me with this.
Im trying to send a boolean data 1 first then after 1min send the data 0 unfortunately i failed, the receiver data received always 0 only.

type or paste code here

Please give me idea what should i do.

Thanks in advance.

Regards,
keven



#include <esp_now.h>
#include <WiFi.h>


/////////////////////////////////////////////////////////////Show_all
unsigned long Currentmillis1;
unsigned long Previoustime1 = 0;
const long Show1_start = 120000;
const long Show1_stop = 60000;
/////////////////////////////////////////////////////////////Show_Group1
unsigned long Currentmillis2;
unsigned long Previoustime2 = 0;
const long Show2_start = 30000;
const long Show2_stop = 15000;
/////////////////////////////////////////////////////////////Show_Group2
unsigned long Currentmillis3;
unsigned long Previoustime3 = 0;
const long Show3_start = 15000;
const long Show3_stop =  7500;
////////////////////////////////////////////////////////// Show status

// REPLACE WITH YOUR ESP RECEIVER’S MAC ADDRESS
uint8_t broadcastAddress1[] = {0xE8, 0x31, 0xCD, 0xD6, 0xE9, 0x20};
uint8_t broadcastAddress2[] = {0xEC, 0x62, 0x60, 0x84, 0x2F, 0x4C};
uint8_t broadcastAddress3[] = {0xEC, 0x62, 0x60, 0x84, 0x31, 0x04};

typedef struct Show_struct {

  bool Show_State;
 
} Show_struct;

Show_struct Show_1st;
Show_struct Show_2nd;
Show_struct Show_3rd;

esp_now_peer_info_t peerInfo;

void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  char macStr[18];
  Serial.print("Packet to: ");
  // Copies the sender mac address to a string
  snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
           mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  Serial.print(macStr);
  Serial.print(" send status:\t");
  Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
 
void setup() {
 
  Serial.begin(115200);
 
  WiFi.mode(WIFI_STA);
 
  if (esp_now_init() != ESP_OK) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  esp_now_register_send_cb(OnDataSent);

  // register peer
  peerInfo.channel = 0;  
  peerInfo.encrypt = false;
   
  memcpy(peerInfo.peer_addr, broadcastAddress1, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
 
  memcpy(peerInfo.peer_addr, broadcastAddress2, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
  memcpy(peerInfo.peer_addr, broadcastAddress3, 6);
  if (esp_now_add_peer(&peerInfo) != ESP_OK){
    Serial.println("Failed to add peer");
    return;
  }
}
 
void loop()
{
      Currentmillis1 = millis();
  if(((Currentmillis1 - Previoustime1) >= Show1_start) && (Show_1st.Show_State = 1))
    {
     Show_1st.Show_State = !Show_1st.Show_State;   
  esp_err_t result1 = esp_now_send(broadcastAddress1,(uint8_t *) &Show_1st,sizeof(Show_struct));
    Previoustime1 = Currentmillis1;
  }
  else if (((Currentmillis1 - Previoustime1) >= Show1_stop) && (Show_1st.Show_State = 0))
  {
    Show_1st.Show_State = !Show_1st.Show_State;
  esp_err_t result1 = esp_now_send(broadcastAddress1,(uint8_t *) &Show_1st,sizeof(Show_struct));
  Previoustime1 = Currentmillis1;
  
  if (result1 == ESP_OK) {
    Serial.println("Show_1st sent");
    Serial.println();
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(500);
  }
 /////////////////////////////////////////////////////////////////Show_Group1
   {
      Currentmillis2 = millis();
  if(Currentmillis2 - Previoustime2 >= Show2_start)
    {
     Show_2nd.Show_State = 1;   
  esp_err_t result1 = esp_now_send(broadcastAddress2,(uint8_t *) &Show_2nd,sizeof(Show_struct));
    Previoustime2 = Currentmillis2;
  }
  else if (Currentmillis2 - Previoustime2 >= Show2_stop)
  {
    Show_2nd.Show_State = 0;
  esp_err_t result2 = esp_now_send(broadcastAddress2,(uint8_t *) &Show_2nd,sizeof(Show_struct));
  Previoustime2 = Currentmillis2;
  
  if (result2 == ESP_OK) {
    Serial.println("Show_2nd sent");
    Serial.println();
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(500);
  }
//////////////////////////////////////////////////////////////////Show_Group2
      Currentmillis3 = millis();
  if(Currentmillis3 - Previoustime3 >= Show3_start)
    {
     Show_3rd.Show_State = 1;   
  esp_err_t result3 = esp_now_send(broadcastAddress3,(uint8_t *) &Show_3rd,sizeof(Show_struct));
  Previoustime3 = Currentmillis3;
    }
  else if(Currentmillis3 - Previoustime3 >= Show3_stop)
    {
     Show_3rd.Show_State = 0;   
  esp_err_t result3 = esp_now_send(broadcastAddress3,(uint8_t *) &Show_3rd,sizeof(Show_struct));
  Previoustime3 = Currentmillis3;
    
  if (result3 == ESP_OK) {
    Serial.println("Show_3rd sent");
    Serial.println();
  }
  else {
    Serial.println("Error sending the data");
  }
  delay(2000);
      }
   }
}

My ideas are:

changing your picture from beeing a lazy person that shows only minimal own effort
to a high engaged person that is witnessing own effort by carefully posting

using the Copy for Forum function to make sure that your complete sketch will be posted.

Maybe you found these words harsch. Well I have chosen these words to make very clear that I'm

  1. willing to help
  2. but only under the condition of you doing that part of the work that you can do.

You posted just a part of your sketch but nothing about the ESP-NOW

press Ctrl-T at least before you post code online
Ctrl-T does autoformatting your code to the standard-indentions
indentions make it much easier to read the code.

You should do yourself a favor and develop the habit to press ctrl-T every few lines of code that you have typed into the editor

You should do that part of the work that you yourself can do
really doing you yourself

This means:

  • adding serial debug-output to your code

  • running this version of your code

  • then posting the code and the debug-output.

You should learn what arrays are:
arrays will shorten down your code from now 226 lines
to around 140 lines

Before that take a

working example-code

that demonstrates sending/receiving data over ESP-NOW

then do a

small modification

and test the demo-code again.

If you have any kind of questions like

  • how to add serial debug-output and where to add the serial debug-output
  • where to find a working ESP-NOW-Demo-code
  • etc. etc.
    just post these questions

If all this is too much work for you
well sit and wait of other user will do the work for you

best regards Stefan

Hi Stefan,

Thank you very much.
I've made improvement in my project.
Your words are not harsh its an encourgement to an open-minded person like me.

Regards Evenmerk

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.