2

I'm using Occidentalis v0.2 and calling modprobe with a python script to read out temperatures from one DS18B20!

I'm new to python as well so bear with me, this is a small part of my code:

with open(path, "r") as f:
  contentArray = []
  for line in f:
    contentArray.append (line)
f.close

s = contentArray[0]
if s.find('YES'):
  return contentArray[0]
else:
  return 88

Example: contentArray[0] can give results as:

68 01 4b 46 7f ff 0c 10 05 : crc=3e NO

or:

68 01 4b 46 7f ff 08 10 05 : crc=05 YES

If the above code is wrong, how do I do to find the YES? Because this states correct CRC. If I have a yes, I actually want to return contentArray[1] (which contains the correct temperature value).

daedalus
  • 10,873
  • 5
  • 50
  • 71
Christian
  • 1,548
  • 2
  • 15
  • 26
  • Such question is not really RaspberryPI related so I think you should use other SE page for it - like stackoverflow. – Krzysztof Adamski Sep 11 '12 at 14:04
  • It's great you've found an answer, but as @KrzysztofAdamski says, the question is probably more appropriate on Stack Overflow. Let's close this. – Alex Chamberlain Sep 11 '12 at 14:08
  • You are both right, but in a sense, it's related to Occidentalis, that is a distro for rasp. Even if it's mostly just python question. :) – Christian Sep 12 '12 at 18:04
  • Hi guys. Although the post includes Raspberry Pi details, the actual question is about the semantics of Python. I think this would get better reception over at StackOverflow, so I'm going to move it over there. – Jivings Sep 13 '12 at 06:04
  • If you use `with` you don't need to `close`. – tripleee Sep 13 '12 at 07:39
  • In the case if no match, the idiomatic / pythonic thing to return would be `None`, I believe (or raise an exception which the caller can catch). – tripleee Sep 13 '12 at 07:44

2 Answers2

1

Solved, I had to make this change in the code:

s = contentArray[0]
if s.find('YES') != -1:
  return contentArray[0]
else:
  return 88
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Christian
  • 1,548
  • 2
  • 15
  • 26
0

Thank you, I will try out the .find command next time. I just scripted a little one-liner for the bash to save temperature readings. Of course the is much space for improvement.

while true; do echo -n "$(date '+%D %T'); " >> output.csv; cat /sys/devices/w1_bus_master1/*/w1_slave | grep -A 1 YES | grep -m 1 t= | cut -c30- >> output.csv; sleep 1; done

As you can see, the keywords "YES" and "t=" are found by the grep command.

McPeppr
  • 687
  • 8
  • 10