[App Inventor IoT ] Lesson 2: Arduino 101 LED fading (PWM)

The second article of operating App Inventor of BLE with Arduino 101 is here! This series of articles describe how to use App Inventor of BLE (Bluetooth Low Energy) to interact with the Arduino 101 development board in various ways. You must import BLE component(.aix) as an extension before using it. A screenshot of the actual app execution is shown below. 

     

YouTube 影片


Arduino 101 is the latest development board under cooperation between Arduino.cc (NOT .org) and Intel, also known as Genuino 101 outside of the USA.


App Inventor

Basically, this example is almost identical with [App Inventor and Arduino: Lesson 1: LED Blink], except we use BLE component in this case. In this example we will use the slider to control the brightness of Arduino 101 D9 LED.

 

Designer Page

Here a Slider that is used to control the brightness of the LED, along with Clock components that regularly send the position of the slider pointer to Arduino 101 through BLE components. The position of the slider pointer is also displayed in the TextBox. You cannot use the original BluetoothClient components to communicate with Arduino because of the difference in the communication protocol. 

Blocks

1. Initialize and connect

In the Initializing window use BluetoothLE1 to scan for available devices, which will detect if your Arduino 101 is ready. 

Next, click the ConnectButton and in the ConnectButton.Click window choose BluetoothLE1 to connect to the specified device. Type in your Arduino 101 bluetooth address here (98:4F…). Please modify the address string according to your Arduino 101.


 

2. Connection confirmed

BluetoothLE1.connected window will be called after the connection is established, as shown below.

 

3. Continuous Transmission Signal

Use the Clock.Timer command to accomplish the following two tasks with a time interval of 100 (every 0.1 second)

·         Display the position of the pointer of the slider in the TextBox.

·         Use BluetoothLE1.WriteIntValue command to send out the pointer position, then with this information Arduino 101 will control the brightness of the LED via the analogWrite command. Type in 19B10011-E8F2-537E-4F6C-D104768A1214″ in both service_uuid and characteristic_uuid columns. Arduino 101 uses this parameter as its BLE service.

 

4. Disconnect

When you press the Button_Disconnect button (Button_Disconnect.Click window), BluetoothLE1 component will disconnect with the specified device (Arduino 101).

 


Arduino 101 Code

Copy the code and paste it onto Arduino 101. Please notice that Arduino 101 has onboard BLE hardware, therefore you don't need to connect other Bluetooth modules such as HC05.

service_uuid and characteristic_uuid are also specified in the sketch as “19B10011-E8F2-537E-4F6C-D104768A1214″,  which must be the same with the string you use in App Inventor.

/*   

  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());

  }

} 






ċ
Ble_pwm.aia
(2k)
曾吉弘,
2016年5月11日 上午6:10
ċ
Ble_pwm.ino
(2k)
曾吉弘,
2016年5月11日 上午6:10
Comments