0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ESP32のJavascriptでBLEペリフェラル/セントラルを制御する

Last updated at Posted at 2025-08-31

ESP32のJavascriptでBLEペリフェラル/セントラルを制御するためのライブラリを追加しました。

プログラム書き込み方法

Webサーバとの併存はメモリが足りなく不安定であるため、Webサーバ機能を無効化しています。
そこで以下のようにして、コンソール出力のUARTから行います。

//___ProgramStart___
から
//___ProgramEnd___
の間にソースコードを記述し、全体をVisual Studio Codeのターミナル(PlatformIO Serial Monitor)から入力する

コピペが楽です。

サンプル

iBeaconサンプル

//___ProgramStart___

import * as bleperi from "BlePeripheral";
import * as input from "Input";

input.onButtonWasPressed(input.BUTTON_A, () =>{
	console.log("button pressed");
	esp32.restart();
});

console.log("Macアドレス=" + bleperi.getMacAddress());

function loop(){
	esp32.update();
}

var manufacturerData_base = [ 0x4c, 0x00, 0x02, 0x15 ];
var uuid = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f];
var major = 0x0123;
var minor = 0x4567;
var txpower = 0xc5; // -59

var manufacturerData = manufacturerData_base.concat(uuid);
manufacturerData = manufacturerData.concat([ (major >> 8) & 0xff, (major >> 0) & 0xff, (minor >> 8) & 0xff, (minor >> 0) & 0xff, txpower]);
//console.log(manufacturerData);

console.log("start ibeacon");
bleperi.startAdvertise2(0, { connectableMode: bleperi.ADV_NONCONN_IND }, {
	flags: 0x06,
	manufacturerData: new Uint8Array(manufacturerData)
});

//___ProgramEnd___

BLEペリフェラル

//___ProgramStart___

import * as bleperi from "BlePeripheral";
import * as input from "Input";

input.onButtonWasPressed(input.BUTTON_A, () =>{
	console.log("button pressed");
	esp32.restart();
});

console.log("Macアドレス=" + bleperi.getMacAddress());

function loop(){
	esp32.update();
}

bleperi.setCallback((type, value) =>{
	console.log("type=" + type);
	console.log("value=" + JSON.stringify(value));
	if( type == "write" ){
		bleperi.setReadValue(new Uint8Array([0x01, 0x23, 0x45]));
		bleperi.notify();
		console.log('notify ok');
	}
});
console.log("start advertising");
bleperi.setSecurity(true, false, true, bleperi.IOCAP_DISPLAY_ONLY);
bleperi.startAdvertise(0, { name: "Esp32Ble" });

//___ProgramEnd___

BLEセントラル

//___ProgramStart___

import * as blecent from "BleCentral";
import * as input from "Input";

input.onButtonWasPressed(input.BUTTON_A, () =>{
	console.log("button pressed");
	esp32.restart();
});

console.log("Macアドレス=" + blecent.getMacAddress());

function loop(){
	esp32.update();
}

blecent.setCallback((type, value) =>{
	console.log("type=" + type);
	console.log("value=" + JSON.stringify(value));
	if( type == "connect" ){
		blecent.subscribe("83444e80-1f85-4471-a94c-2a5e96d9cdc8", "83444e81-1f85-4471-a94c-2a5e96d9cdc8", false);
		blecent.write("83444e80-1f85-4471-a94c-2a5e96d9cdc8", "83444e82-1f85-4471-a94c-2a5e96d9cdc8", new Uint8Array([0x00, 0x01, 0x02]));
		console.log("write ok");
	}
});

console.log("start scan");
blecent.setSecurity(true, false, true, blecent.IOCAP_NO_INPUT_OUTPUT);
blecent.startScan(5000, { activeScan: true }, (list) =>{
	console.log(JSON.stringify(list, null, "\t"));
	
	console.log("connecting");
	blecent.connect("50:02:91:8d:e2:0a", true);
});
//___ProgramEnd___

