Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By mienki
#77031 Hi,

Im trying to run an interrupt on Wemos D1 mini D4 pin.

Do I need any resistor/capacitor when using
Code: Select allpinMode(buttonPin, INPUT_PULLUP);
?
(or just switch between gnd and D4?)


Instead 1 interrupt i get a series (I get an interrupt on pressing and releasing switch despite FALLING option).
The idea is to get an interrupt on pressing the switch.

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const char* ssid = "xxx";
const char* password = "xxx";

const char* mqtt_server = "192.168.1.200";
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);
  delay(1000);
}
 
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);
          delay(1000);
          attachInterrupt(digitalPinToInterrupt(buttonPin), sendmqtt, FALLING);
          }
 
}