Created
December 4, 2018 07:22
-
-
Save mephistoc/30a40a2518301fb0bcdbfcf8e0f82a26 to your computer and use it in GitHub Desktop.
Example for capture and publish temperature and humidity value from DHT sensor on Raspberry Pi with Adafruit Python library.
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
#!/usr/bin/python | |
import sys | |
import time | |
import Adafruit_DHT | |
import requests | |
import json | |
def main(): | |
try: | |
while True: | |
h, t = Adafruit_DHT.read_retry(11,4) | |
print 'Temp: {0:0.1f} C, Humidity: {1:0.1f} %'.format(t,h) | |
_publish_adafruit(t,h) | |
time.sleep(10) | |
except KeyboardInterrupt: | |
pass | |
def _publish_adafruit(temp, hum): | |
try: | |
adaUrl = 'https://io.adafruit.com/api/v2/{YOUR_USER_NAME}/groups/{GROUP_KEY}/data' | |
adaHeaders = {'X-AIO-Key':'{YOUR AIO-KEY}', | |
'Content-Type': 'application/json'} | |
adaPayload = {'feeds':[ | |
{'key':'{TEMPERATURE_SENSOR_KEY}','value':temp}, | |
{'key':'{HUMIDITY_SENSOR_KEY}','value':hum}]} | |
#print('Before Adafruit call') | |
adaRep = requests.post(adaUrl, headers=adaHeaders, data=json.dumps(adaPayload)) | |
print('Call Adafruit API success.') | |
return adaRep | |
except BaseException: | |
print('Adafrui call failed.') | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment