Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reading temperature and humidity data and send it to Favoriot IoT platform.
from gpiozero import Buzzer
from time import sleep
import Adafruit_DHT
import requests
import json
from decimal import Decimal
APIKEY = "YOUR FAVORIOT APIKEY"
DEVICE_DEV_ID = "Your Device Developer ID"
buzzer = Buzzer(26)
SEN_TYPE = Adafruit_DHT.DHT22
SEN_PIN = 18
url = "https://api.favoriot.com/v1/streams"
headers = {
'apikey': APIKEY,
'content-type': "application/json"
}
try:
while 1:
humidity, temperature = Adafruit_DHT.read_retry(SEN_TYPE, SEN_PIN)
if humidity is not None and temperature is not None:
root = {}
root["device_developer_id"] = DEVICE_DEV_ID
data = {}
data["temperature"] = round(temperature, 1)
data["humidity"] = round(humidity, 1)
root["data"] = data
body = json.dumps(root)
print body
response = requests.request("POST", url, headers=headers, data=body)
print response.text
print ""
buzzer.beep(0.1, 0.1, 1)
else:
print "Failed to get reading. Try again!"
buzzer.beep(0.2, 0.2, 3)
sleep(2)
except KeyboardInterrupt:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment