Basic controller, slave with esp now

I want to send simple commands as serial commands from existing program broadcast from controller to slaves to activate leds. need help suggestions thanks
samuel

you could use WiFi sending UDP datagrams to the slave microcontrollers to active the LEDs
to simplify things you could use UDP multicast to send the same message to all slaves - each slave searches the message for its particular control data

I am using esp now with nodemcu, been able to connect controller with 2 slaves. messages are recd and acknowledged. I am not able to send char string data . it compiles and the serial monitor does sends the message typed, but not received by the slaves.s

at present your slaves are conncted via serial lines to the ESP?
EDIT: are you using RS485? if so are allowing time for the transmit to complete before taking DE & RE low ready for receive?
what are the slaves?
could you give us a circuit ?
could you upload the code which worked and the code which does not highlighting the modifications made?

controller code

#include <ESP8266WiFi.h>
#include <espnow.h>
uint8_t broadcastAddress1[] = {0xff, 0x61, 0x05, 0xD4, 0xDE, 0xF6};//
uint8_t broadcastAddress2[] = {0xff, 0xFA, 0xBC, 0x5F, 0x73, 0x29}; //



// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char a[32];
  String b;

} struct_message;



// Create a struct_message called myData
struct_message myData;

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0) {
    Serial.println("Delivery success");
  }
  else {
    Serial.println("Delivery fail");
  }
}





void setup() {
  // Init Serial Monitor
  Serial1.begin(115200);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial1.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_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  esp_now_add_peer(broadcastAddress1, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  esp_now_add_peer(broadcastAddress2, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {
  {
    strcpy(myData.a, "");

    myData.b = "";


    // Send message via ESP-NOW
    esp_now_send(broadcastAddress1, (uint8_t *) &myData, sizeof(myData));
    esp_now_send(broadcastAddress2, (uint8_t *) &myData, sizeof(myData));

  }
}

I cant send a string command like red, white, to the slave although it says message delivered success
thanks
samuel

I have posted the code. any suggestions please

I assume it is a problem with espnow - perhaps it would help to change the title of the post to reflect this? e.g. problem with ESP-NOW communications
I can only find one ESP32 so at present cannot attempt to replicate the problem - will have to order another!
However, tested a couple of ESP8266 devices using example Getting Started with ESP-NOW (ESP8266 NodeMCU with Arduino IDE)
Worked without any problems - transmitter

ESP8266 Board MAC Address:  2C:3A:E8:22:78:DE
Last Packet Send Status: Delivery success
Last Packet Send Status: Delivery success
Last Packet Send Status: Delivery success
Last Packet Send Status: Delivery success

receiver

Bytes received: 56
Char: THIS IS A CHAR
Int: 6
Float: 1.20
String: Hello
Bool: 0

Bytes received: 56
Char: THIS IS A CHAR
Int: 17
Float: 1.20
String: Hello
Bool: 0

Bytes received: 56
Char: THIS IS A CHAR
Int: 2
Float: 1.20
String: Hello
Bool: 0

I note your loop() is continuous without any delays - could the transmitter be overloading the receiver and corrupting the Serial output?
try putting a delay of 2000mSec at the end of loop()?

Thanks. I shifted to 8266 nodemcu and I am receiving the same result as you. instead of the stack error garbage. I also inclued a delay of 6000. works fine. I want to be able to post char inputs from the serial monitor. Can you help in this. Thanks.
Samuel

I made the changess to controller and the result i got


#include <ESP8266WiFi.h>
#include <espnow.h>

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress1[] = {, 0xF6};//
uint8_t broadcastAddress2[] = { 0x73, 0x29}; //

// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
  char rcvdChar [32];

} struct_message;

// Create a struct_message called myData
struct_message  rcvdChar;;

unsigned long lastTime = 0;  
unsigned long timerDelay = 6000;  // send readings timer

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("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() != 0) {
    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_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
 esp_now_add_peer(broadcastAddress1, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  esp_now_add_peer(broadcastAddress2, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
} 

void loop(){ 
  

  while(Serial.available() > 0) {
  Serial.read();
  delayMicroseconds(10);
 
    // Send message via ESP-NOW
    
    esp_now_send(broadcastAddress1, (uint8_t *) &rcvdChar, sizeof(rcvdChar));
    esp_now_send(broadcastAddress2, (uint8_t *) &rcvdChar, sizeof(rcvdChar));
    
 } 
 }   

not sure what you are asking for here?

I want to input different values for the char variable from the serrial controller screen and broadcast it to the slaves. Thanks
Samuel

I have updated the ESP-NOW sender code of post #7
in loop() it reads a string from the keyboard and transmits it

// run on WeMos D1 ESP8266 WiFi Board ESP8266  ESP8266 Board MAC Address:  2C:3A:E8:22:78:DE
/*
  Rui Santos
  Complete project details at https://RandomNerdTutorials.com/esp-now-esp8266-nodemcu-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 <ESP8266WiFi.h>
#include <espnow.h>

// REPLACE WITH RECEIVER MAC Address
uint8_t broadcastAddress[] = {0x60,0x01,0x94,0x1F,0x7B,0xEE};

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

// Create a struct_message called myData
struct_message myData;

unsigned long lastTime = 0;  
unsigned long timerDelay = 2000;  // send readings timer

// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0){
    Serial.println("Delivery success");
  }
  else{
    Serial.println("Delivery fail");
  }
}
 
void setup() {
  // Init Serial Monitor
  Serial.begin(115200);
 
  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  Serial.println();
  Serial.print("ESP8266 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    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_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(OnDataSent);
  
  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
  Serial.setTimeout(1000000L);    // set so Serial.readString() won't timeout
}
 
void loop() {
  if ((millis() - lastTime) > timerDelay) {
    // Set values to send
    strcpy(myData.a, "THIS IS A CHAR");
    myData.b = random(1,20);
    myData.c = 1.2 + myData.b ;
    Serial.print("Enter a string ? ");
    myData.d = Serial.readStringUntil('\n');     // <<<< read string
    myData.e = false;

    // Send message via ESP-NOW
    esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));

    lastTime = millis();
  }

}

modifications of sender code

  1. in setup() so the readString doess not timeout
  Serial.setTimeout(1000000L);    // set so Serial.readString() won't timeout
  1. in loop() to read a string from the keyboard until Enter is pressed
    Serial.print("Enter a string ? ");
    myData.d = Serial.readStringUntil('\n');     // <<<< read string

a run gives - sender

ESP8266 Board MAC Address:  2C:3A:E8:22:78:DE
Enter a string ? test 1
Last Packet Send Status: Delivery success
Enter a string ? test 2
Last Packet Send Status: Delivery success
test Enter a string ? 3
Last Packet Send Status: Delivery success
Enter a string ?

receiver (code not changed)

ESP8266 Board MAC Address:  60:01:94:1F:7B:EE
ESP-NOW initialised
Bytes received: 56
Char: THIS IS A CHAR
Int: 14
Float: 15.20
String: test 1
Bool: 0

Bytes received: 56
Char: THIS IS A CHAR
Int: 7
Float: 8.20
String: test 2
Bool: 0

Bytes received: 56
Char: THIS IS A CHAR
Int: 13
Float: 14.20
String: test 3
Bool: 0

Thank you very much. It is working fine.
samuel

just tried a couple of ESP32 devces with ESP-NOW sender and receiver code from Getting Started with ESP-NOW (ESP32 with Arduino IDE) - all worked OK!

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