Beispiel #1
0
def get_warning_ranges_from_settings(loc_settings):
    try:
        set_dic = pigrow_defs.load_settings(loc_settings)
        toocold = int(set_dic['heater_templow'])
        toohot = int(set_dic['heater_temphigh'])
    except:
        toocold = 17
        toohot = 30
    dangercold = (float(toocold) / 100) * 85
    dangerhot = (float(toohot) / 100) * 115
    return toocold, dangercold, toohot, dangerhot
Beispiel #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 '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
Beispiel #3
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
Beispiel #4
0
 def load_settings():
     setting_path = homedir + "/Pigrow/config/pigrow_config.txt"
     if os.path.isfile(setting_path):
         set_dic = pigrow_defs.load_settings(setting_path)
     gpio_pin = ""
     gpio_pin_on = ""
     if 'gpio_' + device in set_dic:
         if not str(set_dic['gpio_' + device]).strip() == '':
             gpio_pin = int(set_dic['gpio_' + device])
             gpio_pin_on = set_dic['gpio_' + device + '_on']
             GPIO.setup(gpio_pin, GPIO.OUT)
     if 'pulse_' + device in set_dic:
         if not str(set_dic['pulse_' + device]).strip() == '':
             pulse = set_dic['pulse_' + device]
             try:
                 config.on_duration = int(pulse.split(":")[0])
                 config.off_duration = int(pulse.split(":")[1])
             except:
                 print(
                     "pulse_" + device +
                     "= needs to be set as on_time:off_time in pigrow_config.txt"
                 )
                 config.on_duration = 0
                 config.off_duration = 0
         else:
             print(
                 "pulse_" + device +
                 "= needs to be set with on_time:off_time in pigrow_config.txt"
             )
             config.on_duration = 0
             config.off_duration = 0
     else:
         print("pigrow_config.txt needs to have pulse_" + device +
               "= set with on_time:off_time")
         config.on_duration = 0
         config.off_duration = 0
     return gpio_pin, gpio_pin_on
Beispiel #5
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)
Beispiel #6
0
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"

for argu in sys.argv[1:]:
    thearg = str(argu).split('=')[0]
    thevalue = str(argu).split('=')[1]
    if thearg == 'pin' or thearg == 'gpio':
        try:
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
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
Beispiel #9
0
        "Button not identified, please include name= in the commandline arguments."
    )
    sys.exit()

# Read the settings file
homedir = os.getenv("HOME")
sys.path.append(homedir + '/Pigrow/scripts/')
try:
    import pigrow_defs
    script = 'button_watcher.py'
except:
    print("pigrow_defs.py not found, unable to continue.")
    print("make sure pigrow software is installed correctly")
    sys.exit()
loc_dic = pigrow_defs.load_locs(homedir + '/Pigrow/config/dirlocs.txt')
pigrow_settings = pigrow_defs.load_settings(loc_dic['loc_settings'])

# Read the sensor info from the settigns file
button_type = None
button_log = None
button_loc = None
for key, value in list(pigrow_settings.items()):
    button_key = "button_" + button_name
    if button_key in key:
        if "type" in key:
            button_type = value
        elif "log" in key:
            button_log = value
        elif "loc" in key:
            button_loc = value
Beispiel #10
0
def show_info():
    pigrow_config_path = homedir + "/Pigrow/config/pigrow_config.txt"
    '''
    # Check lamp
    '''
    # check config file
    config_dict = pigrow_defs.load_settings(pigrow_config_path)
    lamp_config_set = False
    lamp_msg = ""
    if "gpio_lamp" in config_dict:
        lamp_config_set = True
        # Lamp on
        if "time_lamp_on" in config_dict:
            time_lamp_on = config_dict["time_lamp_on"]
            if ":" in time_lamp_on:
                lamp_on_hour = int(time_lamp_on.split(":")[0])
                lamp_on_min = int(time_lamp_on.split(":")[1])
            else:
                lamp_msg += "Lamp on time not valid"
                lamp_config_set = False
        else:
            lamp_msg += "Lamp on time not set in config dict"
            lamp_config_set = False

        # Lamp off
        if "time_lamp_off" in config_dict:
            time_lamp_off = config_dict["time_lamp_off"]
            if ":" in time_lamp_off:
                lamp_off_hour = int(time_lamp_off.split(":")[0])
                lamp_off_min = int(time_lamp_off.split(":")[1])
            else:
                lamp_msg += "Lamp off time not valid\n"
                lamp_config_set = False
        else:
            lamp_msg += "Lamp off time not set\n"
            lamp_config_set = False
    else:
        lamp_msg += "No lamp set in pigrow_config.txt\n"

    # Time gap
    if lamp_config_set == True:
        on_time = datetime.time(int(lamp_on_hour), int(lamp_on_min))
        off_time = datetime.time(int(lamp_off_hour), int(lamp_off_min))
        aday = datetime.timedelta(days=1)
        if on_time > off_time:
            dateoff = (
                (datetime.datetime.combine(datetime.date.today(), off_time) +
                 aday))
        else:
            dateoff = ((datetime.datetime.combine(datetime.date.today(),
                                                  off_time)))

        length_lamp_on = (
            dateoff -
            datetime.datetime.combine(datetime.date.today(), on_time))
        # message
        lamp_msg += "Lamp turning on at " + str(
            on_time)[:-3] + " and off at " + str(off_time)[:-3]
        lamp_msg += "\n     (duration " + str(
            length_lamp_on)[:-3] + " on, " + str(
                aday - length_lamp_on)[:-3] + " off)\n"

    # check cron

    out = os.popen("crontab -l").read()
    cronlines = out.splitlines()
    on_cron = 0
    off_cron = 0

    def check_time_is_valid(line, hour, min):
        cron_stars = line.split(' ')
        cron_min = cron_stars[0]
        cron_hour = cron_stars[1]
        if not cron_stars[2] == "*" and not cron_stars[
                3] == "*" and not cron_stars[4] == "*":
            return "Cron contains lamp time not set as daily\n", "bad", ""
        else:
            if hour == int(cron_hour) and min == int(cron_min):
                return "", "good", "match"
            return "", "good", "no match"

    if lamp_config_set == True:
        for line in cronlines:
            if "lamp_on.py" in line:
                msg, valid, on_match = check_time_is_valid(
                    line, lamp_on_hour, lamp_on_min)
                if valid == "good":

                    on_cron += 1
            elif "lamp_off.py" in line:
                msg, valid, off_match = check_time_is_valid(
                    line, lamp_off_hour, lamp_off_min)
                if valid == "good":
                    off_cron += 1
                lamp_msg += msg

    if on_cron == 1 and off_cron == 1:
        if on_match == "match" and off_match == "match":
            lamp_msg += "Config and Cron lamp timing synced."
        else:
            lamp_msg += "Warning! Config and Cron lamp timing not synced."
    # erors
    elif on_cron > 1 or off_cron > 1:
        lamp_msg += "Too many lamp switchings in cron to use all features."
    else:
        if lamp_config_set == True:
            lamp_msg += "Lamp on and off times not set in cron."

    return lamp_msg