Bluetooth Weather Station: DHT Sensor testing

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

void setup()
  // Initialize the Serial port
  // Init DHT

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.println(" C");
   // Display humidity
  Serial.print("Humidity: ");
  // Display light level
  Serial.print("Light: ");
  // Wait 500 ms

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:

  1. Libraries: The code includes the "DHT.h" library. This library allows communication with the DHT (Digital Humidity and Temperature) sensor.

  2. 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 to DHT11, indicating that we are using a DHT11 sensor. There are other types like DHT22 and DHT21 with higher accuracy but require different libraries.

  3. DHT Instance: An instance of the DHT class is created named dht, using the DHTPIN and DHTTYPE configurations.

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

  5. Loop Function: This is the main part of the code that runs in a loop repeatedly.

  6. DHT Readings: The code reads the temperature and humidity values from the DHT sensor using the dht.readTemperature() and dht.readHumidity() functions, respectively. These values are stored in variables temperature and humidity.

  7. 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 the light variable.

  8. 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, and Serial.println() is used to move to the next line after each reading.

  9. 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:

  1. Upload the sketch to the Arduino board.

  2. Open the serial monitor, ensuring the serial speed is set to 9,600.

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

// Libraries
#include <SPI.h>
#include "Adafruit_BLE_UART.h"
#include <aREST.h>
#include "DHT.h"

// Pins
#define ADAFRUITBLE_RDY 2     // This should be an interrupt pin, on Uno thats #2 or #3

// DHT sensor
#define DHTPIN 7 
#define DHTTYPE DHT11

// DHT instance

// Create aREST instance
aREST rest = aREST();

// BLE instance

// Variables to be exposed to the API
int temperature;
int humidity;
int light;

void setup(void)
  // Start Serial

  // Start BLE
  // Give name and ID to device
  // Expose variables to API
   // Init DHT
  // 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.
  // Ask what is our current status
  aci_evt_opcode_t status = BTLEserial.getState();
  // Handle REST calls
  if (status == ACI_EVT_CONNECTED) {
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:

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

  2. 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.
  3. Pin Definitions: The code defines the pins used for communication with the Adafruit Bluefruit LE UART Friend module and the DHT sensor.

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

  5. aREST and BLE Setup:

    • aREST instance rest is created.
    • Adafruit_BLE_UART instance BTLEserial is created with the specified pins for communication.
  6. Exposed Variables:

    • The variables temperature, humidity, and light are declared, which will be exposed through the aREST API.
  7. 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, and light variables are exposed to the API using the rest.variable() function.
    • The DHT sensor is initialized using dht.begin().
    • A welcome message is printed to the serial monitor.
  8. 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 and humidity.
    • 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 using rest.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. 

arduino bluetooth weather station

See next part of this Bluetooth weather station tutorial: How to create a simple Android app to connect to the BLE module

Post a Comment

Previous Post Next Post