Esempio n. 1
0
def do_update():
    # in case of error, retry after brief pause
    for retry in (3, 1):
        try:
            timestamp = time.time()
            ambient = temp_sensor.TempSensor("28-0000052f7386")
            freezer = temp_sensor.TempSensor("28-0000055fc26e")
            fridge = temp_sensor.TempSensor("28-0000052f91b1")
            ambient_temp = ambient.get_temperature()
            freezer_temp = freezer.get_temperature()
            fridge_temp = fridge.get_temperature()
            rrdtool.update(
                "/var/1w_files/templog.rrd", "%d:%s:%s:%s" %
                (timestamp, ambient_temp, freezer_temp, fridge_temp))
        except:
            logging.exception("retry in %is because of: ", retry)
            #time.sleep(retry * 1000)
            time.sleep(100)

        # control section
        gpio = gpio_sensor.Gpio("gpio27")

        # if fridge > 4.4C turn off light

        if fridge_temp > 4.4:
            gpio.put_value("0")

        else:

            # otherwise fridge is < 4.4 C
            if freezer_temp > -15:
                # if freezer > -15C turn on light  (warm fridge to start cooling)
                gpio.put_value("1")
            else:
                if freezer_temp < -20:
                    # if freezer < -20C turn off light  (want to drift down to -24 or so)
                    gpio.put_value("0")

        # return
        return
Esempio n. 2
0
def produceData():
    global current
    global client
    global light

    # email configuration
    sender_email = "Your Fridge <*****@*****.**>"
    receiver_email = "*****@*****.**"

    # threshold temp in C
    EMAIL_FRIDGE_THRESH = 7 
    EMAIL_FREEZER_THRESH = -10
    # time in seconds that threshold must be exceeded
    EMAIL_THRESHOLD_TIME = 1200
    # min alert email interval in seconds
    EMAIL_ALERT_INTERVAL = 86400 


    # informative message template
    text = """\
From: {}
To: {}
Subject: HIGH TEMPERATURE ALERT

Hi,

On {}, the fridge was {} deg C and the freezer was {} deg C.

Click http://10.0.0.8:8080/view?scale=day for more information.

"""
    """
    thread to aquire samples continuously
    """
    
    #file = open("/var/1w_files/log.csv", "a")

    # bootstrap timers (take/send readings immediately on start)
    timestamp = time.time() - 5
    cayenne_timestamp = time.time() - 280

    # create sensor/control objects, GPIO and 1wire
    light = gpio_sensor.Gpio("gpio27")
    switch = gpio_sensor.Gpio("gpio22")
    ambient = temp_sensor.TempSensor("28-0000052f7386")
    freezer = temp_sensor.TempSensor("28-0000055fc26e")
    fridge = temp_sensor.TempSensor("28-0000052f91b1")

    # timers for alarm threshold & alert rate limiting
    threshold_timestamp = time.time()
    last_alert_timestamp = 0  # send alert immediately after threshold time

    while(True):

      if (time.time()-timestamp) > 5.0 :
        timestamp = time.time()
        # grab temperature data. skip sensor in case of error
        try:
            ambient_temp = ambient.get_temperature()
        except Exception:
            pass

        try:
            freezer_temp = freezer.get_temperature()
        except Exception:
            pass

        try:
            # fridge thermometer has offset
            #fridge_temp = fridge.get_temperature() - 3.333
            # fridge temp offset dhanged 12/25/2019
            fridge_temp = fridge.get_temperature() + 2.000
        except Exception:
            pass

        light_state = light.get_value()
        switch_state = switch.get_value()

        # light control logic
        FDefrost = freezer_temp >= -6.0  # defrost mode in freezer
        FHigh = (freezer_temp < -6.0) & (freezer_temp > -15.0)  # "high"
        FLow = freezer_temp <= -15.0   # freezer temp is "low"

        RHigh = fridge_temp > 8.0          # fridge temp is "high"
        RBand = (fridge_temp <= 8.0) & (fridge_temp >= 1.5)  # "band"
        RLow = fridge_temp < 1.5          # fridge temp is "low"

        Door = switch_state == 1

        control = (FHigh & RBand) | RLow | Door
        light_state = to_int(control)
        light.put_value(light_state)

        current = {
          "time": timestamp,
          "ambient": ambient_temp,
          "freezer": freezer_temp,
          "fridge": fridge_temp,
          "light": light_state,
          "switch": switch_state,
        }

        print("Producer: "), 
        print(current)

        #update rrdtool database.  rrdtool is used for webpage graphics
        rrdtool.update("/var/1w_files/templog.rrd", \
                 "%d:%s:%s:%s:%s:%s" % (timestamp,  \
                                     ambient_temp,  \
                                     freezer_temp,  \
                                     fridge_temp,   \
                                     light_state,   \
                                     switch_state))

        # update element in queue for socketserver
        try:
          q.get(False)  # empty the old queue element
        except Exception:
          pass          # queue was empty already

        q.put(current, False)  # insert new element

        # log all data to file
        #foo = ("%d,%s,%s,%s,%s\n") % ( current["time"], current["freezer"], current["fridge"], current["ambient"], current["light"] )
        #file.write(foo)
        #file.flush()

        # update cayenne about every 5 minutes
        if ((time.time() - cayenne_timestamp) > 300):
          print("Publish to cayenne")
          cayenne_timestamp = time.time()
          client.celsiusWrite(1, ambient_temp)
          client.celsiusWrite(2, freezer_temp)
          client.celsiusWrite(3, fridge_temp)
          client.virtualWrite(4, light_state, dataType="digital_actuator", dataUnit="d")
          client.loop()

        # check for alarm condition and possibly send email
        if ( (freezer_temp < EMAIL_FREEZER_THRESH) and (fridge_temp < EMAIL_FRIDGE_THRESH) ):
          # everything normal, reset threshold exceeded time to current time
          threshold_timestamp = time.time()

        else:
          # we have a problem, maybe send email
          if (( (time.time() - threshold_timestamp) > EMAIL_THRESHOLD_TIME) and (( time.time() - last_alert_timestamp) > EMAIL_ALERT_INTERVAL) ):
            # send email, threshold exceeded and alert interval exceeded
            print("Send alert email now!");
            proc = Popen(['/usr/sbin/sendmail','-t','-oi'], stdin=PIPE)
            print(text.format(sender_email,receiver_email,time.strftime("%c"),fridge_temp,(fridge_temp*C_TO_F+32),freezer_temp,(freezer_temp*C_TO_F+32)).encode('utf8'))
            proc.communicate(text.format(sender_email,receiver_email,time.strftime("%c"),fridge_temp,freezer_temp).encode('utf8'))
            proc.wait()
            # we sent an alert, so reset rate-limiting timer 
            last_alert_timestamp = time.time()

