flask gpio test
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
				
			|||||||
import RPi.GPIO as GPIO
 | 
					import RPi.GPIO as GPIO
 | 
				
			||||||
from flask import Flask, render_template, request
 | 
					from flask import Flask, render_template, request
 | 
				
			||||||
 | 
					from src.pins import pins_out, pins_in, pins_pwm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
kamera_server = Flask(__name__)
 | 
					kamera_server = Flask(__name__)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -17,14 +18,20 @@ for pin in pins:
 | 
				
			|||||||
   GPIO.setup(pin, GPIO.OUT)
 | 
					   GPIO.setup(pin, GPIO.OUT)
 | 
				
			||||||
   GPIO.output(pin, GPIO.HIGH)
 | 
					   GPIO.output(pin, GPIO.HIGH)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for pin in pins_in:
 | 
				
			||||||
 | 
					   GPIO.setup(pin.nr, GPIO.IN)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@kamera_server.route("/")
 | 
					@kamera_server.route("/")
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
   # For each pin, read the pin state and store it in the pins dictionary:
 | 
					   # For each pin, read the pin state and store it in the pins dictionary:
 | 
				
			||||||
    for pin in pins:
 | 
					    for pin in pins:
 | 
				
			||||||
        pins[pin]['state'] = GPIO.input(pin)
 | 
					        pins[pin]['state'] = GPIO.input(pin)
 | 
				
			||||||
 | 
					    for pin in pins_in:
 | 
				
			||||||
 | 
					        pins[pin]['state'] = GPIO.input(pin)
 | 
				
			||||||
   # Put the pin dictionary into the template data dictionary:
 | 
					   # Put the pin dictionary into the template data dictionary:
 | 
				
			||||||
   templateData = {
 | 
					   templateData = {
 | 
				
			||||||
      'pins' : pins
 | 
					      'pins' : pins,
 | 
				
			||||||
 | 
					      'pins_in' : pins_in
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
   # Pass the template data into the template main.html and return it to the user
 | 
					   # Pass the template data into the template main.html and return it to the user
 | 
				
			||||||
   return render_template('main.html', **templateData)
 | 
					   return render_template('main.html', **templateData)
 | 
				
			||||||
@@ -49,10 +56,13 @@ def action(changePin, action):
 | 
				
			|||||||
   # For each pin, read the pin state and store it in the pins dictionary:
 | 
					   # For each pin, read the pin state and store it in the pins dictionary:
 | 
				
			||||||
   for pin in pins:
 | 
					   for pin in pins:
 | 
				
			||||||
      pins[pin]['state'] = GPIO.input(pin)
 | 
					      pins[pin]['state'] = GPIO.input(pin)
 | 
				
			||||||
 | 
					    for pin in pins_in:
 | 
				
			||||||
 | 
					        pins[pin]['state'] = GPIO.input(pin)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   # Along with the pin dictionary, put the message into the template data dictionary:
 | 
					   # Along with the pin dictionary, put the message into the template data dictionary:
 | 
				
			||||||
   templateData = {
 | 
					   templateData = {
 | 
				
			||||||
      'pins' : pins
 | 
					      'pins' : pins,
 | 
				
			||||||
 | 
					      'pins_in' : pins_in
 | 
				
			||||||
   }
 | 
					   }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   return render_template('main.html', **templateData)
 | 
					   return render_template('main.html', **templateData)
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										0
									
								
								src/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										25
									
								
								src/pins.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								src/pins.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,25 @@
 | 
				
			|||||||
 | 
					pins_out = {
 | 
				
			||||||
 | 
					    'kamera' : 2,
 | 
				
			||||||
 | 
					    'encoder' : 3
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pins_in = {
 | 
				
			||||||
 | 
					    'endschalter1' : {'nr' : 23, 'name' : 'Endschalter, Motor 1', 'state' : GPIO.HIGH},
 | 
				
			||||||
 | 
					    'endschalter2' : {'nr' : 26, 'name' : 'Endschalter, Motor 2', 'state' : GPIO.HIGH},
 | 
				
			||||||
 | 
					    'endschalter3' : {'nr' : 25, 'name' : 'Endschalter, Motor 2', 'state' : GPIO.HIGH}
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pins_pwm = {
 | 
				
			||||||
 | 
					    'm1_a1' : 17,
 | 
				
			||||||
 | 
					    'm1_b1' : 18,
 | 
				
			||||||
 | 
					    'm1_a2' : 27,
 | 
				
			||||||
 | 
					    'm1_b2' : 22,
 | 
				
			||||||
 | 
					    'm2_a1' : 19,
 | 
				
			||||||
 | 
					    'm2_b1' : 13,
 | 
				
			||||||
 | 
					    'm2_a2' : 6,
 | 
				
			||||||
 | 
					    'm2_b2' : 5,
 | 
				
			||||||
 | 
					    'm3_a1' : 16,
 | 
				
			||||||
 | 
					    'm3_b1' : 12,
 | 
				
			||||||
 | 
					    'm3_a2' : 21,
 | 
				
			||||||
 | 
					    'm3_b2' : 20,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
@@ -19,5 +19,13 @@
 | 
				
			|||||||
      <a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
 | 
					      <a href="/{{pin}}/on" class="btn btn-block btn-lg btn-primary" role="button">Turn on</a></div></div>
 | 
				
			||||||
   {% endif %}
 | 
					   {% endif %}
 | 
				
			||||||
   {% endfor %}
 | 
					   {% endfor %}
 | 
				
			||||||
 | 
					   {% for pin in pins_in %}
 | 
				
			||||||
 | 
					    <h3>{{ pins_in[pin].name }}
 | 
				
			||||||
 | 
					    {% if pins_in[pin].state == true %}
 | 
				
			||||||
 | 
					        ist gerade <strong>an</strong></h2><div class="row"><div class="col-md-2">
 | 
				
			||||||
 | 
					    {% else %}
 | 
				
			||||||
 | 
					        ist gerade <strong>aus</strong></h2><div class="row"><div class="col-md-2">
 | 
				
			||||||
 | 
					    {% endif %}
 | 
				
			||||||
 | 
					   {% endfor %}
 | 
				
			||||||
</body>
 | 
					</body>
 | 
				
			||||||
</html>
 | 
					</html>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user