Bluetooth Environmental Monitor

This project uses an Android app created with MIT App Inventor to receive and display environmental data from an Arduino board. The Arduino board fetches data from a DHT11 sensor to measure temperature and humidity. Once the data is collected, it’s transmitted over Bluetooth. The app will display the data on the screen once it receives it.

The Android application will be constructed utilizing a complimentary web-based platform known as MIT App Inventor. The project presents an excellent opportunity to gain familiarity with the interfacing of an Arduino with a smartphone.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Elite Explorer Kit

300+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Breadboard

BUY

Jumper Wires

BUY

Humiture Sensor Module

BUY

1. Build the Cirduit

../_images/08-bluetooth_environmental_monitor_bb.png ../_images/08_ble_home_schematic.png

2. Create the Android App

The Android application will be developed using a free web application known as MIT App Inventor. MIT App Inventor serves as an excellent starting point for Android development, owing to its intuitive drag-and-drop features allowing for the creation of simplistic applications.

Now, let’s begin.

  1. Go to Get Started with MIT App Inventor, and click “online tool” to login. You will require a Google account to register with MIT App Inventor.

    ../_images/08_ai_signup.png
  2. After logging in, navigate to Projects -> Import project (.aia) from my computer. Subsequently, upload the ble_environmental_monitor.aia file located in the path elite-explorer-kit-main\iot_project\08-bluetooth_environmental_monitor.

    You can also directly download here: ble_environmental_monitor.aia

    ../_images/08_ai_import.png
  3. Upon uploading the .aia file, you will see the application on the MIT App Inventor software. This is a pre-configured template. You can modify this template after you have familiarized yourself with MIT App Inventor through the following steps.

  4. In MIT App Inventor, you have 2 primary sections: the Designer and the Blocks. You can switch between these two sections in the upper right corner of the page.

    ../_images/08_ai_intro_1.png
  5. The Designer allows you to add buttons, text, screens, and modify the overall aesthetic of your application.

    ../_images/08_ai_intro_2.png
  6. Next, there’s the Blocks section. This section lets you craft custom functionalities for your app, allowing you to program each component on the app’s GUI to achieve desired features.

    ../_images/08_ai_intro_3.png
  7. To install the application on a smartphone, navigate to the Build tab.

    ../_images/08_ai_intro_4.png
    • You can generate a .apk file. After selecting this option, a page will appear allowing you to choose between downloading a .apk file or scanning a QR code for installation. Follow the installation guide to complete the application installation.

      You also download our pre-compiled APK here: ble_environmental_monitor.apk

    • If you wish to upload this app to Google Play or another app marketplace, you can generate a .aab file.

3. Upload the Code

  1. Open the 08-bluetooth_environmental_monitor.ino file under the path of elite-explorer-kit-main\iot_project\08-bluetooth_environmental_monitor, or copy this code into Arduino IDE.

    Note

    To install the library, use the Arduino Library Manager to search for and install “DHT sensor library” and “ArduinoBLE”.

  2. After selecting the correct board and port, click the Upload button.

  3. Open the Serial monitor(set baudrate to 9600) to view debug messages.

4. App and Bluetooth module Connection

Ensure that the application created earlier is installed on your smartphone.

  1. Initially, turn on Bluetooth on your smartphone.

    ../_images/08_app_1.png
  2. Now open the newly installed Environmental Monitor APP.

    ../_images/08_app_2.png
  3. When you first open this app, two authorization prompts will pop up in succession. These are the permissions required for using Bluetooth.

    ../_images/08_app_3.png
  4. In the APP, click on Connect button to establish a connection between the APP and Bluetooth module.

    ../_images/08_app_4.png
  5. This page displays a list of all paired Bluetooth devices. Choose the xx.xx.xx.xx.xx.xx UNO R4 Home option from the list. The name of each device is listed next to its MAC address.

    ../_images/08_app_5.png
  6. If you don’t see any devices on the page shown above, you can try turning on the location switch of the device (some Android system versions bundle the location switch with the Bluetooth function).

    ../_images/08_app_6.png
  7. After a successful connection, you will be redirected to the main page where it will display temperature and humidity.

    ../_images/08_app_7.png

5. Code explanation

  1. Importing Libraries and Defining Constants

    • Import the required libraries and define constants for the DHT sensor pin and type.

    Note

    To install the library, use the Arduino Library Manager to search for and install “DHT sensor library” and “ArduinoBLE”.

    #include <DHT.h>
    #include <ArduinoBLE.h>
    #define DHTPIN 11
    #define DHTTYPE DHT11
    
  2. Initializing BLE Services and Characteristics

    BLEService environmentalSensingService("181A");
    BLEShortCharacteristic temperatureCharacteristic("2A6E", BLERead | BLENotify);
    BLEUnsignedShortCharacteristic humidityCharacteristic("2A6F", BLERead | BLENotify);
    
  3. Setup Function

    • Initialize the Serial communication, DHT sensor, and BLE.

    void setup() {
      Serial.begin(9600);
      dht.begin();
      if (!BLE.begin()) {
        Serial.println("starting Bluetooth® Low Energy module failed!");
        while (1)
          ;
      }
      setupBle();
    }
    
  4. Main Loop

    • Poll for BLE events and update sensor data at regular intervals.

    • The millis() - lastUpdateTime > updateInterval line ensures that the sensor data is updated every updateInterval milliseconds.

    void loop() {
      BLE.poll();
      if (millis() - lastUpdateTime > updateInterval) {
        // Read sensor data and update BLE characteristics
      }
    }
    
  5. BLE and Debug Functions

    Functions for setting up BLE, printing debug information, and managing BLE events.

    void printDHT(float h, float t) { /* ... */ }
    void setupBle() { /* ... */ }
    void blePeripheralConnectHandler(BLEDevice central) { /* ... */ }
    void blePeripheralDisconnectHandler(BLEDevice central) { /* ... */ }
    
    • printDHT(float h, float t): Used to print the temperature and humidity data read by DHT11 on the serial monitor. This function is for debugging purposes.

    • setupBle(): Initializes Bluetooth, including setting the broadcast name, characteristics, and services.

    • blePeripheralConnectHandler(BLEDevice central) and blePeripheralDisconnectHandler(BLEDevice central): These functions handle events when Bluetooth connects or disconnects. When a device successfully connects with UNO R4 via Bluetooth, the onboard LED lights up. When the device disconnects, the LED turns off.