Notifications
Clear all

ESPnow problems using button on 1 device to control LED on 2nd device

7 Posts
2 Users
1 Reactions
1,031 Views
 Zaq
(@zaq)
Member
Joined: 10 months ago
Posts: 7
Topic starter  

Hi, I am slowly coming to grips with ESPnow. I am able to communicate between devices but controlling things is another matter. In the code below I would like to know what to add and where to add it to keep the "ledPin" on for a programmable period of time. Thanks in advance, Zaq.

 

#include <espnow.h>
const int ledPin = D2;


void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
esp_now_init();
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(onDataReceived);
}
void loop() {
// Nothing to do here, waiting for ESP-NOW messages
}
void onDataReceived(uint8_t *senderMac, uint8_t *data, uint8_t len) {
if (*data == 1)
{
digitalWrite(ledPin, HIGH);
Serial.println("LED ON");
}
else if(*data == 0)
{
#include <ESP8266WiFi.h>
digitalWrite(ledPin, LOW);
Serial.println("LED OFF");


}
}

   
Quote
(@hilldweller)
Member
Joined: 1 year ago
Posts: 111
 

A quick reply. millis() is your friend. An ever incrementing milli-second counter.

 

First you need: unsigned long LEDtime;

When you set or clear the LED ( timed on or timed off ) add:  LEDtime = millis() + 1000 ( one second )

 

Then in loop()

if( millis() > LEDtime )  < turn off or on your LED >

espnow is brilliant and very reliable - I have a house light control using it for over 5 years and it's never missed a beat.

 


   
Ron reacted
ReplyQuote
 Zaq
(@zaq)
Member
Joined: 10 months ago
Posts: 7
Topic starter  

Hi Brian,

As I stated elsewhere I would really like to see your house light control code.


   
ReplyQuote
(@hilldweller)
Member
Joined: 1 year ago
Posts: 111
 

Posted by: @zaq

Hi Brian,

As I stated elsewhere I would really like to see your house light control code.

 

No problem. Being many files I think it's better if I zip them. Watch this space.

 


   
ReplyQuote
(@hilldweller)
Member
Joined: 1 year ago
Posts: 111
 

Posted by: @zaq

Hi Brian,

As I stated elsewhere I would really like to see your house light control code.

 

NOTE: THIS IS POTENTIALLY DANGEROUS CODE, USE IT AT YOUR RISK AND ONLY WHEN SURE YOU DECIDE IT SAFE TO USE. BURNED DOWN HOUSES AND DEATHS ARE DOWN TO YOU.

 

Code quality - I like simple readable code, I'm past optimal code now, some of the serial interfacing and parsing could definitely be done better but I did it the lazy readable way. I love C but it's the best language there is for shooting oneself in the foot with a single out of bounds reference.

 

Lets see if this works, I never zipped in here.

Brian.

 


   
ReplyQuote
 Zaq
(@zaq)
Member
Joined: 10 months ago
Posts: 7
Topic starter  

@hilldweller The code unzipped fine thanks. I will get into it in depth after I resolve some hardware issues.  i now have several devices that were working but are no longer responding as expected. I am able to prove they are OK by putting Tasmota on them, so the problem my be in my code.


   
ReplyQuote
(@hilldweller)
Member
Joined: 1 year ago
Posts: 111
 

Posted by: @zaq

@hilldweller The code unzipped fine thanks. 

 

  i now have several devices that were working but are no longer responding as expected.

 

Glad the zip was OK, I've never zipped in here.

>>working but no longer.

Situation normal - My drive is full of XXXXX_v1.ino XXXXX_v2.ino . If it works then save a "version + 1" and work on that. Then it's so easy to go back to a working one. Never do too many changes before version+1.

Never give up.

 


   
ReplyQuote