Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'startup': 0.05; 'detect': 0.07; 'none:': 0.07; 'odd': 0.07; '"__main__":': 0.09; '__name__': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'skip:o 50': 0.09; 'subject:question': 0.10; 'python': 0.11; 'def': 0.12; 'assume': 0.14; '"current': 0.16; '36,': 0.16; 'added.': 0.16; 'assumptions': 0.16; 'example)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'scripts.': 0.16; 'temperatures': 0.16; 'true:': 0.16; 'variations': 0.16; 'index': 0.16; 'wrote:': 0.18; 'module': 0.19; 'trying': 0.19; 'examples': 0.20; 'machine': 0.22; 'programming': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'connected': 0.24; 'looks': 0.24; 'script': 0.25; 'second': 0.26; 'header:X -Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'robert': 0.30; 'code': 0.31; 'index,': 0.31; "skip:' 40": 0.31; 'file': 0.32; 'stuff': 0.32; 'run': 0.32; '(most': 0.33; 'guess': 0.33; 'skip:s 30': 0.35; 'operate': 0.35; 'test': 0.35; 'but': 0.35; 'there': 0.35; 'false': 0.36; 'thanks': 0.36; 'list': 0.37; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'days': 0.60; 'read': 0.60; 'future': 0.60; "you're": 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'choose': 0.64; '30,': 0.65; 'yes': 0.68; 'skip:r 30': 0.69; 'greetings': 0.72; 'skip:$ 10': 0.81; 'solar': 0.84; 'temperature': 0.84; 'vba': 0.84; 'heat': 0.91; 'sensors': 0.91; 'these.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Variables in a loop, Newby question Date: Wed, 25 Dec 2013 15:52:51 +0100 Organization: None References: <9ad01eef-baf0-4018-833e-0b4dce4b9b85@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848acf.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 169 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1387983158 news.xs4all.nl 2973 [2001:888:2000:d::a6]:50544 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:62707 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 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.