This topic is the third lesson of MIT App Inventor IoT (Internet of Things) tutorials. We are going to introduce how to use your Android phone to continuously read Arduino 101’s analog pin(A0) 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
|
#include <CurieBLE.h>BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you’re programming)BLEService ARead(“19B10010-E8F2-537E-4F6C-D104768A1214”); // BLE AnalogRead Service// BLE LED Switch Characteristic – custom 128-bit UUID, read and writable by centralBLEUnsignedIntCharacteristic AnalogData( “19B10011-E8F2-537E-4F6C-D104768A1214”, BLERead | BLENotify);BLEUnsignedIntCharacteristic AnalogData2(“19B10012-E8F2-537E-4F6C-D104768A1214”, BLERead | BLENotify);int old_data ;void setup() { Serial.begin(9600); // set advertised local name and service UUID: blePeripheral.setLocalName(“ARead”); blePeripheral.setAdvertisedServiceUuid(ARead.uuid());// add service and characteristic: blePeripheral.addAttribute(ARead); blePeripheral.addAttribute(AnalogData); blePeripheral.addAttribute(AnalogData2);// begin advertising BLE Light service: blePeripheral.begin(); Serial.println(“BLE AnalogRead 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()) {int new_data = analogRead(A0);if (old_data != new_data){int data1 = new_data % 128;int data2 = new_data / 128 + 128; //divide new_data into 2 value (1 byte is 8 bits)int send_data = data2 * 256 + data1; AnalogData.setValue(send_data); Serial.println(new_data); old_data = new_data;delay(50);}}// when the central disconnects, print it out: Serial.print(F(“Disconnected from central: “)); Serial.println(central.address());}} |