HC-05 Bluetooth Module Interfacing with NodeMCU

HC-05 Bluetooth Module Interfacing with NodeMCU

Introduction

HC-05 Bluetooth Module
HC-05 Bluetooth Module
HC-05 is a Bluetooth device used for wireless communication with Bluetooth enabled devices (like smartphone). It communicates with microcontrollers using serial communication (USART).
This device adds wireless communication protocol to the embedded applications through which it can communicate with any other Bluetooth enabled device.
AT commands are used to command the Bluetooth module. We can change its settings about password, its name, USART communications settings like baud rate, no. of stop bits or parity etc.
For more information about HC-05 Bluetooth module, its pin and how to use it, refer the topic HC-05 Bluetooth module in the sensors and modules section.
NodeMCU based ESP8266 has UART serial communication module through which it can communicate Bluetooth module. to know about Lua based NodeMCU UART functions refer NodeMCU UART with ESPlorer IDE.

Interfacing Diagram

HC-05 Bluetooth module interface with NodeMCU
HC-05 Bluetooth module interface with NodeMCU

Note: Default Bluetooth name of the device is “HC-05” and default PIN (password) for connection is either “0000” or “1234”.

Example

Let’s develop a small application in which we can control LED ON-OFF through a smartphone.
This is done by interfacing HC-05 Bluetooth module with NodeMCU. Data from HC-05 is received/ transmitted serially by NodeMCU.
In this application, when 1 is sent from smartphone, LED will Turn ON and if 2 is sent LED will get Turned OFF. If received data is other than 1 or 2, it will return message to mobile that select proper option.
We can write codes for NodeMCU DevKit in either Lua Script or C/C++ language. We are using ESPlorer IDE for writing code in Lua scripts and Arduino IDE for writing code in C/C++. To know more refer Getting started with NodeMCU using ESPlorer IDE (which uses Lua scripting for NodeMCU) and Getting started with NodeMCU using Arduino IDE (which uses C language based Arduino sketches for NodeMCU).

Lua Script for Bluetooth

LEDpin = 4

gpio.mode(LEDpin, gpio.OUTPUT)--set LED pin as output pin
gpio.write(LEDpin, gpio.LOW)-- set LED state initially low

--begin uart with specs
uart.setup(0, 9600, 8, uart.PARITY_NONE, uart.STOPBITS_1, 1)
--set callback function on receive to make decision about LED on/off
uart.on("data",1,
function(data)
    if(data == "1") then
        gpio.write(LEDpin, gpio.HIGH)
        print("LED ON")
    elseif(data == "2") then
        gpio.write(LEDpin, gpio.LOW)
        print("LED OFF")
    else
        print("select proper option") 
    end
end, 0)
Below is response received from NodeMCU Bluetooth while sending commands of above example on Bluetooth terminal of smart phone.
Bluetooth terminal window
Also, we can write code for above example from Arduino IDE. To know about how to start using NodeMCU with Arduino IDE refer Getting started with NodeMCU using Arduino IDE.

Arduino Sketch for Bluetooth

int LED = D4;

void setup() {
 pinMode(LED, OUTPUT);
 Serial.begin(9600); /* Define baud rate for serial communication */
}

void loop() {

 if (Serial.available())  /* If data is available on serial port */
    {
      char data_received; 
      data_received = Serial.read();  /* Data received from bluetooth */
      if (data_received == '1')
      {
       digitalWrite(LED, HIGH);
       Serial.write("LED turned ON\n");        
      }
      else if (data_received == '2')
      {
       digitalWrite(LED, LOW);
       Serial.write("LED turned OFF\n");
      }
      else
      {
       Serial.write("Select either 1 or 2");
      }
    }
}


Supporting Files
Source Code

No comments:

Post a Comment