[App Inventor IoT ] Lesson 5: Read DHT11 Temperature/Humidity Sensor


YouTube 影片



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's concept is as same as the previous one: [App Inventor IoT ] Lesson 4: 4-axis robotarm. The difference between the two is the read of analog pin. One is Arduino A0 analog pin, and the other is the temperature and humidity sensor readings element. This article will continue to read the temperature and humidity sensors, and displayed on the screen corresponding to the App Inventor Label.Please refer to the following instructions :

App Inventor Designer:


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:

1. Connection and Disconnection

When the program is initialized, it requires BluetoothLE1 element scan. Please note that this example is directly connect to the specified Bluetooth device (Arduino 101). If you need to select from a plurality of devices, we suggest you can pair Bluetooth first, and tap in from ListPicker is a better way. Note BLE device didn't need to be paired, only need to direct connection to the specified address. When Button_Connect pressed, it will require BluetoothLE1 element connect to the specified address of bluetooth devices (98: 4F ...), and stop scanning after setting the enabled properties of element.


2. 連線確認

From the previous step, BluetoothLE 1.connected event will start after a successful connection, we show some confirmation message here.


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).



5. Clock3(Call once every 0.005 seconds): Control offset and combined datas

Clock3 will execute BluetoothLE.ReadIntValue instruction first, it's to read an integer from Arduino 101. Then set the variable data to read the results of BluetoothLE.IntGattValue instruction, and if the data is between 99 to 256, it is the humidity value. This value decreased by 100 will become the humidity value we want. On the contrary it is the temperature value. The reason we do, because after we read the results of the temperature and humidity we combined them into a value and then sent to App Inventor. This approach can reduce the loss of data, as in the following program fragment:

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);    }
combine temperature / humidity into one value

Please note that service_uuid and characteristic_uuid should filled in "19B10011-E8F2-537E-4F6C-D104768A1214", Arduino 101 use this parameter to represent the BLE services it provides.


6.Disconnect


When the Button_Disconnect pressed, the device will ask BluetoothLE 1 Bluetooth device to disconnect. This practice is not the same as the previous, you need to compare more!




Arduino 101 Code

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());  }}
Read DHT22 temperature/humidity sensor