Variables in a loop, Newby question

Peter Otten __peter__ at web.de
Wed Dec 25 15:52:51 CET 2013


vanommen.robert at gmail.com wrote:

> Indeed this is code what I found on the web to read temperatures from 10
> DS18B20 singlewire sensors.
> 
> My only programming (little) experience is VBA (Excel mostly).
> 
> avgtemperatures = [] is indeed from the original code where this line
> 
> 'avgtemperatures.append(sum(temperatures) / float(len(temperatures)))'
> 
> was added. i removed it.
> 
> You're right about the line sensorids. There are 10 sensors:
> 
>  sensorids = ["28-0000054c4932", "28-0000054c9454", "28-0000054c9fca",
>  "28-0000054c4401", "28-0000054dab99", "28-0000054cf9b4",
>  "28-0000054c8a03", "28-0000054d6780", $00054ccdfa", "28-0000054c4f9d"]
> 
> 
> In this script i want to read the temperatures and make them available to
> other scripts.
>  
> One script to controll my solar water boiler and other heat exchangers
> connected to this boiler. (fire place for example) And in the future I
> want to make the temperatures available on a website and log them in a
> mysql database online.
> 
> But as I said before, I am just a few days trying to learn how to do it.
> 
> Thanks for your time.
> 
> greetings Robert

(Warning: all untested code -- I don't have a Raspberry Pi)

When you use constants as sensor ids your code will only work for one 
machine, with one configuration. I recommend that you read the sensor ids 
once at startup of the script and then operate with these.

For the code poste below I assume that the output of the sensors looks like 
the examples on this page:

http://www.gtkdb.de/index_7_2035.html

Namely the list of sensors...

pi at raspberrypi ~ $ cat /sys/devices/w1_bus_master1/w1_master_slaves
 10-000801e1799b
 10-000801e17146
 10-000801e17bc6

and the state of a single sensor:

pi at raspberrypi ~ $ cat /sys/devices/w1_bus_master1/10-000801e1799b/w1_slave
 2d 00 4b 46 ff ff 02 10 19 : crc=19 YES
 2d 00 4b 46 ff ff 02 10 19 t=22625

You can then deal with the "lowlevel" stuff in a module like the 
following...

$ cat sensors.py
def read_sensorids():
    with open("/sys/devices/w1_bus_master1/w1_master_slaves") as f:
        return [line.strip() for line in f]

def read_sensor(sensorid):
    with open("/sys/bus/w1/devices/{}/w1_slave".format(sensorid)) as f:
        temperature = f.read().rpartition("=")[-1]
    return float(temperature) / 1000.0

def read_sensors(sensorids=None):
    if sensorids is None:
        sensorids = read_sensorids()

    temperatures = {}
    for sensorid in sensorids:
        temperatures[sensorid] = read_sensor(sensorid)

    return temperatures

def print_temperatures(sensorids=None):
    for k, v in read_sensors(sensorids).items():
        print("Sensor {}: {}".format(k, v))

... and use it like so:

$ cat sensors_demo.py
import sensors
import time

def demo1():
    print "Demo1: detect sensors and print temperatures"
    print "current temperatures:"
    sensors.print_temperatures()
    print

def demo2():
    print "Demo 2, detect available sensors"
    print "found the following sensors:"
    for sensor in sensors.read_sensorids():
        print sensor
    print

def demo3():
    print "Demo 3, choose a sensor and read its temperature every second"
    print "found the following sensors:"
    sensorids = sensors.read_sensorids()
    for index, sensor in enumerate(sensorids):
        print "  {}: {}".format(index, sensor)

    index = int(raw_input("Choose an index "))

    follow_sensor = sensorids[index]
    print "following", follow_sensor
    while True:
        print sensors.read_sensor(follow_sensor)
        time.sleep(1)


if __name__ == "__main__":
    demo1()
    demo2()
    demo3()

A (simulated, as you might guess from the odd variations in temperature) run 
of the above:

$ python sensors_demo.py 
Demo1: detect sensors and print temperatures
current temperatures:
Sensor 10-000801e1799b: 45.052
Sensor 10-000801e17146: 23.841
Sensor 10-000801e17bc6: 45.5

Demo 2, detect available sensors
found the following sensors:
10-000801e1799b
10-000801e17146
10-000801e17bc6

Demo 3, choose a sensor and read its temperature every second
found the following sensors:
  0: 10-000801e1799b
  1: 10-000801e17146
  2: 10-000801e17bc6
Choose an index 1
following 10-000801e17146
12.744
39.557
17.345
16.49
49.73
27.925
35.007
44.142
37.187
10.261
44.359
^CTraceback (most recent call last):
  File "sensors_demo.py", line 36, in <module>
    demo3()
  File "sensors_demo.py", line 30, in demo3
    time.sleep(1)
KeyboardInterrupt

Again, as I don't have a machine to test the above some of my assumptions 
may be false -- or worse, true nine times out of ten.




More information about the Python-list mailing list