라즈베리파이 블루투스 통신
관련 패키지 설치
- sudo apt-get install -y bluetooth blueman bluez
- sudo apt-get install -y python-bluetooth
- pip3 install pybluez
- sudo reboot
블루투스 장치 페어링
$ bluetoothctl
[bluetooth]# scan on
Discovery started
[CHG] Controller DC:A6:32:AC:76:8E Discovering: yes
[NEW] Device E4:7D:BD:A4:DD:C5 [TV] Samsung Q7 Series (55)
[NEW] Device 98:D3:51:F9:42:AE HC-05
[NEW] Device 3C:77:E6:CD:A7:FE DESKTOP-K3PUDE3
[NEW] Device 00:51:ED:8D:47:1B S60UPI
[bluetooth]# pair 98:D3:51:F9:42:AE
[CHG] Device 98:D3:51:F9:42:AE Connected: yes
Request PIN code
[agent] Enter PIN code: 1234
[CHG] Device 98:D3:51:F9:42:AE UUIDs: 00001101-0000-1000-8000-00805f9b34fb
[CHG] Device 98:D3:51:F9:42:AE ServicesResolved: yes
[CHG] Device 98:D3:51:F9:42:AE Paired: yes
Pairing successful
[bluetooth]# agent on
[bluetooth]# default-agent
[bluetooth]# exit
라즈베리파이 -> 아두이노
bt_ex01.py
from bluetooth import *
LINE_END = "\r\n"
# Create the client socket
client_socket=BluetoothSocket( RFCOMM )
client_socket.connect(("00:18:91:D7:67:13", 1)) # 접속
try:
while True:
msg = input("Send : ") + LINE_END
client_socket.send(msg) # 전송
except KeyboardInterrupt:
print("Finished")
client_socket.close()
아두이노 -> 라즈베리파이
bt_ex02.py
from bluetooth import *
LINE_END = "\r\n"
# Create the client socket
client_socket=BluetoothSocket( RFCOMM )
client_socket.connect(("00:18:91:D7:67:13", 1)) # 접속
try:
while True:
msg = input("Send : ") + LINE_END
client_socket.send(msg) # 전송
msg = client_socket.recv(1024) # 수신
print(f"recived message : {msg}")
except KeyboardInterrupt:
print("Finished")
client_socket.close()
딜레이가 없어서 제대로 전달되지 않음
btsocket.py
from bluetooth import *
class BtSocket(BluetoothSocket) :
def __init__(self, *args): # 위치기반 매개변수
super().__init__(*args)
self.buf = ''
def readline(self):
ix = self.buf.find('\r\n') # 개행문자 체크
if ix != -1 :
line = self.buf[:ix]
self.buf = self.buf[ix+2:]
return line
self.buf += self.recv(1024).decode()
return self.readline()
bt_ex02_2.py
from bluetooth import *
from btsocket import BtSocket
LINE_END = "\r\n"
# Create the client socket
# client_socket=BluetoothSocket( RFCOMM )
client_socket=BtSocket( RFCOMM )
client_socket.connect(("00:18:91:D7:67:13", 1)) # 접속
try:
while True:
msg = input("Send : ") + LINE_END
client_socket.send(msg) # 전송
# msg = client_socket.recv(1024) # 수신
msg = client_socket.readline()
print(f"recived message : {msg}")
except KeyboardInterrupt:
print("Finished")
client_socket.close()
서보 모터 제어
- AngularServo
- 각도로 서보모터를 조정하는 클래스
- 180도 조정 범위 설정
- AngularServo(25, min_angle=-90, max_angle=90, min_pulse_width=0.0006, max_pulse_width=0.0024)
아두이노 블루투스 컨트롤러 연계
bt_ex03.py
from btsocket import BtSocket
from bluetooth import *
from gpiozero import AngularServo
servo = AngularServo(25, min_angle=-90, max_angle=90,
min_pulse_width=0.0006, max_pulse_width=0.0024)
def control(tokens):
command = int(tokens[0])
if command == 0 : # 주행모드
x = int(tokens[1])
y = int(tokens[2])
elif command == 1 : #카메라 모드
angle = int(tokens[1])
servo.angle = angle
RFADDR = "00:18:91:D7:67:13"
client_socket=BtSocket( RFCOMM )
client_socket.connect((RFADDR, 1))
try:
while True:
line = client_socket.readline() # 동기 함수
print(line)
control(line.split(','))
except KeyboardInterrupt:
print("Finished")
client_socket.close()
아두이노 - 라즈베리파이 간 블루투스 송수신 및 서보모터 동작 확인
'IoT 디바이스 활용 > Raspberry Pi' 카테고리의 다른 글
Raspberry Pi - 자동차 제어 (rccar.py) (0) | 2020.10.19 |
---|---|
Raspberry Pi - 오디오 녹음 및 재생 (0) | 2020.10.19 |
Raspberry Pi - 카카오 음성합성, 음성인식 (0) | 2020.10.19 |
Raspberry Pi - python-picamera - 이미지 프로세싱 (picam.py) (0) | 2020.10.19 |
Raspberry Pi - python-picamera - 동영상 촬영 (0) | 2020.10.19 |
댓글