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
Other
234 stars 124 forks source link

How to use a reed-switch as a contact sensor #307

Closed box24grp335 closed 1 year ago

box24grp335 commented 1 year ago

Hello I have installed sinric pro using g wemos d1r1 board pin 5 as in the sketch I want to use plain contact sensor to notify me so status change on mobile app it shows connected ip address etc but when I trigger the contact it doesn't show change I am using android device

sivar2311 commented 1 year ago

Make sure you have notifications enabled: image

The device must first be switched on to handle the contact sensor: https://github.com/sinricpro/esp8266-esp32-sdk/blob/c03fd1bfae2baf10c5d13a4b008aa6118aeab06f/examples/ContactSensor/ContactSensor.ino#L60-L61

You can also delete line 61. Then the contact sensor will report even if the device has not been explicitly switched on. https://github.com/sinricpro/esp8266-esp32-sdk/blob/c03fd1bfae2baf10c5d13a4b008aa6118aeab06f/examples/ContactSensor/ContactSensor.ino#L61

box24grp335 commented 1 year ago

Thank you for your replyI have everything setup as suggestedThe contact display doesn't changeI am using  a msgnetic Reed contact switch Sent from my Bell Samsung device over Canada's largest network. -------- Original message --------From: sivar2311 @.> Date: 2023-01-07 3:06 p.m. (GMT-06:00) To: sinricpro/esp8266-esp32-sdk @.> Cc: box24grp335 @.>, Author @.> Subject: Re: [sinricpro/esp8266-esp32-sdk] Communication issues (Issue #307) Make sure you have notifications enabled:

The device must first be switched on to handle the contact sensor: https://github.com/sinricpro/esp8266-esp32-sdk/blob/c03fd1bfae2baf10c5d13a4b008aa6118aeab06f/examples/ContactSensor/ContactSensor.ino#L60-L61 You can also delete line 61. Then the contact sensor will report even if the device has not been explicitly switched on. https://github.com/sinricpro/esp8266-esp32-sdk/blob/c03fd1bfae2baf10c5d13a4b008aa6118aeab06f/examples/ContactSensor/ContactSensor.ino#L61

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

kakopappa commented 1 year ago

Since you didn't post your sketch or wiring diagram, it's hard to say where's the problem. First, need to make sure your reed-switch is connected and the program can read the status before sending it to Sinric Pro. Once you sorted it out, you can copy this code to original sketch

#define CONTACT_PIN D5 // where contactsensor is connected to

bool lastContactState = false;
unsigned long lastChange = 0;

void handleContactsensor() {
  unsigned long actualMillis = millis();
  if (actualMillis - lastChange < 250) return;          // debounce contact state transitions (same as debouncing a pushbutton)

  bool actualContactState = digitalRead(CONTACT_PIN);   // read actual state of contactsensor

  if (actualContactState != lastContactState) {         // if state has changed
    Serial.printf("Contactsensor is %s now\r\n", actualContactState?"open":"closed");
    lastContactState = actualContactState;              // update last known state
    lastChange = actualMillis;                          // update debounce time
  }
}

void setup() {
  Serial.begin(9600); Serial.printf("\r\n\r\n");
  pinMode(CONTACT_PIN, INPUT); 
}

void loop() { 
   handleContactsensor();
}

Once you flash the code, check whether it prints the status when reed-switch status changes.

box24grp335 commented 1 year ago

The code is the sinric pro for contact witch is connected to d1 and ground on wemos d1r1Sent from my Bell Samsung device over Canada's largest network. -------- Original message --------From: Aruna Tennakoon @.> Date: 2023-01-07 7:21 p.m. (GMT-06:00) To: sinricpro/esp8266-esp32-sdk @.> Cc: box24grp335 @.>, Author @.> Subject: Re: [sinricpro/esp8266-esp32-sdk] Communication issues (Issue #307) Since you didn't post your sketch or wiring diagram, it's hard to say where's the problem. First, need to make sure your reed-switch is connected and the program can read the status before sending it to Sinric Pro. Once you sorted it out, you can copy this code to

define CONTACT_PIN D5 // where contactsensor is connected to

bool lastContactState = false; unsigned long lastChange = 0;

void handleContactsensor() { unsigned long actualMillis = millis(); if (actualMillis - lastChange < 250) return; // debounce contact state transitions (same as debouncing a pushbutton)

bool actualContactState = digitalRead(CONTACT_PIN); // read actual state of contactsensor

if (actualContactState != lastContactState) { // if state has changed Serial.printf("Contactsensor is %s now\r\n", actualContactState?"open":"closed"); lastContactState = actualContactState; // update last known state lastChange = actualMillis; // update debounce time } }

void setup() { Serial.begin(9600); Serial.printf("\r\n\r\n"); pinMode(CONTACT_PIN, INPUT); }

void loop() { handleContactsensor(); }

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>

box24grp335 commented 1 year ago

From: "Aruna Tennakoon" @.> To: "sinricpro/esp8266-esp32-sdk" @.> Cc: "Tony Hrycyshyn" @.>, "Author" @.> Sent: Saturday, January 7, 2023 7:21:58 PM Subject: Re: [sinricpro/esp8266-esp32-sdk] Communication issues (Issue #307)

Since you didn't post your sketch or wiring diagram, it's hard to say where's the problem. First, need to make sure your reed-switch is connected and the program can read the status before sending it to Sinric Pro. Once you sorted it out, you can copy this code to

define CONTACT_PIN D5 // where contactsensor is connected to

bool lastContactState = false; unsigned long lastChange = 0;

void handleContactsensor() { unsigned long actualMillis = millis(); if (actualMillis - lastChange < 250) return; // debounce contact state transitions (same as debouncing a pushbutton)

bool actualContactState = digitalRead(CONTACT_PIN); // read actual state of contactsensor

if (actualContactState != lastContactState) { // if state has changed Serial.printf("Contactsensor is %s now\r\n", actualContactState?"open":"closed"); lastContactState = actualContactState; // update last known state lastChange = actualMillis; // update debounce time } }

void setup() { Serial.begin(9600); Serial.printf("\r\n\r\n"); pinMode(CONTACT_PIN, INPUT); }

void loop() { handleContactsensor(); }

— Reply to this email directly, [ https://github.com/sinricpro/esp8266-esp32-sdk/issues/307#issuecomment-1374679719 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AI4MBSGQFUNMCQD7L7SB2BLWRIJDNANCNFSM6AAAAAATUCYZ4M | unsubscribe ] . You are receiving this because you authored the thread. Message ID: @.***>

kakopappa commented 1 year ago

can't see the contents. replying to github email with attachments won't attach the images to Github.

box24grp335 commented 1 year ago

sorry i am a newbie and thank you for your help i hope this works

From: "Aruna Tennakoon" @.> To: "sinricpro/esp8266-esp32-sdk" @.> Cc: "Tony Hrycyshyn" @.>, "Author" @.> Sent: Saturday, January 7, 2023 8:04:44 PM Subject: Re: [sinricpro/esp8266-esp32-sdk] Communication issues (Issue #307)

can't see the contents. replying to github email with attachments won't attach the images to Github.

— Reply to this email directly, [ https://github.com/sinricpro/esp8266-esp32-sdk/issues/307#issuecomment-1374686873 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AI4MBSBWHX4SWLPVIHZVAXLWRIODZANCNFSM6AAAAAATUCYZ4M | unsubscribe ] . You are receiving this because you authored the thread. Message ID: @.***>

kakopappa commented 1 year ago

Can you goto GitHub and update the issue instead of replying to this email?

On Sun, 8 Jan 2023 at 9:15 AM box24grp335 @.***> wrote:

sorry i am a newbie and thank you for your help i hope this works

From: "Aruna Tennakoon" @.> To: "sinricpro/esp8266-esp32-sdk" @.> Cc: "Tony Hrycyshyn" @.>, "Author" @.> Sent: Saturday, January 7, 2023 8:04:44 PM Subject: Re: [sinricpro/esp8266-esp32-sdk] Communication issues (Issue

307)

can't see the contents. replying to github email with attachments won't attach the images to Github.

— Reply to this email directly, [ https://github.com/sinricpro/esp8266-esp32-sdk/issues/307#issuecomment-1374686873 | view it on GitHub ] , or [ https://github.com/notifications/unsubscribe-auth/AI4MBSBWHX4SWLPVIHZVAXLWRIODZANCNFSM6AAAAAATUCYZ4M | unsubscribe ] . You are receiving this because you authored the thread. Message ID: @.***>

— Reply to this email directly, view it on GitHub https://github.com/sinricpro/esp8266-esp32-sdk/issues/307#issuecomment-1374688475, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABZAZZTXH67G4MILAJFFPCDWRIPMNANCNFSM6AAAAAATUCYZ4M . You are receiving this because you commented.Message ID: @.***>

box24grp335 commented 1 year ago

The file is in arduino examples sinric pro contacts

sivar2311 commented 1 year ago

Is it really a "communication error" ?

Please enable debug (uncomment line 18) and check the output in serial monitor. Please post the output also in this github issue.

box24grp335 commented 1 year ago

WiFi: IP-Address is 192.168.1.11 Connected to SinricPro

nothing changes when o manually open and close contacts the disokay i get on mobile is a pic of contact and says contact is open

sivar2311 commented 1 year ago

There should be a message in serial monitor when you open / close the reed-switch: Contactsensor is open now / Contactsensor is closed now.

If not, check your wiring / the GPIO you use. If you're on an ESP8266 change #define CONTACT_PIN 5 to #define CONTACT_PIN D5

box24grp335 commented 1 year ago

no message in serial monitor onlly when i go to the sinric portal and press the test open and test close i get response on my mobile i have changed the define using wemos d1 r1 i have the reed switch connected to pins D5 and ground i have also tried replacing switch with jumper wire

sivar2311 commented 1 year ago

Did you change the define? There is a difference between a digital pin number (like D5) and a GPIO number. So you have to change #define CONTACT_PIN to either D5 or 14. See GPIO-Mapping on D1/R1: https://kychem.files.wordpress.com/2016/09/wemos-d1r1-pinout.jpg

box24grp335 commented 1 year ago

yes it was #define CONTACT_PIN D5 how do i include pictures

sivar2311 commented 1 year ago

image

Do you have a working (non SinricPro) sketch using a reed-switch ? Like a simple sketch that detects wheter the switch is open or closed and write the state to serial monitor?

box24grp335 commented 1 year ago

no i dont i tested reed switch with a meter

box24grp335 commented 1 year ago

WiFi: IP-Address is 192.168.1.11 [SinricPro]: Device "62a93fa9fb740f77fc0eef73" does not exist. Creating new device [SinricPro:add()]: Adding device with id "62a93fa9fb740f77fc0eef73". Contactsensor is open now [SinricProDevice::sendEvent]: The event could not be sent. No connection to the SinricPro server. [SinricPro:Websocket]: Connecting to WebSocket Server using SSL (ws.sinric.pro)

sivar2311 commented 1 year ago

I recommend to first write a simple sketch like i mentioned above.(Print the state of the reed switch to serial monitor.) If this sketch is running without any issues, continue using SinricPro.

box24grp335 commented 1 year ago

my board is now connected this is the serial monitor:

WiFi: IP-Address is 192.168.1.11 [SinricPro]: Device "62a93fa9fb740f77fc0eef73" does not exist. Creating new device [SinricPro:add()]: Adding device with id "62a93fa9fb740f77fc0eef73". SinricPro:Websocket: Connecting to WebSocket Server using SSL (ws.sinric.pro)

box24grp335 commented 1 year ago

sorry this is the correct one: "type": "event", "value": { "state": "open" } }, "signature": { "HMAC": "lrRVFg9evil8RG8rgrAjmFgeCVFFKcD9NAA5up4oB+o=" } } [SinricPro:handleSendQueue]: Sending to websocket [SinricPro:handleSendQueue()]: message sent

sivar2311 commented 1 year ago

So, the "open" message has been sent. What happens when you close the reed-switch now? Does it send a "close" message ?

box24grp335 commented 1 year ago

no the contacts are closed but still says opened on mobile

sivar2311 commented 1 year ago

It looks like, something is wrong with your wiring or something else. Again, i recommend to write a simple sketch to detect the reed-contact state first, before using SinricPro.

box24grp335 commented 1 year ago

i will find simple sketch and let you know

sivar2311 commented 1 year ago

I've made a simple "Reed Switch Emulation" (using a push button to emulate the reed switch). Maybe this helps you: https://wokwi.com/projects/353315713757787137

Please see also this article: https://randomnerdtutorials.com/monitor-your-door-using-magnetic-reed-switch-and-arduino/

box24grp335 commented 1 year ago

thank you for the sketch 15:33:36.038 -> rll�r$�n � l� b|���rb� b�nnlnn2b bp�$blrlp�n0 � l � b n�n� � b��nn'l�l�nn $nr���n rr�p�n r�b  b n�n  b��nn' l�nn $nr���n rlr��n rl� $ l��n�`

box24grp335 commented 1 year ago

thank you for your help i think i have resolved the issue. on the link for the sample contact sketch i noticed you sent 5 volt power with a resistor to the D5 pin it is now reporting open and closed i had one pin to D5 without power and resistor and it wouldn't work only thing left is to reverse the open and closed reporting

i reversed the high and low in the example code but the mobile is reporting reverse when the contacts are closed it is reporting open

sivar2311 commented 1 year ago

Please use 3.3V instead of 5V !!! The ESP's are not 5V tolerant.

There are two ways to invert the signal.

  1. Change your wiring to PULL-DOWN wiring. See this article for explanation: https://www.circuitbasics.com/pull-up-and-pull-down-resistors/

  2. Alternatively, invert the incoming signal in the sketch. In the example ContactSensor.ino, change line 66 from bool actualContactState = digitalRead(CONTACT_PIN); // read actual state of contactsensor to bool actualContactState = !digitalRead(CONTACT_PIN); // read actual state of contactsensor

For reference: Pull-Down wiring: https://wokwi.com/projects/353315713757787137 Pull-Up wiring: https://wokwi.com/projects/353347971389999105

box24grp335 commented 1 year ago

Thank you that resolved my issues