# end of while(True)



      else:
        # check for cayenne publish or commands
        client.loop()
        time.sleep(0.1)
Esempio n. 3
0
#!/usr/bin/python
# this code runs every minute to collect stats and control the light buld

import logging
import logging.handlers
#import rrdtool
import temp_sensor
import gpio_sensor
import time
import sys

light = gpio_sensor.Gpio("gpio27")
switch = gpio_sensor.Gpio("gpio22")


def do_update():
    # grab temperature data. in case of error, retry after brief pause
    #  for retry in (5, 1):
    #    try:
    #      timestamp = time.time()
    #      ambient = temp_sensor.TempSensor("28-0000052f7386")
    #      freezer = temp_sensor.TempSensor("28-0000055fc26e")
    #      fridge = temp_sensor.TempSensor("28-0000052f91b1")
    #      ambient_temp = ambient.get_temperature()
    #      freezer_temp = freezer.get_temperature()
    #      fridge_temp = fridge.get_temperature()
    #    except:
    #      logging.exception("retry in %is because of: ", retry)
    #      time.sleep(1)

    # control section
Esempio n. 4
0
def produceData():
    global current
    """
    thread to aquire samples continuously
    """
    
    file = open("/var/1w_files/log.csv", "a")

    timestamp = time.time() - 5
    light = gpio_sensor.Gpio("gpio27")
    switch = gpio_sensor.Gpio("gpio22")
    ambient = temp_sensor.TempSensor("28-0000052f7386")
    freezer = temp_sensor.TempSensor("28-0000055fc26e")
    fridge = temp_sensor.TempSensor("28-0000052f91b1")

    while(True):
      if (time.time()-timestamp) > 5.0 :
        timestamp = time.time()
        # grab temperature data. in case of error, retry after brief pause
        #for retry in (5, 1):
        try:
            ambient_temp = ambient.get_temperature()
        except Exception:
            pass
            #print("retry %is: ambient", retry)
            #time.sleep(0.5)

        #for retry in (5, 1):
        try:
            freezer_temp = freezer.get_temperature()
        except Exception:
            pass
            #print("retry %is: ambient", retry)
            #time.sleep(0.5)

        #for retry in (5, 1):
        try:
            fridge_temp = fridge.get_temperature()
        except Exception:
            pass
            #print("retry %is: ambient", retry)
            #time.sleep(0.5)

        light_state = light.get_value()
        switch_state = switch.get_value()

        FDefrost = freezer_temp >= 0.0  # defrost mode in freezer
        FHigh = (freezer_temp < 0.0) & (freezer_temp > -12.0)  # "high"
        FLow = freezer_temp <= -12.0   # freezer temp is "low"

        RHigh = fridge_temp > 6.0          # fridge temp is "high"
        RBand = (fridge_temp <= 6.0) & (fridge_temp > 2.0)  # "band"
        RLow = fridge_temp <= 2.0          # fridge temp is "low"

        Door = switch_state == 1

        control = (FHigh & RBand) | RLow | Door
        light_state = to_int(control)
        light.put_value(light_state)

        current = {
          "time": timestamp,
          "ambient": ambient_temp,
          "freezer": freezer_temp,
          "fridge": fridge_temp,
          "light": light_state,
          "switch": switch_state,
        }

        print("Producer: "), 
        print(current)

        rrdtool.update("/var/1w_files/templog.rrd", \
                 "%d:%s:%s:%s:%s:%s" % (timestamp,  \
                                     ambient_temp,  \
                                     freezer_temp,  \
                                     fridge_temp,   \
                                     light_state,   \
                                     switch_state))

        try:
          q.get(False)  # empty the old queue element
        except Exception:
          pass          # queue was empty already

        q.put(current, False)  # insert new element

        foo = ("%d,%s,%s,%s,%s\n") % ( current["time"], current["freezer"], current["fridge"], current["ambient"], current["light"] )
        file.write(foo)
        file.flush()

      else:
         time.sleep(0.05)