Open Penguin-Lin opened 5 years ago
Hi @Penguin-Lin !
Not entirely sure. At first glance, I'd suspect it has to do with how you're setting up raspi-i2c: https://github.com/nebrius/raspi-i2c
Without code that reproduces the issue, there isn't much I can help with.
Thanks, -Kevin
Hey @kfitzgerald !
I'm a beginner in js and RPi. I wanna receive the data from two ADS1115 devices connected to my RPi. I wrote the module initilize_ADS1115.js which creates and returns the ADS1115 object. This object gets parameters such as address, pga, sps.
Next in module server.js I use this function from the module initilize_ADS1115.js, create the different objects Of course I transmit different addresses. I use a promise. Next I wanna receive the data from these two devices in time gap using setInterval. But I get the error in console which you can see at the picture.
I tried to use different time gaps. It doen't work.
I need to notice that the ADS 1115 devices work well separately.
How can I fix it? Do you know?
Regards, Niki.
I received the same error message and realized what was wrong. It's the same issue you had and it's a timing issue. The
init(()=>{
// do stuff
})
takes time to setup, but it returns before it's finished initializing. This isn't a problem when you run everything inside the init function. However, you were returning the Promise immediately with setTimeout(()=> resolve(), 0);
. If you set your timeout to be a higher value your code would work fine.
Here's an example from my test:
console.time('loaded')
console.time('returned')
init(()=>{
console.timeEnd('loaded')
})
console.timeEnd('returned')
// returned: 0.458ms
// loaded: 370.882ms
(And, btw, @kfitzgerald, thanks for a great module! I wish I found this a couple years ago but am now converting my Python code to JS and this ADC would have been a challenge convert on my own!)
Hi @tagyoureit
Appreciate the kind words!
As you found, the module was written with callbacks pre async/await was mainlined into node. Hopefully someday we can get the module enhanced to accept callbacks and return a promise, but I'm afraid I don't have the time at the moment to work on that.
Instead, if you're looking to run the init process linearly, instead of using callbacks, you could do something like:
await new Promise(resolve => init(resolve));
Node's util.promisify
could be helpful converting some of the module's callback functions to promises, but the additional arguments in the callbacks (e.g. volts) might get lost.
Thanks again, -Kevin
how to fix that? i don't know what happened..