sinricpro / esp8266-esp32-sdk

Library for https://sinric.pro - simple way to connect your device to Alexa, Google Home, SmartThings and cloud
https://sinric.pro
227 stars 121 forks source link

Reed switch issue #344

Closed LongingIngernot closed 8 months ago

LongingIngernot commented 8 months ago

Hello! I am trying to use an esp32 board to monitor a door's state. The problem is that the reed switch, while contacts are close, it's not doing anything and while they're distant it's constantly fluctuating between open and close.

sivar2311 commented 8 months ago

Hi @LongingIngernot

What's your wiring diagram?

Sounds like the pin is floating and not set to a defined level when the contact is open.

Do you use a pulldown resistor like in this diagram? image

LongingIngernot commented 8 months ago

I have it wired to pin 26 and to GND, though I don't have a resistor

sivar2311 commented 8 months ago

Then the pin is floating when the contact is open.

Use a pull-down resistor and change the wiring as shown in the diagram.

LongingIngernot commented 8 months ago

Also, can I use a switch device/example to toggle a garage door(acting as momentary) or should I the garage door example?

sivar2311 commented 8 months ago

For this, I recommend using a garage door device. Alexa, for example, has an additional PIN query (for security) that you don't have with a normal switch device.

LongingIngernot commented 8 months ago

How would I set it up? Also, could I include a contact sensor with it?

sivar2311 commented 8 months ago

How would I set it up?

This question cannot be answered in general terms and depends on several factors (e.g. the electronics that drive your garage door).

Also, could I include a contact sensor with it?

I recommend using a contact sensor to determine the position of the door (open or closed).

LongingIngernot commented 8 months ago

I fixed the floating pin by using the pinMode(CONTACT_PIN, INPUT_PULLUP); function but Sinric Pro displays the opposite of the state, i.e.when it's open it shows closed on Sinric and vice-versa. Also, can I integrate a contact sensor in the garage tile/device?

sivar2311 commented 8 months ago

Simply invert the reading by adding a ! in front of digitalRead it like so:

  bool contactState = !digitalRead(CONTACT_PIN);

Also, can I integrate a contact sensor in the garage tile/device?

Yes, but without knowing more about your hardware, wiring, code, etc., these types of questions are too general to answer. This issue is about how to use a reed switch. I propose to open a new issue with detailed information and specific questions.