2

I'm working on a project for fun. I need to send data to my PHP Script which will write it to a MySQL database using MySQLi. My Raspberry PI needs to send data to the script through a url when a tracked input pin receives an on signal.

Python Code:

import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library

def button_callback1(channel):
    print("Left(07) energized.")

def button_callback2(channel):
    print("Right(13) energized.")

GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering

GPIO.setup(07, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 07 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(07,GPIO.RISING,callback=button_callback1) # Setup event on pin 07 rising edge

GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 13 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(13,GPIO.RISING,callback=button_callback2) # Setup event on pin 13 rising edge

message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up

In my callback I need to send data to "localhost/datagrab.php" in the format "localhost/datagrab.php?pin=#PIN#&status=1" whenever a pin is energized.

I plan to expand on this further in the future and have the python script visit the same link and pass the variable status as 0 if there is no on signal within 15 seconds.

Please put me in the correct direction

Thanks

Sam

1 Answer 1

1

It was a little unclear from your question if you wanted to actually OPEN the webpage and view it, or simply call it with updated information? Here are options for either.

To just call it like a script:

import subprocess

# if the script doesn't need output.
subprocess.call("php /path/to/your/script.php?pin=#" + pin + "#&status=1") 
#obviously modify '+ pin +'  to match your variable

# if you want output
proc = subprocess.Popen("php /path/to/your/script.php?pin=#" + pin + "#&status=1", shell=True, 
stdout=subprocess.PIPE)
script_response = proc.stdout.read()

The output could be useful if it throws some sort of error on insert failures, etc..

If you want to actually have your Python open the browser when it makes the call you would want to go something like this - using the "webbrowser" module:

import webbrowser

webbrowser.open("http://your/php/script.php?pin=#"+ pin +"#&status=1")  
# open the file in the browser with your variable matched to your script

There are lots of references to these methods here on stack and elsewhere, but this would be an easy place to start to build out your callback section.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.