와이파이를 제대로 사용할 수 없는 등의 환경에 유용할 것 같은데.. 마냥 편하진 않다. 느리기도 하고...
0. bluetoothd 실행 옵션
bluez 5 부터 일부 커맨드라인 툴들이 deprecated 되었다.
hciconfig 는 바로 지원을 하는 것 같은데, SPP 추가를 위한 sdptool 이 작동하지 않음.
sudovi /lib/systemd/system/bluetooth.service
ExecStart=/usr/lib/bluetooth/bluetoothd -C
데몬파일을 열어서 이렇게 수정해주자.(마지막에 -C 옵션 추가)
sudo systemctl daemon-reload
sudo systemctl restart bluetooth
그 후 블루투스 서비스를 재시작해주면 된다.
1. SP profile 추가
RPi 의 블루투스 프로파일에 SPP, Serial Port Profile 을 추가해야한다.
sudo sdptool add SP
위 명령어로 추가 가능. 재부팅, 서비스를 재시작 등을 하면 없어진다.
이 작업을 자동으로 해주고 싶으면,
블루투스 데몬 파일(/lib/systemd/system/bluetooth.service)에 아래와 같이 추가해줘도 됨.
ExecStartPost=/usr/bin/sdptool add SP
# 데몬 파일을 수정해줬으면 리붓하거나 reload & restart 해주자.sudo systemctl daemon-reload
sudo systemctl restart bluetooth
추가가 되었는지는 아래와 같이 확인할 수 있다.
sudo sdptool browse local
(...)
Service Name: Serial Port
Service Description: COM Port
Service Provider: BlueZ
Service RecHandle: 0x10005
Service Class ID List:
"Serial Port" (0x1101)
Protocol Descriptor List:
"L2CAP" (0x0100)
"RFCOMM" (0x0003)
Channel: 1
Language Base Attr List:
code_ISO639: 0x656e
encoding: 0x6a
base_offset: 0x100
Profile Descriptor List:
"Serial Port" (0x1101)
Version: 0x0100
2. 다른 장치에서 파이를 검색 가능하게 설정
# Page Scan & Inquiry Scansudo hciconfig hci0 piscan
#ORsudo btmgmt connectable on
sudo btmgmt discov yes# Page Scan (기본값)sudo hciconfig hci0 pscan
#ORsudo btmgmt connectable on
sudo btmgmt discov no
# Scan 비활성화sudo hciconfig hci0 noscan
#ORsudo btmgmt connectable off
sudo btmgmt discov no
Scan 을 활성화해두면 명령어로 재부팅 전까지 다른 장치의 블루투스 스캔에 응답할 수 있다.
Page Scan(connectable) 은 이미 서로 알고 있는, 즉 페어링된 적이 있는 기기와 연결하는 방식이고
Inquiry Scan(discoverable) 은 서로 모르는 기기가 연결하기 위한 방법이다.
(즉, 처음 연결이 필요할때만 Inquiry Scan 모드를 활성화해주면 된다.)
hciconfig hci0 명령어로 확인해보면 각 PSCAN ISCAN 키워드로 활성화 상태를 알려주고,
btmgmt info 명령어로 확인해본다면 current settings에 discoverable 이 있는지로 Inquiry Scan 활성화 여부를 확인할 수 있다.
3. RFCOMM 실행, 서비스화
RFCOMM 을 통해서 시리얼 연결을 터미널로 연결한다.
#rfcomm watch [dev] [channel] [cmd]
# watch: listen 과 동일하지만, 자식 프로세스가 종료되거나 클라이언트와 연결이 끊길 경우 다시 listening 시작함.
sudo rfcomm watch hci0 1 getty rfcomm0 115200 vt100
이 명령어를 실행하면 다른 장치에서 블루투스로 파이에 연결했을때 터미널로 연결해준다.
특정 계정 자동 로그인을 해주고 싶다면 마지막에 -a <사용자> 를 추가해주자.
sudo rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a pi
이 명령어를 매번 실행해줄 수 없으므로, 이것도 데몬화한다.
/etc/systemd/system/rfcomm.service 파일을 아래와 같이 만들어 준다.
CrossPack is a development environment for Atmel’s AVR® microcontrollers running on Apple’s Mac OS X, similar to AVR Studio on Windows. It consists of the GNU compiler suite, a C library for the AVR, the AVRDUDE uploader and several other useful tools.
2. AVRDUDE 패치
컴파일된 바이너리를 avr로 업로드할때 사용되는 avrdude... 뭐가 문제인지는 모르겠지만, CrossPack 에서 설치해주는 avrdude 6.0.1 버전은 호환이 되지 않는다.
5 버전을 사용하거나, 6 이상 버전에 패치를 적용해주어야 한다. 작성시점 기준으론 5.11.1 혹은 6.3 버전.
import serial
ser = serial.Serial('/dev/ttyS0',9600, timeout=3)
ser.readline().strip().decode('utf-8')
ser.write(b'TEST\n')
ser.write('TEST\n'.encode('utf-8')
serial 변수 생성 이후 open 하고 싶다면,
ser = serial.Serial(timeout=3)
ser.port ='/dev/ttyS0'whileTrue:try:
ser.open()if ser.is_open:break
time.sleep(1)except:pass