Using Bluetooth saves a lot of wear and tear on your USB ports, and eliminates the risk of damaging your computer if you make a wiring mistake.
Above: The NodeMCU ESP8266 development board (right) connected to a HC-06 bluetooth module (left)
In a previous article I talked about getting started with the NodeMCU platform, which is a programming environment for the ESP8266 WiFi microcontroller.
If you use the "NodeMCU Development Kit" reference board, you get the ESP8266 module itself, plus a USB interface that you can use to power the board, and also to download your code to it. The popular Arduino physical computing platform also has built in USB.
However if, like me, you use an ultralight laptop computer with a limited number of USB ports, it might be more convenient to use a Bluetooth interface to download code from your PC. This also has the advantage that there is literally no way that any mistake you make in your experimental circuits could damage your PC.
The ESP8266 doesn't have Bluetooth built in, but it is easy to add Bluetooth by using a Bluetooth-to-serial module. The "HC-06" module is available for about $4 from many vendors, such as here.
This module acts as a bluetooth character device, and once you have paired with it, you can use it just like any other serial port, using the same tools you would use to download your programs over USB.
Setting up your device
You can think of the HC-06 module as a dual interface bridging device. Anything received wireslessly on the Bluetooth side is transmitted as asynchronous serial via the wired interface. Anything received on the wired serial side is transmitted via the wireless bluetooth interface. Once the device is connected it is "transparent", that is the device simply relays whatever it receives without changing it.
Out of the box the HC-06 module answers to the name 'linvor', and operates at 9600 bits per second (bps). Arduino and ESP8266 typically work at 115200 bps, so we need to reprogram the HC-06 to work at this speed. To do this, we must connect to the serial interface on the HC-06 and send some commands.
If your application works at 9600, and you are happy with the default device name, you can skip this section.
To reprogram the HC-06 you will need a 5-volt serial port. The easiest way to get this is to buy or borrow a USB-to-serial adapter. This module is one I prefer, as it is switchable between 5v and 3.3v for the serial signalling voltage. Mac users might prefer this device which uses a USB chip from FTDI, as I have found the drivers for this chip are well supported on Mac.
To program your device, use jumper cables to wire like so:
- 5v on HC-06 connects to 5v on USB adapter
- Ground on HC-06 connects to ground on USB adapter
- Transmit (TX) on HC-06 connects to Receive (RX) on USB adapter
- Receive (RX) on HC-06 connects to Transmit (TX) on USB adapter
Note that transmit and receive are "crossed over". If you think about it, it makes sense, whatever the USB adapter transmits, the bluetooth module must recieve.
Above: Wiring a USB-to-serial adapter for initial programming of a HC-06 Bluetooth adapter
Once you have the physical connections made, it's time to connect to a PC and use a Serial Terminal program. If you have Windows, use Hyper Terminal, if you have Linux or Mac use "/usr/bin/cu".
Note: The HC-06 module has a quirk, you must send an entire command in under around half a second, or the module will ignore the command. Don't try to type the commands, prepare them in a text editor, then paste each command into the terminal window one by one.
We are going to send three commands to the HC-06
- AT+VERSION - Request the device to report its version. We do this only to confirm that the connection works.
- AT+NAME=nodemcu2 - Give the HC-06 device a name. I have chosen to name it "nodemcu2" (because I have another HC-06 that I named "nodemcu")
- AT+BAUD8 - Set the serial data rate to 115200 bps
The HC-06 understands a limited number of "AT commands" which is a command scheme commonly used by analog modems. See this article if you want to know more about the commands that the module understands.
To begin, we connect to the USB-serial adapter by running a program called 'cu' (which stands for 'Call Up' as it was originally used with serial-connected analog modems). On Linux this is as below. On Mac it will be /dev/tty.something depending on the model of USB adapter you have.
cu -l /dev/ttyUSB0 -s 9600
Once you are connected, paste in the below commands one at a time. You will not see the text that you paste on screen, but you should see answers to the commands as shown in the screenshot below.
To exit the cu program you must press the return, tilde (~) and period (.) keys in succession.
Once you have completed the reprogramming, disconnect, and reconnect using 115200 bps. To test that the device is now working at 115200 bps, paste the version command again. You are now ready to pair the device, and are finished with the USB-to-serial adapter.
AT+VERSION AT+NAME=nodemcu AT+BAUD8
Above: configuring a HC-06 adapter by pasting commands into a terminal window
Troubleshooting your USB adapter
If you cannot get the HC-06 to answer, confirm that your USB adapter is working. By connecting its transmit pin back to its own receive pin, you create a "loop back" situation, where anything the USB converter transmits, it also receives.
Connect to the device and type some text. You should see it appear on screen. If you disconnect the wire and type some more, text should not appear.
Above: testing a USB-to-serial adapter in loop-back arrangement
Pairing your HC-06
In order to have your computer present the HC-06 as a usable device, you must "Pair" the device using your computer's bluetooth settings. This is a one-time operation. You can pair an HC-06 with several computers if you need to.
On a Mac, open the System Preferences app and select the bluetooth panel. Look for your HC-06 to appear in the "available devices" pane, and select "pair".
Above: MacOs is searching for Bluetooth devices available to be paired. Here we have two devices already paired, and have discovered two unpaired devices.
Note that the pairing will fail initially, because your Mac does not know the PIN for the HC-06. The default pin for HC-06 is "1234". Select "Options" and enter the correct PIN.
Above: Initial pairing of a HC-86 will fail to complete as MacOS does not know the pairing code. Click 'options' to enter the code
Above: Entering the pairing code for a HC-06 module. The default code is '1234' (which is the same code I have on my luggage).
Once your device is paired, it will appear as a file in the /dev/directory, eg. my device is /dev/tty.nodemcu2-DevB.
Above: Successful pairing of a HC-06 bluetooth module.
On Linux or Windows your bluetooth device will be /dev/ttySomething or COMsomething: respectively.
Using the HC-06 to communicate with serial devices
Now that you have a paired HC-06 you can connect to it like you would any other serial port (such as the USB-to-serial adapter we used above).
cu -l /dev/tty.nodemcu-DevB -s 115200
When using an IDE to interact with NodeMCU devices, your bluetooth serial port should now appear in the menu of available serial ports.
Above: Selecting the HC-06 bluetooth adapter in the ESPlorer IDE software
Thank you for this interesting article.
I read your blog now share great information here.
lcd monitors manufacturer
Jamulal Casino Resort Spa-Maharashtra, India - Jtmhub.com
JtmHub offers world-class casino and slot gaming, with over 700 exciting slots, table 남원 출장샵 games, and a 계룡 출장안마 luxury 전라북도 출장샵 spa resort. 구리 출장안마 Visit 이천 출장마사지 JTM Hub to play our
Post a Comment