Loop getting stuck on python on Raspberry PI when using Dweet.io

109 views Asked by At

I'm very new to python so bear with me. I'm trying to change the state of a GPIO pin to control a relay based on the state of a dweet.io variable. The variable is command, it is either on, off, or done. The program checks the dweet until it isn't equal to done, meaning it's either on or off, and changes the state of the relay accordingly. Then, it changes the dweet back so that command equals done, and does it all over again. However, it only works once. After that, it just continuously changes the dweet back to done.

import RPi.GPIO as GPIO
import time
import dweepy
import os

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

dweepy.dweet_for('my_thing', {'command': 'done'})
url = dweepy.get_latest_dweet_for("my_thing")
dict = url[0]
command = dict["content"][str("command")]


while True:
        1 == 1

        while command == "done":
                time.sleep(2)
                url = dweepy.get_latest_dweet_for("my_thing")
                dict = url[0]
                command = dict["content"][str("command")]


        else:
                if command == "on":
                        GPIO.output(7,GPIO.LOW)
                        time.sleep(2)
                        dweepy.dweet_for('my_thing', {'command': 'done'})

                else:
                        GPIO.output(7,GPIO.HIGH)
                        time.sleep(2)
                        dweepy.dweet_for('my_thing', {'command': 'done'})

I thought that since it works once, I should be able to simply start the program again after it changes the dweet back to done and get rid of the infinite loop. This works, however I can't run it when the Pi boots.

import RPi.GPIO as GPIO
import time
import dweepy
import os

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

dweepy.dweet_for('my_thing', {'command': 'done'})
url = dweepy.get_latest_dweet_for("my_thing")
dict = url[0]
command = dict["content"][str("command")]




while command == "done":
        time.sleep(2)
        url = dweepy.get_latest_dweet_for("my_thing")
        dict = url[0]
        command = dict["content"][str("command")]


else:
        if command == "on":
                GPIO.output(7,GPIO.LOW)
                time.sleep(2)
                dweepy.dweet_for('my_thing', {'command': 'done'})
                execfile('dweetrelay.py')

        else:
                GPIO.output(7,GPIO.HIGH)
                time.sleep(2)
                dweepy.dweet_for('my_thing', {'command': 'done'})
                execfile('dweetrelay.py')

So I either need to figure out why the loop gets stuck or figure out why the second version doesn't run in the background.

Thanks in advance and I'm grateful for any clues.

1

There are 1 answers

1
Matt Peddicord On

If you set your command to done, from then on it will always be done. Hence the loop.