[App Inventor IoT ] Lesson 1: Arduino 101 LED Blink

This topic is the first lesson of MIT App Inventor IoT (Internet of Things) tutorials. We are going to introduce how to use your Android phone to control Arduino 101's LED blinking, via App Inventor's BluetoothLE (Bluetooth 4.0 Low Energy)component.

MIT App Inventor will use Arduino 101 as the core dev board of its IoT solutions, you can develop various kinds of interactive project with the kit (planning).  You must import BLE component(.aix) as an extension before using it. A screenshot of the actual app execution is shown below. Enjoy~

2016-01-26-14.50.47-1024x768

YouTube 影片

Arduino 101 is the latest dev board under cooperation between Arduino.cc and Intel, which is named as Genuino 101 out side USA.

More topics: 

Part List

1. Android phone (Must have BLE hardware, but I think it's not a problem for most Android devices today).

2. Arduino 101

3. LED or Relay Module (optional, you can use onboard LED directly)


App Inventor 

Basically, this project is almost identical with [App Inventor and Arduino: Lesson 1: LED Blink], except the BLE component. We are going to control Arduino 101's onboard LED blinking while you press the button. Don't forget, you can easily extent this project with real LEDs or relay modules to become a basic smart home demonstration.

Designer 

Familiar, right? You will use this kind of interface frequently along our tutorials. Quite easy interface with one of one ListPicker and two Button components. And one Bluetooth client components for Bluetooth communication. Please check the description below:

1. ConnectButton (Button): Click to connect to specified BLE device, which is Arduino 101 in this project.
2. TurnOnButton (Button): Click to send string "0" to Arduino 101.
3. TurnOffButton (Button): 
Click to send 
string "1" to Arduino 101.
4. Button_Disconnect 
(Button): Click to close connection between Android phone and Arduino 101.
5. *BluetoothLE (non visible): experimental components for BLE communication. Please import BLE .aix file to your AI2 project.



How to play:

For simplicity, this project connect to your Arduino 101 directly, which means the Bluetooth address is assigned in the code. If your app has to connect from several devices, I think pairing with these devices first and pick them by ListPicker component in App Inventor is a better solution.

How to pair your Arduino 101 and Android phone: First you must pair the Bluetooth module with your Android phone. After you connect all the hardware, you can see that the Bluetooth module's red LED is flashing, means ready for connecting. Then please open your Android phone's Bluetooth setting page (which should under Settings... , differs on each phone), click the name of your module (something like HC-05). Please enter 1234 (or 0000) when there is a prompt asking the paring key. Done~

There is only BTList which is enabled when you first open the app. Please click BTList, it will show all paired Bluetooth devices on your phone. Please click the name of your module, it should take 1~2 seconds to connect and back the main screen. You can see that the [LED Turn On] button is now clickable and the module's LED is keep lighted up. Just click the button, the LED on Arduino will light up and the button's text will become "LED Turn Off". Click again will turn off the LED and switch back the button text. 

Please click the [Disconnect] button when you don't want to play anymore, this will disconnect the connection of your phone and bluetooth module and the module's LED will flashing again.

Blocks 

1. Initialize and connect

While initializing(Screen1.Initializing event), BluetoothLE1 component will scan for avalible devices, which means your Arduino 101 should be ready by then. 

When you click the Button_Connect (ConnectButton_Connect.Click event),  BluetoothLE1 component will try to connect the specified device, we put our Arduino 101's bluetooth address here (98:4F:EE:0F and TextBox.Text for less user input, total 12 digits). Please check your Arduino 101 for the real address.

2. Connection confirmed

BluetoothLE1.connected event will be called after the connection is established, we show "Connected" on Label1 and adjust other components to be usable.


3. Control LED with 1,0

When you click the Button_LED (Button_LED.Click event), it will use BluetoothLE1.WriteIntValue method to send out an integer 1 or to your Arduino 101 according  Button_LED's Text.

According to the Arduino sketch, Arduino 101 will light up #13 LED when it received this value (1) or light off when receive 0. 

Please notice that service_uuid and characteristic_uuid should use “19B10010-E8F2-537E-4F6C-D104768A1214″ and “19B10011-E8F2-537E-4F6C-D104768A1214″, which means Arduino 101's BLE service. These pair of UUID must be identical with which in Arduino sketch.

You can change the trigger event to sensor value, Google Speech recognizer, slider... etc, or add more case in Arduino sketch to interface more I/O. Example:  [App Inventor IoT ] Lesson 4: 4-axis robotarm

4. Disconnect

When you press the Button_Disconnect button(Button_Disconnect.Click event), BluetoothLE1 component will close the connection with specified device (Arduino 101) and set all the components to initial state. You can connect to Arduino 101 once again.




Arduino 101

This sketch is contributed by MIT App Inventor team. Please notice that Arduino 101 has onboard BLE hardware, therefore you don't have to connect Bluetooth modules like HC05.

There are two BLE objects in the sketch: blePeripheral(Arduino 101) and bleCentral(Android). blePeripheral is used to configure all the attributes of the Arduino 101, such as service_uuid and characteristic_uuid. And bleCentral is reponsible for the connection between the board and phone.

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

BLEService lightService("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service

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


The core of this sketch is that we use incom = EDStatus.value() to check what Arduino 101 has received. Arduino 101 will light up #13 LED when it receive an integer 1; Otherwise (integer 0 in our case) it will keep the LED off.

while (central.connected()) {//Serial.println(LEDStatus.written());if (LEDStatus.written()){ incom = LEDStatus.value(); Serial.print("incom "); Serial.println(incom);if (incom != last_incom) //compare current value with last one{if (incom == 1) //integer 1{ Serial.println("LED On");digitalWrite(LED, HIGH);}else{ Serial.println("LED Off");digitalWrite(LED, LOW);}} last_incom = incom;}}

Complete Arduino 101 sketch

#include <CurieBLE.h>#define LED 13BLEPeripheral blePeripheral;  // BLE Peripheral Device (the board you're programming)BLEService ControlLED("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE AnalogRead Service// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by centralBLEUnsignedIntCharacteristic LEDStatus("19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite  );int incom = 0;int last_incom = 0;void setup() {  Serial.begin(9600);  // set Light pin to output mode  // set advertised local name and service UUID:  blePeripheral.setLocalName("ControlLED");  blePeripheral.setAdvertisedServiceUuid(ControlLED.uuid());  // add service and characteristic:  blePeripheral.addAttribute(ControlLED);  blePeripheral.addAttribute(LEDStatus);  // begin advertising BLE Light service:  blePeripheral.begin();  Serial.println("BLE AnalogRead service.");  pinMode(LED, OUTPUT);}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()) {      //Serial.println(LEDStatus.written());      if (LEDStatus.written())      {        incom = LEDStatus.value();        Serial.print("incom ");        Serial.println(incom);        if (incom != last_incom)        {          if (incom == 1)  //integer 1          {            Serial.println("LED On");            digitalWrite(LED, HIGH);          }          else             //integer 0 in our case          {            Serial.println("LED Off");            digitalWrite(LED, LOW);          }        }        last_incom = incom;      }    }    delay(100);  }  // when the central disconnects, print it out:  Serial.print(F("Disconnected from central: "));  Serial.println(central.address());}
曾吉弘,
2017年8月16日 晚上11:15
v.4
曾吉弘,
2017年5月2日 凌晨3:43
v.4
曾吉弘,
2017年8月16日 晚上11:15
v.1
曾吉弘,
2016年10月6日 晚上8:30
v.1