Part List
1. Arduino 101
2. Android phone (Must have BLE hardware, but I think it’s not a problem for most Android devices today).
3. Grove – Temperature&Humidity Sensor Pro. Special article in this series will be used Grove sensor kit for Arduino kit to produce, because MIT App Inventor also use this as the main teaching kit.
This article only use the label to render temperature and humidity sensor’s value, you can add different visual elements or trigger reactionon the phone. For example, through the TextToSpeech element to read out the value or send email automatically, etc. This article uses 3 Clock element, you can refer to this practice, if you have a better ideas, it’s very welcome to share with us.
These three Clock elements’ parameters are the better after several tests. You may need to adjust these parameters to your actual situation. Depending on the difference protocol, you can’t use the BluetoothClient element to communicate with your Arduino 101.
App Inventor Blocks:
3. Clock1 (Call once per second): Continuous reading the return value of Arduino 101.
This example uses 3 Clock elements, please see the following instructions:
Clock1 according to the variable values of disconnect (true / false) to know whether there is connecting with the specified Arduino 101. If true, it will continue to determine whether there is a connection. If it is in the connection status, it wil be disconnected (BluetoothLE.DisconnectWithAddress), otherwise start scanning (BluetoothLE.StartScanning). If the variable value of disconnect is false, then began the most important combination of variable values, but you need to start Clock2 and Clock3’s timer event. And update the variables of the temperature and humidity to the corresponding Label. Nothing to it, right?
4. Clock2(Call once every 0.9 seconds):
Clock2 will put it’s timer event and Clock3’s timer event closed every 0.9 seconds, and then followed the actual situation to start (that means the variable values of disconnect).
while (central.connected()) { int h = dht.readHumidity(); int t = dht.readTemperature(); if (datachoice == 0) { send_data = h + 100;//send humidity datachoise = 1; } else { send_data = t; //send temperature datachoice = 0; } DHT22_Data.setValue(send_data); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.println(); delay(50); }
You just need to copy the draft code and uploaded to the Arduino 101. Please note Arduino 101 has Bluetooth 4.0 that you don’t have an external Bluetooth device such as HC05.
In this code, you should specify service_uuid and characteristic_uuid, they are “19B10011-E8F2-537E-4F6C-D104768A1214” here.
#include <CurieBLE.h>#include "DHT.h"#define DHTPIN 2DHT dht(DHTPIN, DHT22);BLEPeripheral blePeripheral; // BLE Peripheral Device (the board you're programming)BLEService HT("19B10010-E8F2-537E-4F6C-D104768A1214"); // BLE Service// BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by centralBLEUnsignedIntCharacteristic DHT22_Data( "19B10011-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);int datachoise = 0;int send_data = 0;void setup() { Serial.begin(9600); // set advertised local name and service UUID: blePeripheral.setLocalName("HT"); blePeripheral.setAdvertisedServiceUuid(HT.uuid()); // add service and characteristic: blePeripheral.addAttribute(HT); blePeripheral.addAttribute(DHT22_Data); // begin advertising BLE service: blePeripheral.begin(); Serial.println("BLE Read service start."); dht.begin();}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 h = dht.readHumidity(); int t = dht.readTemperature(); if (datachoise == 0) { send_data = h + 100;//send humidity datachoise = 1; } else { send_data = t; //send temperature datachoise = 0; } DHT22_Data.setValue(send_data); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.println(); delay(50); } // when the central disconnects, print it out: Serial.print(F("Disconnected from central: ")); Serial.println(central.address()); }}