Guest User

Untitled

a guest
Jan 24th, 2016
208
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Written by David Neuy
  2. # Version 0.1.0 @ 03.12.2014
  3. # This script was first published at: http://www.home-automation-community.com/
  4. # You may republish it as is or publish a modified version only when you
  5. # provide a link to 'http://www.home-automation-community.com/'.
  6.  
  7. # Lightly Modified by Sean Kelley
  8. # To include second sensor for soil temperature
  9. # No need to provide a link to Sean @ http://www.brickstobits.com
  10.  
  11. #install dependency with 'sudo easy_install apscheduler' NOT with 'sudo pip install apscheduler'
  12. import os, sys, Adafruit_DHT, time
  13. from datetime import datetime, date
  14. from apscheduler.schedulers.background import BackgroundScheduler
  15.  
  16. #load drivers for soil temp sensor
  17. os.system('modprobe w1-gpio')
  18. os.system('modprobe w1-therm')
  19.  
  20. # soil sensor output file
  21. soil_temp_sensor = '/sys/bus/w1/devices/28-000006c3a429/w1_slave'
  22.  
  23. #air temp sensor setup
  24. sensor = Adafruit_DHT.AM2302 #DHT11/DHT22/AM2302
  25. pin = 4
  26. sensor_name = "outside"
  27. hist_temperature_file_path = "sensor-values/temperature_" + sensor_name + "_log_" + str(date.today().year) + ".csv"
  28. hist_soil_temperature_file_path = "sensor-values/temperature_" + 'soil' + "_log_" + str(date.today().year) + ".csv"
  29. latest_temperature_file_path = "sensor-values/temperature_" + sensor_name + "_latest_value.csv"
  30. latest_soil_temp_file_path = "sensor-values/temperature_" + 'soil' + "_latest_value.csv"
  31. hist_humidity_file_path = "sensor-values/humidity_" + sensor_name + "_log_" + str(date.today().year) + ".csv"
  32. latest_humidity_file_path = "sensor-values/humidity_" + sensor_name + "_latest_value.csv"
  33. csv_header_temperature = "timestamp,temperature_in_fahrenheit\n"
  34. csv_header_soil_termperature = "timestamp,temperature_in_fahrenheit\n"
  35. csv_header_humidity = "timestamp,relative_humidity\n"
  36. csv_entry_format = "{:%Y-%m-%d %H:%M:%S},{:0.1f}\n"
  37. sec_between_log_entries = 60
  38. latest_humidity = 0.0
  39. latest_temperature = 0.0
  40. latest_soil_temperature = 0.0
  41. latest_value_datetime = None
  42.  
  43. # SOIL TEMP CODE functions
  44.  
  45. def temp_raw():
  46. f = open(soil_temp_sensor, 'r')
  47. lines = f.readlines()
  48. f.close()
  49. return lines
  50.  
  51. def read_soil_temp():
  52.  
  53. lines = temp_raw()
  54. while lines[0].strip()[-3:] != 'YES':
  55. time.sleep(0.2)
  56. lines = temp_raw()
  57. temp_output = lines[1].find('t=')
  58.  
  59. if temp_output != -1:
  60. temp_string = lines[1].strip()[temp_output+2:]
  61. temp_c = float(temp_string) / 1000.0
  62. temp_f = temp_c * 9.0 / 5.0 + 32.0
  63. return temp_f
  64.  
  65. # AIR TEMP/HUMIDTY functions
  66.  
  67. def write_header(file_handle, csv_header):
  68. file_handle.write(csv_header)
  69.  
  70. def write_value(file_handle, datetime, value):
  71. line = csv_entry_format.format(datetime, value)
  72. file_handle.write(line)
  73. file_handle.flush()
  74.  
  75. def open_file_ensure_header(file_path, mode, csv_header):
  76. f = open(file_path, mode, os.O_NONBLOCK)
  77. if os.path.getsize(file_path) <= 0:
  78. write_header(f, csv_header)
  79. return f
  80.  
  81. def write_hist_value_callback():
  82. write_value(f_hist_temp, latest_value_datetime, latest_temperature)
  83. write_value(f_hist_hum, latest_value_datetime, latest_humidity)
  84.  
  85. # added for soil temp
  86. write_value(f_hist_soil_temperature, latest_value_datetime, latest_soil_temperature)
  87.  
  88. def write_latest_value():
  89. with open_file_ensure_header(latest_temperature_file_path, 'w', csv_header_temperature) as f_latest_value: #open and truncate
  90. write_value(f_latest_value, latest_value_datetime, latest_temperature)
  91. with open_file_ensure_header(latest_humidity_file_path, 'w', csv_header_humidity) as f_latest_value: #open and truncate
  92. write_value(f_latest_value, latest_value_datetime, latest_humidity)
  93. # added for soil temp
  94. with open_file_ensure_header(latest_soil_temp_file_path, 'w', csv_header_soil_termperature) as f_latest_value: #open and truncate
  95. write_value(f_latest_value, latest_value_datetime, latest_soil_temperature)
  96.  
  97. # Mix of step one code and new soil code (step 2)
  98.  
  99. f_hist_temp = open_file_ensure_header(hist_temperature_file_path, 'a', csv_header_temperature)
  100. f_hist_hum = open_file_ensure_header(hist_humidity_file_path, 'a', csv_header_humidity)
  101.  
  102. #soil added
  103. f_hist_soil_temperature = open_file_ensure_header(hist_soil_temperature_file_path, 'a', csv_header_soil_termperature)
  104.  
  105. print "Ignoring first 2 sensor values to improve quality..."
  106. for x in range(2):
  107. Adafruit_DHT.read_retry(sensor, pin)
  108.  
  109. print "Creating interval timer. This step takes almost 2 minutes on the Raspberry Pi..."
  110. #create timer that is called every n seconds, without accumulating delays as when using sleep
  111. scheduler = BackgroundScheduler()
  112. scheduler.add_job(write_hist_value_callback, 'interval', seconds=sec_between_log_entries)
  113. scheduler.start()
  114. print "Started interval timer which will be called the first time in {0} seconds.".format(sec_between_log_entries);
  115.  
  116. try:
  117. while True:
  118. hum, temp = Adafruit_DHT.read_retry(sensor, pin)
  119. if hum is not None and temp is not None:
  120. ftemp = (temp *9.0) / 5.0 + 32.0
  121. latest_humidity, latest_temperature = hum, ftemp
  122. latest_value_datetime = datetime.today()
  123. latest_soil_temperature = read_soil_temp()
  124. write_latest_value()
  125.  
  126. time.sleep(1)
  127.  
  128. except (KeyboardInterrupt, SystemExit):
  129. scheduler.shutdown()
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×