Skip to content

Instantly share code, notes, and snippets.

@joelthchao
Last active March 18, 2018 11:13
Show Gist options
  • Save joelthchao/d7b18f06c624ebc7a0c8bd816bd030fa to your computer and use it in GitHub Desktop.
Save joelthchao/d7b18f06c624ebc7a0c8bd816bd030fa to your computer and use it in GitHub Desktop.
DHT
#!/home/pi/miniconda3/bin/python
from datetime import datetime
import sys
import Adafruit_DHT
def main():
sensor = Adafruit_DHT.AM2302
pin = 4
record_file = '/home/pi/joel/dht_monitor/record/dht.csv' # TODO: change to your own path
timestr = datetime.now().strftime('%Y-%m-%d-%H:%M:%S')
humidity = None
temperature = None
try:
humidity, temperature = Adafruit_DHT.read_retry(
sensor, pin, retries=5, delay_seconds=1)
except ValueError as e:
print('Encounter sensor problem')
with open(record_file, 'a') as f:
print('{},{},{}'.format(timestr, humidity, temperature), file=f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment