0

I'm having some trouble getting my nodejs program to find my DS18B20 temp sensor.

While the pi4's pins aren't shown this is the gist of the wiring. I have a 4.7K ohm jumper resistor between my yellow signal wire which connects to the blue GPIO16 wire and the red +5v wire. Maybe worth noting, I've also tried this using +3.3v

My wiring

See comments in image

My code

const tc = require('w1temp'); // https://github.com/kolarcz/node-w1temp
const Gpio = require('onoff').Gpio; // https://github.com/fivdi/onoff+
const temps = {
    calc: {
        huntingChkAvg: 0, // Used to avoid recording bounce between temps 0.1 degree apart
        prevAvg: 0,
        avg: 0,
        calcAvg: function (tcIds) {
            let avg = 0;
            Object.keys(temps).map((key) => {
                if (key !== 'calc') {
                    avg += ((temps[key] * 9) / 5) + 32
                }
            })
            this.avg = (avg / tcIds.length)
            if (this.prevAvg === 0) { this.prevAvg = avg }
        }
    }
}
module.exports = {
   logTemp: function () {
        tc.getSensorsUids()
        .then(function (tcIds) {
            function round (val, precision) {
                let multiplier = Math.pow(10, precision || 0);
                return Math.round(val * multiplier) / multiplier;
            }
            console.log('IDs: ', tcIds);
            for (let i = 0; i < tcIds.length; i++) {
                temps[tcIds[i]] = null
                tc.getSensor(tcIds[i], enablePolling=true, interval=250).then(function (sensor) {
                    sensor.on('change', function (temp) {
                        temps[tcIds[i]] = temp
                        temps.calc.calcAvg(tcIds)
                        let avg = temps.calc.avg
                        let f = round(temps.calc.prevAvg, 3)
                        if (!(f - 0.1 < avg > f + 0.1) && avg !== temps.calc.huntingChkAvg) {
                            temps.calc.huntingChkAvg = temps.calc.prevAvg 
                            temps.calc.prevAvg = avg
                           console.log(round(avg, 1))
                        } else if (!(f - 0.1 < avg > f + 0.1)) {
                            temps.calc.huntingChkAvg = temps.calc.prevAvg
                            temps.calc.prevAvg = avg 
                        }
                    })
                }).catch((err) => { console.log('getSensor Error: ', err) });
            }
        }).catch((err) => { console.log('Error: ', err) });
    }
}

See the comments in the image below but basically a device seems to be getting added for the DS18B20 but the node-w1temp getSensor() method isn't finding it.

My checks

enter image description here

  • Your nodeW1Temp might be out of date. You might first try my very short, fully debugged, plug and play Python test program to make sure GPIO, pullup etc, wiring is OK, before moving to your nodeJS version. : (1) raspberrypi.stackexchange.com/questions/100203/… (2) raspberrypi.stackexchange.com/questions/102078/…. Happy New Year and Cheers. – tlfong01 Dec 31 '19 at 1:15
  • 1
    I switched to gpio4, the default 1wire gpio, and it found the sensor. For some reason it just wasn't picking up that I had set gpio16 in /boot/config.txt. – Brad W Dec 31 '19 at 20:05
  • Nice to hear the 2020 good news. So after all nodeWTemp i s a good DS18B20 driver. Have a nice year. Cheers. – tlfong01 Jan 1 at 0:40
  • Please don't post pictures of text. Instead paste the text direct into the question. – Ingo Jan 2 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.