-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (53 loc) · 1.63 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
import json
import logging
import time
import requests
from lib import serial
logging.basicConfig(
level=logging.INFO,
format='[%(levelname)s] [%(asctime)s] %(message)s',
handlers=[logging.StreamHandler()],
)
TARGET_URL = 'http://localhost:8000/sensor/radar/'
def main():
# Django 서버가 실행 중인지 확인
try:
res = requests.get(TARGET_URL)
except requests.exceptions.ConnectionError:
logging.error(
f'"{TARGET_URL}"에 연결할 수 없습니다.'
' Django 서버를 실행한 후 다시 시도해주세요.'
' 서버를 실행하는 방법은 다음과 같습니다:'
'\n\t1. iotProject 디렉터리로 이동'
'\n\t2. python manage.py runserver'
)
return
# 시리얼 포트 연결
port = serial.select_port()
ser = serial.get_serial(port)
# 데이터 전송
while True:
# 센서에서 데이터 수신
data = ser.json()
logging.info(f'{json.dumps(data)}')
if not data:
continue
# 서버에 데이터 전송
res = requests.post(TARGET_URL, data={
'x': 1,
'y': data['Mv'],
'echo_cm': data['L_Dist'],
})
res.raise_for_status()
logging.info(f'{res.status_code} {res.json()}')
res = requests.post(TARGET_URL, data={
'x': 0,
'y': data['Mv'],
'echo_cm': data['R_Dist'],
})
res.raise_for_status()
logging.info(f'{res.status_code} {res.json()}')
time.sleep(0.2)
if __name__ == '__main__':
main()