한석현 한석현 03-15
update 2024.03.15
@c419fb43e36ff3989c5cab2b2257c447f3024a85
 
221020/edgex/dht_push.py (added)
+++ 221020/edgex/dht_push.py
@@ -0,0 +1,50 @@
+import time, sys, requests, json
+import board
+import adafruit_dht
+
+# 데이터 핀 3번을 사용면, GPIO2로 설정
+pin = board.D2
+dhtDevice = adafruit_dht.DHT11(pin)
+
+# EdgeX server ip
+edgexip = "192.168.1.191"
+
+while True:
+    try:
+        # 온도 및 습도 값을 읽어옴
+        temperature_c = dhtDevice.temperature
+        temperature_f = temperature_c * (9 / 5) + 32
+        humidity = dhtDevice.humidity
+        print(
+            "온도: {:.1f} F / {:.1f} C    습도: {}% ".format(
+                temperature_f, temperature_c, humidity
+            )
+        )
+
+        if temperature_c is not None and humidity is not None :
+            urlTemp = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/temperature' % edgexip
+            urlHum  = 'http://%s:49986/api/v1/resource/Temp_and_Humidity_sensor_cluster_01/humidity' % edgexip
+
+            headers = {'content-type': 'application/json'}
+            response = requests.post(urlTemp, data=json.dumps(int(temperature_c)), headers=headers,verify=False)
+            response = requests.post(urlHum, data=json.dumps(int(humidity)), headers=headers,verify=False)
+
+
+
+
+        else:
+            print('Read error')
+            time.sleep(100)
+
+
+    except RuntimeError as error:
+        # 오류 발생 시 계속 진행
+        print(error.args[0])
+        time.sleep(2.0)
+        continue
+    except Exception as error:
+        # 예기치 않은 오류 시 종료
+        dhtDevice.exit()
+        raise error
+
+    time.sleep(2.0)
 
221020/edgex/dht_test.py (added)
+++ 221020/edgex/dht_test.py
@@ -0,0 +1,31 @@
+import time
+import board
+import adafruit_dht
+
+# 데이터 핀 3번을 사용면, GPIO2로 설정
+pin = board.D2
+dhtDevice = adafruit_dht.DHT11(pin)
+
+while True:
+    try:
+        # 온도 및 습도 값을 읽어옴
+        temperature_c = dhtDevice.temperature
+        temperature_f = temperature_c * (9 / 5) + 32
+        humidity = dhtDevice.humidity
+        print(
+            "온도: {:.1f} F / {:.1f} C    습도: {}% ".format(
+                temperature_f, temperature_c, humidity
+            )
+        )
+
+    except RuntimeError as error:
+        # 오류 발생 시 계속 진행
+        print(error.args[0])
+        time.sleep(2.0)
+        continue
+    except Exception as error:
+        # 예기치 않은 오류 시 종료
+        dhtDevice.exit()
+        raise error
+
+    time.sleep(2.0)
Add a comment
List