+++ 221020/edgex/Temp_Hum_Sensor.py
... | ... | @@ -0,0 +1,37 @@ |
| 1 | +import sys, time, requests, json | |
| 2 | +import Adafruit_DHT | |
| 3 | + | |
| 4 | +sensor = Adafruit_DHT.DHT11 | |
| 5 | +pin = 11 | |
| 6 | + | |
| 7 | +#edgexip = "192.168.0.131" | |
| 8 | + | |
| 9 | +try: | |
| 10 | + | |
| 11 | + while True : | |
| 12 | + | |
| 13 | + rawHum, rawTemp = Adafruit_DHT.read_retry(sensor, pin) | |
| 14 | + | |
| 15 | + if rawHum is not None and rawTemp is not None : | |
| 16 | + #print("Temperature = {0:0.1f}*C Humidity = {1:0.1f}%".format(rawHum, rawTemp)) | |
| 17 | + | |
| 18 | +# urlTemp = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/temperature' % edgexip | |
| 19 | +# urlHum = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/humidity' % edgexip | |
| 20 | + | |
| 21 | + humval = str(rawHum) | |
| 22 | + tempval = str(rawTemp) | |
| 23 | + | |
| 24 | +# headers = {'content-type': 'application/json'} | |
| 25 | +# response = requests.post(urlTemp, data=json.dumps(int(rawTemp)), headers=headers,verify=False) | |
| 26 | +# response = requests.post(urlHum, data=json.dumps(int(rawHum)), headers=headers,verify=False) | |
| 27 | + | |
| 28 | + print("Temp: %s\N{DEGREE SIGN}C, humidity: %s%%" % (tempval, humval)) | |
| 29 | + | |
| 30 | + else : | |
| 31 | + print('Read error') | |
| 32 | + time.sleep(100) | |
| 33 | +except KeyboardInterrupt: | |
| 34 | + print("Terminated by Keyboard") | |
| 35 | + | |
| 36 | +finally: | |
| 37 | + print("End of Program") |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?