Author Topic: How to make GET request when a button is pressed on GPIO - Rpi?  (Read 2243 times)

0 Members and 1 Guest are viewing this topic.

Offline abdullahsebaTopic starter

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
Hi

How 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: [Select]
$(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.
This is my right hand this is my wrong hand
 

Offline SlumberMachine

  • Newbie
  • Posts: 1
  • Country: us
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #1 on: April 11, 2017, 02:59:01 am »
wget http://192.168.1.100/current_state.xml?pw=admin&Relay12=1

or any other method of sending a simple url connect.
 
The following users thanked this post: abdullahseba

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #2 on: April 11, 2017, 02:28:19 pm »
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.
 
The following users thanked this post: abdullahseba

Offline tombi

  • Regular Contributor
  • *
  • Posts: 163
  • Country: au
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #3 on: April 11, 2017, 05:31:40 pm »
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 python

from subprocess import call
import RPi.GPIO as gpio
import os
import time

# Define a function to keep script running
def loop():
    while True:
        time.sleep(5)

# Define a function to run when an interrupt is called
def 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
 
The following users thanked this post: abdullahseba

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12856
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #4 on: April 11, 2017, 05:57:51 pm »
Ditch Windows IOT and use its native Rasbian Linux on the PI.
Then its simply a matter of using the python requests library to do a request.get instead of a shutdown in tombi's code above.  n.b. first you have to install requests.
 

Offline abdullahsebaTopic starter

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #5 on: April 11, 2017, 06:02:16 pm »
Ditch Windows IOT and use its native Rasbian Linux on the PI.
Then its simply a matter of using the python requests library to do a request.get instead of a shutdown in tombi's code above.  n.b. first you have to install requests.
I prefer windows.. Linux is too messy.
This is my right hand this is my wrong hand
 

Offline tombi

  • Regular Contributor
  • *
  • Posts: 163
  • Country: au
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #6 on: April 12, 2017, 06:12:07 am »
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/pushbutton

Been 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
 
The following users thanked this post: abdullahseba

Offline abdullahsebaTopic starter

  • Frequent Contributor
  • **
  • Posts: 335
  • Country: gb
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #7 on: April 12, 2017, 06:19:49 am »
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/pushbutton

Been 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

Thanks that perfect. Any examples of making the GET request tho?
This is my right hand this is my wrong hand
 

Offline tombi

  • Regular Contributor
  • *
  • Posts: 163
  • Country: au
Re: How to make GET request when a button is pressed on GPIO - Rpi?
« Reply #8 on: April 12, 2017, 10:40:48 am »
Don't do .NET in my day job (Java mostly) but this might do it

var client = new HttpClient();
var content = await client.GetStringAsync(new Uri("http://somehost"));

Have a look here
https://docs.microsoft.com/en-us/uwp/api/Windows.Web.Http.HttpClient#Windows_Web_Http_HttpClient_GetStringAsync_Windows_Foundation_Uri_
 
The following users thanked this post: abdullahseba


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf