diff --git a/analogSensor.py b/analogSensor.py new file mode 100644 index 0000000..0050a9d --- /dev/null +++ b/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) diff --git a/distanceSensor.py b/distanceSensor.py new file mode 100644 index 0000000..d6764fe --- /dev/null +++ b/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) + diff --git a/ldrLed.py b/ldrLed.py new file mode 100644 index 0000000..d2f9e47 --- /dev/null +++ b/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 + diff --git a/remoteGpioFromHome.py b/remoteGpioFromHome.py new file mode 100644 index 0000000..541c441 --- /dev/null +++ b/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() + diff --git a/remoteGpioFromPi.py b/remoteGpioFromPi.py new file mode 100644 index 0000000..63d5fa6 --- /dev/null +++ b/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() + diff --git a/servo.py b/servo.py new file mode 100644 index 0000000..0d262bd --- /dev/null +++ b/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) diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..2efa276 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,20 @@ + + + + LEDS! + + + +
+ +
+ +
+ +
+ +

{{led_status}}

+ + + + diff --git a/webLed.py b/webLed.py new file mode 100644 index 0000000..1937f50 --- /dev/null +++ b/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)