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