티스토리 뷰
HC-06 연결(Arduino Code RXD/TXD 선언 시 하드웨어 연결과 반대로 define)
VCC : 5V
GND : GND
RXD : Arduino D3
TXD : Arduino D2
[Arduino IDE 사용] / [Visual Studio Code사용]
Arduino IDE or VS code 초기 셋팅 및 설정 참고
https://jinozblog.tistory.com/207
[Arduino Code]
HC-06 Bluetooth 연결
#include <SoftwareSerial.h>
#define BT_RXD 2 //hc06 TX to ArdUno D2
#define BT_TXD 3 //hc06 RX to ArdUno D3
SoftwareSerial hc06(BT_RXD, BT_TXD);
const int baud_rate = 9600;
String stringsum;
void setup() {
Serial.begin(baud_rate);
hc06.begin(baud_rate);
}
void loop() {
float data0 = 22.3;
float data1 = 12.6;
stringsum = "ia" + String(data0) + "b" + String(data1) + "f";
Serial.println(stringsum);
hc06.println(stringsum);
delay(1000);
}
Arduino Serial Monitoring
[python Code]
MacBook Pro 13(2019년)
모델 식별자: MacBookPro15,2
macOS Sonoma
HM-10 Bluetooth 연결 안됨
HC-06 Bluetooth 연결 안됨
iphone App. LightBlue 확인 가능
https://jinozblog.tistory.com/215
MacBook Air 2014
macOS Bigsur 가능
HC06 Bluetooth pyserial Read
python 실행 시 Arduino IDE Serial Monitor 창을 닫고 실행해야함. port가 사용 중이기 때문에 통신이 안됨!!
1. Serial Port 찾기
python library pyserial Install
terminal 창에서 : pip install pyserial
vscode 사용 시 하단 터미널(terminal)에서 다음과 같이 입력
findport.py
import serial.tools.list_ports as serial_port
port_lists = serial_port.comports()
for port_list in port_lists:
print(port_list)
출력 결과
macOS 환경에서 device(Arduino Uno Serial Port 연결, HC-06 Bluetooth module은 연결 안됨)
Serial Port : /dev/cu.usbmodem141201
Ubuntu 환경에서 (Arduino Uno Serial Port 연결, HC-06 Bluetooth 연결)
Serial Port : /dev/ttyACM0
Bluetooth Port : /dev/rfcomm0
window 환경에서 테스트하지는 못했지만 COM 뒤에 숫자가 붙는 형식으로 나타남
2. Serial Port Read
Test 환경 : Ubuntu, Serial Port / Blutooth 둘다 확인
OS 환경에 따라 아래 코드 port_ser, port_ble Device 경로 수정
import serial
import time
import pandas as pd
baud_rate = 9600
port_ser = '/dev/ttyACM0'
port_ble = '/dev/rfcomm0'
ard_ser = serial.Serial(port_ser, baud_rate)
ard_ble = serial.Serial(port_ble, baud_rate)
df_header = ['Data_A','Data_B']
df = pd.DataFrame(columns=df_header, index=None)
for t in range(5):
serial_data = ard_ser.readline().decode()
bluetooth_data = ard_ble.readline().decode()
print("[serial]:{}".format(serial_data))
print("[bluetooth]:{}".format(bluetooth_data))
try:
if bluetooth_data[0] == 'i':
data_A = bluetooth_data[bluetooth_data.index('a')+1:bluetooth_data.index('b')]
data_B = bluetooth_data[bluetooth_data.index('b')+1:bluetooth_data.index('f')]
else:
data_A = 0
data_B = 0
except:
pass
df.loc[t] = [data_A,data_B]
print(df)
time.sleep(1)
df.to_csv('./save.csv')
출력결과
datasave : save.csv
'python > Lecture' 카테고리의 다른 글
[python][vscode] Signal Test Code (0) | 2023.11.30 |
---|---|
[arduino][python] DS18B20 Digital Sensor pyserial 통신 (0) | 2023.11.23 |
[arduino] DS18B20 Digital Sensor 연결 및 BLE 통신 to App. (0) | 2023.11.23 |
[arduino] DS18B20 Digital Sensor 연결 및 wifi 통신 to Web (0) | 2023.11.23 |
[arduino] DS18B20 Digital Sensor 연결 및 wifi 통신 with MariaDB (0) | 2023.11.22 |
- Total
- Today
- Yesterday
- 코로나
- r
- MacOS
- 자가격리
- pyserial
- DAQ
- template
- vscode
- raspberrypi
- Python
- Model
- SSH
- CSV
- Pandas
- DS18B20
- git
- server
- COVID-19
- arduino
- Raspberry Pi
- sublime text
- Django
- Templates
- Regression
- github
- analysis
- 라즈베리파이
- 확진
- 코로나19
- ERP
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |