logo elektroda
logo elektroda
X
logo elektroda

[ESP8266] How to prevent code from being called more than once when a button is pressed?

mienki 876 2
ADVERTISEMENT
Treść została przetłumaczona polish » english Zobacz oryginalną wersję tematu
  • #1 17327107
    mienki
    Level 10  
    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.


    #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);  
              }
      
    }
    .
  • ADVERTISEMENT
  • #2 17328547
    JacekCz
    Level 42  
    As an aside: on most systems I would not like the disconnect/attachment of the interrupt handler in a loop. As I understand it, this is set at startup.
    MAYBE a one cycle algorithm would need to be conditioned.

    I am not familiar with the ESP8266
  • #3 17329329
    krzbor
    Level 27  
    A 100nF capacitor should be added in parallel to the switch. Of course, the pull-up should also be there.
ADVERTISEMENT