+++ 221020/edgex/dht_push.py
... | ... | @@ -0,0 +1,50 @@ |
1 | +import time, sys, requests, json | |
2 | +import board | |
3 | +import adafruit_dht | |
4 | + | |
5 | +# 데이터 핀 3번을 사용면, GPIO2로 설정 | |
6 | +pin = board.D2 | |
7 | +dhtDevice = adafruit_dht.DHT11(pin) | |
8 | + | |
9 | +# EdgeX server ip | |
10 | +edgexip = "192.168.1.191" | |
11 | + | |
12 | +while True: | |
13 | + try: | |
14 | + # 온도 및 습도 값을 읽어옴 | |
15 | + temperature_c = dhtDevice.temperature | |
16 | + temperature_f = temperature_c * (9 / 5) + 32 | |
17 | + humidity = dhtDevice.humidity | |
18 | + print( | |
19 | + "온도: {:.1f} F / {:.1f} C 습도: {}% ".format( | |
20 | + temperature_f, temperature_c, humidity | |
21 | + ) | |
22 | + ) | |
23 | + | |
24 | + if temperature_c is not None and humidity is not None : | |
25 | + urlTemp = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/temperature' % edgexip | |
26 | + urlHum = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/humidity' % edgexip | |
27 | + | |
28 | + headers = {'content-type': 'application/json'} | |
29 | + response = requests.post(urlTemp, data=json.dumps(int(temperature_c)), headers=headers,verify=False) | |
30 | + response = requests.post(urlHum, data=json.dumps(int(humidity)), headers=headers,verify=False) | |
31 | + | |
32 | + | |
33 | + | |
34 | + | |
35 | + else: | |
36 | + print('Read error') | |
37 | + time.sleep(100) | |
38 | + | |
39 | + | |
40 | + except RuntimeError as error: | |
41 | + # 오류 발생 시 계속 진행 | |
42 | + print(error.args[0]) | |
43 | + time.sleep(2.0) | |
44 | + continue | |
45 | + except Exception as error: | |
46 | + # 예기치 않은 오류 시 종료 | |
47 | + dhtDevice.exit() | |
48 | + raise error | |
49 | + | |
50 | + time.sleep(2.0) |
+++ 221020/edgex/dht_test.py
... | ... | @@ -0,0 +1,31 @@ |
1 | +import time | |
2 | +import board | |
3 | +import adafruit_dht | |
4 | + | |
5 | +# 데이터 핀 3번을 사용면, GPIO2로 설정 | |
6 | +pin = board.D2 | |
7 | +dhtDevice = adafruit_dht.DHT11(pin) | |
8 | + | |
9 | +while True: | |
10 | + try: | |
11 | + # 온도 및 습도 값을 읽어옴 | |
12 | + temperature_c = dhtDevice.temperature | |
13 | + temperature_f = temperature_c * (9 / 5) + 32 | |
14 | + humidity = dhtDevice.humidity | |
15 | + print( | |
16 | + "온도: {:.1f} F / {:.1f} C 습도: {}% ".format( | |
17 | + temperature_f, temperature_c, humidity | |
18 | + ) | |
19 | + ) | |
20 | + | |
21 | + except RuntimeError as error: | |
22 | + # 오류 발생 시 계속 진행 | |
23 | + print(error.args[0]) | |
24 | + time.sleep(2.0) | |
25 | + continue | |
26 | + except Exception as error: | |
27 | + # 예기치 않은 오류 시 종료 | |
28 | + dhtDevice.exit() | |
29 | + raise error | |
30 | + | |
31 | + time.sleep(2.0) |
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?