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+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
1. Build the Cirduit
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.
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.
After logging in, navigate to Projects -> Import project (.aia) from my computer. Subsequently, upload the
ble_environmental_monitor.aia
file located in the pathelite-explorer-kit-main\iot_project\08-bluetooth_environmental_monitor
.You can also directly download here:
ble_environmental_monitor.aia
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.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.
The Designer allows you to add buttons, text, screens, and modify the overall aesthetic of your application.
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.
To install the application on a smartphone, navigate to the Build tab.
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
Open the
08-bluetooth_environmental_monitor.ino
file under the path ofelite-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”.
After selecting the correct board and port, click the Upload button.
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.
Initially, turn on Bluetooth on your smartphone.
Now open the newly installed Environmental Monitor APP.
When you first open this app, two authorization prompts will pop up in succession. These are the permissions required for using Bluetooth.
In the APP, click on Connect button to establish a connection between the APP and Bluetooth module.
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.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).
After a successful connection, you will be redirected to the main page where it will display temperature and humidity.
5. Code explanation
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
Initializing BLE Services and Characteristics
Define the UUIDs for BLE Environmental Sensing Service and Characteristics. We are using the pre-defined UUIDs provided by Bluetooth SIG. The environmentalSensingService is assigned
0x181A
, while0x2A6E
and0x2A6F
are reserved for temperature and humidity, respectively.
BLEService environmentalSensingService("181A"); BLEShortCharacteristic temperatureCharacteristic("2A6E", BLERead | BLENotify); BLEUnsignedShortCharacteristic humidityCharacteristic("2A6F", BLERead | BLENotify);
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(); }
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 } }
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)
andblePeripheralDisconnectHandler(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.