0

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 request looks like if it were in JS ajax:

$(function() {
    $('#button').change(function() {
      var x =$(this).prop('checked')
if(x == true){
$.get( "192.168.1.100/current_state.xml?pw=admin&Relay12=1" )
$.get( "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" )
}
    })

  })

Note: http missing cuz of rep!

So if #button was from the GPIO thats what I want it to do.

0

Your example uses jQuery or some simialr JavaScript library used on the client.

You can easiliy do WebRequests in .NET

HttpWebResponse response = await webrequest.GetResponseAsync("url");

It is all based on the Standard API that can be found on the MSDN

But the MSDN is not always that easy to read and understand so StackOverflow always has the best answers on these type of code questions.

It all depends how you write the code and when the application starts then the request will be made.

0

I see you prefer Windows IOT, but since you didn't require it in your question I'll go ahead and point out that in Raspbian you can run a python script as a daemon which listens for GPIO events and simulates a keypress. The easiest way to simulate a keypress in Raspbian might be with uinput. Then you can have your javascript (or jQuery) respond to a regular keypress event with an onkeypress handler. There probably is a similar technique in Windows IOT, but someone else will have to chime in for that. Since this is a technique I've used before with some success, I'll provide an example.

import uinput
from gpiozero import Button
# Setup your input device
button = Button(2)
# Setup your simulated keyboard.
# You have to list the keys you will use.
device = uinput.Device([uinput.KEY_W,uinput.KEY_N])
# Loop forever
while True:
  # Test the state of the input: Closed (is_pressed) or open.
  if button.is_pressed:
    device.emit_click(uinput.KEY_W)
  else:
    device.emit_click(uinput.KEY_N)

It's probably better to have your code wait for the state to change with an event handler, but this is just a start for demonstration purposes. Your HTML might include a body tag like this:

<body onkeypress="handleKeypress(event);">

The (event) part is important because that is what passes the event information to your handleKeypress function, which you must define elsewhere with your javascript similar to this:

function handleKeypress(event) {
  var key = event.keyCode;
  switch (key) {
    case 87:
      window.location.href="http://192.168.1.100/current_state.xml?pw=admin&Relay12=1"
      break;
    case 78:
      window.location.href="http://192.168.1.100/current_state.xml?pw=admin&Relay12=0"
      break;
    default:
      document.prompt("Unicode KEY code: " + key);
  }
}

You'll notice my examples are absent of any jQuery, but there are equivalents in jQuery if you prefer.

0

I'm not sure why you send the request once with params, and again without, but here's a simple example in Python with what you described:

from gpiozero import Button
import requests
from signal import pause

btn = Button(2)

url = 'http://192.168.1.100/current_state.xml'

def relay_on():
    params = {
        'pw': 'admin',
        'Relay12': 1,
    }
    requests.get(url, params=params)
    requests.get(url)

def relay_off():
    params = {
        'pw': 'admin',
        'Relay12': 0,
    }
    requests.get(url, params=params)
    requests.get(url)

btn.when_pressed = relay_on
btn.when_released = relay_off

pause()
  • Old question but thanks! I think I need the second request without parameters to reinitialize the thing or something. Can't remember now. – Abdullah Seba May 1 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.