simong93
Posts: 2
Joined: Fri May 03, 2019 7:17 pm

Push Button Loads Webpage

Fri May 03, 2019 7:28 pm

Hey All, I have been playing around and researching for the last 3 days with no luck, ive got the raspberry PI 3 Model B 1.2V and what i wanted to do is when i press one of 4 buttons it would load a website so say button 1 google button 2 yahoo etc etc etc.

I thought i would start small and just make it so when i press a button it prints hello world or something but none of the tutorials work or they are only for leds :( this is my current setup but any help would be amazing :)
Image
Image
and my code is

Code: Select all

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

GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)
 
while True:
        input = GPIO.input(18)
        if(input == False): 
                print "IT works" 
                time.sleep(0.2) 

but even my code dosent work.

User avatar
MrYsLab
Posts: 334
Joined: Mon Dec 15, 2014 7:14 pm
Location: Noo Joysey, USA

Re: Push Button Loads Webpage

Fri May 03, 2019 10:17 pm

Depending upon how your switch is wired, setting the pull-up resistor may not be correct. Try removing it and see if that works:

Code: Select all

# GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(18,GPIO.IN)
You may also have to reverse the logic when testing the input value.

Interacting with the browser may not be that simple unless you are very familiar with HTML and JavaScript. If you are more familiar with Python, you might want to use tkinter or Guizero., or if you really want to play with web pages, you might like to look at remi. https://github.com/dddomodossola/remi

Be aware that GUIs normally have their event loop, so having a while True loop in your code will hang your program. There are solutions to this dilemma, so after you get your button code working, that next challenge can be tackled.

simong93
Posts: 2
Joined: Fri May 03, 2019 7:17 pm

Re: Push Button Loads Webpage

Sat May 04, 2019 9:10 am

MrYsLab wrote:
Fri May 03, 2019 10:17 pm
Depending upon how your switch is wired, setting the pull-up resistor may not be correct. Try removing it and see if that works:

Code: Select all

# GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(18,GPIO.IN)
You may also have to reverse the logic when testing the input value.

Interacting with the browser may not be that simple unless you are very familiar with HTML and JavaScript. If you are more familiar with Python, you might want to use tkinter or Guizero., or if you really want to play with web pages, you might like to look at remi. https://github.com/dddomodossola/remi

Be aware that GUIs normally have their event loop, so having a while True loop in your code will hang your program. There are solutions to this dilemma, so after you get your button code working, that next challenge can be tackled.
Good Morning, i managed to get every button to work last night :D and when you press a button it says Button 1-4 has been pushed

Code: Select all

import RPi.GPIO as GPIO

# Use GPIO numbers not pin numbers
GPIO.setmode(GPIO.BCM)

def button_callback(channel):
    print("Button was pushed 1! ")
def button_callback2(channel):
    print("Button was pushed 2! ")
def button_callback3(channel):
    print("Button was pushed 3! ")
def button_callback4(channel):
    print("Button was pushed 4! ")
    
# Ignore warning for now
GPIO.setwarnings(False)
# Set pin 17 to be an input pin and set initial value to be pulled low (off)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Add callback with options
GPIO.add_event_detect(18,GPIO.RISING,callback=button_callback, bouncetime=300)

GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Add callback with options
GPIO.add_event_detect(23,GPIO.RISING,callback=button_callback2, bouncetime=300)

GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Add callback with options
GPIO.add_event_detect(24,GPIO.RISING,callback=button_callback3, bouncetime=300)

GPIO.setup(25, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Add callback with options
GPIO.add_event_detect(25,GPIO.RISING,callback=button_callback4, bouncetime=300)

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

woot woot now i need to make it so when i press the button instead of printing the message it goes to a webpage, i am familiar with HTML,PHP,CSS etc etc etc

klricks
Posts: 6442
Joined: Sat Jan 12, 2013 3:01 am
Location: Grants Pass, OR, USA
Contact: Website

Re: Push Button Loads Webpage

Sat May 04, 2019 4:18 pm

simong93 wrote:
Sat May 04, 2019 9:10 am
....
woot woot now i need to make it so when i press the button instead of printing the message it goes to a webpage, i am familiar with HTML,PHP,CSS etc etc etc
Many ways to do what you want.
You can install xdotool. xdotool will allow you to issue keyboard and/or mouse control commands in a script.

Code: Select all

sudo apt update
sudo apt install xdotool
You could open all the web sites at once (in TABs)

Code: Select all

chromium-browser www1 www2 www3 www4 etc.
Now in the switch code send (with xdotool) [CTRL] 1 to show the leftmost tab when button 1 is pressed. [CTRL] 2 to show 2nd tab from left when button 2 is pressed etc.

-OR-
Send [Alt] [F4] to completely close the current chromium and then start a new browser
For example when button 2 is pressed send [Alt] [F4] then:

Code: Select all

chromium-browser www2
(note depending on what else is running on your desktop you may need to start an instance of chromium before a button is pressed as [Alt] [F4] will close whatever is on the desktop. If no other programs are running then you are OK).


-OR-
Simplify things and use a single button to cycle through all the tabs by sending [CTRL] [TAB].
Unless specified otherwise my response is based on the latest and fully updated Raspbian Buster w/ Desktop OS.

Return to “Python”