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:
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)); } } }