Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62707

Re: Variables in a loop, Newby question

From Peter Otten <__peter__@web.de>
Subject Re: Variables in a loop, Newby question
Date 2013-12-25 15:52 +0100
Organization None
References <9ad01eef-baf0-4018-833e-0b4dce4b9b85@googlegroups.com> <c13e6d17-1703-41f7-81ed-65d9d8b4c259@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4612.1387983158.18130.python-list@python.org> (permalink)

Show all headers | View raw


vanommen.robert@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@raspberrypi ~ $ cat /sys/devices/w1_bus_master1/w1_master_slaves
 10-000801e1799b
 10-000801e17146
 10-000801e17bc6

and the state of a single sensor:

pi@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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 08:07 -0800
  Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 11:20 -0500
  Re: Variables in a loop, Newby question "Tobias M." <tm@tobix.eu> - 2013-12-24 17:24 +0100
  Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-24 17:29 +0100
  Re: Variables in a loop, Newby question bob gailer <bgailer@gmail.com> - 2013-12-24 12:26 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 09:54 -0800
    Re: Variables in a loop, Newby question Joel Goldstick <joel.goldstick@gmail.com> - 2013-12-24 13:10 -0500
    Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-24 13:42 -0500
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-25 13:35 -0500
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-24 10:27 -0800
    Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 02:54 +0000
      Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-25 16:42 +1100
        Re: Variables in a loop, Newby question Denis McMahon <denismfmcmahon@gmail.com> - 2013-12-25 15:27 +0000
          Re: Variables in a loop, Newby question Cameron Simpson <cs@zip.com.au> - 2013-12-26 12:01 +1100
          Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 12:35 +1100
            Re: Variables in a loop, Newby question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-12-26 16:41 +1100
              Re: Variables in a loop, Newby question Dave Angel <davea@davea.name> - 2013-12-26 03:14 -0500
              Re: Variables in a loop, Newby question Chris Angelico <rosuav@gmail.com> - 2013-12-26 19:24 +1100
    Re: Variables in a loop, Newby question Peter Otten <__peter__@web.de> - 2013-12-25 15:52 +0100
    Re: Variables in a loop, Newby question Michael Torrie <torriem@gmail.com> - 2013-12-25 23:34 -0700
  Re: Variables in a loop, Newby question Larry Hudson <orgnut@yahoo.com> - 2013-12-25 00:13 -0800
  Re: Variables in a loop, Newby question vanommen.robert@gmail.com - 2013-12-27 00:53 -0800
    Re: Variables in a loop, Newby question Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-27 11:39 -0500

csiph-web