[App Inventor 物聯網應用-藍牙4.0 BLE] Lesson 2 – LED PWM呼吸燈

App Inventor BLE 元件搭配 Arduino 101 第二篇文章來囉!本系列專文是介紹如何使用 App Inventor 的 BLE (Bluetooth Low Energy) 元件搭配 Arduino 101 開發板的各種互動專題。請用 http://ble-test.appinventor.mit.edu 這個測試用伺服器來測試。

 

2019/08 update

http://ble-test.apinventor.mit.edu 伺服器已關閉,請直接匯入 BLE extension 到 MIT AI2 正式伺服器。

請改用MIT App Inventor團隊於 2018/11/24 release 的 BLE extension,並改用 WriteIntegers 指令即可。由下圖可看到可透過 signed 欄位指定為有號數或無號數。

實際app執行畫面如下圖,先來看執行畫面吧:

[youtube=”https://www.youtube.com/watch?v=ihMyDoxapBQ”]

 

Arduino 101 是 Arduino.cc (注意不是 .org) 與 Intel 合作的最新開發板,在美國以外的區域稱為 Genuino 101

延伸閱讀:

[Make雜誌國際中文版]Arduino 101 介紹

Arduino.cc 論壇的 callbackLED 教學(手機端並非使用 App Inventor )

Arduino 101 購買連結

Arduino 101 開箱文

Arduino 101 BLE 第一課 LED閃爍

Arduino 101 BLE 第三課 讀取類比腳位狀態

Arduino 101 BLE 第四課 控制機器手臂

硬體架構

  1. Arduino 101
  2. Android 手機
  3. LED,長腳接在 Arduino 101的D9腳位。 請注意 Arduino 101 只有 D3,5,6,9 這四隻腳支援 PWM,比 Arduino UNO少兩隻喔!

App Inventor 程式

基本上概念與咱們的[雙A計畫]完全相同,只是改用 BLE 元件而已。本範例將使用 Slider 滑桿控制 Arduino 101 的 D9 LED 漸明漸暗

Screenshot_2016-04-24-17-02-31-88

Designer 頁面

版面相當清爽,在此使用一個 Slider 元件來控制 LED 亮度,搭配 Clock 元件定期把指針位置透過 BLE元件發送給 Arduino 101 。同時也會把 Slider 的指針位置顯示在 TextBox 中。由於通訊協定的不同,您當然無法用原本的 BluetoothClient元件來與 Arduino 101 溝通。

ble03

Blocks 頁面

1. 連線與斷線

程式初始化時,要求 BluetoothLE1元件進行掃描。請注意本範例是直接對指定藍牙裝置(Arduino 101)連線。如果您日後需要從多個裝置中選擇的話,建議還是先配對再從 ListPicker 中點選是比較好的做法。請注意 BLE 元件不需要事先配對,只要指定 address 即可直接連線。

ConnectButton按下時,要求BluetoothLE1元件對指定位址的藍牙裝置(98:4F…) 進行連線。

ble01
ble02 

2. 連線確認

承上一步,BluetoothLE1.connected事件會在連線成功後啟動,我們在此顯示一些確認訊息。Screen的狀態列(title) 很好用,別冷落它囉

ble_03_07

3. 連續發送訊號

在此使用 Clock.Timer事件,每0.1秒 (TimerInterval 為100),執行下列兩件事:

a. 把 Slider 的指針位置顯示在 TextBox 中

b. 使用 BluetoothLE1.WriteIntValue 指令把Slider 的指針位置送出去,Arduino 101 在接收到這個數值之後就會透過 analogWrite 指令來控制 LED 的亮度。請注意 service_uuid 與 characteristic_uuid 這兩個欄位請都填入“19B10011-E8F2-537E-4F6C-D104768A1214”,Arduino 101 都使用這個參數代表它所提供的 BLE 服務。

ble04

4. 斷線

Button_Disconnect按下時,要求BluetoothLE1元件對指定藍牙裝置斷線。

Arduino 101 程式碼

本草稿碼請直接複製後上傳至 Arduino 101 即可。請注意 Arduino 101 已經具備藍牙4.0,因此不必再外接 HC05 這類的藍牙裝置

文中一樣要寫明 service_uuid 與 characteristic_uuid,在此都是“19B10011-E8F2-537E-4F6C-D104768A1214”

Arduino 101 LED PWM
/*
A simple demo of how to control Arduino 101' LED fading with App Inventor's BLE component.
By CAVEDU (service@cavedu.com)
*/

#include <CurieBLE.h>

BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)
BLEService lightService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEUnsignedIntCharacteristic switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);


const int lightPin = 9; // pin to use for the Light

void setup() {
Serial.begin(9600);

// set Light pin to output mode
pinMode(lightPin, OUTPUT);

// set advertised local name and service UUID:
blePeripheral.setLocalName("Light Service");
blePeripheral.setAdvertisedServiceUuid(lightService.uuid());

// add service and characteristic:
blePeripheral.addAttribute(lightService);
blePeripheral.addAttribute(switchCharacteristic);

// set the initial value for the characeristic:
switchCharacteristic.setValue(0);

// begin advertising BLE Light service:
blePeripheral.begin();

Serial.println("BLE Light service.");
}

void loop() {
// listen for BLE peripherals to connect:
BLECentral central = blePeripheral.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());

// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the Light:
if (switchCharacteristic.written()) {
Serial.println(switchCharacteristic.value());
int light = map(switchCharacteristic.value(),10,49,0,255);
analogWrite(lightPin,light);
Serial.println(light);
}
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}

7 thoughts on “[App Inventor 物聯網應用-藍牙4.0 BLE] Lesson 2 – LED PWM呼吸燈

  1. qiaox says:

    您好,请教一下有关于App Inventor 使用BLE元件的问题,烦请您方便的时候回复我。
    1App Inventor 的BLE组件,是不是最新版的App Inventor 已经能使用了,由于身处大陆,即时翻墙后您给出的网址 http://ble-test.apinventor.mit.edu 也是无法打开。
    2App Inventor 的BLE组件兼容性有没有要求;

    • CAVEDU 阿吉 - 雜工 says:

      您好,ble-test 是 MIT 的暫時性服務器,據我所知應該這個月就會發布。至於 BLE组件兼容性,這要看您用哪一款Android 手機,目前應該除了小米之外都還算正常使用,我個人使用小米在寫App Inventor 會有一些奇怪的問題

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *