🎉 Added script and README
This commit is contained in:
parent
71b73f08b6
commit
8fd14a3170
2 changed files with 107 additions and 1 deletions
41
README.md
41
README.md
|
@ -1,2 +1,41 @@
|
|||
# rpi-nightlight
|
||||
# Raspberry Pi nightlight
|
||||
|
||||
A simple Python script which turns your Raspberry Pi into a motion activated nightlight.
|
||||
|
||||
## Installation
|
||||
|
||||
You will need the following dependencies:
|
||||
|
||||
```
|
||||
pip3 install RPi.GPIO rpi-ws281x
|
||||
```
|
||||
|
||||
Clone this repo or just download the python script and put it into `/usr/local/bin` for example and make it executeable.
|
||||
|
||||
```
|
||||
chmod +x /usr/local/bin/nightlight.py
|
||||
```
|
||||
|
||||
|
||||
Probably you want to start this script at boot, put this in `/etc/cron.d/nightlight`
|
||||
```
|
||||
@reboot root /usr/local/bin/nightlight.py > /dev/null 2>&1
|
||||
```
|
||||
|
||||
## GPIO PINs
|
||||
|
||||
Connect the PIR sensor as following (I have used a HC-SR501):
|
||||
|
||||
```
|
||||
VCC: 5V
|
||||
DATA: GPIO 23
|
||||
GND: GND
|
||||
```
|
||||
|
||||
Connect the Neopixel strip (some ws281x will do) as following:
|
||||
|
||||
```
|
||||
VCC: 5V
|
||||
DATA: GPIO 18
|
||||
GND: GND
|
||||
```
|
||||
|
|
67
nightlight.py
Executable file
67
nightlight.py
Executable file
|
@ -0,0 +1,67 @@
|
|||
#!/usr/bin/python
|
||||
import RPi.GPIO as GPIO
|
||||
import time
|
||||
from rpi_ws281x import PixelStrip, Color, Adafruit_NeoPixel
|
||||
|
||||
SENSOR_PIN = 23
|
||||
LIGHT_SENSOR_PIN = 21
|
||||
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(SENSOR_PIN, GPIO.IN)
|
||||
GPIO.setup(LIGHT_SENSOR_PIN, GPIO.IN)
|
||||
|
||||
LED_COUNT = 15
|
||||
|
||||
led_duration = 0
|
||||
|
||||
strip = Adafruit_NeoPixel(LED_COUNT, 18, 800000, 5, False, 255)
|
||||
strip.begin()
|
||||
|
||||
|
||||
def turn_on_light(channel):
|
||||
if GPIO.input(LIGHT_SENSOR_PIN):
|
||||
# motion detected
|
||||
# reset LED duration
|
||||
global led_duration
|
||||
led_duration = 0
|
||||
colorWipe(strip, Color(255, 0, 32))
|
||||
else:
|
||||
# there was motion but it is not dark yet
|
||||
colorWipe(strip, Color(0, 0, 0))
|
||||
|
||||
|
||||
def turn_off_light(channel):
|
||||
# turn off light
|
||||
colorWipe(strip, Color(0, 0, 0))
|
||||
|
||||
|
||||
def colorWipe(strip, color, wait_ms=100):
|
||||
for i in range(strip.numPixels()):
|
||||
strip.setPixelColor(i, color)
|
||||
strip.show()
|
||||
time.sleep(wait_ms / 1000.0)
|
||||
|
||||
|
||||
try:
|
||||
GPIO.add_event_detect(
|
||||
SENSOR_PIN, GPIO.RISING, callback=turn_on_light
|
||||
)
|
||||
GPIO.add_event_detect(
|
||||
LIGHT_SENSOR_PIN, GPIO.RISING, callback=turn_off_light, bouncetime=500
|
||||
)
|
||||
while True:
|
||||
if strip.getPixelColor(0) > 0:
|
||||
# check if LED is on for more than X seconds
|
||||
if led_duration > 180:
|
||||
# turn off LED
|
||||
colorWipe(strip, Color(0, 0, 0))
|
||||
led_duration = 0
|
||||
else:
|
||||
led_duration += 1
|
||||
else:
|
||||
led_duration = 0
|
||||
time.sleep(1)
|
||||
except KeyboardInterrupt:
|
||||
colorWipe(strip, Color(0, 0, 0))
|
||||
print("Exiting...")
|
||||
GPIO.cleanup()
|
Loading…
Reference in a new issue