SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    any python programmers around here?
Go
New
Find
Notify
Tools
Reply
  
any python programmers around here? Login/Join 
Seeker of Clarity
Picture of r0gue
posted
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/03

Then 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! Smile




 
Posts: 11379 | Registered: August 02, 2004Reply With QuoteReport This Post
Member
posted Hide Post
I 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")
 
Posts: 231 | Location: Reidville, SC | Registered: October 24, 2009Reply With QuoteReport This Post
Member
Picture of Hawkins
posted Hide Post
quote:
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.


--------------
July NoVA Sigshoot: Shooter's Paradise; 0900 (9AM) 23July05

My Signature is almost a decade out of date!
 
Posts: 4686 | Location: VA | Registered: April 17, 2003Reply With QuoteReport This Post
Seeker of Clarity
Picture of r0gue
posted Hide Post
Thanks guys! I'll give it a shot.




 
Posts: 11379 | Registered: August 02, 2004Reply With QuoteReport This Post
Seeker of Clarity
Picture of r0gue
posted Hide Post
Got 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.




 
Posts: 11379 | Registered: August 02, 2004Reply With QuoteReport This Post
  Powered by Social Strata  
 

SIGforum.com    Main Page  Hop To Forum Categories  The Lounge    any python programmers around here?

© SIGforum 2024