Creating the Arduino sketch
Let's now build a simple sketch to test our sensor via Bluetooth. We are going to build a sketch so that it measures the temperature and humidity from the DHT sensor when a given command is received on the serial port.
We start by including the DHT library so that we can use the DHT11 sensor:
#include "DHT.h"
Then, define the sensor's pin and type:
#define DHTPIN 7 #define DHTTYPE DHT11
Note that if you are using a different DHT sensor, you will need to modify the code here. For example, if you are using a DHT22 sensor, simply type the following:
#define DHTTYPE DHT22
You will also need to define an instance of the DHT sensor and to send the sensor type and sensor pin as an argument:
DHT dht(DHTPIN, DHTTYPE);
In the setup()
function of the sketch, you will have to initialize the DHT sensor's instance and start the serial connection:
dht.begin(); Serial.begin(115200);
We need to use a speed of 115,200 bauds here instead of the standard 9600 bauds...