Advertisement
blastermak

Python/Flask script for IPOMEDT

Jan 31st, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.38 KB | None | 0 0
  1. from flask import Flask, render_template, request
  2. import datetime
  3. import time
  4. import os
  5. import RPi.GPIO as GPIO
  6. import threading
  7.  
  8. app = Flask(__name__)  
  9.  
  10. class myThread (threading.Thread):
  11.     def __init__(self, threadID, name):
  12.         threading.Thread.__init__(self)
  13.         self.threadID = threadID
  14.         self.name = name
  15.     def run(self):
  16.         print "Starting " + self.name
  17.         rasp_motor.zetThreadAan()
  18.         print "Exiting " + self.name
  19.  
  20.  
  21.  
  22. class Motor:
  23.     buttonPinHome = 0
  24.     ledPin = 0
  25.     ldrPin = 0
  26.     motorPins = []
  27.     thread1 = None
  28.     sequenceUp = [[1,0,0,0], [0,0,0,1], [0,0,1,0], [0,1,0,0]]
  29.     sequenceDown = [[0,0,0,1], [1,0,0,0], [0,1,0,0], [0,0,1,0]]
  30.     isAutoOn = False
  31.      
  32.  
  33.     def __init__(self):
  34.         #GPIO.cleanup()  #clean up als er iets gecrashed was?
  35.         GPIO.setmode(GPIO.BCM) # we gebruiken pin-namen
  36.          
  37.         # Detecteer welke raspi het is voor de pin layout
  38.         if GPIO.RPI_REVISION == 3: # Model B+
  39.             self.motorPins = [17, 18, 27, 22]
  40.             self.buttonPinHome = 21
  41.             self.ledPin = 20
  42.             self.ldrPin = 5
  43.             print ">> Model B+"
  44.         else: # Model B
  45.             self.motorPins = [24, 25, 8, 7]
  46.             self.buttonPinHome = 23
  47.             self.ledPin = 4
  48.             self.ldrPin = 2
  49.             print ">> Model B"
  50.  
  51.          
  52.         # motor pins configgen  
  53.         print ">> Setting up motor pins" + str(self.motorPins)
  54.         for pin in self.motorPins:
  55.             GPIO.setup(pin,GPIO.OUT)
  56.             GPIO.output(pin, False)
  57.              
  58.         # schakelaar pin configgen
  59.         GPIO.setup(self.buttonPinHome, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  60.  
  61.         #led configgen
  62.         GPIO.setup(self.ledPin, GPIO.OUT)
  63.          
  64.         #ldr configgen
  65.         GPIO.setup(self.ldrPin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  66.          
  67.          
  68.     def isHome(self):
  69.         return (not GPIO.input(self.buttonPinHome))
  70.          
  71.          
  72.     def move(self, direction, steps=1):
  73.         print ">> going " + direction + "steps: " + str(steps) #debug
  74.          
  75.         sequence = []
  76.         if direction == "up":
  77.             sequence = self.sequenceUp
  78.         else: # down
  79.             sequence = self.sequenceDown
  80.          
  81.         # settings
  82.         waitTime = 0.0025  # 25ms per winding, dus 100ms per step
  83.         stepCount = 0
  84.         sequenceCount = steps * 4 # 1 step is een hele cyclus, dus 4 sequences
  85.          
  86.         # loop
  87.         while sequenceCount > 0:
  88.             if direction == "up" and self.isHome(): # eerst checken of we nog verder naar boven kunnen
  89.                 print "we zijn home"
  90.                 break
  91.                  
  92.             for pin in range(0, 4):
  93.                 xpin = self.motorPins[pin] # welke pin moet aan of uit
  94.                 if sequence[stepCount][pin] == 1: # moet de pin aan zijn in deze sequence
  95.                     print ">> Step Count %i, enable pin %i" % (stepCount, xpin)
  96.                     GPIO.output(xpin, True) # Pin aanzetten
  97.                 else:
  98.                     GPIO.output(xpin, False) # Pin uitzetten
  99.              
  100.             if stepCount < 3: #reset de stepCount als er geen steps meer zijn
  101.                 stepCount += 1
  102.             else:
  103.                 stepCount = 0
  104.             sequenceCount -= 1
  105.              
  106.             time.sleep(waitTime) # ff slapen
  107.         return None
  108.      
  109.     def home(self):
  110.         while not self.isHome():
  111.             self.move("up", steps=1)
  112.         return None
  113.  
  114.     def reboot(self):
  115.         os.system("sudo shutdown -r now")
  116.         return None
  117.  
  118.     def shutdown(self):
  119.         os.system("sudo poweroff")
  120.         return None
  121.          
  122.     def auto_on(self):
  123.         self.isAutoOn = True
  124.         self.thread1 = myThread(1, "Thread-1")
  125.         self.thread1.start()
  126.         print "auto on"
  127.         return render_template('auto_on.htm')
  128.          
  129.     def auto_off(self):
  130.         self.isAutoOn = False
  131.         print "auto_off"
  132.          
  133.         return None
  134.          
  135.     def readFile(self, filePath):
  136.         try:
  137.             file = open(filePath, "r")
  138.             lines = [line.rstrip('\n') for line in file]
  139.             file.close()
  140.         except:
  141.             lines = "An error occurred!"
  142.          
  143.         return lines
  144.          
  145.     def my_callback(self):
  146.         if GPIO.input(self.ldrPin) == 1:
  147.             self.move("down", 1)
  148.         else:
  149.             self.move("up", 1)
  150.      
  151.     def zetThreadAan(self):
  152.         richting = "down"
  153.         while self.isAutoOn:
  154.          
  155.              
  156.             while GPIO.input(self.ldrPin) == 1:
  157.                 self.move("down", 1)
  158.             else:
  159.                 self.move("up", 1)
  160.         self.thread1.exit()
  161.              
  162.         # counter = 0
  163.         # GPIO.output(self.ledPin, False)
  164.         # self.home()
  165.         # self.move("down", 100)
  166.         # while GPIO.input(self.ldrPin) != 1:
  167.             # time.sleep(0.001)
  168.         # curr_state = GPIO.input(self.ldrPin)
  169.         # while self.isAutoOn == True:
  170.             # if GPIO.input(self.ldrPin) == 1:
  171.                 # self.move("down", 1)
  172.             # if GPIO.input(self.ldrPin) == 0:
  173.                 # self.move("up", 1)
  174.              
  175.             # #curr_state = GPIO.input(self.ldrPin)
  176.         # self.thread1.exit()
  177.         # GPIO.output(self.ledPin,False)
  178.         # checkLicht = GPIO.input(self.ldrPin)
  179.         # print "zta start"
  180.          
  181.         # print "checkLicht: " + str(checkLicht)
  182.         # while checkLicht != 1:
  183.             # time.sleep(0.001)
  184.              
  185.             # checkLicht = GPIO.input(self.ldrPin)
  186.         # self.home()
  187.         # rasp_motor.move("down", 10)
  188.         # print "exiting checkLicht loop"
  189.         # while self.isAutoOn:
  190.             # GPIO.output(self.ledPin, False)
  191.             # self.move("down", 200)
  192.             # self.move("up", 200)
  193.             # GPIO.output(self.ledPin, False)
  194.             # # verzend = GPIO.input(self.ldrPin)
  195.             # # while verzend == 0:
  196.                 # # self.move("up", 1)
  197.                 # # GPIO.output(self.ledPin, False)
  198.             # # while verzend == 1:
  199.                 # # self.move("down", 1)
  200.                 # # GPIO.output(self.ledPin, True)
  201.             # # verzend = GPIO.input(self.ldrPin)  
  202.             # if not self.isAutoOn:
  203.                 # self.thread1.exit()
  204.             # print "isautoon loop"
  205.         # print "zta false"
  206.  
  207.          
  208. rasp_motor = Motor()
  209.  
  210.  
  211.  
  212.  
  213.  
  214.  
  215. @app.route('/')
  216. def index():
  217.     now = datetime.datetime.now()
  218.     timeString = now.strftime("%Y-%m-%d %H:%M")
  219.     templateData = {
  220.         'bla' : timeString
  221.     }
  222.     return render_template('index.htm', **templateData)
  223.  
  224.      
  225. @app.route('/home.htm')
  226. def home():
  227.     rasp_motor.home()
  228.     return render_template('home.htm')
  229.      
  230.      
  231. @app.route('/up.htm', methods=['GET'])
  232. def up():
  233.     rasp_motor.move("up", int(request.args.get("steps", "1")))
  234.     return render_template('up.htm')
  235.      
  236.      
  237. @app.route('/down.htm', methods=['GET'])
  238. def down():
  239.     rasp_motor.move("down", int(request.args.get("steps", "1")))
  240.     return render_template('down.htm')
  241.      
  242.      
  243. @app.route('/xpos.htm')
  244. def xpos():
  245.     templateData = {
  246.         'bla' : rasp_motor.readFile("/boot/x.txt")
  247.     }
  248.     return render_template('xpos.htm', **templateData)
  249.  
  250.      
  251. @app.route('/ypos.htm')
  252. def ypos():
  253.     templateData = {
  254.         'bla' : rasp_motor.readFile("/boot/y.txt")
  255.     }
  256.     return render_template('ypos.htm', **templateData)
  257.      
  258.  
  259. @app.route('/shutdown.htm')
  260. def shutdown():
  261.     rasp_motor.shutdown()
  262.     templateData = {
  263.     'bla' : "System going down!"
  264.     }
  265.     return render_template('index.htm', **templateData)
  266.  
  267.      
  268. @app.route('/reboot.htm')
  269. def reboot():
  270.     rasp_motor.reboot()
  271.     templateData = {
  272.     'bla' : "System going for reboot"
  273.     }
  274.     return render_template('index.htm', **templateData)
  275.  
  276.      
  277.      
  278.      
  279. @app.route('/wave')
  280. def wave():
  281.     rasp_motor.home()
  282.     rasp_motor.isAutoOn = False;
  283.     GPIO.output(rasp_motor.ledPin,False)
  284.     rasp_motor.move("down", 100)
  285.     counter = 0
  286.     while rasp_motor.isAutoOn == False:
  287.          
  288.         GPIO.output(rasp_motor.ledPin, True)
  289.         rasp_motor.move("down", 200)
  290.          
  291.         GPIO.output(rasp_motor.ledPin, False)
  292.         time.sleep(0.5)
  293.         rasp_motor.move("up", 200)
  294.          
  295.         counter += 1
  296.         # if counter == 0:
  297.             # GPIO.output(rasp_motor.ledPin,False)
  298.             # rasp_motor.move("down", 200)          
  299.             # time.sleep(0.5)
  300.             # GPIO.output(rasp_motor.ledPin,True)
  301.             # rasp_motor.move("down", 200)
  302.         # GPIO.output(rasp_motor.ledPin,False)
  303.         # rasp_motor.move("up", 200)
  304.         # GPIO.output(rasp_motor.ledPin,True)
  305.         # rasp_motor.move("down", 200)
  306.         # counter += 1
  307.     return render_template('bscript.htm')
  308.  
  309. @app.route('/basicscript')
  310. def bscript():
  311.     return render_template('bscript.htm')
  312.      
  313. @app.route('/auto_on.htm')
  314. def auto_on():
  315.     rasp_motor.auto_on()
  316.     return render_template('auto_on.htm')
  317.  
  318. @app.route('/auto_off.htm')
  319. def auto_off():
  320.     rasp_motor.auto_off()
  321.     tekst = "harhar"
  322.     templateData = {
  323.         'bla' : tekst
  324.     }
  325.     return render_template('auto_off.htm', **templateData)
  326.  
  327.  
  328.  
  329.  
  330. if __name__ == "__main__":
  331.     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