Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template, request
- import datetime
- import time
- import os
- import RPi.GPIO as GPIO
- import threading
- app = Flask(__name__)
- class myThread (threading.Thread):
- def __init__(self, threadID, name):
- threading.Thread.__init__(self)
- self.threadID = threadID
- self.name = name
- def run(self):
- print "Starting " + self.name
- rasp_motor.zetThreadAan()
- print "Exiting " + self.name
- class Motor:
- buttonPinHome = 0
- ledPin = 0
- ldrPin = 0
- motorPins = []
- thread1 = None
- sequenceUp = [[1,0,0,0], [0,0,0,1], [0,0,1,0], [0,1,0,0]]
- sequenceDown = [[0,0,0,1], [1,0,0,0], [0,1,0,0], [0,0,1,0]]
- isAutoOn = False
- def __init__(self):
- #GPIO.cleanup() #clean up als er iets gecrashed was?
- GPIO.setmode(GPIO.BCM) # we gebruiken pin-namen
- # Detecteer welke raspi het is voor de pin layout
- if GPIO.RPI_REVISION == 3: # Model B+
- self.motorPins = [17, 18, 27, 22]
- self.buttonPinHome = 21
- self.ledPin = 20
- self.ldrPin = 5
- print ">> Model B+"
- else: # Model B
- self.motorPins = [24, 25, 8, 7]
- self.buttonPinHome = 23
- self.ledPin = 4
- self.ldrPin = 2
- print ">> Model B"
- # motor pins configgen
- print ">> Setting up motor pins" + str(self.motorPins)
- for pin in self.motorPins:
- GPIO.setup(pin,GPIO.OUT)
- GPIO.output(pin, False)
- # schakelaar pin configgen
- GPIO.setup(self.buttonPinHome, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- #led configgen
- GPIO.setup(self.ledPin, GPIO.OUT)
- #ldr configgen
- GPIO.setup(self.ldrPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- def isHome(self):
- return (not GPIO.input(self.buttonPinHome))
- def move(self, direction, steps=1):
- print ">> going " + direction + "steps: " + str(steps) #debug
- sequence = []
- if direction == "up":
- sequence = self.sequenceUp
- else: # down
- sequence = self.sequenceDown
- # settings
- waitTime = 0.0025 # 25ms per winding, dus 100ms per step
- stepCount = 0
- sequenceCount = steps * 4 # 1 step is een hele cyclus, dus 4 sequences
- # loop
- while sequenceCount > 0:
- if direction == "up" and self.isHome(): # eerst checken of we nog verder naar boven kunnen
- print "we zijn home"
- break
- for pin in range(0, 4):
- xpin = self.motorPins[pin] # welke pin moet aan of uit
- if sequence[stepCount][pin] == 1: # moet de pin aan zijn in deze sequence
- print ">> Step Count %i, enable pin %i" % (stepCount, xpin)
- GPIO.output(xpin, True) # Pin aanzetten
- else:
- GPIO.output(xpin, False) # Pin uitzetten
- if stepCount < 3: #reset de stepCount als er geen steps meer zijn
- stepCount += 1
- else:
- stepCount = 0
- sequenceCount -= 1
- time.sleep(waitTime) # ff slapen
- return None
- def home(self):
- while not self.isHome():
- self.move("up", steps=1)
- return None
- def reboot(self):
- os.system("sudo shutdown -r now")
- return None
- def shutdown(self):
- os.system("sudo poweroff")
- return None
- def auto_on(self):
- self.isAutoOn = True
- self.thread1 = myThread(1, "Thread-1")
- self.thread1.start()
- print "auto on"
- return render_template('auto_on.htm')
- def auto_off(self):
- self.isAutoOn = False
- print "auto_off"
- return None
- def readFile(self, filePath):
- try:
- file = open(filePath, "r")
- lines = [line.rstrip('\n') for line in file]
- file.close()
- except:
- lines = "An error occurred!"
- return lines
- def my_callback(self):
- if GPIO.input(self.ldrPin) == 1:
- self.move("down", 1)
- else:
- self.move("up", 1)
- def zetThreadAan(self):
- richting = "down"
- while self.isAutoOn:
- while GPIO.input(self.ldrPin) == 1:
- self.move("down", 1)
- else:
- self.move("up", 1)
- self.thread1.exit()
- # counter = 0
- # GPIO.output(self.ledPin, False)
- # self.home()
- # self.move("down", 100)
- # while GPIO.input(self.ldrPin) != 1:
- # time.sleep(0.001)
- # curr_state = GPIO.input(self.ldrPin)
- # while self.isAutoOn == True:
- # if GPIO.input(self.ldrPin) == 1:
- # self.move("down", 1)
- # if GPIO.input(self.ldrPin) == 0:
- # self.move("up", 1)
- # #curr_state = GPIO.input(self.ldrPin)
- # self.thread1.exit()
- # GPIO.output(self.ledPin,False)
- # checkLicht = GPIO.input(self.ldrPin)
- # print "zta start"
- # print "checkLicht: " + str(checkLicht)
- # while checkLicht != 1:
- # time.sleep(0.001)
- # checkLicht = GPIO.input(self.ldrPin)
- # self.home()
- # rasp_motor.move("down", 10)
- # print "exiting checkLicht loop"
- # while self.isAutoOn:
- # GPIO.output(self.ledPin, False)
- # self.move("down", 200)
- # self.move("up", 200)
- # GPIO.output(self.ledPin, False)
- # # verzend = GPIO.input(self.ldrPin)
- # # while verzend == 0:
- # # self.move("up", 1)
- # # GPIO.output(self.ledPin, False)
- # # while verzend == 1:
- # # self.move("down", 1)
- # # GPIO.output(self.ledPin, True)
- # # verzend = GPIO.input(self.ldrPin)
- # if not self.isAutoOn:
- # self.thread1.exit()
- # print "isautoon loop"
- # print "zta false"
- rasp_motor = Motor()
- @app.route('/')
- def index():
- now = datetime.datetime.now()
- timeString = now.strftime("%Y-%m-%d %H:%M")
- templateData = {
- 'bla' : timeString
- }
- return render_template('index.htm', **templateData)
- @app.route('/home.htm')
- def home():
- rasp_motor.home()
- return render_template('home.htm')
- @app.route('/up.htm', methods=['GET'])
- def up():
- rasp_motor.move("up", int(request.args.get("steps", "1")))
- return render_template('up.htm')
- @app.route('/down.htm', methods=['GET'])
- def down():
- rasp_motor.move("down", int(request.args.get("steps", "1")))
- return render_template('down.htm')
- @app.route('/xpos.htm')
- def xpos():
- templateData = {
- 'bla' : rasp_motor.readFile("/boot/x.txt")
- }
- return render_template('xpos.htm', **templateData)
- @app.route('/ypos.htm')
- def ypos():
- templateData = {
- 'bla' : rasp_motor.readFile("/boot/y.txt")
- }
- return render_template('ypos.htm', **templateData)
- @app.route('/shutdown.htm')
- def shutdown():
- rasp_motor.shutdown()
- templateData = {
- 'bla' : "System going down!"
- }
- return render_template('index.htm', **templateData)
- @app.route('/reboot.htm')
- def reboot():
- rasp_motor.reboot()
- templateData = {
- 'bla' : "System going for reboot"
- }
- return render_template('index.htm', **templateData)
- @app.route('/wave')
- def wave():
- rasp_motor.home()
- rasp_motor.isAutoOn = False;
- GPIO.output(rasp_motor.ledPin,False)
- rasp_motor.move("down", 100)
- counter = 0
- while rasp_motor.isAutoOn == False:
- GPIO.output(rasp_motor.ledPin, True)
- rasp_motor.move("down", 200)
- GPIO.output(rasp_motor.ledPin, False)
- time.sleep(0.5)
- rasp_motor.move("up", 200)
- counter += 1
- # if counter == 0:
- # GPIO.output(rasp_motor.ledPin,False)
- # rasp_motor.move("down", 200)
- # time.sleep(0.5)
- # GPIO.output(rasp_motor.ledPin,True)
- # rasp_motor.move("down", 200)
- # GPIO.output(rasp_motor.ledPin,False)
- # rasp_motor.move("up", 200)
- # GPIO.output(rasp_motor.ledPin,True)
- # rasp_motor.move("down", 200)
- # counter += 1
- return render_template('bscript.htm')
- @app.route('/basicscript')
- def bscript():
- return render_template('bscript.htm')
- @app.route('/auto_on.htm')
- def auto_on():
- rasp_motor.auto_on()
- return render_template('auto_on.htm')
- @app.route('/auto_off.htm')
- def auto_off():
- rasp_motor.auto_off()
- tekst = "harhar"
- templateData = {
- 'bla' : tekst
- }
- return render_template('auto_off.htm', **templateData)
- if __name__ == "__main__":
- app.run(host='0.0.0.0', port=5000, debug=True) # if permission denied; change port
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement