Skip to content

Instantly share code, notes, and snippets.

@JohnRudolfLewis
Created October 28, 2018 17:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnRudolfLewis/3095689bb80fd6ef6b94240519ff282c to your computer and use it in GitHub Desktop.
Save JohnRudolfLewis/3095689bb80fd6ef6b94240519ff282c to your computer and use it in GitHub Desktop.
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)
@Kshukla
Copy link

Kshukla commented Mar 1, 2021

Are there any instruction to implement this mode ?

@JohnRudolfLewis
Copy link
Author

Are there any instruction to implement this mode ?

never wrote any... but I recall that all I did was to set this up to run as a service so that it starts up each time the pi reboots. But first, you will want to modify the constants as appropriate for your setup, both the button pin number, and the equipment ids.

@Kshukla
Copy link

Kshukla commented Mar 1, 2021

Got it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment