ESP8266 MQTT - Interrupt problems
Posted: Sat Oct 09, 2021 7:17 pm
Hi Community,
I got a new project running and it's giving me a terrible headache.
Maybe you could help me get better.
It's basically a very simple sketch. Upon getting a Pin pulled low my ESP01 should simply send a string via mqtt to my server and reset this string 10 seconds after.
For a first try I did this with a very simple loop which checked the pinstate and triggered the publish when the according pin was found low. with a small delay to ease the load.
This approach did work mostly but sometimes triggered a wrong alert which really did annoy me.
So to get things sorted out I tried to implement a interrupt but here's the catch:
When activating a interrupt the mqtt connection breaks down. the reconnect does work though but it reconnects like every second.
Could you help me solve this riddle? (The interrupt pin doesn't seem to be the problem as it's the same with pin 1 and 3[RX])
Thanks in advance
I got a new project running and it's giving me a terrible headache.
Maybe you could help me get better.
It's basically a very simple sketch. Upon getting a Pin pulled low my ESP01 should simply send a string via mqtt to my server and reset this string 10 seconds after.
For a first try I did this with a very simple loop which checked the pinstate and triggered the publish when the according pin was found low. with a small delay to ease the load.
This approach did work mostly but sometimes triggered a wrong alert which really did annoy me.
So to get things sorted out I tried to implement a interrupt but here's the catch:
When activating a interrupt the mqtt connection breaks down. the reconnect does work though but it reconnects like every second.
Could you help me solve this riddle? (The interrupt pin doesn't seem to be the problem as it's the same with pin 1 and 3[RX])
Code: Select all
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Netzwerk
const char* ssid = "xxx";
const char* password = "xxx";
const char* mqtt_server = "192.168.178.10";
//Vars
volatile bool Alert;
// Pins
const int Ring_PIN = 1;
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
for(int i = 0; i<500; i++){
delay(1);
}
}
}
void reconnect() {
while (!client.connected()) {
if (client.connect("Doorbell")) {
client.publish("Doorbell.Status","Ready!");
}
else {
for(int i = 0; i<5000; i++){
delay(1);
}
}
}
}
void setup() {
//INPUT
pinMode(Ring_PIN, INPUT_PULLUP);
//Vars
Alert = false;
//WiFi and MQTT
setup_wifi();
client.setServer(mqtt_server, 1883);
reconnect();
//Interrrupt
attachInterrupt(digitalPinToInterrupt(Ring_PIN), Alarm, FALLING);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (Alert)
{
client.publish("Doorbell.Status","RING!");
delay(10000);
client.publish("Doorbell.Status","Ready!");
Alert = false;
}
}
void Alarm()
{
Alert = true;
}
Thanks in advance