Variables in a loop, Newby question
Michael Torrie
torriem at gmail.com
Thu Dec 26 01:34:13 EST 2013
On 12/24/2013 11:27 AM, 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).
>
Definitely you'll want to learn python before you go much farther in
this project. Check out the online docs, tutorials, etc.
> In this script i want to read the temperatures and make them
> available to other scripts.
Here's some almost runnable code that might help you get down the right
track. You'll have to fill in some of the stuff.
temperatures.py:
import time
sensorids = [...] #fill in these with your ids
def _read_sensor(id):
# open /sys/bus/w1/deevices/id/slave
text = ''
while text.split("\n")[0].find("YES") == -1:
tfile = open("/sys/bus/w1/devices/"+ id +"/w1_slave")
text = tfile.read()
tfile.close()
time.sleep(0.1)
temperaturedata = text.split("\n")[1].split(" ")[9]it(" ")[9]
temperature = float(temperaturedata [2:]) / 1000
return temperature
def get_temperatures():
# not using list comprehensions but we could
temps = []
for sensor in sensorids:
temps.append(_read_sensor(sensor))
Then in your other python modules (scripts):
import temperatures
print temperatures.get_temperatures() # should print a list
Hope this gives you a bit of structure you can use. All the code you
need to fill in can come from the code you posted previously.
More information about the Python-list
mailing list