So I didn’t just want to ask the question without trying as much as I could on my own. I am attempting to add a second door switch to my circuit with little to no success. At one point I was able to see the doors separately, don’t really know how I did it but it never worked properly. Also , no matter what I do < I cannot get Telegram to post a response without it bouncing . I have tried changing the interval from 1500 to 2000 , adding a .22mfd cap in parallel with the switch and still it wants to repeat readings. Just seems ridiculous that I my only recourse would be to use 2 seperate boards to achieve the results I want. I am using the Doiting ESP12F board ,here is what I have done to the code:
Start your code here
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Set GPIOs for LED and reedswitch
const int reedSwitch = 4;
const int reedSwitchB = 5;
const int led = 2; //Indicator for overhead garage door.
const int led2 = 13; //Indicator for side garage door.
// Detects whenever the door changed state
bool changeState = false;
bool changeState2 = false;
// Holds reedswitch state (1=opened, 0=close)
bool state;
bool state2;
String doorState;
String doorState2;
// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0;
const long interval = 1500;
const char* ssid = “*****”;
const char* password = “************”;
// Initialize Telegram BOT
#define BOTtoken “*********************************” // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click “start” on a bot before it can
// message you
#define CHAT_ID “**********”
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void changeDoorStatus() {
Serial.println(“State changed”);
changeState = true;
changeState2 = true;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
configTime(0, 0, “pool.ntp.org”); // get UTC time via NTP
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
// Read the current door state
pinMode(reedSwitch, INPUT_PULLUP);
pinMode(reedSwitchB,INPUT_PULLUP):
state = digitalRead(reedSwitch);
state2 = digitalRead(reedSwitchB);
// Set LED state to match door state
pinMode(led, OUTPUT);
pinMode(led2, OUTPUT);
digitalWrite(led, state);
digitalWrite(led2, state2);
// Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);
attachInterrupt(digitalPinToInterrupt(reedSwitchB), changeDoorStatus, CHANGE);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
bot.sendMessage(CHAT_ID, “Bot started up”, “”);
}
void loop() {
if (changeState) {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
// If a state has occured, invert the current door state
state = !state;
state2 = !state;
if (state) {
doorState = “closed”;
Serial.println (“Garage overhead closed”);
}
else {
doorState = “open”;
Serial.println (“Garage overhead open”);
delay(500);
}
state2 = !state;
if (state2) {
doorState2 = “closed”;
Serial.println (“Garage side door closed”);
}
else {
doorState2 = “open”;
Serial.println (“Garage side door open”);
}
digitalWrite(led, state);
digitalWrite(led2, state2);
changeState = false;
changeState2 = false;
Serial.println(state);
Serial.println(doorState);
Serial.println (state2);
Serial.println(doorState2);
//Send notification
bot.sendMessage(CHAT_ID, “The door is ” + doorState, “”);
bot.sendMessage(CHAT_ID, “The door is ” + doorState2, “”);
}
}
}
Hi.
I’m sorry for taking so long to get back to you. I missed your question and only noticed it today.
Your code is very confusing. It uses interrupts and digitalRead() at the same time??
Then, you configured an interrupt for each pin, which is fine. But, then, you call the same callback function?? That way you don’t know which pin caused the interrupt. The way it is now, it will change both states when one interrupt is triggered. I don’t think that is what you want.
Do you have a working code for one reed switch?
Share that code with me. Then, we can proceed to try to add another reed switch.
Regards,
Sara
Yes , it’s one of yours, I just tried to duplicate your code for the door sensor with Telegram. I just added a second reedSwitch and followed along with the rest of the code modified it for second switch . But even when I use just the original circuit and code , I get bounce on switch in serial monitor and in the Telegram app. For some reason it wiser the door open then it will bounce a door closed message . Thanks
Hi again.
Which type of reed switch are you using? Maybe it is too sensitive?
Do you have an oscilloscope? If you do, you can check the behavior of the reed switch and check how much time it takes bouncing.
You can try to run the Arduino debounce sketch example (that is used for pushbuttons) and see how much time you need to debounce.
Regards,
Sara
I am currently just using a breadboard to test this circuit, and using a jumper wire, Found out at one point the tactile switch I was using was intermittent. Yes I have a Extech Multimeter that is a graphical display but am not really comfortable or know how to use it properly to do this ,I need to learn how to use the scope part of this meter.I will look for the debounce sketch example . So I started fresh, dumped the code and reinstalled it for the sake of doing it using the original code given in your sketch. After a few cycles I would see that with the door closed and then opened it would change it’s reporting ,door is open but sows on the Telegram app its open, led status light(13)is on . So all I can assume here is that the Wemos is not seeing the change of state even though the program has .If I close the door the status light goes out but Telegram shows door open. If I close the door and restart the Wemos (reset) led off,open door led off for a few seconds then on and Telegram shows door is closed even though door is actually open. Here is the code I am using, is there something wrong in the way it is written?
Start your code here
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Set GPIOs for LED and reedswitch
const int reedSwitch = 4;
const int led = 2; //optional
// Detects whenever the door changed state
bool changeState = false;
// Holds reedswitch state (1=opened, 0=close)
bool state;
String doorState;
// Auxiliary variables (it will only detect changes that are 1500 milliseconds apart)
unsigned long previousMillis = 0;
const long interval = 1500;
const char* ssid = “******”;
const char* password = “*******”;
// Initialize Telegram BOT
#define BOTtoken “************:******************************” // your Bot Token (Get from Botfather)
// Use @myidbot to find out the chat ID of an individual or a group
// Also note that you need to click “start” on a bot before it can
// message you
#define CHAT_ID “*********”
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
// Runs whenever the reedswitch changes state
ICACHE_RAM_ATTR void changeDoorStatus() {
Serial.println(“State changed”);
changeState = true;
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
configTime(0, 0, “pool.ntp.org”); // get UTC time via NTP
client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
// Read the current door state
pinMode(reedSwitch, INPUT_PULLUP);
state = digitalRead(reedSwitch);
// Set LED state to match door state
pinMode(led, OUTPUT);
digitalWrite(led, state);
// Set the reedswitch pin as interrupt, assign interrupt function and set CHANGE mode
attachInterrupt(digitalPinToInterrupt(reedSwitch), changeDoorStatus, CHANGE);
// Connect to Wi-Fi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
bot.sendMessage(CHAT_ID, “Bot started up”, “”);
}
void loop() {
if (changeState) {
unsigned long currentMillis = millis();
if (currentMillis – previousMillis >= interval) {
previousMillis = currentMillis;
// If a state has occured, invert the current door state
state = !state;
if (state) {
doorState = “closed”;
}
else {
doorState = “open”;
}
digitalWrite(led, !state);
changeState = false;
Serial.println(state);
Serial.println(doorState);
//Send notification
bot.sendMessage(CHAT_ID, “The door is ” + doorState, “”);
}
}
}
The code is the same as the one we provided in our project. It was working just fine when we published it.
It seems like a debouncing problem. Testing with a wire is a great alternative to try to understand if the code is working as expected.
If you don’t have a good wi-fi connection, it might take some time to send the message, so you may receive the wrong door state.
Try the debounce sketch and see if it works as expected. You can then, try to use a debounce sketch with digitalRead() instead of interrupts for this project.
Regards,
Sara
So what do I need to do on the renounce sketch to see the results, as written it doesn’t display anything. I know I have to add Serial.println but what else’s do I need todo. Sounds like it’s pretty basic but that’s also me .
You may just want to go back to the basics. Remove all of the WiFi stuff and the Telegram stuff. Get the state stuff working reliably first. Test with printing to the serial port as you have suggested. Once you have that, add the Wifi and Telegram and see if it’s still stable. I would keep with the interrupts.
With further reading I found that using attachInterrupt does not give you the option of finding out which pin caused the interrupt. With that you would need two separate ISR’s. To get this working you need more granularity. If you look at https://github.com/espressif/arduino-esp32/issues/1111 usmanshahid001 comments about half way through with “Try this code”. His code basically allows you to pass any variable into your ISR (In his case it’s just the PIN number but it could be a struct with all sorts of data).
For debouncing the switch I would suggest a hardware solution – https://circuitdigest.com/electronic-circuits/what-is-switch-bouncing-and-how-to-prevent-it-using-debounce-circuit
Steve , thanks, been reading about debouching and I understand that it is a condition typical with switches and that some cheap switches makes it even worse. I have tried a 22 uf electrolytic capacitors, wasn’t a DC cap but should have worked, it didn’t. I know I could just put an ic in the circuitry but was hoping to not have to add to complicate it, keep it simple and clean is what I was going after.
Steve , thanks, been reading about debouching and I understand that it is a condition typical with switches and that some cheap switches makes it even worse. I have tried a 22 uf electrolytic capacitors, wasn’t a DC cap but should have worked, it didn’t. I know I could just put an ic in the circuitry but was hoping to not have to add to complicate it, keep it simple and clean is what I was going after.
Switch debouncing is difficult. It’s more art than science. You either have a complicated software setup or a hardware setup. It may not be a debouncing problem. I have gone down that rabbit hole before only to find it had nothing to do with debouncing. To really find out you would need an oscilloscope.
Not sure how much I want to waste on this project, for something so simple, it’s been a chore. I know I am the problem, not knowing enough or understanding it . I have done what you suggested, just the switches and no WiFi or Telegram but still could not get both switches to report independently. Seems that they will show both switches when used , I did create them independently and they work until I combine them. As far a a O scope , I have an Etech graphical meter but really not versed on using it .
Hi Larry.
For debouncing multiple buttons, you may want to take a look at this article: https://www.e-tinkers.com/2021/05/the-simplest-button-debounce-solution/
Regards,
Sara