Dr. Arne Jachens

1wire Temperature Sensor DS 18B20

Raspberry_DS1820.svg

To activate the Device Tree for the 1wire, add the following line:
sudo nano /boot/config.txt

dtoverlay=w1-gpio-pullup,gpiopin=4,extpullup=on

After reboot there should be:
more /sys/bus/w1/devices/28-3c01b556c01a/w1_slave
showing:

45 01 55 05 7f a5 81 66 97 : crc=97 YES
45 01 55 05 7f a5 81 66 97 t=20312
where the last number indicates 20.3 °C.

http://www.netzmafia.de/skripten/hardware/RasPi/Projekt-Onewire/index.html


https://www.msxfaq.de/sonst/bastelbude/raspi/raspi-1wire.htm

Python interface

class HvcOneWire_DS1820:
    def  __init__(self):
        self.sensors = {}
        self.sensors["oven"]  = "28-3c01b5568a59"
        self.sensors["Tfolu"] = "28-3c01b55605f9"
        self.sensors["Terde"] = "28-3c01b5566967"
        self.sensors["Twalu"] = "28-3c01b5566647"
        for  s in self.sensors:
            HvcOneWire_DS1820.T[s] = -99
            
        return

    def  look4sensors(self):
        """
        Initially, check for  the 1-wire slaves available
        """
        file = open('/sys/devices/w1_bus_master1/w1_master_slaves')
        w1_slaves = file.readlines()
        file.close()
        sensNames = []
        for  line in w1_slaves:
            thisSensorName = line.split("\n")[0]
            sensNames.append(thisSensorName)
            
        return sensNames

    def  read(self):
        """
        For each of the DS1820 sensors, return the temperature.
        """
        for  s in self.sensors:
            try:
                file = open('/sys/bus/w1/devices/' + str(self.sensors[s]) + '/w1_slave')
                filecontent = file.read()
                file.close()

                # find and convert the temperature
                tempVal = filecontent.split("\n")[1].split(" ")[9]
                temperature = float(tempVal[2:]) / 1000.0
                HvcOneWire_DS1820.T[s] = temperature
            except:
                continue
                
        return HvcOneWire_DS1820.T

    #persistant values:
    T = {}



if __name__ == "__main__":
    import   time, threading
    
    w1 = HvcOneWire_DS1820()
    sensNames = w1.look4sensors()
    print("Sensor identifiers found:")
    print(sensNames)
    
    # align sensor identifiers to your naming, put this to init()
    sensors = {}
    sensors["oven"] = "28-3c01b5568a59"
    print("Sensor association")
    print(w1.sensors)



    next_call = time.time()
    while True:
        #read temperatures
        T = w1.read()
    
        # show Temperaturs
        for  s in T:
            print(str(s) + ':\t {0:4.1f}'.format( T[s] )+" °C")
        print("---------")

        next_call = next_call+10;
        time.sleep(next_call - time.time())