I'm making a system with one green and one red led. it must communicate to thee ESP's The script I rewrite is not working " good " it is with ESP-Now I hope someone good find what is wrong.The code I rewrite:
// Script rewrite with 2 led's and two buttons which is NOT correct !
// Include Libraries
#include <WiFi.h>
#include <esp_now.h>
// Define LED and push button state booleans
bool buttonDown_2 = false;
bool ledOn_2 = false;
bool buttonDown_1 = false;
bool ledOn_1 = false;
// Define LED and pushbutton pins
#define STATUS_LED_1 15
#define STATUS_BUTTON_1 5
#define STATUS_LED_2 14
#define STATUS_BUTTON_2 4
void formatMacAddress(const uint8_t *macAddr, char *buffer, int maxLength)
// Formats MAC Address
{
snprintf(buffer, maxLength, "%0xFF:%0xFF:%0xFF:%0xFF:%0xFF:%0xFF", macAddr[0], macAddr[1], macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
}
//void receiveCallback(const uint8_t *macAddr, const uint8_t *data, int dataLen)
void receiveCallback(const esp_now_recv_info_t* esp_now_info, const uint8_t *data, int dataLen)
// Called when data is received
{
// Only allow a maximum of 250 characters in the message + a null terminating byte
char buffer[ESP_NOW_MAX_DATA_LEN + 1];
int msgLen = min(ESP_NOW_MAX_DATA_LEN, dataLen);
strncpy(buffer, (const char *)data, msgLen);
// Make sure we are null terminated
buffer[msgLen] = 0;
// Format the MAC address
//char macStr[18];
//formatMacAddress(macAddr, macStr, 18);
// Send Debug log message to the serial port
//Serial.printf("Received message from: %s - %s\n", macStr, buffer);
Serial.printf("Received message %s\n", buffer);
// Check switch status
if (strcmp("on", buffer) == 0)
{
ledOn_1 = true;
}
else
{
ledOn_1 = false;
}
digitalWrite(STATUS_LED_1, ledOn_1);
if (strcmp("on", buffer) == 0)
{
ledOn_2 = true;
}
else
{
ledOn_2 = false;
}
digitalWrite(STATUS_LED_2, ledOn_2);
}
void sentCallback(const uint8_t *macAddr, esp_now_send_status_t status)
// Called when data is sent
{
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)
// Emulates a broadcast
{
// Broadcast a message to every device in range
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);
}
// Send message
esp_err_t result = esp_now_send(broadcastAddress, (const uint8_t *)message.c_str(), message.length());
// Print results to serial monitor
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()
{
// Set up Serial Monitor
Serial.begin(115200);
delay(1000);
// Set ESP32 in STA mode to begin with
WiFi.mode(WIFI_STA);
Serial.println("ESP-NOW Broadcast Demo");
// Print MAC address
Serial.print("MAC Address: ");
Serial.println(WiFi.macAddress());
// Disconnect from WiFi
WiFi.disconnect();
// Initialize ESP-NOW
if (esp_now_init() == ESP_OK)
{
Serial.println("ESP-NOW Init Success");
esp_now_register_recv_cb(receiveCallback);
esp_now_register_send_cb(sentCallback);
}
else
{
Serial.println("ESP-NOW Init Failed");
delay(3000);
ESP.restart();
}
// Pushbutton uses built-in pullup resistor
pinMode(STATUS_BUTTON_1, INPUT_PULLUP);
pinMode(STATUS_BUTTON_2, INPUT_PULLUP);
// LED Output
pinMode(STATUS_LED_1, OUTPUT);
pinMode(STATUS_LED_2, OUTPUT);
}
void loop()
{
if (digitalRead(STATUS_BUTTON_1))
{
// Detect the transition from low to high
if (!buttonDown_1)
{
buttonDown_1 = true;
// Toggle the LED state
ledOn_1 = !ledOn_1;
digitalWrite(STATUS_LED_1, ledOn_1);
if (digitalRead(STATUS_BUTTON_2))
// Detect the transition from low to high
if (!buttonDown_2)
buttonDown_2 = true;
// Toggle the LED state
ledOn_2 = !ledOn_2;
digitalWrite(STATUS_LED_2, ledOn_2);
// Send a message to all devices
if (ledOn_1)
{
broadcast("on");
}
else
{
broadcast("off");
// Send a message to all devices
if (ledOn_2)
{
broadcast("on");
}
else
{
broadcast("off");
}
}
}
// Delay to avoid bouncing
delay(500);
}
else
{
// Reset the button state
buttonDown_1 = false;
buttonDown_2 = false;
}
}
ESP-Now which sending information to 2 led on and off with 2 buttons.
ESP-Now which sending information to 2 led on and off with 2 buttons.
I'm living in the Netherlands, and try to make a project with ESP-NOW. It is with a green and re led and two buttons. I rewrite a script but it is not working well. The system is with 3 ESP's and the all communicate with each other.
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
This line in the program is the "problem" if (strcmp("on", buffer) == 0) It can only look to one GPO port. That is why both led's are going on ! I found out that there are other arguments for strncmp
maybe someone know which must be the right line ( the right argument ) : int strncmp(const char *s1, const char *s2, size_t n);
A lot off viewers has red the program thanks for that !! Gr. Wim
maybe someone know which must be the right line ( the right argument ) : int strncmp(const char *s1, const char *s2, size_t n);
A lot off viewers has red the program thanks for that !! Gr. Wim
I'm living in the Netherlands, and try to make a project with ESP-NOW. It is with a green and re led and two buttons. I rewrite a script but it is not working well. The system is with 3 ESP's and the all communicate with each other.
-
- Posts: 9757
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
Thing is, you're sending 'on' when you want led1 to turn on, and 'on' as well if you want led2 to turn on. You'd need to send two different messages if you want to distinguish the two LEDs from eachother! The strcmp is OK here, strncmp would be used if you only want to compare the first n bytes to be compared, e.g. (strcmp("apekooien","apennootjes",3)==0) is true.
(Also, for next time, 'The script I rewrite is not working " good "' is just about the worst symptom description you can give. If you want to increase the likelihood of people wanting to help you, it's better to specifically describe what you want the script to do, what it does now, plus anything you already tried to make it work. If you simply say 'it's not working', you leave us to figure out what the code should do and what it likely does now, and that's an extra step making a whole lot of people decide to not look at your issue in-depth.)
(Also, for next time, 'The script I rewrite is not working " good "' is just about the worst symptom description you can give. If you want to increase the likelihood of people wanting to help you, it's better to specifically describe what you want the script to do, what it does now, plus anything you already tried to make it work. If you simply say 'it's not working', you leave us to figure out what the code should do and what it likely does now, and that's an extra step making a whole lot of people decide to not look at your issue in-depth.)
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
I think I can ex plane not so good in English !!
Bedankt voor je deels geweldige antwoord heb daar zeker notitie van genomen. Was zeker niet slim van mij. Maar m'n leeftijd ...
Ik denk door de keuze van een "eigen button poort het probleem zou zijn "opgelost".
Vandaar mijn verbazing dat het script niet werkte. En dus kwam ik op het idee dat de vergelijkt script regel het probleem is/was.
Door verschillende porten voor de buttons te gebruiken hoopte ik dat aanpassing van deze regel de oplossing zou zijn.
En mijn Engels en mijn kennis is onvoldoende om te begrijpen of er bij andere argamunten de porten vergelijken het probleem zou zijn opgelost. En ik dacht in het antwoord door het stukje Nederlannds mischien is dat persson die het reply gaf misschien ....
Translate in word :
Thanks for your partly great answer, have certainly taken note of that. Certainly wasn't
I think by choosing a "custom button port the problem would be solved".
Hence my surprise that the script didn't work. And so I came up with the idea that the compare script rule is/was the problem.
By using different an port for the buttons, I hoped that changing this rule would be the solution.
And my English and my knowledge is insufficient to understand whether comparing the ports with other arguments would solve the problem. And I thought in the answer because of the piece of Dutch maybe that's the person who gave the reply maybe ....
Bedankt voor je deels geweldige antwoord heb daar zeker notitie van genomen. Was zeker niet slim van mij. Maar m'n leeftijd ...
Ik denk door de keuze van een "eigen button poort het probleem zou zijn "opgelost".
Vandaar mijn verbazing dat het script niet werkte. En dus kwam ik op het idee dat de vergelijkt script regel het probleem is/was.
Door verschillende porten voor de buttons te gebruiken hoopte ik dat aanpassing van deze regel de oplossing zou zijn.
En mijn Engels en mijn kennis is onvoldoende om te begrijpen of er bij andere argamunten de porten vergelijken het probleem zou zijn opgelost. En ik dacht in het antwoord door het stukje Nederlannds mischien is dat persson die het reply gaf misschien ....
Translate in word :
Thanks for your partly great answer, have certainly taken note of that. Certainly wasn't
I think by choosing a "custom button port the problem would be solved".
Hence my surprise that the script didn't work. And so I came up with the idea that the compare script rule is/was the problem.
By using different an port for the buttons, I hoped that changing this rule would be the solution.
And my English and my knowledge is insufficient to understand whether comparing the ports with other arguments would solve the problem. And I thought in the answer because of the piece of Dutch maybe that's the person who gave the reply maybe ....
I'm living in the Netherlands, and try to make a project with ESP-NOW. It is with a green and re led and two buttons. I rewrite a script but it is not working well. The system is with 3 ESP's and the all communicate with each other.
-
- Posts: 9757
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
Jeuh, Nederlander hier. Met 'poort' bedoel je de GPIO port? Dat is maar een onderdeel van het antwoord. Je code detecteert de individuele knoppen idd correct, maar daarna ga je de fout in: als knop 1 is ingedrukt stuur je 'on', als knop 2 is ingedrukt verstuur je precies hetzelfde (ook 'on'). Je zou het kunnen veranderen door bijvoorbeeld 'on1' te sturen voor de eerste knop en 'on2' voor de tweede, hetzelfde voor 'off1' en 'off2'. Verander je ontvangstcode op dezelfde manier en je hebt een goede kans dat het werkt.
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
Bedankt voor de tip/het idee. Mis toch een stukje kennis, zou je de wijzigingen kunnen aan geven in het/of een bestand ?
Alvast bedankt. Ga verder nu in het nederlands indien oke
Alvast bedankt. Ga verder nu in het nederlands indien oke
I'm living in the Netherlands, and try to make a project with ESP-NOW. It is with a green and re led and two buttons. I rewrite a script but it is not working well. The system is with 3 ESP's and the all communicate with each other.
-
- Posts: 9757
- Joined: Thu Nov 26, 2015 4:08 am
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
Tis jouw code: doe eens een poging om te implementeren wat ik zei? Als je het fout heb corrigeer ik je wel.
Re: ESP-Now which sending information to 2 led on and off with 2 buttons.
In Englisch so other viewers can understand!
Thanks for your answer. The problem is, compiling the date gives no problem/errors.
But …I like when I give the ESP32 power both led’s are going on
I changed False in True for both led’s , with a good result.
But when I pressed the button green and red going on, pressing again both are off ?????
bool buttonDown_2 = false;
bool ledOn_2 = true;
bool buttonDown_1 = false;
bool ledOn_1 = true;
.-.-.-.-.-.-.-.-.--..-.-.--.-..-.-.-.-..-
// Check switch status
if (strcmp("on1", buffer) == 0)
{
ledOn_1 = true;
}
else
{
ledOn_1 = false;
}
digitalWrite(STATUS_LED_1, ledOn_1);
if (strcmp("on2", buffer) == 0)
{
ledOn_2 = true;
}
else
{
ledOn_2 = false;
}
digitalWrite(STATUS_LED_2, ledOn_2);
..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
/ Send a message to all devices
if (ledOn_1)
{
broadcast("on1");
}
else
{
broadcast("off1");
// Send a message to all devices
if (ledOn_2)
{
broadcast("on2");
}
else
{
broadcast("off2");
}
}
}
Thanks for your answer. The problem is, compiling the date gives no problem/errors.
But …I like when I give the ESP32 power both led’s are going on
I changed False in True for both led’s , with a good result.
But when I pressed the button green and red going on, pressing again both are off ?????
bool buttonDown_2 = false;
bool ledOn_2 = true;
bool buttonDown_1 = false;
bool ledOn_1 = true;
.-.-.-.-.-.-.-.-.--..-.-.--.-..-.-.-.-..-
// Check switch status
if (strcmp("on1", buffer) == 0)
{
ledOn_1 = true;
}
else
{
ledOn_1 = false;
}
digitalWrite(STATUS_LED_1, ledOn_1);
if (strcmp("on2", buffer) == 0)
{
ledOn_2 = true;
}
else
{
ledOn_2 = false;
}
digitalWrite(STATUS_LED_2, ledOn_2);
..-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
/ Send a message to all devices
if (ledOn_1)
{
broadcast("on1");
}
else
{
broadcast("off1");
// Send a message to all devices
if (ledOn_2)
{
broadcast("on2");
}
else
{
broadcast("off2");
}
}
}
I'm living in the Netherlands, and try to make a project with ESP-NOW. It is with a green and re led and two buttons. I rewrite a script but it is not working well. The system is with 3 ESP's and the all communicate with each other.
Who is online
Users browsing this forum: No registered users and 17 guests