├── README.md
├── templates
└── picam.html
└── picam.py
/README.md:
--------------------------------------------------------------------------------
1 | PiCam
2 | =====
3 |
4 | Python code and HTML template for running the Raspberry Pi based PiCam.
5 |
--------------------------------------------------------------------------------
/templates/picam.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | PiCam
5 |
11 |
12 |
19 |
20 |
21 |
22 |
47 |
48 |
49 |
--------------------------------------------------------------------------------
/picam.py:
--------------------------------------------------------------------------------
1 | from RPIO import PWM
2 | import time
3 | import atexit
4 | from flask import Flask, render_template, request
5 | app = Flask(__name__)
6 |
7 | # This function maps the angle we want to move the servo to, to the needed PWM value
8 | def angleMap(angle):
9 | return int((round((1950.0/180.0),0)*angle) +550)
10 |
11 | # Create a dictionary called pins to store the pin number, name, and angle
12 | pins = {
13 | 23 : {'name' : 'pan', 'angle' : 90},
14 | 22 : {'name' : 'tilt', 'angle' : 90}
15 | }
16 |
17 | # Create two servo objects using the RPIO PWM library
18 | servoPan = PWM.Servo()
19 | servoTilt = PWM.Servo()
20 |
21 | # Setup the two servos and turn both to 90 degrees
22 | servoPan.set_servo(23, angleMap(90))
23 | servoPan.set_servo(22, angleMap(90))
24 |
25 | # Cleanup any open objects
26 | def cleanup():
27 | servo.stop_servo(23)
28 | servo.stop_servo(22)
29 |
30 | # Load the main form template on webrequest for the root page
31 | @app.route("/")
32 | def main():
33 |
34 | # Create a template data dictionary to send any data to the template
35 | templateData = {
36 | 'title' : 'PiCam'
37 | }
38 | # Pass the template data into the template picam.html and return it to the user
39 | return render_template('picam.html', **templateData)
40 |
41 | # The function below is executed when someone requests a URL with a move direction
42 | @app.route("/")
43 | def move(direction):
44 | # Choose the direction of the request
45 | if direction == 'left':
46 | # Increment the angle by 10 degrees
47 | na = pins[23]['angle'] + 10
48 | # Verify that the new angle is not too great
49 | if int(na) <= 180:
50 | # Change the angle of the servo
51 | servoPan.set_servo(23, angleMap(na))
52 | # Store the new angle in the pins dictionary
53 | pins[23]['angle'] = na
54 | return str(na) + ' ' + str(angleMap(na))
55 | elif direction == 'right':
56 | na = pins[23]['angle'] - 10
57 | if na >= 0:
58 | servoPan.set_servo(23, angleMap(na))
59 | pins[23]['angle'] = na
60 | return str(na) + ' ' + str(angleMap(na))
61 | elif direction == 'up':
62 | na = pins[22]['angle'] + 10
63 | if na <= 180:
64 | servoTilt.set_servo(22, angleMap(na))
65 | pins[22]['angle'] = na
66 | return str(na) + ' ' + str(angleMap(na))
67 | elif direction == 'down':
68 | na = pins[22]['angle'] - 10
69 | if na >= 0:
70 | servoTilt.set_servo(22, angleMap(na))
71 | pins[22]['angle'] = na
72 | return str(na) + ' ' + str(angleMap(na))
73 |
74 | # Function to manually set a motor to a specific pluse width
75 | @app.route("//")
76 | def manual(motor,pulsewidth):
77 | if motor == "pan":
78 | servoPan.set_servo(23, int(pulsewidth))
79 | elif motor == "tilt":
80 | servoTilt.set_servo(22, int(pulsewidth))
81 | return "Moved"
82 |
83 | # Clean everything up when the app exits
84 | atexit.register(cleanup)
85 |
86 | if __name__ == "__main__":
87 | app.run(host='0.0.0.0', port=80, debug=True)
88 |
89 |
90 |
--------------------------------------------------------------------------------