-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtea.py
More file actions
61 lines (53 loc) · 2.05 KB
/
tea.py
File metadata and controls
61 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#Tea Preparer server
#Copyright (C) 2016 ResonantWave
#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>.
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import RPi.GPIO as GPIO
import time
import cgi
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
def stir():
global pwm
for i in range(180): # Move in one direction
pwm.ChangeDutyCycle(float(i)/10.0 + 2.5)
time.sleep(0.01)
for i in range(180, 0, -1): # Aaaand in the other
pwm.ChangeDutyCycle(float(i)/10.0 + 2.5)
time.sleep(0.01)
class MainHandler(BaseHTTPRequestHandler):
def do_GET(self):
p = self.path.split("?")
path = p[0][1:].split("/")
params = {}
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
if len(p) > 1:
params = cgi.parse_qs(p[1], True, True)
if 'time' in params: # check for 'time' in the GET request
moveTime = str(params['time']).replace('\'', '').replace('[', '').replace(']', '') # remove the ['']
if moveTime.isdigit():
endTime = time.time() + float(moveTime)
global pwm
pwm = GPIO.PWM(18, 100)
pwm.start(5)
pwm.ChangeDutyCycle(float(0)/10.0 + 2.5) # move to initial position
while time.time() < endTime:
stir()
else:
self.wfile.write('0')
try:
server = HTTPServer(('', 8000), MainHandler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()