I am starting a project on my raspberry pi in which I am trying to control a servo hat (pimoroni pan tilt) from a web server.
How can I host a Lan website from the pi, then control parts of a python script from buttons on that website?
There are many approaches. Guess the simplest is to use a HTTP form that send to a cgi (in your case directly a python script) You need to
there are many options... keep it simple in the beginning.
I know it is not a correct way to answer. You can consider it as just a comment.(since I can't make comment without 50 reputation I adding this as answer). For this work I will suggest you Flask server, or bottle server. Controlling Python program using Apache server is little bit difficult. But the Bottle and Flask server uses Python web framework itself. I believes this article will help you alot. If you want to use apache2 and php google about shell_exec or exec function of php. I believes this question is already asked on Stack exchange. Now I will show you an example to turning on and off light using(controlling gpio pins) using bottle server and know the status of switch. The switch is connected to gpio 17 and led is connected to gpio 18. We also will create a button using html to control LED.
from bottle import route, run import RPi.GPIO as GPIO
host = 'IP address of RasPi'
pinButton = 17
pinLed = 18
GPIO.setmode(GPIO.BCM) # pin 17 as input with pull-up resistor
GPIO.setup(pinButton, GPIO.IN, pull_up_down=GPIO.PUD_UP) # pin 18 as output
GPIO.setup(pinLed, GPIO.OUT) LedState = False
def read_button():
if GPIO.input(pinButton):
return 'Released'
else:
return 'Pressed'
def update_led():
GPIO.output(pinLed, LedState) def toggleLed():
global LedState
LedState = not LedState
@route('/')
@route('/<arg>')
def index(arg=""):
if arg == "toggle":
toggleLed()
update_led() response = "<html>\n"
response += "<body>\n"
response += "<script>\n"
response += "function changed()\n"
response += "{window.location.href='/toggle'}\n"
response += "</script>\n"
response += "Button: " + read_button() +"\n"
response += "<br/><br/>\n"
response += "<input type='button' onClick='changed()' value=' LED '/>\n"
response += "</body></html>\n"
return response
run(host=host, port=80)
You could use something like remot3.it (new weaved "face") to be able to connect to your RPi outside your local network. You can create commands associated with a job, or connect via ssh and run your script.