Created
March 29, 2015 06:03
-
-
Save jmkelly/4887e3c569e8ef1ded98 to your computer and use it in GitHub Desktop.
temperature reading from DHT22 and putting onto RabbitMq queue
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 Adafruit_DHT | |
import pika | |
import threading | |
from datetime import datetime | |
import uuid | |
import time | |
import json | |
sensor = Adafruit_DHT.DHT22 | |
pin = 4 | |
def read(): | |
humidity, temperature = Adafruit_DHT.read(sensor, pin) | |
current_datetime = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") | |
if temperature is not None and humidity is not None: | |
print 'temp: {0:0.1f}C humid: {1:0.1f}% at {2}'.format(temperature, humidity, current_datetime) | |
id = uuid.uuid4() | |
output={ | |
'id':str(id), | |
'created_at':current_datetime, | |
'temperature': temperature, | |
'humidity': humidity | |
} | |
return output | |
else: | |
raise ValueError('temperature and humidity data is nothing at {0}'.format(current_datetime)) | |
connection = pika.BlockingConnection() | |
channel = connection.channel() | |
channel.queue_declare(queue='temperatures', durable=True) | |
#read every 10 sec and queue up the reading | |
while True: | |
data = read() | |
channel.basic_publish(exchange='', routing_key='temperatures', | |
body=json.dumps(data), | |
properties=pika.BasicProperties(delivery_mode=2)) | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If the sensor is the BMP180 what parameters i need change ?