This is 2nd part of the tutorial Bluetooth Weather Station with Arduino where we will test the DHT Sensor that we connected to the Arduino. In the 1st part we showed how to configure the hardware.
DHT Sensor Testing
We are about to create a straightforward Arduino sketch to validate all the sensors integrated into the project. The purpose of this sketch is to verify the accuracy of the connections before proceeding with the development of our Android app utilizing Bluetooth.
Here's the comprehensive sketch to test the sensors:
// Code to measure data and print it on the Serial monitor
// Libraries
#include "DHT.h"
// DHT sensor
#define DHTPIN 7
#define DHTTYPE DHT11
// DHT instance
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
// Initialize the Serial port
Serial.begin(9600);
// Init DHT
dht.begin();
}
void loop()
{
// Measure from DHT
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Measure light level
float sensor_reading = analogRead(A0);
float light = sensor_reading/1024*100;
// Display temperature
Serial.print("Temperature: ");
Serial.print((int)temperature);
Serial.println(" C");
// Display humidity
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println("%");
// Display light level
Serial.print("Light: ");
Serial.print(light);
Serial.println("%");
Serial.println("");
// Wait 500 ms
delay(500);
}
This Arduino code is a simple program to measure data from different sensors and display it on the Serial Monitor. It uses three sensors: DHT11 for temperature and humidity, and a light sensor connected to analog pin A0.
Let's break down the code step-by-step:
Libraries: The code includes the "DHT.h" library. This library allows communication with the DHT (Digital Humidity and Temperature) sensor.
DHT Sensor Configuration: The DHT sensor is defined using macros.
DHTPIN
is set to 7, which represents the digital pin to which the DHT sensor is connected.DHTTYPE
is set toDHT11
, indicating that we are using a DHT11 sensor. There are other types like DHT22 and DHT21 with higher accuracy but require different libraries.DHT Instance: An instance of the DHT class is created named
dht
, using theDHTPIN
andDHTTYPE
configurations.Setup Function: In the setup function, the Serial communication is initialized with a baud rate of 9600 (bits per second). The DHT sensor is also initialized using the
dht.begin()
function.Loop Function: This is the main part of the code that runs in a loop repeatedly.
DHT Readings: The code reads the temperature and humidity values from the DHT sensor using the
dht.readTemperature()
anddht.readHumidity()
functions, respectively. These values are stored in variablestemperature
andhumidity
.Light Sensor Reading: The code reads the light sensor's analog value using the
analogRead(A0)
function. The value is then converted to a percentage representing the light level and stored in thelight
variable.Serial Monitor Output: The code prints the temperature, humidity, and light level on the Serial Monitor. The
Serial.print()
function is used to display the values, andSerial.println()
is used to move to the next line after each reading.Delay: The code introduces a 500-millisecond delay using the
delay(500)
function before starting the next iteration of the loop. This delay allows time for the sensors to stabilize between readings and prevents excessive data updates on the Serial Monitor.
In summary, this Arduino code reads temperature and humidity from a DHT11 sensor and light level from an analog light sensor. It then displays these values on the Serial Monitor, providing a simple way to monitor the environmental conditions around the Arduino board.
Let's proceed with testing the basic Arduino sketch to verify the functionality of our sensors. Follow these steps to conduct the test successfully:
Upload the sketch to the Arduino board.
Open the serial monitor, ensuring the serial speed is set to 9,600.
Observe the output in the serial monitor. The results may vary depending on your surroundings, but you should expect to see something like this:
- Temperature: 26°C
- Humidity: 35%
- Light: 75.42%
Creating the Arduino Sketch
With confirmation that our sensors are functioning accurately, we are ready to compose the definitive sketch enabling communication between the Arduino board and the forthcoming Android application. Below is the comprehensive sketch for this phase:
// Control Arduino board from BLE
// Enable lightweight
#define LIGHTWEIGHT 1
// Libraries
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <aREST.h>
#include "DHT.h"
// Pins
#define ADAFRUITBLE_REQ 10
#define ADAFRUITBLE_RDY 2 // This should be an interrupt pin, on Uno thats #2 or #3
#define ADAFRUITBLE_RST 9
// DHT sensor
#define DHTPIN 7
#define DHTTYPE DHT11
// DHT instance
DHT dht(DHTPIN, DHTTYPE);
// Create aREST instance
aREST rest = aREST();
// BLE instance
Adafruit_BLE_UART BTLEserial = Adafruit_BLE_UART(ADAFRUITBLE_REQ, ADAFRUITBLE_RDY, ADAFRUITBLE_RST);
// Variables to be exposed to the API
int temperature;
int humidity;
int light;
void setup(void)
{
// Start Serial
Serial.begin(9600);
// Start BLE
BTLEserial.begin();
// Give name and ID to device
rest.set_id("001");
rest.set_name("weather_station");
// Expose variables to API
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);
rest.variable("light",&light);
// Init DHT
dht.begin();
// Welcome message
Serial.println("Weather station started");
}
void loop() {
// Measure from DHT
float t = dht.readTemperature();
float h = dht.readHumidity();
temperature = (int)t;
humidity = (int)h;
// Measure light level
float sensor_reading = analogRead(A0);
light = (int)(sensor_reading/1024*100);
// Tell the nRF8001 to do whatever it should be working on.
BTLEserial.pollACI();
// Ask what is our current status
aci_evt_opcode_t status = BTLEserial.getState();
// Handle REST calls
if (status == ACI_EVT_CONNECTED) {
rest.handle(BTLEserial);
}
}
This code is an Arduino sketch that allows the Arduino board to be controlled and accessed through Bluetooth Low Energy (BLE) using an Android application. The sketch is designed to create a weather station that measures temperature, humidity, and light levels and exposes these values through a REST API using the aREST library.Let's break down the code step by step:
Enabling Lightweight: The code starts with the definition
#define LIGHTWEIGHT 1
, which enables the use of lightweight features. The purpose of this is not mentioned explicitly in the code, but it might refer to reducing the memory footprint of the code to optimize for resource-constrained environments like Arduino boards.Libraries: Several libraries are included in the code:
SPI.h
: This library is used for Serial Peripheral Interface (SPI) communication.Adafruit_BLE_UART.h
: This library facilitates communication over BLE using the Adafruit Bluefruit LE UART Friend module.aREST.h
: This library allows easy creation of RESTful APIs on the Arduino.DHT.h
: This library supports the DHT (Digital Humidity and Temperature) sensor.
Pin Definitions: The code defines the pins used for communication with the Adafruit Bluefruit LE UART Friend module and the DHT sensor.
DHT Sensor Configuration: The code sets up the DHT sensor with the specified pin and type (DHT11). The
DHT
instance is created with this configuration.aREST and BLE Setup:
aREST
instancerest
is created.Adafruit_BLE_UART
instanceBTLEserial
is created with the specified pins for communication.
Exposed Variables:
- The variables
temperature
,humidity
, andlight
are declared, which will be exposed through the aREST API.
- The variables
Setup Function:
- The serial communication with a baud rate of 9600 is initiated.
- BLE communication is started using
BTLEserial.begin()
. - The device is given a name "weather_station" and an ID "001" to identify it on the network.
- The
temperature
,humidity
, andlight
variables are exposed to the API using therest.variable()
function. - The DHT sensor is initialized using
dht.begin()
. - A welcome message is printed to the serial monitor.
Loop Function:
- The loop function continuously reads data from the DHT sensor and the light sensor.
- The measured temperature and humidity are stored in their respective variables
temperature
andhumidity
. - The light sensor reading is converted to a percentage and stored in the
light
variable. - The
BTLEserial.pollACI()
function is used to handle communication with the BLE module. - The current BLE status is obtained using
BTLEserial.getState()
. - If the status is
ACI_EVT_CONNECTED
, the Arduino handles REST API calls usingrest.handle(BTLEserial)
.
In summary, this code sets up an Arduino-based weather station that can be accessed and controlled via BLE using an Android application. It measures temperature, humidity, and light levels, making this data available through aREST API endpoints.
Now is the moment to transfer the sketch to your Arduino board. Once this step is complete, you can proceed with creating the Android app to control the Arduino board using the BLE sketch.
See next part of this Bluetooth weather station tutorial: How to create a simple Android app to connect to the BLE module