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

timeout on first connect #42

Open
btsimonh opened this issue Feb 16, 2023 · 0 comments
Open

timeout on first connect #42

btsimonh opened this issue Feb 16, 2023 · 0 comments

Comments

@btsimonh
Copy link

Hi @guymcswain,

When a host is not available at startup:

if I don't specify pi.timeout, then I get the error event fired.

If I DO specify pi.timeout, then I don't get 'error', 'connected' or 'disconnected', but the pigpio instance stops and releases, and node exits. So there is no way to detect connection failure, and try, for example, an alternative address?

example code (try with and without timeout set....):

let host = '192.168.1.185' || process.env.npm_package_config_host;
let proc = require('process');
// comment out to stop debug
proc.env['DEBUG'] = 'pigpio';
proc.env['PIGPIO'] = '1';
// Connect to the Rpi...
console.log('#### trying to connect to '+host);
let opts = {
    host: host, // the host it will try to connect to port 8888 on....
    timeout: 3, // retry timer in s?
};
const pigpio = require('../pigpio-client').pigpio(opts);  
// a simple wait routine that can be called using await...
function wait(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}
async function keepNodeAlive(){
    while (1){
        await wait(2000);
    }
}
//keepNodeAlive();

const ready = new Promise((resolve, reject) => {
  pigpio.once('connected', resolve);
  pigpio.once('error', reject);
});

pigpio.on('error', (e)=>{ console.log('#### pigpio error (on call)',e); });
pigpio.on('disconnected', (e)=>{ console.log('#### pigpio disconnected',e); });

ready.then(async (info) => {
    // display information on pigpio and connection status
    console.log('#### pigpio connected\n'+JSON.stringify(info,null,2));
    pigpio.end(()=>{ console.log('#### pigpio ended - should exit node now'); })
}).catch((e)=>{
    console.log('#### pigpio error (once call)', e);
});
console.log('#### Waiting for pigpio connection to '+host);
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

1 participant