Python Forum

Full Version: aqurium control
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
ive made this script with alot of help, i wondered if there was a way to make the time settings adjustable via the lcd im using, just like you would with say an alarm clock...anyone out there who can help?

import sys
import time

import dothat.backlight as backlight
import dothat.lcd as lcd
import dothat.touch as nav
from dot3k.menu import Menu, MenuOption
from time import sleep
import datetime

sys.path.append ('/usr/local/lib/python2.7/dist-packages')
sys.path.append('/home/pi/Pimoroni/displayotron/examples')
sys.path.append('/home/pi/.local/lib/python2.7/site-packages')


from plugins.clock import Clock
from plugins.graph import IPAddress, GraphTemp, GraphCPU, GraphNetSpeed, GraphSysReboot, GraphSysShutdown
from plugins.text import Text
from plugins.utils import Backlight, Contrast

from astral import Astral
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setup(13,GPIO.OUT)
GPIO.setup(5,GPIO.OUT)
import schedule

a = Astral()
a.solar_depression = 'civil'
city = a['London']


class Lights(MenuOption):
    def __init__(self):
        self._mode = 0
        self._message = ''
        self._timeout = 0
        self._status = False
        self._auto = True
        self._sun_rise = None
        self._sun_set = None
        self._current_status = 'off'
        MenuOption.__init__(self)

    def get_status(self):
        return self._current_status

    def is_on(self):
        return self._status

    def is_auto(self):
        return self._auto

    def set_times(self, sun_rise, sun_set):
        self._sun_rise = sun_rise
        self._sun_set = sun_set

    def redraw(self, menu):
        if self._mode == 0:
            menu.write_row(0, "Lights {}".format(str(datetime.datetime.now().time())[:8]))
            menu.write_row(1, "{} {}".format("[LIGHTS]" if self._status else " LIGHTS ", "[AUTO]" if self._auto else " AUTO "))
            menu.write_row(2, "R:{}  S:{}".format(str(self._sun_rise)[:-3], str(self._sun_set)[:-3]))
        elif self._mode == 1:
            if self._timeout <= time.time():
                self._mode = 0
            menu.clear_row(0)
            menu.write_row(1, self._message)
            menu.clear_row(2)

    def display_message(self, message, timeout=1):
        self._message = message
        self._timeout = time.time() + timeout
        self._mode = 1

    def job(day):
        self.daylights_on

    def job(night):
        self.nightlights_on

    def job(off):
        self.lights_off

    def daylights_on(self): #Activates Daylights
        self._current_status = 'day'
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,1)
        self._status = True
        self.display_message("DayLights: On", 1)

    def nightlights_on(self): #Activates Nightlights
        self._current_status = 'night'
        GPIO.output(13,0)
        GPIO.output(5,1)
        GPIO.output(13,0)
        self._status = True
        self.display_message("NightLights: On", 1)


    def lights_off(self): # Deactivates any light mode
        self._current_status = 'off'
        GPIO.output(5,0)
        self._status = False
        self.display_message("Lights: Off", 1)

    def light_toggle(self): # Toggles Between Day/Night Modes
        if (GPIO.input(13) == True):
            self.nightlights_on() 
        else:
            self.daylights_on()

    def auto_on(self): # Sets an automatic timer to activate lights
        self._auto = True
        self.display_message("Auto: On", 1)

    def auto_off(self): # Sets an automatic timer to dectivate lights
        self._auto = False
        self.display_message("Auto: Off", 1)

    def right(self):
        self.auto_on()

    def left(self):
        self.auto_off()
        return True

    def up(self):
        self._auto = False
        self.light_toggle()

    def down(self):
        self._auto = False
        self.lights_off()

lights = Lights()

#Unordered menu
menu = Menu(
    structure={
            'Power Options': {
                'Reboot':GraphSysReboot(),
                'Shutdown':GraphSysShutdown(),
                },
            'Aquarium': {
                'Lighting': {
                    'Control': lights,
                    }
                },
        'Clock': Clock(backlight),
        'Status': {
            'IP': IPAddress(),
            'CPU': GraphCPU(backlight),
            'Temp': GraphTemp()
        },
        'Settings': {
            'Display': {
                'Contrast': Contrast(lcd),
                'Backlight': Backlight(backlight)
            }
        }
    },
    lcd=lcd,
    
    input_handler=Text())

nav.bind_defaults(menu)

required_status = 'off'

def schedule_nightlights():
    global required_status
    required_status = 'night'
    return schedule.CancelJob

def schedule_daylights():
    global required_status
    required_status = 'day'
    return schedule.CancelJob

def schedule_off():
    global required_status
    required_status = 'off'
    return schedule.CancelJob

def schedule_update():
    now = datetime.datetime.now()
    sun = city.sun(date=now.date(), local=True)
    sun_rise = sun['sunrise'].time()
    sun_set = sun['sunset'].time()
    dateSTR = datetime.datetime.now().strftime("%H:%M:%S" )
    
    lights.set_times(sun_rise, sun_set)

    schedule.clear('lights') # Cancel all pending sunrise/sunset jobs
    
    # With our newly calculated times, schedule the jobs
    schedule.every().day.at("17:47").do(schedule_nightlights).tag('lights')
    schedule.every().day.at("17:45").do(schedule_daylights).tag('lights')
    schedule.every().day.at("17:46").do(schedule_off).tag('lights')


# Update the schedule with the latest sunrise/sunset times
schedule.every().day.at("00:00").do(schedule_update).tag('update')

# Call schedule update once at startup to force set the initial rise/set times
schedule_update()

while True:
    menu.redraw()
    
    schedule.run_pending()

    if lights.is_auto():
        if lights.get_status() != required_status:
            if required_status == 'day':
                lights.daylights_on()
            if required_status == 'night':
                lights.nightlights_on()
            if required_status == 'off':
                lights.lights_off()

    time.sleep(1.0 / 20)
wow, thank you
it will be useful for my aquarium and for my fish :)