Last active
November 23, 2019 12:26
-
-
Save rajasiman/cef147470c689e3fd4a97e73edcb7949 to your computer and use it in GitHub Desktop.
DHT22 Logger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import time | |
| import socket | |
| import Adafruit_DHT | |
| DHT_SENSOR = Adafruit_DHT.DHT22 | |
| DHT_PIN = 4 | |
| try: | |
| f = open('/home/pi/sensorlogs/DHT22.csv', 'a+') | |
| if os.stat('/home/pi/sensorlogs/DHT22.csv').st_size == 0: | |
| f.write('Date,Time,Temperature,Humidity,hostname\r\n') | |
| except: | |
| pass | |
| while True: | |
| humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN) | |
| if humidity is not None and temperature is not None: | |
| f.write('{0},{1},{2:0.1f}*C,{3:0.1f},{4}%\r\n'.format(time.strftime('%m/%d/%y'), time.strftime('%H:%M'), temperature, humidity, socket.gethostname())) | |
| else: | |
| print("Failed to retrieve data from humidity sensor") | |
| time.sleep(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment