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

Moderator: igrr

User avatar
By Abraxas
#92570 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])

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
User avatar
By Andreas_Mainz
#92589 Hello,

your interrupt function "Alam" MUST be of type IRAM_ATTR . I copied an Interrupt example below:

In Loop ypu have only to check: If (T5_Event ) { digitalwrite(T5_state)}

void IRAM_ATTR ISR_T5()
{
T5_Event_Rising = millis();
if ((T5_Event_Rising - T5_Event_Falling) > 10) // Bouncing passed
{
attachInterrupt(Kontakt, ISR_T5_RELEASE, RISING);
T5_Event = 1;
T5_State = 1;
}
}
void IRAM_ATTR ISR_T5_RELEASE()
{
T5_Event_Falling = millis();
timer1 = (T5_Event_Falling - T5_Event_Rising);
if (timer1 > 10) // Bouncing passed
{
attachInterrupt(Kontakt, ISR_T5, FALLING);
T5_Event = 1;
T5_State = 0;
}
}

I hope this helps..
BR Andreas
User avatar
By Abraxas
#92602 Hi,

yeah well No.... sadly my compiler (Arduino IDE) doesn't recognize this Attribute so all I get are errors that my function isn't declared anymore... (bummer!)

But I did a bit of research regarding this Flag and it seems it is automatically set in Arduino IDE.

I could try a different compiler to prove my point but haven't yet.
Any suggestions? Eclipse maybe?

BR Abraxas