Browse Source

init commit

main
rra 3 years ago
parent
commit
75e880b1e1
  1. 13
      analogSensor.py
  2. 15
      distanceSensor.py
  3. 32
      ldrLed.py
  4. 28
      remoteGpioFromHome.py
  5. 21
      remoteGpioFromPi.py
  6. 29
      servo.py
  7. 20
      templates/index.html
  8. 34
      webLed.py

13
analogSensor.py

@ -0,0 +1,13 @@
from gpiozero import MCP3008
from time import sleep
#remember to enable SPI in raspi-config > interfaces before you run this the first time
#How to wire the MCP3008: https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
#gpiozero documentation about MCP3008: https://gpiozero.readthedocs.io/en/stable/api_spi.html
ldr = MCP3008(channel=0, device=0)
while True:
print(ldr.value)
sleep(0.01)

15
distanceSensor.py

@ -0,0 +1,15 @@
from gpiozero import DistanceSensor
from time import sleep
#https://gpiozero.readthedocs.io/en/stable/recipes.html#distance-sensor
sensor = DistanceSensor(
echo=19,
trigger=26,
max_distance=4,
queue_len=5)
while True:
print('distance', sensor.distance * 100)
sleep(0.5)

32
ldrLed.py

@ -0,0 +1,32 @@
#remember to enable SPI in raspi-config > interfaces before you run this the first time
#How to wire the MCP3008:
#https://learn.adafruit.com/raspberry-pi-analog-to-digital-converters/mcp3008
#gpiozero about MCP3008:
#https://gpiozero.readthedocs.io/en/stable/api_spi.html
#gpiozero tools to modify values:
#https://gpiozero.readthedocs.io/en/stable/_modules/gpiozero/tools.html
from gpiozero import MCP3008, PWMLED
from gpiozero.tools import clamped, smoothed, scaled
from time import sleep
ldr = MCP3008(channel=0, device=0)
led = PWMLED(17)
while True:
reading = smoothed(ldr,3) #smooth samples
value = scaled(reading, 0,1.0,0.01,0.5) #scale samples
brightness = clamped(value,0,1.0) #clamp samples
for i in brightness:
if i < 0.15:
i = 0
print(i)
led.value = i

28
remoteGpioFromHome.py

@ -0,0 +1,28 @@
import sys
from gpiozero import PWMLED
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
from time import sleep
#If you are working on MacOS or linux make an ssh tunnel first:
#ssh -L 8888:localhost:8888 kanelbulle
factory = PiGPIOFactory(host='localhost')
#If you are working on Windows:
#first ask Roel to open a port in the interaction.tools firewall for you
#then
#factory = PiGPIOFactory(host='kanelbulle.interaction.tools', port=8889)
led = PWMLED(17, pin_factory=factory)
while True:
try:
for i in range(0,10):
a = float(i/10)
led.value = a
sleep(0.1)
print(a)
except KeyboardInterrupt:
led.close()
sys.exit()

21
remoteGpioFromPi.py

@ -0,0 +1,21 @@
import sys
from gpiozero import PWMLED
from gpiozero.pins.pigpio import PiGPIOFactory
from signal import pause
from time import sleep
#This code is meant to be run FROM a Pi connected to the interaction.tools network
#It allows you to read/write Pins from other PIs on the network
bobPi = PiGPIOFactory(host='10.10.10.25')
alicePi = PiGPIOFactory(host='10.10.10.26')
led = LED(17)
button_alice = Button(17, pin_factory=bobPi)
button_bob = Button(17, pin_factory=alicePi)
led.source = all_values(button_alice, button_bob)
pause()

29
servo.py

@ -0,0 +1,29 @@
from gpiozero import Servo
from time import sleep
# https://gpiozero.readthedocs.io/en/stable/recipes.html#servo
#these pulse-width values come from the elektrobit product description
maxPW=2.1/1000
minPW=0.9/1000
# these values are empirically determined and represent maxima
# 0.3 will turn slow in one direction, -0.5 slow in the other
left = 1
right = -1
stop = -0.2
servo = Servo(
17,
initial_value=None,
min_pulse_width=minPW,
max_pulse_width=maxPW)
while True:
servo.value = left
sleep(1)
servo.value = right
sleep(1)
servo.value = stop
sleep(2)

20
templates/index.html

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html>
<head>
<title>LEDS!</title>
</head>
<body>
<form action="/on/" method="post">
<button name="forwardBtn" type="submit">On</button>
</form>
<form action="/off/" method="post">
<button name="forwardBtn" type="submit">Off</button>
</form>
<p>{{led_status}} </p>
</body>
</html>

34
webLed.py

@ -0,0 +1,34 @@
from flask import Flask, render_template, Response, request, redirect, url_for
from gpiozero import LED
#This sketch allows you to control an LED from the browser.
#When you run the sketch navigate to yourpi.interaction.tools:8000 to try it
led = LED(17)
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html', led_status="Press 'on' or 'off'")
@app.route("/on/", methods=['POST'])
def turn_led_on():
led.on()
led_status = 'LED is on'
#Return the page result
return render_template('index.html', led_status=led_status)
@app.route("/off/", methods=['POST'])
def turn_led_off():
led.off()
led_status= 'LED is off'
#Return the page result
return render_template('index.html', led_status=led_status)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
Loading…
Cancel
Save