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