Make your own Internet of Things

The Internet of Things has well-and-truly reached buzzword status and everyone is making all sorts of gadgets designed to hook into your phone or tablet – as well as the internet. But don’t think you’d need a multinational corporation with a stratospheric budget – you can do it yourself for about $25.

Bluetooth module

bt-1The key is a tiny little Bluetooth module called the HC-06 that you can pick up eBay for as little as $5 including shipping.

Like any other Bluetooth-ready device, it can sync or ‘pair’ with your Android or iOS phone or tablet, even your PC or notebook with a suitable Bluetooth transceiver.

We’re going to take a quick look at how it works by combining it with the DHT22 temperature/humidity sensor from a previous Arduino Basics masterclass to create a simple remote weather station you can read using a free Android app.

Building the project

circuitHere’s a quick list of what you need to build this project:

  • Arduino Uno R3 microcontroller board
  • DIY prototype shield
  • 7k-ohm 0.5-watt resistor
  • 10k-ohm 0.5-watt resistor
  • DHT22 sensor
  • HC-06 Bluetooth module
  • About 10 Dupont breadboard wires

Follow the diagram to build up the project and the circuit diagram tells you how it all works. Grab the Arduino source code from our website, load it into the Arduino IDE (available here), compile it and flash it to the Arduino board.

Plug the DIY prototype shield into the board and power it up. If you keep the USB cable plugged into your PC, press the Monitor button top-right of the IDE window, set the Serial port speed at the bottom-right of the Serial Monitor window to ‘9600’ and you should begin to see temperature and humidity readings.

The reason for the two resistors is that we need to do some very basic voltage-level translation – the Bluetooth module runs off 3.3VDC, but the Arduino I/O pins are 5VDC. We make a simple voltage divider using the resistors to drop the Arduino I/O pin voltage from 5VDC to 3.4VDC, which is close enough to 3.3VDC.

Getting the Android app

linkTo communicate with the Bluetooth module, you’ll need a Bluetooth-ready Android phone or tablet and the free ArduDroid app from Google Play. Download it, install it. Power up the project and go to your Bluetooth options in the Android Settings menu and pair up to the Bluetooth module, which should appear as ‘HC-06’ in the ‘available devices’ list. When you’re asked for it, the password should be ‘1234’. When that’s done, launch the ArduDroid app, press the menu button and tap on ‘Connect me to a Bluetooth device’.

When you see the HC-06 module listed in the new window, tap on it. This should take you back to the main app window and if you now look at the HC-06 module on your project, the red LED that was flashing should now be lit solid, indicating it’s linked to your device.

Tap the ‘Get Data’ button on the ArduDroid main screen and you should see the temperature and humidity data now appear. Data readings are taken one per second, so tap the button again to get the latest. Bluetooth is only good for up to around ten metres, so make sure you’re quite close initially, just to make sure the project is working correctly.

How the code works

outputIn order to see output on the Serial Monitor and communicate with the Bluetooth module at the same time, we’ve incorporated the AltSoftSerial library to handle the HC-06 module while the Arduino’s hardware serial port talks to the Arduino IDE and your PC.

To cut down on the wiring, we’re also using the trick of powering the DHT22 sensor via the Arduino digital I/O pins. We do that with the DHTvcc, DHTdat, DHTgnd pinMode writes inside the setup() method. (Remember, in Arduino, the setup() method runs once before handing over to the loop() method, which loops until the power is removed).

The last thing the setup() method does is to initialise the sensor and indicate to the Serial Monitor whether the sensor is working correctly or has a problem.

Bluetooth data transfer

When you press a button on the ArduDroid app, it transmits a character string to the Bluetooth module, which reads it one byte at a time. So the first thing we have to do is check whether there is any data available using the arduinoBT.available() method:

if (arduinoBT.available()) {

If no data is available, we send the Arduino code around the loop again – we wait one second (1000 milliseconds), take a temp/humidity reading and check if the Bluetooth module has received any data.

When that if-statement is true, we then read the first available character and check to see if it’s the start-command character ‘*’. If it is, we read the command byte, the pin byte, followed by the pin-value byte. The form of the bytes received is *-10-99-99-#.

Because we want the Arduino to transmit back the current temp and humidity levels, the only byte we’re interested in is that pin byte. If that pin byte is ‘99’, the app’s ‘Get Data’ button has been pressed, requesting the temp and humidity readings. We send the latest readings back using the AltSoftSerial library’s ‘print’ command, which is more like the standard Serial.print and much easier to use than reading incoming bytes.

We print the data from variables DHTtemp and DHThumid to the ‘ArduinoBT’ software serial port connected to the Bluetooth module, it transmits the data, the Android device’s Bluetooth module picks it up and displays it on the app.

Give it a go

rwsUsing a Bluetooth module with Arduino is pretty much just setting up a wireless serial port – it’s a matter of reading bytes coming in that usually indicate a data request from a phone or tablet app and sending back the required data as text using the Serial.print function.

The top data transfer speed is actually set by the Arduino and the AltSoftSerial library to 57600 bytes per second (bps). The Bluetooth module itself defaults to 9600bps, which is old dialup-modem territory, but still fine for basic text and control data transfer like we’re using here. Not bad for $25.