user2669997
user2669997

Reputation:

Reading python request

I am new to python and I am trying to run the following code using requests

import requests
import wiringpi2
import time

wiringpi2.wiringPiSetupGpio()
wiringpi2.pinMode(17,1)
wiringpi2.digitalWrite(17,1)

while 1:
    relaystatus = requests.get('http://stevesolarhome.com/WaterControl.txt')
    if relaystatus == "1":
        wiringpi2.digitalWrite(17,1)
    elif relaystatus == "0":
        wiringpi2.digitalWrite(17,0)
    time.sleep (2)

the GPIO pins do not react to the file being changed. The file only contains the number 1 or 0 at any time. I know the URL works and the request returns the correct number from the text file. I also know the GPIO pins work but this script does not work. I assume the file being read is not in the correct format to be used in the 'if' line

Upvotes: 0

Views: 59

Answers (1)

Thtu
Thtu

Reputation: 2032

requests.get(url) will return a request object. To get the underlying content, call the text attribute.

while 1:
    request = requests.get('http://stevesolarhome.com/WaterControl.txt')
    if request.text == "1":
          ... do stuff ...

Upvotes: 1

Related Questions