ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [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);

        }

      }

    }

    댓글

Designed by Tistory.