예제 #1
0
def load_settings(loc_locs):
    loc_dic = pigrow_defs.load_locs(loc_locs)
    set_dic = pigrow_defs.load_settings(loc_dic['loc_settings'])
    if 'loc_dht_log' in loc_dic:
        loc_dht_log = loc_dic['loc_dht_log']
    else:
        print("!! DHT log not found !!")
    if 'err_log' in loc_dic:
        err_log = loc_dic['err_log']
    else:
        err_log = 'emergency_error_log.txt'
    return loc_dic, set_dic, loc_dht_log, err_log
예제 #2
0
def load_settings(loc_locs):
    loc_dic = pigrow_defs.load_locs(loc_locs)
    set_dic = pigrow_defs.load_settings(loc_dic['loc_settings'])
    if 'err_log' in loc_dic:
        err_log = loc_dic['err_log']
    else:
        err_log = 'emergency_error_log.txt'
    if 'box_name' in set_dic:
        box_name = set_dic['box_name']
    else:
        box_name = "undetermined"

    return loc_dic, set_dic, err_log, box_name
예제 #3
0
def find_paths_from_dirlocs():
    try:
        sys.path.append(homedir + '/Pigrow/scripts/')
        import pigrow_defs
        script = 'temp_graph.py'
        loc_locs = homedir + '/Pigrow/config/dirlocs.txt'
        loc_dic = pigrow_defs.load_locs(loc_locs)
        graph_path = loc_dic['graph_path']
        graph_path = graph_path + "dht_temp_graph.png"
        log_location = loc_dic['loc_dht_log']
        loc_settings = loc_dic['loc_settings']
    except:
        graph_path = "./dht_temp_graph.png"
        log_location = "./dht22_log.txt"
        loc_settings = ""
    return graph_path, log_location, loc_settings
