Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications not working after network disconnect then reconnect. #22

Open
Toeedev opened this issue Feb 22, 2020 · 21 comments
Open

Notifications not working after network disconnect then reconnect. #22

Toeedev opened this issue Feb 22, 2020 · 21 comments

Comments

@Toeedev
Copy link

Toeedev commented Feb 22, 2020

I also noticed, that button.notify has massive delay. I had to wait like 20-30 seconds to get event change. I ended up checking pin manually, but maybe I am doing something wrong ?
( I used your exact example )
Is it how it works, or I am doing it wrong. Are there any other listeners for edge?

@guymcswain
Copy link
Owner

The example should work. Something else must be going on - perhaps the network?

To see all socket transactions, including notifications, you can monitor them on a console by setting the node process environment variable PIGPIO=1.

@Toeedev
Copy link
Author

Toeedev commented Feb 23, 2020

I am not getting any response in console from notify, only after 30 seconds maybe
notification received: chunk size = 12

can't see any problems with network, I am on local network, If i check pin manually it all works fine, every second.

@guymcswain
Copy link
Owner

This indicates that pigpio-client is not receiving any packets. The problem lies somewhere else. Perhaps in your setup.

@ghost
Copy link

ghost commented Feb 23, 2020

Any, idea how I could debug that? I don't have anything additional, just pi with node on WiFi local network.

@guymcswain
Copy link
Owner

A volt meter to know that you are getting the proper levels when you think the pin should report 1 or 0.

@Toeedev
Copy link
Author

Toeedev commented Feb 23, 2020

when I read it manually with button.read() , every second , get's values right.

@guymcswain
Copy link
Owner

Please provide a test case that shows the failure.

@Toeedev
Copy link
Author

Toeedev commented Feb 23, 2020

I do get reaction from notify, but sometimes it takes 30 seconds and more, or it doesn't notify at all

const connectOptions = { host: config.slave.host, timeout: 0 } // timeout!
logger.info('Connecting to slave...')
const pigpio = client.pigpio(connectOptions) // start connecting
slave.PIR = pigpio.gpio(config.slave.pir)
slave.PIR.pullUpDown(1)
slave.PIR.modeSet('input')

slave.PIR.notify((level, tick) => {
  console.log(`Button changed to ${level} at ${tick} usec`)
})

@guymcswain
Copy link
Owner

You need to first connect the PIR input to something you know is providing transitions between valid pin levels, in other words, pulses.

You can create a pulses by writing to the same pin for which you want to receive notifications.

Disconnect any external circuitry to the input pin first!

For example, create a mock "PIR pulse":

await slave.PIR.write(1);
await sleep(1000);  // wait one second
await slave.PIR.write(0);

Run this within an interval timer to get a repeating pulse train.

@ghost
Copy link

ghost commented Feb 23, 2020

I am doing it by hardware, switching PIR pin between 3v3 and GND, is that not the same?

@guymcswain
Copy link
Owner

Ok, so demonstrate there is a problem with the library.

@Toeedev
Copy link
Author

Toeedev commented Feb 23, 2020

I don't know if it's library, but I don't do anything complicated yet.
so I added that loop

slave.PIR.notify((level, tick) => {
  console.log(`Button changed to ${level} at ${tick} usec`)
})
while (true) {
    slave.PIR.read().then((read) => {
      console.log(read)
    })
    await wait(2000)
  }

and my console output is ( waiting for notify is actually longer than that, just didn't want to paste it all here):

0
0
1
1
1
1
1
1
Button changed to 1 at 193389531 usec
1
1
0
0

I am changing state by hardware ( jump wire from pin to either GND or 3V3 on PI ).

@guymcswain
Copy link
Owner

guymcswain commented Feb 23, 2020

```javascript
slave.PIR.read().then((err, read) => {
if (err) console.log('err: ', err)
else console.log(read)
})

@guymcswain
Copy link
Owner

Sorry, you're using the promise. So use try/catch.

@guymcswain
Copy link
Owner

Run your script again using: PIGPIO=1 node yourscriptname.js
Maybe slow it down to not generate so much output.

@Toeedev
Copy link
Author

Toeedev commented Feb 23, 2020

ok I found the issue. I think I was setting up notify at wrong time. When I've done it on event connect. It works fine.
I will have to make hardware pull down resistor, as it triggers loads of event on single state change.
Well I guess it was my fault after all ;)

@guymcswain
Copy link
Owner

Well, glad you got it sorted out.

@guymcswain
Copy link
Owner

ok I found the issue. I think I was setting up notify at wrong time. When I've done it on event connect. It works fine.

Makes sense. The notify would be associated with either no socket or a socket that got closed somehow. So if you do get a socket disconnect, you will need to re-init your notifications!

@ghost
Copy link

ghost commented Feb 23, 2020

I got to that, when was reconnecting after disconnection. I have to reinit all gpios to make it work after reconnection.
This library has a big potential to create smart houses/devices. With pm2 it can be pretty reliable thing.

@guymcswain
Copy link
Owner

pm2 - I'll have to lean more about it. Thanks for the tip.

@guymcswain
Copy link
Owner

I'm re-opening this issue because notifications should be self re-initialized to keep consistent with gpio objects which re-connect without requiring the application to re-initialize them.

I'm also going to change the title to make it clear this issue deals with socket disconnection and later reconnection.

I have a few other items to clean up before I release a fix in 1.5.2 in a few days.

@guymcswain guymcswain reopened this Feb 27, 2020
@guymcswain guymcswain changed the title Button notify - big delay Notifications not working after network disconnect then reconnect. Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants