-
[Arduino] 안드로이드 앱 실습 (mit, bluetooth, chat)전기전자공학/실습 2018. 1. 31. 17:25
#블루투스 이름, 속도, pin번호 정하기
모니터:
CMD: AT
Return: OK
CMD: AT
Return: OK
CMD: AT+NAMEsongBT
Return: OKsetname
CMD: AT+BAUD4
Return: OK9600
CMD: AT+PIN1234
Return: OKsetPIN
CMD:
코드:
#include <SoftwareSerial.h>
#define RXD_PIN 2
#define TXD_PIN 3
SoftwareSerial bt(RXD_PIN, TXD_PIN);
char data;
void setup() {
Serial.begin(9600);
bt.begin(9600);
Serial.println("Ready...");
}
void loop() {
Serial.flush();
Serial.print("CMD: ");
while(!Serial.available());
while(Serial.available()) {
data = Serial.read();
if(data == -1) break;
bt.print(data);
Serial.print(data);
delay(1);
}
Serial.println();
delay(1000);
Serial.print("Return: ");
while(bt.available())
{
data = bt.read();
if(data == -1) break;
Serial.print(data);
delay(1);
}
Serial.print("\n\n");
}
예제 02 Aruduino와 smart App의 블루투스 통신 실습
#include <SoftwareSerial.h>
#define RXD_PIN 2
#define TXD_PIN 3
#define ledPin 13
#define BUFF_SIZE 256
SoftwareSerial swBT(RXD_PIN, TXD_PIN);
uint8_t buffer[BUFF_SIZE];
uint8_t index = 0;
uint8_t data;
void setup() {
swBT.begin(9600);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(ledPin, LOW);
while(!swBT.available());
digitalWrite(ledPin, HIGH);
while(swBT.available())
{
data = swBT.read();
buffer[index++] = data;
if(index == BUFF_SIZE || data == '\0')
break;
delay(1);
}
for(uint8_t i = 0; i < index; ++i) {
swBT.write(buffer[i]);
}
index = 0;
}
실습 휴대폰 - 아두이노 채팅
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2,3);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
btSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(btSerial.available()){
Serial.write(btSerial.read());
}
if(Serial.available()){
btSerial.write(Serial.read());
}
}
\===========================================
#include <SoftwareSerial.h>
SoftwareSerial btSerial(2,3);
String btString ="";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
btSerial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
while(btSerial.available()){
char btChar = (char)btSerial.read();
btString+=btChar;
delay(10);
}
if(!btString.equals("")){
Serial.println("recv : "+btString);
btString="";
}
while(Serial.available()){
char btChar = (char)Serial.read();
btString+=btChar;
delay(10);
}
if(!btString.equals("")){
btSerial.println(btString);
// Serial.println("INPUT : "+btString);
btString="";
}
}
실습:mit app inventor 폰에 깔아서 arduino led on/off 폰으로 제어
#include <SoftwareSerial.h>
SoftwareSerial BTSerial(2,3);
void setup() {
// put your setup code here, to run once:
BTSerial.begin(9600);
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
if(BTSerial.available()){
char data = BTSerial.read();
//Serial.write(data);
if(data=='0'){
digitalWrite(13, HIGH);
}else if(data=='1'){
digitalWrite(13,LOW);
}
}
}
'전기전자공학 > 실습' 카테고리의 다른 글
MNIST 데이터 + 차원 축소 + LSTM 으로 학습시키기 (1) (0) 2019.12.13 [Visual studio 2015&2017 - OpenCV 3.4.2 설치] (0) 2018.08.16 [Raspberry3] ubuntu mate OS 설치 (0) 2018.02.02 [Arduino] Wifi (0) 2018.02.01 [Arduino] 스텝모터, 서보 모터, IR 수신 (0) 2018.01.29