Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Implementing a feed mode for the reef-pi
#!/usr/bin/env python
"""Feedmode.py: Feedmode extends reef-pi by implementing a proper feedmode buttom/timer."""
# Copyright 2018 John Rudolf Lewis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time, json, requests, datetime
import RPi.GPIO as GPIO
BUTTON_PIN = 12
USERNAME = "reef-pi"
PASSWORD = "reef-pi"
POWERHEAD_ID = 2
RETURNPUMP_ID = 1
FEED_DURATION_SECONDS = 300
feedmode_running = False
feedmode_expire_at = datetime.datetime.now()
def _login():
session = requests.Session();
session.post("http://localhost/auth/signin", data = json.dumps({"user":USERNAME,"password":PASSWORD}))
return session
def _toggle_equipment(session, id, on):
r = session.get("http://localhost/api/equipment/{id}".format(id=id))
if r.status_code != 200:
return False
equipment = json.loads(r.text)
equipment['on'] = on
r = session.post("http://localhost/api/equipment/{id}".format(id=id), data = json.dumps(equipment))
return r.status_code == 200
def _equipment_on(session, id):
r = session.get("http://localhost/api/equipment/{id}".format(id=id))
if r.status_code != 200:
return False
return json.loads(r.text)['on']
def _on_button_press(channel):
global feedmode_running
global feedmode_expire_at
session = _login()
if not feedmode_running:
feedmode_running = True
feedmode_expire_at = datetime.datetime.now() + datetime.timedelta(seconds=FEED_DURATION_SECONDS)
_toggle_equipment(session, RETURNPUMP_ID, False)
print str(datetime.datetime.now()), " ReturnPump turned off."
elif _equipment_on(session, POWERHEAD_ID):
_toggle_equipment(session, POWERHEAD_ID, False)
print str(datetime.datetime.now()), " PowerHead turned off."
else:
_toggle_equipment(session, POWERHEAD_ID, True)
print str(datetime.datetime.now()), " PowerHead turned on."
_toggle_equipment(session, RETURNPUMP_ID, True)
print str(datetime.datetime.now()), " ReturnPump turned on."
feedmode_running = False
def _on_timer():
global feedmode_running
if feedmode_running and feedmode_expire_at < datetime.datetime.now():
session = _login()
_toggle_equipment(session, POWERHEAD_ID, True)
print str(datetime.datetime.now()), " PowerHead turned on."
_toggle_equipment(session, RETURNPUMP_ID, True)
print str(datetime.datetime.now()), " ReturnPump turned on."
feedmode_running = False
GPIO.setmode(GPIO.BCM)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(BUTTON_PIN, GPIO.FALLING, callback=_on_button_press, bouncetime=500)
print str(datetime.datetime.now()), " Ready!"
while True:
_on_timer()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.