• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

SOLVED Raspberry Pi Python loop question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.
Joined
Dec 13, 2005
Ok, I'm starting to realize I may have gotten in over my head trying to teach myself python. But, I just picked up a Raspberry Pi and plan to screw around with it and a couple arduinos. So while I'm waiting for the arduinos, I figured I'd play around with the GPIO port on the Pi with a couple LEDs.

Now of course, being a member of this forum, my first thought is how high can I clock this thing. What's a problem with OCing? Heat. So I tossed an LED and resistor on one of the pins, wrote a basic program, said let there be light, that works.

Now the trouble. My plan is to have an LED that blinks when it warms up, stays on when it gets hotter. Fairly basic but still gets into some of the lower level stuff.

Here's what I have so far:

Code:
import time
import RPi.GPIO as GPIO

GPIO.cleanup()
GPIO.setmode(GPIO.BOARD)
GPIO.setup(7,GPIO.OUT)

while True:
    # gets temp, temp is in mC
    tempReading = 0
    f = open('/sys/class/thermal_zone0/temp', 'r')
    # cuts off newline
    tempReading = f.readline(5)
    
    # mid temps
    if tempReading <= 50000 and tempReading >= 45000:
        GPIO.output(7,GPIO.HIGH)
        time.sleep(0.5)
        GPIO.output(7,GPIO.LOW)
        time.sleep(0.5)
        print("In mid loop", tempReading)
   # high temps
   elif tempReading > 50000:
        GPIO.output(7,GPIO.HIGH)
        time.sleep(1)
        print("In high loop", tempReading)
   # low temps
   elif tempReading < 45000:
        GPIO.output(7,GPIO.LOW)
        time.sleep(1)
        print("In low loop", tempReading)
   else:
       print("Temp error", tempReading)
       time.sleep(1)

For the most part, it seems to be working. Goes in, grabs the temperature, but then just keeps dropping into the "High" if statement. The printed temps show between 42,000-43,000 (42C-43C, which are the same as grabbing it directly from the command line). if I hard code it to something like 45,000, does the same thing.

I'm using the newer model B Pi with 512MB of ram running Raspbian in case that has anything to do with it.
 
Try this: Cast tempReading to an int, to see if it throws any errors. I'm guessing that it may be tempReading may be a string, and comparing an int and a string can give bizarre results.

Here's how you'd do that cast:
Code:
    # ... snip ....
    # cuts off newline
    tempReading = f.readline(5)
    tempReading = int(tempReading)
    # ... snip ...
 
Try this: Cast tempReading to an int, to see if it throws any errors. I'm guessing that it may be tempReading may be a string, and comparing an int and a string can give bizarre results.

Here's how you'd do that cast:
Code:
    # ... snip ....
    # cuts off newline
    tempReading = f.readline(5)
    tempReading = int(tempReading)
    # ... snip ...

Huh, thought I tried something like that, guess I messed it up since it's working now. Thanks.
 
Try this: Cast tempReading to an int, to see if it throws any errors. I'm guessing that it may be tempReading may be a string, and comparing an int and a string can give bizarre results.

Here's how you'd do that cast:
Code:
    # ... snip ....
    # cuts off newline
    tempReading = f.readline(5)
    tempReading = int(tempReading)
    # ... snip ...

Yup you are exactly right, but default python treats everything as a string.

You can actually shorten this code by doing this

Code:
tempReading = int(f.readline(5))

That way you dont have to reassign the same variable.

Both are perfectly acceptable though
 
Back