Hello
I'm currently working on a garage door security project.
The goal is to watch latches and lock, make sure everything stays closed, and push states changes to mqtt.
I'm using A3144 hall sensor and strong magnet with a typical d1mini implentation - see image - (not using decoupling capacitor, maybe an issue..)
The software mainly consists of 3 interrupts watching state changes.
Two issues I've noticed so far :
- Although there's no magnet movement, sensors outputs could toggle for no reason (quite sure this could be a capacitor issue as read elsewhere)
- Last time I've tried the setup for a long time, the d1 mini esp seems to have crashed. I'm suspecting something is wrong with my code.
Anyways I'm receiving mqtt updates but it's not reliable..
Any suggestions ?
Thanks for reading me.
my code below :
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
const int pinSensorSA = D1;
const int pinSensorSB = D2;
const int pinSensorSL = D5;
#define WLAN_SSID "ssid"
#define WLAN_PASS "pass"
#define AIO_SERVER "pi-ha.retz.lan"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "mqtt"
#define AIO_KEY "pass"
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish pushSA = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/retz/atelier/door/side_a");
Adafruit_MQTT_Publish pushSB = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/retz/atelier/door/side_b");
Adafruit_MQTT_Publish pushSL = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/retz/atelier/door/lock");
Adafruit_MQTT_Publish pushStatus = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/retz/atelier/door/status");
volatile byte stateSA = HIGH;
volatile byte stateSB = HIGH;
volatile byte stateSL = HIGH;
void MQTT_connect();
const long interval = 5000;
unsigned long previousMillis = 0;
void ICACHE_RAM_ATTR onChangeSL() {
stateSL = !stateSL;
if(stateSL) {
Serial.println(F("SL Open"));
pushSL.publish("open");
} else {
Serial.println(F("SL Closed"));
pushSL.publish("closed");
}
}
void ICACHE_RAM_ATTR onChangeSA() {
stateSA = !stateSA;
if(stateSA) {
Serial.println(F("SA Open"));
pushSA.publish("open");
} else {
Serial.println(F("SA Closed"));
pushSA.publish("closed");
}
}
void ICACHE_RAM_ATTR onChangeSB() {
stateSB = !stateSB;
if(stateSB) {
Serial.println(F("SB Open"));
pushSB.publish("open");
} else {
Serial.println(F("SB Closed"));
pushSB.publish("closed");
}
}
void setup() {
Serial.begin(115200,SERIAL_8N1,SERIAL_TX_ONLY);
pinMode(pinSensorSA, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinSensorSA), onChangeSA, CHANGE);
pinMode(pinSensorSB, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinSensorSB), onChangeSB, CHANGE);
pinMode(pinSensorSL, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pinSensorSL), onChangeSL, CHANGE);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
MQTT_connect();
pushStatus.publish("started");
}
void loop() {
MQTT_connect();
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
pushStatus.publish("update");
if(stateSA) {
pushSA.publish("open");
} else {
pushSA.publish("closed");
}
if(stateSB) {
pushSB.publish("open");
} else {
pushSB.publish("closed");
}
if(stateSL) {
pushSL.publish("open");
} else {
pushSL.publish("closed");
}
}
}
void MQTT_connect() {
int8_t ret;
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
-> My sensors are quite far from the board (1 meter). Do I need to place the capacitor next to the board ? Or next to the sensor ?
-> Does my interrupts implementation could be a source of crash ?