簡體   English   中英

使用Processing在Android和Arduino之間進行藍牙通信

[英]Bluetooth communication between Android and Arduino using Processing

我正在嘗試使用Android的Processing在Android和Arduino之間進行雙向藍牙通信。 我已成功使用serial.begin(9600)將數據從Android傳輸到Arduino。 通過在Arduino程序中使用SoftwareSerial並使用serial.begin(9600)代替bluetooth.begin(9600),我成功地將數據從Arduino傳輸到Android。

但是,當嘗試使用bluetooth.x命令將數據從Android傳輸到Arduino時,它不起作用。 這是Arduino代碼:

  if (bluetooth.available()) // Wait until a character is received
  {
    char val = (char)bluetooth.read();
    //Serial.println(val);

    switch(val) // Perform an action depending on the command
    {
      case 'w'://turn the light on when a 'w' is received
      on();
      break;

      case 'q'://turn the light off when a 'q' is received
      off();
      break;

      //default://otherwise remain in the previous state
      //idle();
      break;
    }
  }

on()和off()函數可打開和關閉Arduino上的LED。 如前所述,這在我使用serial.x命令而不是bluetooth.x命令時有效。 另外,我正在使用Ketai進行Android處理。 我正在使用Processing 2.0.1,Arduino 1.0.5,Android 2.3.6。

這是相關的開始代碼:

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(0,1);  //TX 0, RX 1

多一點代碼將不勝感激...

你有沒有包括這樣的東西?

#include <SoftwareSerial.h>

int bluetoothTx = 2;
int bluetoothRx = 3;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

編輯:

這與我使用的類似。 您首先上傳未連接藍牙的代碼,然后再連接藍牙。 然后,您可以簡單地使用Serial.doSomething()因為您使用的是相同的引腳,因此不需要#include <SoftwareSerial.h> 但是您需要確保波特率相同。

您可以嘗試以下代碼以確保其正常工作:

void setup(){

    Serial.begin(9600); // or wathever your bluetooth module baudrate is

}

void loop(){

    Serial.println("Hello World!"); // to make sure it works.
    delay(500);

}

您還應該確保您已將Arduino通過藍牙連接到計算機。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM