Skip to content

Instantly share code, notes, and snippets.

Created March 28, 2015 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/53d286b95a6fa99ac9ae to your computer and use it in GitHub Desktop.
Save anonymous/53d286b95a6fa99ac9ae to your computer and use it in GitHub Desktop.
temperature reading from DHT22 and putting onto RabbitMq queue
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