guymcswain / pigpio-client

A nodejs client for pigpio socket interface.
MIT License
28 stars 11 forks source link

Notifications not working after network disconnect then reconnect. #22

Open Toeedev opened 4 years ago

Toeedev commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ghost commented 4 years ago

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

guymcswain commented 4 years ago

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

Toeedev commented 4 years ago

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

guymcswain commented 4 years ago

Please provide a test case that shows the failure.

Toeedev commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

guymcswain commented 4 years ago

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

Toeedev commented 4 years ago

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 commented 4 years ago

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

guymcswain commented 4 years ago

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

guymcswain commented 4 years ago

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

Toeedev commented 4 years ago

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 commented 4 years ago

Well, glad you got it sorted out.

guymcswain commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

guymcswain commented 4 years ago

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.