Created
March 28, 2015 23:27
-
-
Save anonymous/53d286b95a6fa99ac9ae to your computer and use it in GitHub Desktop.
temperature reading from DHT22 and putting onto RabbitMq queue
This file contains 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