2

I have set up 3 temperature sensors (DS18B20) and connected them to what I believe is a Raspberry Pi 4. I have only been using the Pi 4 for easy testing before I move everything over to the Pi zero headless. (I have not been able to successfully use SSH with the Pi zero. So please don't ask me to see what settings and information are on the Raspberry Pi zero, although if you have an easy way to get access to the Pi zero without SSH that would also help!) On the Pi 4 everything works together flawlessly. But, once I move it over to the Raspberry Pi zero, I get no data.

So here is where I think the problem is:

sensor_1 = '/sys/bus/w1/devices/28-000004b91a2d/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-000004b8a26d/w1_slave'
sensor_3 = '/sys/bus/w1/devices/28-000004b8fb4c/w1_slave'

Does the device ID change after moving the sensors to the PI zero? (The device ID's were found on the Raspberry Pi 4)

The rest of the code in case it helps.

import time
import sys
from datetime import datetime
import csv


# System path to the sensor, further system paths could be via an array
# or other variables can be added here.
# 28-02161f5a48ee you have to replace with your sensor!
sensor_1 = '/sys/bus/w1/devices/28-000004b91a2d/w1_slave'
sensor_2 = '/sys/bus/w1/devices/28-000004b8a26d/w1_slave'
sensor_3 = '/sys/bus/w1/devices/28-000004b8fb4c/w1_slave'

#now = str(datetime.now())


def readTempSensor(sensorName):
    """I read the temperature of the DS18B20 from the system bus."""
    f = open(sensorName, 'r')
    lines = f.readlines()
    f.close()
    return lines


def readTempLines(sensorName):
    lines = readTempSensor(sensorName)
    # As long as the data could not be read, I am in an endless loop
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = readTempSensor(sensorName)
    temperaturStr = lines[1].find('t=')
    # I check if the temperature was found.
    if temperaturStr != -1:
        tempData = lines[1][temperaturStr + 2:]
        tempCelsius = float(tempData) / 1000.0
        tempKelvin = 273 + float(tempData) / 1000
        tempFahrenheit = float(tempData) / 1000 * 9.0 / 5.0 + 32.0
        # Return as an array - [0] tempCelsius => Celsius ...
        return [tempCelsius, tempKelvin, tempFahrenheit]


# where the script starts
def main():
    # creates header for CSV file
    f = open('/home/pi/Mamdau2DATA/TempDATA.csv', 'a')
    f.write('Date and Time')
    f.write(',')
    f.write('Temperature Sensor 1')
    f.write(',')
    f.write('Temperature Sensor 2')
    f.write(',')
    f.write('Temperature Sensor 3')
    f.write(' ')
    f.write('\n')
    f.close()

    try:
        while True:
            now = str(datetime.now())

            # I provide my measurement with a timestamp and have it displayed in the console.
            print("Temperature Sensor 1 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_1)[0]) + " °C")
            print("Temperature Sensor 2 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_2)[0]) + " °C")
            print("Temperature Sensor 3 at " + time.strftime('%H:%M:%S') + "   Temp. : " + str(readTempLines(sensor_3)[0]) + " °C")
            temperature_1 = readTempLines(sensor_1)[0]
            temperature_2 = readTempLines(sensor_2)[0]
            temperature_3 = readTempLines(sensor_3)[0]
            # The next measurement takes place after 10 seconds

            # Datenlogger
            f = open('/home/pi/Mamdau2DATA/TempDATA.csv', 'a')
            f.write(now)
            #f.write(time.strftime('%Y-%m-%d %H:%M:%S'))
            f.write(',')
            f.write('{0:0.2f}'.format(temperature_1))  # Temperature
            f.write(',')
            f.write('{0:0.2f}'.format(temperature_2))
            f.write(',')
            f.write('{0:0.2f}'.format(temperature_3))
            f.write(' ')  # Abstand/Leerzeichen
            f.write('\n')  # Neue Zeile
            f.close()

            # and so on

            time.sleep(1)
    except KeyboardInterrupt:
        # Program ends when CTRL + C is pressed.
        print('Temperature measurement is ended')
    except Exception as e:
        print(str(e))
        sys.exit(1)
    finally:
        # The program ends here so that no error is written to the console.
        print('Program has ended.')
        sys.exit(0)


if __name__ == '__main__':
    main()
  • Hello @ViperSniper0501, your program looks OK. Perhaps you can try my demo program to compare and contrast with yours. My completely listed program is fully debugged and plug and play. You just copy and run it without setting any parameter. The DS18B20s are auto detected and IDs listed: (1) "DS18B20 Temperature Sensor - Rpi 3/4 Driver, Wiring, Detection, and Python Programming": raspberrypi.stackexchange.com/questions/100203/…. Happy temperature sensing and cheers. – tlfong01 Mar 18 at 8:44
5

Each individual DS18B20 has its own unique ID # assigned at the factory. That device will have the same ID for its entire life and will never change. So if you unplug it from one host computer and plug into another, the ID will remain the same.

As I recall from reading the spec. sheet for the device, the ID is stored in non-volatile bits in the device and is programmed as a part of the manufacturing process.

| improve this answer | |
5

No, the device id does not change, it is hardwired into the DS18B20.

| improve this answer | |
1

Just be aware that low cost devices MAY have the same address hardwired in them...

If in doubt - buy from reputable sellers (the temp readings are also more consistent I find).

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.