LoginSignup
0
0

More than 1 year has passed since last update.

ESP32とjoyconのBluetooth通信がうまくいかんだ話

Last updated at Posted at 2021-12-18

概要

ESP32でおもしろいことしたいなーと思い立ち、結果よくわからないまま失敗したあほの記事

やりたかったこと

ご存じESP32にはBluetooth通信の機能がついている。そしてjoyconにもBluetooth通信の機能がある。
じゃあいけるじゃん!と思い、やってみた

やったこと

まずbluetooth-command-line-toolsというのを用いてjoyconのMACアドレスを取得する。

image.png

次に以下のようなコードをESP32に書き込んだ

joyconL.ino
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

String MACadd = "D4:F0:57:D9:81:80";
uint8_t address[6]  = {0xD4, 0xF0, 0x57, 0xD9, 0x81, 0x80};
String name = "Joy-Con (L)";
char *pin = "1234"; //<- standard pin would be provided by default
bool connected;

void setup() {
  Serial.begin(115200);
  //SerialBT.setPin(pin);
  SerialBT.begin("ESP32_test", true); 
  //SerialBT.setPin(pin);
  Serial.println("The device started in master mode, make sure remote BT device is on!");

  // connect(address) is fast (upto 10 secs max), connect(name) is slow (upto 30 secs max) as it needs
  // to resolve name to address first, but it allows to connect to different devices with the same name.
  // Set CoreDebugLevel to Info to view devices bluetooth address and device names
  //connected = SerialBT.connect(name);
  connected = SerialBT.connect(address);

  if(connected) {
    Serial.println("Connected Succesfully!");
  } else {
    while(!SerialBT.connected(10000)) {
      Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app."); 
    }
  }
  // disconnect() may take upto 10 secs max
  if (SerialBT.disconnect()) {
    Serial.println("Disconnected Succesfully!");
  }
  // this would reconnect to the name(will use address, if resolved) or address used with connect(name/address).
  SerialBT.connect();
}

void loop() {
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  delay(20);
}

まあつながりませんでしたが。

反省

私は失敗したがやってる人はいたからできることではある
ESP32がbluetooth3.0に対応していない可能性を考えたが、ネットにbluetooth3.0のコントローラーと通信してそうな記事があったので違いそう
また再挑戦はします……

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0