Beer thermometer with raspberry pi and ds18b20

Hell yeah! it worked! After changing it to bcm 4 it showed the correct address and temp.
Finally!
Now, how do I change the default pin, and secondly, how do I connect two sensors if I can’t change the gpio pin by default…

Also, after only two readings, it stops working, not showing more temps, this is my current code:

var W1Temp = require('/usr/local/lib/node_modules/w1temp');
var blynkLib = require('blynk-library');
var AUTH = 'token';

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

// Automatically update sensors value every 5 seconds
setInterval(function() {
W1Temp.getSensor('28-051684c0d6ff').then(function (sensor) {
        // print actual temperature
        var temp = sensor.getTemperature();
        console.log('Actual temp:', temp, '°C');

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

@raul good that you have at least had some readings from the sensor.

We tried with the timed interval and in the end went with the “on change” script. Not ideal but we have additional code to cover the permanently updating temperature readings and formatting to 1 decimal place etc.

I suspect you can change the default pin by making the entry in the file on the SD card and then modprobe. Don’t know if you need to remove the existing “default” but I suspect not.

With these sensors you should be able to wire 10 sensors up to the same pin as they use the chip ID’s. If I were you I would leave it where it is and just wire in the second sensor to the same pin.

Yeah, the easiest way is to connect them in series, as I read in some other projects, the only problem is that I am not sure of the way I should connect them, since one has a built in resistor and the other does not

In fact for Your realization this one build in resistor is sufficient for whole bus. Cause I assume the length is in meters. My bus is like star with resistor on RPi and sensors from one meter to five, If You end Your “serial” bus by second resistor, nothing should happens.

you can also read temperatures from command line to identify sensors:

cat /sys/bus/w1/devices/28-000002da51d5/w1_slave

Or you can use BASH script like this to read whole temperatures:

temperatures()                                                                                                                                                                      
{                                                                                                                                                                              
W1DIR="/sys/bus/w1/devices"                                                                                                                                                    
                                                                                                                                                                           
#exit if no one wire sensor exists                                                                                                                                             
if [ ! -d $W1DIR ]                                                                                                                                                             
then                                                                                                                                                                           
    echo "There is no sensors!"                                                                                                                                     
    exit 1                                                                                                                                                                 
fi                                                                                                                                                                             
                                                                                                                                                                           
#list of sensors                                                                                                                                                                 
DEVICES=$(ls $W1DIR)                                                                                                                                                           
                                                                                                                                                                           
#Output sting                                                                                                                                                               
OUTPUT=""                                                                                                                                                                      
                                                                                                                                                                           
#cycle across all founded sensors                                                                                                                                                  
for DEVICE in $DEVICES                                                                                                                                                         
do                                                                                                                                                                             
    #ignor master                                                                                                                                                        
    if [ $DEVICE != "w1_bus_master1" ]                                                                                                                                      
    then                                                                                                                                                                   
            #read sensor                                                                                                                                                 
            ANSWER=$(cat $W1DIR/$DEVICE/w1_slave)                                                                                                                          
                                                                                                                                                                           
            #Check answer and CRC; because if sensor disapear its address will be  9x00 but CRC will be valid                                                                  
            echo -e "$ANSWER" | grep -e "00 00 00 00 00 00 00 00 00"  >&2                                                                                                  
            if [ $? -ne 0 ]
            then                                                                                                                                                           
                    #Temp is valid if CRC is valid                                                                                                          
                    echo -e "$ANSWER" | grep -q "YES"  >&2                                                                                                                 
                    if [ $? -eq 0 ]                                                                                                                                        
                    then                                                                                                                                                   
                            #temp is OK                                                                                                                                 
                            #Get only temp from two line answer                                                                                                 
                            TEMP=$(echo -e "$ANSWER" | grep "t=" | cut -f 2 -d "=")                                                                                        
                                                                                                                                                                           
                            INTEGER=$(($TEMP/1000)) #integers                                                                                                           
                            FRAC=$(($TEMP%1000)) #decimals                                                                                                           
                                                                                                                                                                           
                            #handle minus frac! int (-1,0)°C                                                                                                            
                            if [ "$FRAC" -lt "0" ] #is rest minus?                                                                                                     
                            then                                                                                                                                           
                                    FRAC=$(($FRAC * -1)) #del minus                                                                                                   
                                    if [ "$INTEGER" -ge "0" ] #is INTEGER 0 and more?                                                                                         
                                    then                                                                                                                                   
                                            INTEGER="-0" #this write minus to result, zero will be add next                                                         
                                    fi                                                                                                                                     
                            fi                                                                                                                                             
                            #Handle one or two cyfer result - int (-1, 1)°C                                                                                         
                            if [ "$FRAC" -lt "100" ] #is result less than  100?                                                                                            
                            then                                                                                                                                           
                                    if [ "$FRAC" -lt "10" ] #is result less than 10?                                                                                       
                                    then                                                                                                                                   
                                            FRAC="00"$FRAC #add two zeros                                                                                                
                                    else                                                                                                                                   
                                            FRAC="0"$FRAC #add one zero                                                                                               
                                    fi                                                                                                                                     
                            fi                                                                                                                                             
                                   
                            #This will print the integer part to output, it can be commented out                                                                                  
                            echo $INTEGER >&2                 
                                                                                                         
                            #store result                                                                                                                                
                            OUTPUT=`echo "$OUTPUT":"$INTEGER.$FRAC"`                                                                                                       
                    else                                                                                                                                                   
                            #CRC is invalid - error                                                                                                                            
                            echo "$DEVICE=CRC ERROR" >&2                                                                                                                   
                                                                                                                                                                           
                    fi                                                                                                                                                     
            fi                                                                                                                                                             
    fi                                                                                                                                                                     
                                                                                                                                                                           
                                                                                                                                                                           
done                                                                                                                                                                           
                                                                                                                                                                           
echo $OUTPUT                                                                                                                                                                   
}
1 Like

Thanks for the script mate! I think that in a future I will make something like that, will help me as a guide.
As for the connections, could you please make a little sketch with the wires? Since even after your explanation, I don’t really get how to connect them
Cheers!

Like a star I mean something like this (use 4K7):

And as a “serial” bus this:

My solution for monitoring temperatures is here with resistor on the bottom:

Allright, the only problem is that i have two types of sensors, one with a built in resistor and one like yours, so I don’t know how to connect them using only one gpio pin…
Thanks for the pics and explanation, really appreciate it

And what sensors do You have? What type?

@fisero the sensors @raul has are both DS18B20’s. One is the bare chip and the the other is the complete unit including resistor.

Tried your bash script, nothing happened. No error, no data, nothing. cat is fine.

Script is a function which should be included to other scripts. So remove from the begging temperatures(){ and from end } and than You will see the result. I use this to get temperatures for my log in RRD database see fisero.eu.

1 wire bus should work with 1K resistor if it is not so long (max 10 meters). So He can use as many sensors with build in resistor until he do not reach this paralel value. Question is now, what value of resistor is used? If 4K7 than max 4 sensors can be used (+ others without resistors).

@fisero script is working better now with the following output:

22
cat: /sys/bus/w1/devices/w1_bus_master1/w1_slave: No such file or directory
w1_bus_master1=CRC ERROR
:22.000 

Pretty sure it will be the standard 4K7 for the built in resistor.

My output:

10                                                                                                                                                                             
22                                                                                                                                                                             
35                                                                                                                                                                             
22                                                                                                                                                                             
cat: /sys/bus/w1/devices/w1_bus_master1/w1_slave: No such file or directory                                                                                                    
w1_bus_master1=CRC ERROR                                                                                                                                                       
:10.062:22.437:35.625:22.375

I see that I keep some debug output. Usually I route output to stderr and keep just variables. But I hope that this piece of code can help someone.

My latest is:

22
cat: /sys/bus/w1/devices/w1_bus_master1/w1_slave: No such file or directory
w1_bus_master1=CRC ERROR
Temperature is: 22.437 C

I only have 1 sensor hooked up at the moment, will try more later.
Why are we seeing the middle 2 lines?

It looks like the first condition not catch the device master… I see… There is a string w1_bus_master instead of w1_bus_master1 I’ve to update the script above.

This is the sensor, so how do you suggest I make the connections?

@fisero much better, thanks for the script.

@raul it’s not that important, I just plugged in a second sensor with built in 4K7, so both have 4K7, and it’s fine.

21
24
Temperature is: 21.562 C  Temperature is: 24.375 C

So you have 10K resistor and You can use up to 10 sensors like this on one bus like it is on pictures I posted before. D1 is for indicate power and D2 should indicate communication without influence on dominant state on bus.

Allright, have them connnectyed and reading temps correctly, now I face other problems:

Temp changed on the inside: 23.875 °C
Connecting to: blynk-cloud.com 8441
Temp changed on the outside: 23 °C
Temp changed on the outside: 23.062 °C
Temp changed on the inside: 23.812 °C
Temp changed on the outside: 23 °C
Temp changed on the outside: 23.062 °C
Temp changed on the inside: 23.75 °C
Connecting to: blynk-cloud.com 8441

As you can see, raspi never connects to the server, I will investigate and post my results here
Thank you two for your work and interest, I will post a guide as soon as I have all functioning

This is my code btw

var W1Temp = require('/usr/local/lib/node_modules/w1temp');
var blynkLib = require('/usr/local/lib/node_modules/blynk-library');
var AUTH = 'aa46fedd9fcf42e4b7983939647db885';

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

//Get IDs fo the sensors connected
//W1Temp.getSensorsUids().then(function (sensorsUids) {
//  console.log(sensorsUids);
//});

//28-051684c0d6ff built-in temp 
//28-041692675aff ds18b20 temp
W1Temp.getSensor('28-041692675aff').then(function (sensor) {
  
  var tempInside = sensor.getTemperature();

  sensor.on('change', function (tempInside) {
  console.log('Temp changed on the inside:', tempInside,'°C');

  //Report it to server
  blynk.virtualWrite(0, tempInside);
  });
  
});

W1Temp.getSensor('28-051684c0d6ff').then(function (sensor){
  
  var tempOutside = sensor.getTemperature();

  sensor.on('change', function (tempOutside) {
  console.log('Temp changed on the outside:', tempOutside,'°C');

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

});

I know it could be more stylish, but I will let this things for a future

Try a regular TCP connection by replacing:

var blynk = new blynkLib.Blynk(AUTH);

with:

var blynk = new Blynk.Blynk(AUTH, options = {
  connector : new Blynk.TcpClient()
});

tried with

var blynk = new blynkLib.Blynk(AUTH, options = {
  connector : new blynkLib.TcpClient()
});

equivalent output

Connecting to TCP: blynk-cloud.com 8442
Connecting to TCP: blynk-cloud.com 8442
Connecting to TCP: blynk-cloud.com 8442
Connecting to TCP: blynk-cloud.com 8442
Connecting to TCP: blynk-cloud.com 8442
Temp changed on the outside: 23.125 °C
Connecting to TCP: blynk-cloud.com 8442
Temp changed on the outside: 23.062 °C
Temp changed on the outside: 23.125 °C
Connecting to TCP: blynk-cloud.com 8442