From dfe6554d6f2d1678a8e16cfde072df3c327a1a51 Mon Sep 17 00:00:00 2001 From: Craig Date: Sun, 5 Jan 2020 20:31:51 +0000 Subject: [PATCH] port over code from test project. --- auto-start.sh | 17 +++++++++++++++++ relay.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100755 auto-start.sh create mode 100644 relay.py diff --git a/auto-start.sh b/auto-start.sh new file mode 100755 index 0000000..292a708 --- /dev/null +++ b/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 diff --git a/relay.py b/relay.py new file mode 100644 index 0000000..d6f4a91 --- /dev/null +++ b/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")