Temperature sensor DS18b20 and Raspberry Pi doesn't run forever and also doesn't send values on timer

Hi,
just for starter I am really beginner at all this and I just usualy google long enough to find a working solution. This is actually my first post so please excuse me if I’ll make some mistake. But now I am facing problem that seems strange.

I am running script as mentioned below (everythings fine) and it keeps running for aprox. day but then crashes. I think it is some mem related problem and right now i am loggin TOP for Node process - I’ll post it when it stop working again. Node uses 12,5% of mem at start up and keep rising.

I’ve even deleted print in terminal (hoping it save some mem) because I run it autostarted from /etc/rc.local using line below and I dont need this access.

#Run Temperature Sensor
node /home/pi/temp/index.js < /dev/null &

I think it is realted to the number of outputs from the sesnor because i can speed up mem usage with hassling my hot finger arround sensor and forcing it to send more changes.

I wanted to send temp after defined time (eg 2 sec) but this just doesnt want to run in loop and get stucked afer twice sending temp to the blynk server (commong problem regarding to https://community.blynk.cc/t/solved-raspberry-pi-with-ds18b20-temp-sensor-stops-after-6-readings/14011/7 )

Setup:
Raspberry Pi Model B Revision 2.0 - running Raspbian Stretch
temperature sensor ds18b20
connected via USB Wi-Fi dongle
Blynk Cloud

My code:

var blynkLib = require('blynk-library');
var W1Temp = require('w1temp');
var AUTH = 'Auth-Token';

// Setup Blynk (SSL)
var blynk = new blynkLib.Blynk(AUTH);

// Automatically update sensors value whenever temp changes

W1Temp.getSensor('28-000009b293db').then(function (sensor) {


        sensor.on('change', function (temp) {

            //Report it to server
        blynk.virtualWrite(1, temp);
   });
   });

Can anyone tell me what am I doing wrong and why it crashes? Or can anyone help me send temp value st specific interval instead on change? (I know 2 different problems)

I am even open to use C instead of Node but I coudn’t find any working example online

Thanks a lot

No idea on the longevity of the sketch run… I have a NodeJS one that sometimes quits after a day, other times keeps going for weeks… doing the exact same things all the time.

PS. using the same model of RPi, but running on Ethernet

You can use an interval timer to read then send your data. See an LED flashing example I made, using a few different timers… a single interval timer principle will be the same for your use case

Thanks @Gunner

But how do you solve this issue with randomly stopping NodeJS?

I’ll try to use your example soon.

Since I do not know how or why it happens… I don’t. But with RPis since they do lots of other stuff in the background, like raspbian, and tend to be power hungry, it could be anything.

So to keep Node JS script running forever and autostart on boot I’ve used “Forever”. https://www.npmjs.com/package/forever

I am running it with line at /etc/rc.local

export PATH=$PATH:/opt/nodejs/bin/&&forever start /path/to/your/script.js < /dev/null &

Actually i dont know why there has to be “export PATH=$PATH:/opt/nodejs/bin/”
but I’ve installed Node JS using this tutorial and it doesn’t work without it. http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/raspberry-pi/how-to-install-nodejs-library-on-linux

But when I change getting temp on timer instead when temp changes using code below it just sends two values and then the script freezes.

var blynkLib = require('blynk-library');
var W1Temp = require('w1temp');
var AUTH = 'HERE_COMES_YOUR_TOKEN'; 

// Setup Blynk (SSL)
var blynk = new blynkLib.Blynk(AUTH); 

// Automatically update sensors value every 2 sec
setInterval(function() {
W1Temp.getSensor('28-000009b293db').then(function (sensor) {
	// print actual temperature 
	var temp = sensor.getTemperature();
	console.log('Actual temp:', temp, '°C');
            //Report it to server
	blynk.virtualWrite(0, temp);
   }); 	
}, 2000);

Does anyone know what might be wrong?

Are you using the timer function the correct way?

How about this (Untested!! - probably syntax issues?)…

var myVar = setInterval(myTimer, 2000);

function myTimer() {
W1Temp.getSensor('28-000009b293db').then(function (sensor) {
	// print actual temperature 
	var temp = sensor.getTemperature();
	console.log('Actual temp:', temp, '°C');
            //Report it to server
	blynk.virtualWrite(0, temp);
   }); 	
}

not working

still the same issue reads the temperature twice and then freezes.

Then perhaps the issue is in the reading of the sensor. Is it “freezing” or is the script throwing an error and stopping? I have seen that when testing a DHT sensor but still haven’t figured out how to get proper error trapping.

The sensor is working properly when I read it just using python and printing into terminal.

Script only freeze it doesnt show any error it stop printing values and sending it into Blynk

I would love to provide some debug info but i don’t know how. Just tell me.

But JS and Python are different… thus probably using different methods or “libraries” for that sensor… and the JS one (w1temp) may have issues? I don’t see any setup commands for which pins you are using for the sensor, or are you doing so in the RPi setup?

@Gunner thanks for your help you gave me idea that someting might be wrong with w1temp package so here is a solution I’ve found.

This works (at least now):

   var Blynk = require('blynk-library');
var sensor = require('ds18b20-raspi');
var AUTH = 'tokenplacebro';

// Setup Blynk (SSL)
var blynk = new Blynk.Blynk(AUTH,
options= { addr:'blynk-cloud.com', port:443 } );

// Automatically update sensor value every 1 sec

var myVar = setInterval(myTimer, 1000);

function myTimer() {
const tempC = sensor.readSimpleC();
console.log(`${tempC} °C`);

//report to the Blynk
        blynk.virtualWrite(1, tempC);
   };

As you can see I am using this ↓↓ NPM package to read DS18b20 sensor and it doesn’t freeze. Works only for one sensor connected at a time but I guess it is possible to read more of them after some tweaking.

1 Like

Yes it can, based on the device ID that each sensor has.

Actually can you tell me what is the difference between declining dependencies with VAR or CONST?

Not a clue :stuck_out_tongue_winking_eye: … but you can Google it.

this help me a lot! Thanks!

1 Like