Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
computer:raspberry_pi_light_sensor [2020/07/13 10:05]
tdobes
computer:raspberry_pi_light_sensor [2020/07/13 11:22]
tdobes
Line 140: Line 140:
 import requests import requests
 import xml.etree.ElementTree as ET import xml.etree.ElementTree as ET
 +import datetime
 +import os
  
 ISY='​isy994-ems.williams.lcl'​ ISY='​isy994-ems.williams.lcl'​
Line 145: Line 147:
 ISY_VAR_TYPE=2 ISY_VAR_TYPE=2
 ISY_VAR_NAME='​outdoor_bright'​ ISY_VAR_NAME='​outdoor_bright'​
-SECS_TO_AVERAGE=5+ISY_SECS_TO_AVERAGE=5
 SAMPLES_PER_SECOND=8 SAMPLES_PER_SECOND=8
 +DATAFILE='/​mnt/​data/​Insteon home automation/​brightness_sensor_log.csv'​
 +DATAFILE_SECS_TO_AVERAGE=60
 DEBUG = False DEBUG = False
  
Line 155: Line 159:
     if var_def.attrib['​name'​] == var_name:     if var_def.attrib['​name'​] == var_name:
       return int(var_def.attrib['​id'​])       return int(var_def.attrib['​id'​])
 +
 +def write_to_file(value):​
 +  global DEBUG, DATAFILE
 +  if DEBUG: print('​...writing to file...',​ DATAFILE, value)
 +  try:
 +    f = open(DATAFILE,​ '​a+'​)
 +    f.seek(0, os.SEEK_END)
 +    if f.tell() == 0:
 +      # write header
 +      if DEBUG: print('​writing header to file')
 +      f.write('​Timestamp,​Sensor Value\n'​)
 +    f.write('​%s,​%.2f\n'​ % (datetime.datetime.now().isoformat(timespec='​seconds'​),​ value))
 +    f.close()
 +  except:
 +    print('​WARNING:​ Unable to write this data to file.  Uh-oh.'​)
 +
 +def submit_to_isy(isy_var_id,​ light):
 +  global DEBUG, ISY, ISY_VAR_TYPE,​ ISY_CREDS
 +  submit_vars = (ISY, ISY_VAR_TYPE,​ isy_var_id, light)
 +  if DEBUG: print('​...submitting to isy...',​ submit_vars)
 +  try:
 +    requests.get('​http://​%s/​rest/​vars/​set/​%d/​%d/​%d'​ % submit_vars,​ auth=ISY_CREDS)
 +  except:
 +    print('​WARNING:​ Unable to submit this data to ISY.  Uh-oh.'​)
  
 if __name__ == "​__main__":​ if __name__ == "​__main__":​
Line 162: Line 190:
   isy_var_id = isy_get_var_id(ISY,​ ISY_CREDS, ISY_VAR_TYPE,​ ISY_VAR_NAME)   isy_var_id = isy_get_var_id(ISY,​ ISY_CREDS, ISY_VAR_TYPE,​ ISY_VAR_NAME)
  
 +  lightavg = 0
 +  lightfileavg = 0
 +  lightavgitems = 0
 +  lightfileavgitems = 0
   while True:   while True:
-    ​lightavg = 0 +    light = veml7700.light 
-    for i in range(0, SECS_TO_AVERAGE * SAMPLES_PER_SECOND):​ +    if DEBUG: print('​...ambient light:',​ light) 
-      ​light = veml7700.light +    lightavg += light 
-      if DEBUG: print('​...ambient light:',​ light) +    ​lightavgitems += 
-      lightavg += light +    ​lightfileavg += light 
-      ​time.sleep(1/​SAMPLES_PER_SECOND) +    lightfileavgitems +1 
-    light = lightavg / (SECS_TO_AVERAGE * SAMPLES_PER_SECOND) +    if lightavgitems >= (ISY_SECS_TO_AVERAGE * SAMPLES_PER_SECOND): 
-    if DEBUG: print('​AVERAGE of ambient light:',​ light) +      ​submit_to_isy(isy_var_id, ​lightavg / lightavgitems
-    try+      ​lightavg = 0 
-      ​submit_vars = (ISY, ISY_VAR_TYPE, ​isy_var_id, ​light+      lightavgitems = 0 
-      if DEBUG: print('​..submitting to isy...',​ submit_vars+    ​if lightfileavgitems >= (DATAFILE_SECS_TO_AVERAGE * SAMPLES_PER_SECOND): 
-      ​requests.get('http://​%s/​rest/​vars/​set/​%d/​%d/​%d'​ % submit_vars,​ auth=ISY_CREDS+      ​write_to_file(lightfileavg ​lightfileavgitems
-    ​except:​ +      ​lightfileavg = 0 
-      ​print('​WARNING:​ Unable to submit this data to ISY ​Uh-oh.'​)+      ​lightfileavgitems = 0 
 +    time.sleep(1/​SAMPLES_PER_SECOND)
 EOF EOF
 chmod +x /​opt/​veml7700/​veml7700_to_isy.py chmod +x /​opt/​veml7700/​veml7700_to_isy.py
Line 183: Line 216:
 </​code>​ </​code>​
  
-===== Part 4: Autostart as Service =====+===== Part 4: Network storage (for data export) ===== 
 +<​code>​ 
 +mkdir -p /mnt/data 
 +echo >> /​etc/​fstab 
 +echo $'​memoryalpha:/​tank/​data\t/​mnt/​data\tnfs\t_netdev,​auto,​soft\t0\t0'​ >> /​etc/​fstab 
 +aptitude install nfs-common 
 +mount /mnt/data 
 +</​code>​ 
 + 
 +===== Part 5: Autostart as Service =====
 <​code>​ <​code>​
 adduser --system --home /​nonexistent --no-create-home --gecos "Light Sensor"​ sensor adduser --system --home /​nonexistent --no-create-home --gecos "Light Sensor"​ sensor
computer/raspberry_pi_light_sensor.txt · Last modified: 2020/09/08 14:30 by tdobes
Recent changes RSS feed Driven by DokuWiki Valid XHTML 1.0 Valid CSS