0

I am trying to implement the code below on a raspberry pi with node.js. This code is just an extension of the example found here https://github.com/maugdog/max31855 and trying to implement a while loop to run this forever. When I try to implement a while loop, the temperatures never print out on the console but if I use for loops it seems to work (unless I use a for loop with statement 1 and statement 3 omitted which is what I have read is basically a while loop). Any idea what I am doing wrong? This code calls out other functions such as the max31855 and I have gone into that and put some 'console.log' instructions to try and determine where the code hangs but I have not been successful in figuring this out. I thought maybe this has to do with the callback features of node.js but I just don't know. This is basic code so not sure what fundamental flaw I am making.

Example code to read once:

    var max31855 = require('max31855');

var thermoSensor = new max31855();
thermoSensor.readTemp(function(temp) {
thermoSensor.readTempC(function(temp) {
    console.log('Temp in degrees celsius: ', temp);
});

My code to read over and over:

while(true){            
var max31855 = require('max31855');

var thermoSensor = new max31855();
thermoSensor.readTemp(function(temp) {
thermoSensor.readTempC(function(temp) {
    console.log('Temp in degrees celsius: ', temp);
});

}

Mulepunch
  • 13
  • 6
  • Perhaps this [Why does a while loop block the event loop?](https://stackoverflow.com/questions/34824460/why-does-a-while-loop-block-the-event-loop/34825352#34825352) will explain. Also, see [How can we block the event loop](https://stackoverflow.com/questions/61358303/how-can-we-block-event-loop/61358589#61358589). Probably you should poll the sensor with `setInterval()` and some reasonable interval time such as a few seconds between intervals. – jfriend00 Apr 28 '21 at 18:50

2 Answers2

0

use simple recursion.

function getTemp() {
var max31855 = require('max31855');

var thermoSensor = new max31855();
thermoSensor.readTemp(function(temp) {
thermoSensor.readTempC(function(temp) {
    console.log('Temp in degrees celsius: ', temp);
});
getTemp();
} 

getTemp();
Phix
  • 9,364
  • 4
  • 35
  • 62
  • Thanks. I tried both options but they both function like the original while loop. The code is running but nothing prints to the console. When this code executes, it calls the script max31855.js which can be found here: github.com/maugdog/max31855/blob/master/max31855.js I have gone into that code and placed 'console.log' instructions in multiple places to see where it stops printing. My first print statement is just as the code calls the .readTempC function and that prints continuously. My next print statement is as it calls the this._read32 function and that prints continuously also. – Mulepunch Apr 28 '21 at 19:17
  • My next print statement is as the code enters the this._spi.read function under the MAX31855.prototype._read32 function and that doesn't print. Just not sure why? – Mulepunch Apr 28 '21 at 19:18
0

Between jfriend00's and Phix's comments above, I have found a solution. Below is the code which works just incase anyone is interested or stumbles on this later. Seems I need to look more in depth with blocking the event loop like jfriend00 explained.

function getTemp() {

var max31855 = require('max31855');

var thermoSensor = new max31855():
thermoSensor.readTempC(function(temp) {
    console.log('Temp in degrees celsius: ', temp);
 });
}

setInterval(getTemp,100);
Mulepunch
  • 13
  • 6