Arduino sketch

For this project, you will need the Arduino IDE installed, along with the DHT sensor library and the Python PySerial module.

We'll first test the Bluetooth module to see if everything is connected correctly. You need to pair the Bluetooth module with your computer first. It depends on your OS, but you will usually have a "Bluetooth preferences" menu to search for new Bluetooth devices:
Once the device is paired with your computer, you can reopen the Arduino IDE and test if the Bluetooth connection is working. In Tools>Serial Port, you should have new choices for your Bluetooth device. Choose the second one:
You can now work with the Arduino IDE as if the Arduino board was directly connected to your computer: you can plug your Arduino board to an external source of power like a battery, and use the Bluetooth connection to upload sketches.

To try it out, just load the "Blink" sketch, and click on upload: after a while the sketch should be uploaded (it takes longer than with a USB cable) and the onboard LED of the Arduino Uno should blink.

We now need to write the code for the Arduino, so it measures the temperature & humidity when it receives a given command on the Serial port. This command will later be sent by your computer, but for now we'll just make a simple test to make sure the Arduino part is working.

The core of the sketch is to make the Arduino answer with the temperature & humidity measurement on the Serial port when a given character is received. I chose the character "m" for "measurement" to make the Arduino send the measurements over the Serial port. This is the part of the code that does exactly that:

byte c = Serial.read ();

// If a measurement is required, measure data and send it back
if (c == 'm'){

   int h = (int)dht.readHumidity();
   int t = (int)dht.readTemperature();

   // Send data (temperature,humidity)
   Serial.println(String(t) + "," + String(h));
}

This is the complete sketch for this part:

// Bluetooth temperature sensor
#include "DHT.h"

// Pin for the DHT sensor
#define DHTPIN 7    
#define DHTTYPE DHT22
// #define DHTTYPE DHT11

// Create instance for the DHT sensor
DHT dht(DHTPIN, DHTTYPE);

// Setup
void setup(void)
{
  dht.begin();
  Serial.begin(115200);
}

void loop(void)
{
    // Get command
    if (Serial.available()) {

      // Read command
      byte c = Serial.read ();

      // If a measurement is requested, measure data and send it back
      if (c == 'm'){

          int h = (int)dht.readHumidity();
          int t = (int)dht.readTemperature();

          // Send data (temperature,humidity)
          Serial.println(String(t) + "," + String(h));
      }
  }
}
Now upload the sketch (via Bluetooth of course!), open the serial monitor, and type in "m" and click send. This is what you should see on the Serial port:
This means whenever the Arduino will receive the character "m", it is going to return to correct measurements (in this case the temperature was at 26 degrees Celsius and the humidity was at 34 %).
Last updated on May 04, 2015 Published on Feb 07, 2014