PLAYBULBへの接続

//___ProgramStart___

import * as blecent from "BleCentral";
import * as input from "Input";

function loop(){
  esp32.update();
}

var colors = [
  [0x00, 0xff, 0x00, 0x00],
  [0x00, 0x00, 0xff, 0x00],
  [0x00, 0x00, 0x00, 0xff]
];
var color_index = -1;

input.onButtonWasPressed(input.BUTTON_A, () =>{
  color_index = ( color_index + 1 ) % colors.length;
  blecent.write("0000FF02-0000-1000-8000-00805F9B34FB", "0000FFFC-0000-1000-8000-00805F9B34FB",
    new Uint8Array(colors[color_index])
  );
});

console.log("scan start");
blecent.startScan(5000, { activeScan: true }, (list) =>{
  console.log("scan ended");
//  console.log(JSON.stringify(list, null, '\t'));
  var target = list.filter(item => item.name?.startsWith("PLAYBULB"));
  console.log(JSON.stringify(target, null, '\t'));

  if( target.length > 0)
    blecent.connect(target[0].address);
});

blecent.setCallback( (type, data) =>{
  console.log(type, JSON.stringify(data, null, '\t'));
  if( type == 'connect' ){
    console.log("connected");
    var value = blecent.read("0000FF02-0000-1000-8000-00805F9B34FB", "0000FFFF-0000-1000-8000-00805F9B34FB");
    console.log(JSON.stringify(value, null, '\t'));
  }
});

//___ProgramEnd___

(参考) ESP32で動作するJavascript実行環境

ESP32で動作するJavascript実行環境を公開しています。

「電子書籍:M5StackとJavascriptではじめるIoTデバイス制御」

サポートサイト

(参考)ESP32で動作するJavascriptライブラリ

ライブラリ名 概要
Esp32 ESP32 に関連する基本的な機能を提供します。
Console UART にデバッグ文を出力します。
Audio I2S に接続されたスピーカから MP3 音声を再生します。
Camera ESP32 に接続されたカメラの画像撮影機能を提供します。
Crypto 暗号機能を提供します。
Env I2C に接続された温湿度センサ(SHT30、DH12)を操作します。
EspNow EspNow の機能を提供します。
Gpio ESP32 の GPIO を制御します。
Http HttpGateway を介して、HTTPS 通信します。
Imu ESP32 に接続された 6 軸姿勢センサを制御します。
Ir ESP32 に接続した赤外線送受信機を制御します。
Input ボタンの押下を検出します。
Lcd ESP32 に接続した LCD の表示を制御します。
Ledc ESP32 の GPIO ピンに対して PWM 出力します。
Pixels ESP32 に接続した RGB LED を制御します。
Prefs ESP32 の不揮発メモリの読み書きをします。
Rtc ESP32 に接続した RTC から時刻を設定・取得します。
Sd ESP32 に接続された microSD カードのストレージに対するファイルの読み書きをします。
Udp UDP パケットを送受信します。
Uart UART 通信のための機能を提供します。
Utils base64、url エンコード、HTTP 通信などのユーティリティです。
Websocket Websocket によるサーバ通信機能を提供します。
WebsocketClient Websocket によるクライアント通信機能を提供します。
Wire 周辺デバイスとの I2C 通信のための機能を提供します。
UnitColor カラーセンサユニットを制御します。
UnitGas ガスセンサーユニットを制御します。
UnitGesture ジェスチャーユニットを制御します。
UnitPbhub I/O ハブユニットを制御します。
UnitSonicIo 超音波測距ユニット I/O を制御します。
UnitAngle8 8 ポテンショメータユニットを制御します。
UnitEnvPro 環境センサ Pro ユニットを制御します。
UnitImuPro IMU Pro ユニットを制御します。
BlePeripheral BLEペリフェラル機能を提供します。★今回追加
BleCentral BLEセントラル機能を提供します。★今回追加

以上

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?