最近剛出爐的7697,具有BLE功能,SimplePeripheral範例中使用BLE點亮LED,在範例目錄…\Arduino15\packages\LinkIt\hardware\linkit_rtos\0.7.11\libraries\LBLE\examples\SimplePeripheral之下有個aia檔,它用了實驗性的Bluetooth LE extension,匯入AppInventor後的畫面如下:

 

SimplePeripheral只用到INT類型的characteristic來控制LED寫入1時點亮寫入0時熄滅,在Android系統可以使用BLE ScannerBLE UUID ExplorerApp軟體來測試。其實閱讀…\Arduino15\packages\LinkIt\hardware\linkit_rtos\0.7.11\libraries\LBLE\src之程式碼可以發現,7697還有支援字串類型的characteristic,不過因為BLE的封包很短,扣除掉一些header後,在android系統每個封包最多只能傳送20 bytes。如果想用字串來傳遞WiFi APssidpassword,除非自行切割長字串,不然也只能使用20 bytesssid與密碼。下面這個程式加入了字串類型的characteristic,當使用Appcharacteristic進行寫入之後,7697LBLEPeriphral.cpp會自動保存其寫入的資料,之後可用Characteristic.isWritten()判斷而將寫入的資料取出。使用BLE UUID Explorer的操作畫面,掃描裝置結果如下圖:

 點選裝置後,點開Unknown service後可看到3個characteristic畫面如下圖:

第一個Unknown characteristic是INT類型點選進入讀寫畫面,寫入1後,7697上的LED就會點亮,這跟用AppInventor程式的效果相同畫面如下圖:

第2個或第3個Unknown characteristic是String類型寫入字串後再按Read讀回,證明7697的確有將資料保存起來畫面如下圖:

 此時在Arduino終端機上可以見到輸入的字串畫面如下圖:

 

程式碼如下

#include <LBLE.h>

#include <LBLEPeriphral.h>

 

// Define a simple GATT service with only 1 characteristic

LBLEService nodeService("19B10010-E8F2-537E-4F6C-D104768A1214");

LBLECharacteristicInt switchCharacteristic("19B10011-E8F2-537E-4F6C-D104768A1214", LBLE_READ | LBLE_WRITE);

LBLECharacteristicString ssidCharacteristic("19B10012-E8F2-537E-4F6C-D104768A1214", LBLE_READ | LBLE_WRITE);

LBLECharacteristicString passCharacteristic("19B10013-E8F2-537E-4F6C-D104768A1214", LBLE_READ | LBLE_WRITE);

String ssid="ssid";

String pass="pass";

 

void setup() {

 

  // Initialize LED pin

  pinMode(LED_BUILTIN, OUTPUT);

  digitalWrite(LED_BUILTIN, LOW);

 

  //Initialize serial and wait for port to open:

  Serial.begin(9600);

  Serial.println("node begin");

 

  // Initialize BLE subsystem

  LBLE.begin();

  while (!LBLE.ready()) {

    delay(100);

    Serial.print(".");

  } 

  Serial.println("BLE ready");

 

  Serial.print("Device Address = [");

  Serial.print(LBLE.getDeviceAddress());

  Serial.println("]");

 

  // configure our advertisement data.

  // In this case, we simply create an advertisement that represents an

  // connectable device with a device name

  LBLEAdvertisementData advertisement;

  advertisement.configAsConnectableDevice("BLE LED");

 

  // Configure our device's Generic Access Profile's device name

  // Ususally this is the same as the name in the advertisement data.

  LBLEPeripheral.setName("BLE LED");

 

  // Add characteristics into ledService

  nodeService.addAttribute(switchCharacteristic);

  nodeService.addAttribute(ssidCharacteristic);

  nodeService.addAttribute(passCharacteristic);

 

  // Add service to GATT server (peripheral)

  LBLEPeripheral.addService(nodeService);

 

  // start the GATT server - it is now

  // available to connect

  LBLEPeripheral.begin();

 

  // start advertisment

  LBLEPeripheral.advertise(advertisement);

}

 

void loop() {

  delay(100);

  if (switchCharacteristic.isWritten()) {

    const char value = switchCharacteristic.getValue();

    switch (value) {

      case 1:

        digitalWrite(LED_BUILTIN, HIGH);

        break;

      case 0:

        digitalWrite(LED_BUILTIN, LOW);

        break;

      default:

        Serial.println("Unknown value written");

        break;

    }

  }

  if (ssidCharacteristic.isWritten()) {

        ssid = ssidCharacteristic.getValue();

        Serial.print("ssid="); Serial.println(ssid);

  }

  if (passCharacteristic.isWritten()) {

        pass = ssidCharacteristic.getValue();

        Serial.print("pass="); Serial.println(pass);

  }

}

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 ghostyguo 的頭像
    ghostyguo

    No More Codes

    ghostyguo 發表在 痞客邦 留言(0) 人氣()