HC-05 Bluetooth module (Part 3)

Part 1, 2, 3

Now that we have the hardware and that we understand the fundamentals of module programming, let’s build a simple network. In this example, we will use a master and a slave module, each of them plugged onto a “basic platform” (see part 1).

The slave module shall be configured using the following set of commands:

  • AT: check if the module is responding
  • AT+STATE: Get the current module state
  • AT+ADDR?: Get the module address
  • AT+NAME=”slave”: Set the module name
  • AT+PSWD=”1234″: Set device’s pswd; master and slave must share the same pswd
  • AT+UART=38400,0,0: Set device’s serial param
  • AT+ROLE=0: Set the module’s role (slave)

The address of each module is expressed in three colon separated values: e.g. 98D3:31:F73314.

The master module shall be configured using the following set of commands:

  • AT: check if the module is responding
  • AT+STATE: Get the current module state
  • AT+NAME=”master”: Set the module name
  • AT+PSWD=”1234″: Set device’s pswd; master and slave must share the same pswd
  • AT+UART=38400,0,0: Set device’s serial param
  • AT+ROLE=1: Set the module’s role (master)
  • AT+RMAAD: Delete all authenticated previous paired devices
  • AT+BIND=98D3,71,F5D7D8: bind slave module to this master

Please note that the address is entered in three coma separated groups of values. Once the programming done, both module are ready to be used. Plug them on their respective platform. For a few seconds, both module’s LED will flash at a fast rate and then burst two flashes at a slow rate on completion of the pairing.
The best way to exercise these instructions is to load the following sketches on both platforms

#include <SoftwareSerial.h>

SoftwareSerial btSerial(2, 3); 

void setup()
{
    Serial.begin(38400);
    Serial.println("Ready");
    btSerial.begin(38400);     
}

void loop()
{
    /* read and print data from HC-05 module */
    if (btSerial.available())   
    {
        while (btSerial.available())
        {
            Serial.write(btSerial.read());
        }
    }  
    /* read data from console and send them wirelessly through the HC-05 module */
    if (Serial.available())    
    {
        while (Serial.available())
        {
            btSerial.write(Serial.read());
        }
    }  
}

Open two separate consoles (eg Putty) and link them to each platform.

Start with the platform holding the slave module: set the module to AT (or command) mode (See part 2), enter the commands as explained above, collect its serial number, reset the module to normal operation. Proceed with the master module set the module to AT mode (See part 2), enter the commands as explained above, collect its serial number, reset the module to normal operation. On completion of the configuration, check if the module managed to pair and start exchanging data through their respective consoles.

Leave a Reply

You must be logged in to post a comment.