October 01, 2017, 04:20 PM
r0gueany python programmers around here?
I have a script I'm trying to mod. Instead of controlling the Raspberry Pi GPIO pins, I want it to send an HTTP request. I guess a "get"?
The old code: <i>(specifically after the IF part where it starts muckin with the GPIO).</i>
class device_handler(debounce_handler):
"""Publishes the on/off state requested,
and the IP address of the Echo making the request.
"""
#TRIGGERS = {str(sys.argv[1]): int(sys.argv[2])}
#TRIGGERS = {"office": 52000}
TRIGGERS = {"dogdog": 52000,"catcat":51000}
def act(self, client_address, state, name):
print("State", state, "from client @", client_address)
# GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
# GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
# GPIO.output(int(7), state) ## State is true/false
if name=="dogdog":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(7), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(7), state) ## State is true/false
elif name =="catcat":
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(int(11), GPIO.OUT) ## Setup GPIO Pin to OUTPUT
GPIO.output(int(11), state) ## State is true/false
What I want is to replace that with code that will send an HTTP command out the ethernet port:
this -->
http://192.168.1.4/30000/03Then I'll want a little delay (1/2 second), then I'll send the off command. Then my evil genius thingy will do cool stuff and I'll post about it here!

October 01, 2017, 10:54 PM
SirBeepI only do a slight lil bit of python, so I don't know off the top of my head which version you're likely to be using, but a quick one liner in python 3:
import urllib.request
urllib.request.urlopen("http://192.168.1.4/30000/03").read()
slightly differnt in 2. or you may have higher level requests library available:
import requests
r = requests.get("http://192.168.1.4/30000/03")
October 01, 2017, 11:14 PM
Hawkinsquote:
import requests
r = requests.get("http://192.168.1.4/30000/03")
SirBeep's basically got it.
http requests are dead easy in python with requests or urllib
If you can understand what a GET or POST request *is*, then you can do it easily, no mucking around with network code.
October 02, 2017, 07:02 PM
r0gueThanks guys! I'll give it a shot.
October 04, 2017, 08:33 PM
r0gueGot it working, and got the delay to work. But the relay board calls for 12v and I only have a 5v supply, so while the LED does it's thing to indicate it's working, the relay won't throw. All my donor supplies are from USB, so 5v. I'll have to scrounge up a 12v source before I can show off my franken light switch.