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()