pigpio-client icon indicating copy to clipboard operation
pigpio-client copied to clipboard

timeout on first connect

Open btsimonh opened this issue 1 year ago • 0 comments

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);

btsimonh avatar Feb 16 '23 07:02 btsimonh