1
0
Fork 0
Browse Source

port over code from test project.

unstable
Craig Oates 4 years ago
parent
commit
dfe6554d6f
  1. 17
      auto-start.sh
  2. 48
      relay.py

17
auto-start.sh

@ -0,0 +1,17 @@
#!/bin/bash
# Auto Start
# ================================================================
# This script is used to start the roaming-light-relay controller
# when the Raspberry Pi is turned on. The main thing is does is
# activate the projects virtual environment before running the
# actual program in this project. This file is referenced in
# /etc/xdg/lxsession/LXDE-pi/autostart. The file just referenced
# is a Raspbian specific file and I have found it to do what I want
# better than using crontab -- like traditional Linux set-ups.
# Refer to your Roaming Light: Project Notes file for more info.
# IT IS ASSUMED THIS FILE IS IN THE PROJECTS ROOT FOLDER.
# IT WILL NOT WORK AS INTENDED OUTWISE.
source /home/pi/repos/roaming-light-relay/proj-env/bin/activate
python3 /home/pi/repos/roaming-light-relay/relay.py

48
relay.py

@ -0,0 +1,48 @@
# Need to install python-rpi.gpio
import RPi.GPIO as GPIO
import time
import requests # Used to grab light reading via the REST API.
relay_pin = 11
#GPIO.setmode(GPIO.BCM) # GPIO umbers instead of board numbers.
GPIO.setmode(GPIO.BOARD)
GPIO.setup(relay_pin, GPIO.OUT)
try:
while True:
# r = requests.get('http://192.168.1.228:5000/api/readings/latest/1')
r = requests.get('http://35.176.14.135/api/readings/latest/1')
#print(r.status_code)
data = r.json()
#print(data)
the_reading = data.get("reading")
# print(the_reading)
if the_reading > 30:
print(f"[INFO] Light is on -- {the_reading}.")
GPIO.output(relay_pin, GPIO.HIGH)
else:
print(f"[INFO] Light is off -- {the_reading}.")
GPIO.output(relay_pin, GPIO.LOW)
# last = list(data.keys())[-1]
# reading = data.get(last)
# print(reading["active"])
# if reading["active"] == True:
# print("Setting the relay to hight")
# GPIO.output(relay_pin, GPIO.HIGH)
# else:
# print("Setting the relay to low")
# GPIO.output(relay_pin, GPIO.LOW)
# print(r.light-reading)
# Set the pin to high
# print("Setting the relay to hight")
# GPIO.output(relay_pin, GPIO.HIGH)
# time.sleep(2)
# Set the pin to low
# print("Setting the relay to low")
# GPIO.output(relay_pin, GPIO.LOW)
#print(GPIO)
# time.sleep(2)
except KeyboardInterrupt:
GPIO.cleanup()
print("Cleaned up GPIO and quitting")