1

I power a RPi 4B, and DS18B20 sensors. My aim is to build an measurement system for our heating system. With 15-20 temperature senors.

  • Useing 4,7 kOhm pullup Resistor to 3 V
  • Powering star typology
  • VCC 3 V
  • useing python in two files: DS18B20classfile_sleep4.py and DS18B20multiple_time.py
  • long sensor cable over 30 m included

But I started small, with one to four sensors. 1 sensor works Ok (no problems for 1 week run time). But, if I use more, I get after a several days following error.

'list index out of range'

I am wondering very much. Because, a few days it seems to be fine. And later there comes the error.

After reading the error. I can start compileing without any problem. Hence, It might be only a temporary problem? What might be the reason? you find my code below the text. I got it from AZ-delivery. Did just a few changes (timestamp and sleep.time)

Hopeing for your advices

greetings Gari

DS18B20classfile_sleep4.py

import os
import glob
import time

class DS18B20:

def __init__(self):
    os.system('modprobe w1-gpio')
    os.system('modprobe w1-therm')
    base_dir = '/sys/bus/w1/devices/'
    device_folder = glob.glob(base_dir + '28*')
    self._count_devices = len(device_folder)
    self._devices = list()
    i = 0
    while i < self._count_devices:
        self._devices.append(device_folder[i] + '/w1_slave')
        i += 1

def device_names(self):
    names = list()
    for i in range(self._count_devices):
        names.append(self._devices[i])
        temp = names[i][20:35]
        names[i] = temp

    return names

def _read_temp(self, index):
    f = open(self._devices[index], 'r')
    lines = f.readlines()
    f.close()
    time.sleep(4)
    return lines

def tempC(self, index = 0):
    lines = self._read_temp(index)
    retries = 5
    while (lines[0].strip()[-3:] != 'YES') and (retries > 0):
      print('wait')
      time.sleep(4)
      lines = self._read_temp(index)
      retries -=1

    if retries == 0:
        return 998

    equals_pos = lines[1].find('t=')
    if equals_pos !=-1:
        temp = lines[1][equals_pos + 2:]
        return float(temp) / 1000
    else:
        return 999 # error

def device_count(self):
    return self._count_devices

DS18B20multiple_time.py

import time
from DS18B20classfile_sleep4 import DS18B20

degree_sign = u'\xb0' # degree sign devices = DS18B20() count = devices.device_count() names = devices.device_names()

print('[press ctrl+c to end the script]') try: # Main program loop while True: i = 0 print('\nReading temperature, number of sensors: {}' .format(count)) while i < count: container = devices.tempC(i) print('{}.temp: {:.3f}{}C, {:.3f}{}F of the device {}' .format(i+1, container, degree_sign, container * 9.0 / 5.0 + 32.0, degree_sign, names[i]), time.strftime('%H:%M:%S')) i = i + 1

    time.sleep(1)

# Scavenging work after the end of the program

except KeyboardInterrupt: print('Script end!')

Gari
  • 11
  • 1
  • 2
    this has lots and lots of information - at a guess, the Pi does not provide sufficient drive current for what you want to achieve – Bravo Jul 16 '21 at 00:24

0 Answers0