예제 #4
0
            GPIO.output(gpio_pin, GPIO.HIGH)
            gpio_pin_dir = 'high'
        elif gpio_pin_on == "high":
            gpio_pin_dir = 'low'
            GPIO.output(gpio_pin, GPIO.LOW)
        else:
            msg +=("      !!       CAN'T DETERMINE GPIO DIRECTION   !!\n")
            msg +=("      !!  run config program or edit config.txt !!\n")
            msg +=("      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
            pigrow_defs.write_log(script, 'Failed - no direction set in config', switch_log)
            return msg
    else:
        msg +=("      !!               NO fans SET            !!\n")
        msg +=("      !!  run config program or edit config.txt !!\n")
        msg +=("      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
        pigrow_defs.write_log(script, 'Failed - due to none set in config', switch_log)
        return msg

    msg +=("      ##            by switching GPIO "+str(gpio_pin)+" to "+gpio_pin_dir+"  ##\n")
    msg +=("      #############################################\n")
    pigrow_defs.write_log(script, 'fans turned off', switch_log)
    return msg

if __name__ == '__main__':

    ### default settings
    loc_dic = pigrow_defs.load_locs(homedir + "/Pigrow/config/dirlocs.txt")
    set_dic = pigrow_defs.load_settings(loc_dic['loc_settings'], err_log=loc_dic['err_log'],)
    msg = fans_off(set_dic, loc_dic['loc_switchlog'])
    print (msg)
예제 #5
0
#!/usr/bin/python
import os
import sys
import time
import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
homedir = os.getenv("HOME")

try:
    sys.path.append(homedir + '/Pigrow/scripts/')
    import pigrow_defs
    script = 'log_lm393smd.py'
    loc_locs = homedir + '/Pigrow/config/dirlocs.txt'
    loc_dic = pigrow_defs.load_locs(loc_locs)
    log_path = loc_dic['log_path']
    log_path = log_path + "soilmoistD_log.txt"
    #log_path = loc_dic['loc_lm393smd_log']
    loc_settings = loc_dic['loc_settings']
    set_dic = pigrow_defs.load_settings(loc_settings)
    gpio_pins = (set_dic['lm393moistD_sensor'])
    gpio_pins = gpio_pins.split(",")
    gpio_pin_list = []
    for pin in gpio_pins:
        gpio_pin_list.append(str(pin))
except Exception as e:
    print("Using Defaults because - " + str(e))
    gpio_pin_list = ['21']
    log_path = homedir + "/Pigrow/logs/soilmoistD_log.txt"
예제 #6
0
def show_info():
    # Read config
    loc_dic = pigrow_defs.load_locs(homedir + "/Pigrow/config/dirlocs.txt")
    set_dic = pigrow_defs.load_settings(loc_dic['loc_settings'],
                                        err_log=loc_dic['err_log'])

    text_out = ""
    checkdht_start = False
    trigger_watcher_start = False
    watcher_button_start = False
    # determine which control method is enabled
    out = subprocess.getoutput("crontab -l")
    for line in out.splitlines():
        if not line.strip()[0] == "#":
            if "@reboot" in line:
                if "checkDHT.py" in line:
                    checkdht_start = True
                    dht_cronline = line
                elif "trigger_watcher.py" in line:
                    trigger_watcher_start = True
                    tw_cronline = line
                elif "watcher_button.py" in line:
                    watcher_button_start = True

    # write text
    #
    not_runing_msg = "is not currently runnning, restart your pi and if you still get this error "
    not_runing_msg += "then there could be a problem, check error logs or try running the script manually on your"
    not_runing_msg += " pi to diagnose the issue."
    #
    if not checkdht_start and not trigger_watcher_start:
        text_out += "No control script set, if you wish to control devices then enable"
        text_out += " trigger_watcher.py to start on reboot."
    elif checkdht_start and trigger_watcher_start:
        text_out += "trigger_watcher.py and checkDHT.py are both set to run on reboot, these"
        text_out += "will conflict and may cause problems, it's highly recomended you remove one. "
        text_out += "Keep trigger_watcher to use log based triggers."
    elif checkdht_start:
        text_out += "Using checkDHT.py - this is now obsolete, to use log based triggers switch to using trigger_watcher.py\n"
        if subprocess.getoutput("pidof -x checkDHT.py") == "":
            text_out += "- checkDHT.py " + not_runing_msg
        else:
            text_out += "- checkDHT.py is currently running."
    elif trigger_watcher_start:
        text_out += "Using trigger_watcher.py\n"
        if subprocess.getoutput("pidof -x trigger_watcher.py") == "":
            text_out += "- trigger_watcher.py " + not_runing_msg
        else:
            text_out += "- trigger_watcher.py is currently running."
    text_out += "\n\n"
    # Add check to see if sctipt is running

    #
    if checkdht_start:
        #
        # old code for obsolete system
        #

        dht_msg = ""
        #heater on and off temps
        if "gpio_heater" in set_dic:
            dht_msg += "heater enabled, "
        else:
            dht_msg += "no heater gpio, "
        # low
        if "heater_templow" in set_dic:
            heater_templow = set_dic["heater_templow"]
            dht_msg += "Temp low; " + str(heater_templow) + " "
        else:
            dht_msg += "\nheater low temp not set\n"
            heater_templow = None
        # high
        if "heater_temphigh" in set_dic:
            heater_temphigh = set_dic["heater_temphigh"]
            dht_msg += "temp high: " + str(heater_temphigh) + " (Centigrade)\n"
        else:
            dht_msg += "\nheater high temp not set\n"
            heater_temphigh = None

        #
        # read humid info
        if "gpio_humid" in set_dic or "gpio_dehumid" in set_dic:
            dht_msg += "de/humid linked, "
        else:
            dht_msg += "de/humid NOT linked, "
        # low
        if "humid_low" in set_dic:
            humid_low = set_dic["humid_low"]
            dht_msg += "humidity low; " + str(humid_low)
        else:
            dht_msg += "\nHumid low not set\n"
            humid_low = None
        # high
        if "humid_high" in set_dic:
            humid_high = set_dic["humid_high"]
            dht_msg += " humidity high: " + str(humid_high) + "\n"
        else:
            dht_msg += "humid high not set\n\n"
            humid_high = None

        #
        #add gpio message to the message text
        if "dht22sensor" in set_dic:
            dht_msg += "DHT Sensor on pin " + str(set_dic['dht22sensor'] +
                                                  "\n")
            if "log_frequency" in set_dic:
                log_frequency = set_dic["log_frequency"]
                dht_msg += "Logging dht every " + str(
                    log_frequency) + " seconds. \n"
            else:
                log_frequency = ""
                dht_msg += "DHT Logging frequency not set\n"
            #check to see if log location is set in dirlocs.txt
            try:
                dht_msg += "logging to; " + dirlocs_dict['loc_dht_log'] + "\n"
            except:
                dht_msg += "No DHT log locaion in pigrow dirlocs\n"
        else:
            dht_msg += "DHT Sensor not set up for checkDHT.py\n"

        #extra args used to select options modes, if to ignore heater, etc.
        cmd_string = dht_cronline[8:]
        first_space = cmd_string.find(" ")
        extra_args = cmd_string[first_space:].strip()

        if not extra_args == "":
            dht_msg += "extra args = " + extra_args + "\n"

        #heater
        if "use_heat=true" in extra_args:
            dht_msg += "heater enabled, "
        elif "use_heat=false" in extra_args:
            dht_msg += "heater disabled, "
        else:
            dht_msg += "heater enabled, "

        #humid
        if "use_humid=true" in extra_args:
            dht_msg += "humidifier enabled, "
        elif "use_humid=false" in extra_args:
            dht_msg += "humidifier disabled, "
        else:
            dht_msg += "humidifier enabled, "

        #dehumid
        if "use_dehumid=true" in extra_args:
            dht_msg += "dehumidifier enabled, "
        elif "use_dehumid=false" in extra_args:
            dht_msg += "dehumidifier disabled, "
        else:
            dht_msg += "dehumidifier enabled, "

        #who controls fans
        if "use_fan=heat" in extra_args:
            dht_msg += "fan switched by heater "
        elif "use_fan=hum" in extra_args:
            dht_msg += "fan switched by humidifer "
        elif "use_fan=dehum" in extra_args:
            dht_msg += "fan switched by dehumidifer "
        elif "use_fan=hum" in extra_args:
            dht_msg += "dht control of fan disabled "
        else:
            dht_msg += "fan swtiched by heater"

        # add text to output
        text_out += dht_msg

    if trigger_watcher_start:
        text_out += ""

    return text_out
예제 #7
0
            pigrow_defs.write_log(script,
                                  'Failed - no direction set in config',
                                  switch_log)
            return msg

    else:
        msg += ("      !!               NO humid SET             !!\n")
        msg += ("      !!  run config program or edit config.txt  !!\n")
        msg += ("      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
        pigrow_defs.write_log(script, 'Failed - due to none set in config',
                              switch_log)
        return msg

    msg += ("      ##            by switching GPIO " + str(gpio_pin) + " to " +
            gpio_pin_on + "  ##")
    msg += ("      #############################################\n")
    pigrow_defs.write_log(script, 'Humidifier turned on', switch_log)
    return msg


if __name__ == '__main__':

    # default settings
    loc_dic = pigrow_defs.load_locs("/home/pi/Pigrow/config/dirlocs.txt")
    set_dic = pigrow_defs.load_settings(
        loc_dic['loc_settings'],
        err_log=loc_dic['err_log'],
    )
    msg = humid_on(set_dic, loc_dic['loc_switchlog'])
    print msg
예제 #8
0
def show_info():
    #
    switch_log_path = homedir + "/Pigrow/logs/switch_log.txt"
    if not os.path.isfile(switch_log_path):
        return "No switch log."
    cmd = "cat " + switch_log_path
    out =  os.popen(cmd).read()
    switch_log = out.splitlines()
    switch_dict = {}
    # read the switch log into lists
    timing_dict = {}
    for line in switch_log:
        line = line.split("@")
        script = line[0]
        time   = line[1]
        message = line[2]
        if "_on.py" in script:
            device = script.split("_on.py")[0]
            device_timing = ["on", time]
            if device in timing_dict:
                timing_dict[device] = timing_dict[device] + [device_timing]
            else:
                timing_dict[device] = [device_timing]
        elif "_off.py" in script:
            device = script.split("_off.py")[0]
            device_timing = ["off", time]
            if device in timing_dict:
                timing_dict[device] = timing_dict[device] + [device_timing]
            else:
                timing_dict[device] = [device_timing]
    # if last switch was on then calculate to current time
    for key, value in timing_dict.items():
        if value[-1][0] == "on":
            #print(key, " is currently on")
            time_now = datetime.datetime.now()
            timing_dict[key] = timing_dict[key] + [["off", str(time_now)]]


    #print (timing_dict[lamp])
    duration_dict = {}
    for key, value in timing_dict.items():
        #print( key, value)
        duration = datetime.timedelta(minutes=0)
        time_on = None
        for item in value:
            if item[0] == "on":
                time_on = datetime.datetime.strptime(item[1], '%Y-%m-%d %H:%M:%S.%f')
            else:
                if not time_on == None:
                    time_off = datetime.datetime.strptime(item[1], '%Y-%m-%d %H:%M:%S.%f')
                    step_duration = time_off - time_on
                    duration = duration + step_duration
                    time_on = None
            duration_dict[key] = duration

    # Read config for power settings
    loc_dic = pigrow_defs.load_locs(homedir + "/Pigrow/config/dirlocs.txt")
    set_dic = pigrow_defs.load_settings(loc_dic['loc_settings'], err_log=loc_dic['err_log'])
    # cost per kwh from settings dict
    if "cost_kwh_cent" in set_dic:
        cost_kwh_cent = float(set_dic["cost_kwh_cent"])
    else:
        cost_kwh_cent = None
    # device power
    #device_power_dict = {"lamp":300, "heater":20}
    device_power_dict = {}
    for key, value in set_dic.items():
        if "_watts" in key:
            device = key.split("_watts")[0]
            device_power_dict[device] = float(value)
    #
    text_out = ""
    for key, value in duration_dict.items():
        #print(key, value)
        text_out += key + " has been on for " + str(value) + "\n"
        if key in device_power_dict:
            duration_in_hours = (value.total_seconds() / 60) / 60
            kwhs = (duration_in_hours * device_power_dict[key]) / 1000
            text_out += "using " + str(round(kwhs, 3)) + " KWh\n"
            if not cost_kwh_cent == None:
                cost_of_device = kwhs * cost_kwh_cent
                text_out += "At a cost of " + str(round((cost_of_device /100) , 2)) + "\n"
        text_out += "\n"

    return text_out
예제 #9
0
    else:
        msg += ("      !!               NO lamp SET             !!\n")
        msg += ("      !!  run config program or edit config.txt  !!\n")
        msg += ("      !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n")
        pigrow_defs.write_log(script, 'Failed - due to none set in config',
                              switch_log)
        return msg

    msg += ("      ##            by switching GPIO " + str(gpio_pin) + " to " +
            gpio_pin_on + "  ##\n")
    msg += ("      #############################################\n")
    pigrow_defs.write_log(script, 'lamp turned on', switch_log)
    return msg


if __name__ == '__main__':

    ### default settings
    dirlocs_path = homedir + "/Pigrow/config/dirlocs.txt"
    if os.path.isfile(dirlocs_path):
        loc_dic = pigrow_defs.load_locs(dirlocs_path)
        set_dic = pigrow_defs.load_settings(
            loc_dic['loc_settings'],
            err_log=loc_dic['err_log'],
        )
        msg = lamp_on(set_dic, loc_dic['loc_switchlog'])
        print(msg)
    else:
        print("!!!! Locations file does not exist at " + dirlocs_path)