Welcome,
I have a problem with an interrupt on the ESP8266.
The idea is to make a circuit that is connected to WIFI and sends a command to the mqtt server as soon as possible (when the button is pressed).
I want a piece of code to execute once when the button is pressed.
Meanwhile, it executes several times.
Is it enough if I use INPUT_PULLUP and a capacitor between the switch pins?
A pin from the ESP8266 connected with the switch to GND and an electrolyte capacitor in parallel with the switch pins.
Thank you for your help.
.
I have a problem with an interrupt on the ESP8266.
The idea is to make a circuit that is connected to WIFI and sends a command to the mqtt server as soon as possible (when the button is pressed).
I want a piece of code to execute once when the button is pressed.
Meanwhile, it executes several times.
Is it enough if I use INPUT_PULLUP and a capacitor between the switch pins?
A pin from the ESP8266 connected with the switch to GND and an electrolyte capacitor in parallel with the switch pins.
Thank you for your help.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "XXX";
const char* password = "XXX";
const char* mqtt_server = "192.168.1.200"; //adres rpi na ktorym dziala mosquitto
const char* mqttUser = "mosquitto";
const char* mqttPassword = "mosquitto1";
const int mqttPort = 1883;
const int buttonPin = 2;
volatile byte buttonState = LOW;
unsigned long aktualnyCzas = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(115200);
Serial.println("----");
WiFi.begin( ssid, password );
while (WiFi.status() != WL_CONNECTED)
{
delay(1);
}
client.setServer(mqtt_server, mqttPort);
while (!client.connected())
{
if (client.connect("Przycisk2", mqttUser, mqttPassword ))
{
}
else
{
}
}
attachInterrupt(digitalPinToInterrupt(buttonPin), sendmqtt, FALLING);
}
void sendmqtt() {
buttonState = 1;
}
void loop() {
client.loop();
if (buttonState == 1)
{
detachInterrupt(buttonPin);
buttonState=0;
client.publish("ha/switch1", "ON");
aktualnyCzas = millis();
Serial.println(aktualnyCzas);
attachInterrupt(digitalPinToInterrupt(buttonPin), sendmqtt, FALLING);
}
}