I’ve been looking at getting a ESP8266 NodeMCU to talk over Bluetooth using an HC-06.
The example will provide a simple way of echoing data from Bluetooth to a serial port.
The HC-06 exposes a serial port that the NodeMCU can use. In this example, I’m going to echo data between a the HC-06 and the serial port connected up to my Mac. As the NodeMCU only supports one serial port in hardware, we need to use Software Serial as well. This provides us a second serial port.
Wiring between the HC-06 and the NodeMCU is as follows
GND to GND
VCC to 3V
TXD to D4
RXD to D3
The code is a variation on the example code provided by Software Serial. We need to set the baud rate to 9600, and specify pins D4 and D3 for RX and TX.
#include <SoftwareSerial.h> #define BAUD_RATE 9600 SoftwareSerial swSer(D4, D3); void setup() { Serial.begin(BAUD_RATE); swSer.begin(BAUD_RATE); Serial.println("\nSoftware serial test started"); for (char ch = ' '; ch <= 'z'; ch++) { swSer.write(ch); } swSer.println(""); } void loop() { while (swSer.available() > 0) { Serial.write(swSer.read()); yield(); } while (Serial.available() > 0) { swSer.write(Serial.read()); yield(); } }
To test this on a Mac, plug it into the serial port, and set the Arduino Serial Monitor to 9600 baud. The message “Software serial test started” should appear.
Go to Bluetooth and add the device. Mine showed up as HC-06. The default pairing code is 1234
. Once paired, open a Terminal window and look for the device in /dev/
. My device name is /dev/tty.HC-06-SPPDev
. Connect to the device by typing
screen /dev/tty.HC-06-SPPDev
The message “ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz
” should appear.
What you type in one window is now echoed to the other window.