How to make GET request when a button is pressed on GPIO - Rpi? - Page 1
Forum section: electronics / beginners

author: abdullahseba
author_rank: Frequent Contributor
timestamp: April 10, 2017, 09:01:09 pm
content: HiHow can I make a get request when a button connected to the GPIO on Rpi is turned on and off?Preferably on Windows IOT,I have 2 URLs that trigger an IP relay on and off depending on the url.There is no response so it must be simple yet I'm not sure where to start.Here is what my requst looks like if it were in ajax:Code:$(function() {$('#button').change(function() {var x =$(this).prop('checked')if(x == true){$.get( "http://192.168.1.100/current_state.xml?pw=admin&Relay12=1" )$.get( "http://192.168.1.100/current_state.xml" )}else{$.get( "http://192.168.1.100/current_state.xml?pw=admin&Relay12=0" )$.get( "http://192.168.1.100/current_state.xml" )}})})So if `#button` was from the GPIO thats what I want it to do.Thanks in advance.

author: SlumberMachine
author_rank: Newbie
timestamp: April 11, 2017, 02:59:01 am
content: wgethttp://192.168.1.100/current_state.xml?pw=admin&Relay12=1or any other method of sending a simple url connect.

author: rstofer
author_rank: Super Contributor
timestamp: April 11, 2017, 02:28:19 pm
content: My Pi's came with a directory full of Python code to interface with the hardware. Somewhere in that pile of code, there must be something that will help.

author: tombi
author_rank: Regular Contributor
timestamp: April 11, 2017, 05:31:40 pm
content: Here is a python script I used to shutdown my raspberry Pi when a GPIO pin changes state. You can easily adapt it I think.#!/usr/bin/env pythonfrom subprocess import callimport RPi.GPIO as gpioimport osimport time# Define a function to keep script runningdef loop():while True:time.sleep(5)# Define a function to run when an interrupt is calleddef shutdown(pin):os.system("sudo shutdown -h now")gpio.setmode(gpio.BCM)gpio.setup(4, gpio.IN)gpio.add_event_detect(4, gpio.FALLING, callback=shutdown, bouncetime=200)loop() # Run the loop function to keep script running

author: Ian.M
author_rank: Super Contributor
timestamp: April 11, 2017, 05:57:51 pm
content: Ditch Windows IOT and use its native Rasbian Linux on the PI.Then its simply a matter of using the pythonrequestslibrary to do a request.get instead of a shutdown in tombi's code above. n.b. first you have to install requests.

author: abdullahseba
author_rank: Frequent Contributor
timestamp: April 11, 2017, 06:02:16 pm
content: Quote from: Ian.M on April 11, 2017, 05:57:51 pmI prefer windows.. Linux is too messy.

author: tombi
author_rank: Regular Contributor
timestamp: April 12, 2017, 06:12:07 am
content: Sorry - missed that it was Windows IOT.Googling for Windows IOT button interrupt yielded this page which I think will help. It includes a sample project for reacting to a button press.https://developer.microsoft.com/en-us/windows/iot/samples/pushbuttonBeen meaning to try Windows IOT as it would make creating nice LCD GUIs easier. For my project it was no good as Windows didn't support the serial device I was using so I gave up and used Kivy for the GUI instead.If you use Pycharm and Python you get a pretty nice IDE to work in. If you buy a license you can remote deploy and debug too.So maybe Linux is more messy but then it is also less hidden and much more widely supported (more people writing free code for the platform). Swings and roundabouts.Tom

author: abdullahseba
author_rank: Frequent Contributor
timestamp: April 12, 2017, 06:19:49 am
content: Quote from: tombi on April 12, 2017, 06:12:07 amThanks that perfect. Any examples of making the GET request tho?

author: tombi
author_rank: Regular Contributor
timestamp: April 12, 2017, 10:40:48 am
content: Don't do .NET in my day job (Java mostly) but this might do itvar client = new HttpClient();var content = await client.GetStringAsync(new Uri("http://somehost"));Have a look herehttps://docs.microsoft.com/en-us/uwp/api/Windows.Web.Http.HttpClient#Windows_Web_Http_HttpClient_GetStringAsync_Windows_Foundation_Uri_