한석현 한석현 05-23
update 2024.05.23
@0b5e5700a8a9c1f6fe20a63c61f33bc6a7631a16
 
random_data_push.py (added)
+++ random_data_push.py
@@ -0,0 +1,60 @@
+import time, sys, requests, json
+#import board
+#import adafruit_dht
+import random
+
+# 데이터 핀 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_c = random.uniform(35, 40)
+        temperature_f = temperature_c * (9 / 5) + 32
+        
+        #습도
+        #humidity = dhtDevice.humidity
+        humidity = random.uniform(20, 80)
+        
+        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()
+        print("Unexpected error:", error)
+        sys.exit()
+        raise error
+
+    time.sleep(2.0)
Add a comment
List