I made a code for the esp8266 with cayenne.
There is a section to toggle a relay by pushing a push button.
this works fine without cayenne.loop included…
I need cayenne.loop to give information to my dashboard and also to toggle the output from a trigger or so.
When i uncomment the //cayenne.loop in the void loop section,
the button doesnt always toggle the output.
I need to push the button for at least 3 seconds and then wait for more than 3seconds before ik can toggle it again :(.
Anyone has an idea to fix this?
Thanks in advance.
#include <CayenneMQTTESP8266.h>
// WiFi network info.
char ssid[] = "---";
char wifiPassword[] = "---";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "---";
char password[] = "---";
char clientID[] = "---";
const int DRUKKNOP = 13;
const int RELAIS = 12;
int relayState = 0;
int buttonState;
int lastButtonState = 0;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
//Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(DRUKKNOP, INPUT);
pinMode(RELAIS, OUTPUT);
digitalWrite(RELAIS, relayState);
}
void loop() {
//Cayenne.loop();
// Get the current state of the button
int reading = digitalRead(DRUKKNOP);
// Has the button gone high since we last read it?
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState){
buttonState = reading;
if (buttonState == HIGH) {
relayState = !relayState;
}
}
}
digitalWrite(RELAIS, relayState);
Cayenne.virtualWrite(1, relayState);
lastButtonState = reading;
}
What do you mean with external interupt?
If i do use cayenne.loop i cant change the relay status from the dashboard without delay (if i put in the code)
but then de physical push button connected to the esp goes slow like mentioned before.
And i do need the program in the esp because you can not make a momentary switch tot toggle relay trigger in the cayenne triggers ( because, 1- the triggers don’t always work and 2- you can not make a IF button pressed AND relay LOW set relay high,…)
Thx for the reply, just tried it but i still have to push the button like 2 seconds an when i’m lucky, the relay toggle’s. when it doesnt, i have to relaise the button ant press it again for 2 of 3 second
What version of esp8266?
Try not to use pin 13 for DRUKKNOP it can cause issues on some code/hardware combos because of built-in led and resistor for it.
im testing on a node mcu but it needs to be programmed in a 12E after testing.
Tried other gpio’s and there is no difference.
When i put in a code with those gpio without cayenne involved i can push it 4 times/sec and it works.
connect to cayenne and my digital button (drukknop) only toggle’s the relay after pushing it about 2sec. when de relay is toggled, tha status on the cayenne dashboard changes emedialty… its just the button to toggle relay that needs a lot of time to think?