I would like to call a python function when an image (button) is clicked which would allow me to control the servo from the web interface.
Here is part of my jQuery code:
$('#left_button').click(function(){
$.post("cameraservo2.py", {direction:"left"}).done(function (reply) {
$('#camerapos').empty().append(reply);
alert("left button clicked");});
});
And this is my Python code:
#!/usr/bin/python
def index (self, **data):
import pigpio
import time
servos=4
key = data['direction']
m=1500
while (m >= 500 and m <= 2500):
if (key =="left"):
m=m+100
elif (key =="right"):
m=m-100
pigpio.start()
pigpio.set_servo_pulsewidth(servos, m)
servostatus= "Servo {} {} micro pulses".format(servos[0], key, m)
print servostatus
time.sleep(1)
pigpio.stop()
return servostatus
My problem is when the button is clicked, it will alert "left button is clicked" which means it has run through the python file. But instead of showing the "servostatus", i get the whole python code displayed in my #camerapos
div.
Please let me know if i need to post more information. Thanks!!