I was looking for a way to publish to MQTT or HTTP, and I struggled using the ESP8266 WiFi module to accomplish this. I was looking to create a web dashboard of data sent from an Arduino. Luckily, I found this great library that helped me accomplish this: https://github.com/RoanBrand/ArduinoSerialToTCPBridgeClient
Through this, web dashboards or websites can display data from the Arduino without the need for a WiFi module like the ESP8266. This process can be used for HTTP as well (use the other example program in the Serial to TCP library) or without a Bluetooth module (connection with a serial cable between Arduino and laptop).
Here are the steps that I took:
1. Configure the Bluetooth module through AT commandsI used this tutorial to wire Arduino and set to AT mode:
https://www.instructables.com/AT-command-mode-of-HC-05-Bluetooth-module/
First, wire the Arduino and HC-05 module like this:
VCC to 5V, GND to GND, TX to pin 10, RX to pin 11, and State/EN to pin 9
Upload the code found on the instructable (https://www.instructables.com/AT-command-mode-of-HC-05-Bluetooth-module/) to the Arduino Uno.
To enter AT mode, initially disconnect the board from power. Next, hold down the button on the HC-05 Bluetooth module. While pressing down the button, apply power to the board again. Hold down the button until the LED stays on (around one second), then release the button. If the LED on the Bluetooth module is slowly blinking, then the module is in AT mode.
Open the Arduino IDE serial monitor to enter commands. Change the baud rate to 9600, and change "Newline" to "Both NL and CR".
I followed this video for configuration commands:
Here is a summary of the commands from the video.
1. Enter "AT" to the serial monitor. If you receive a response of "OK", the Bluetooth module is ready to receive commands.
2. Enter "AT+UART = 115200, 0, 0" to set the baud rate to 115200. You should receive "OK".
3. Enter "AT + POLAR = 1, 0". You should receive "OK".
After the HC-05 is successfully configured, enter "AT + RESET" to exit AT mode. The LED should go back to blinking quickly.
Remove the jumper wire connected to pin 9 of the Arduino and the State/EN pin of HC-05. Rewire the jumper cables so that the TX pin of the HC-05 connects to the RX pin (pin 0) of the Arduino, and the RX pin of the HC-05 connects to the TX pin (pin 1) of the Arduino.
Connect the Bluetooth module to power. Then, open Settings and view the list of available Bluetooth devices. The HC-05 module should appear. Pair the Bluetooth module by selecting the HC-05 in available devices (or any other name it has) and connecting with the pin "1234". This will be repeated later on, but this is just so that the serial port appears for the next steps.
Note: the HC-05 needs to be forgotten and reconnected using the pin number (1234) each time the Arduino is disconnected then reconnected to power.
3. Download the bridge protocol that enables the connectionDownload the code at this link: https://github.com/RoanBrand/SerialToTCPBridgeProtocol
Make sure you can find this in your files.
4. Find the port of the Bluetooth moduleOpen a new terminal window and type this command:
ls /dev/tty.*
This will list the available serial ports:
Copy the port listed for your HC-05 Bluetooth module. If you want to use the serial connection between your computer and Arduino instead, copy the port for the USB.
5. Open the config.json file inside the bridge protocol folder (SerialToTCPBridgeProtocol-master)I used Visual Studio Code to open the file. Set the port to the port from the list that you just copied:
Save the file with the changes.
6. Download the Serial to TCP Bridge Client libraryVisit this link and download the library: https://github.com/RoanBrand/ArduinoSerialToTCPBridgeClient
Move this to where your Arduino libraries are located. You can also download this from the Arduino library manager.
7. Upload the example code to the ArduinoOpen the library MQTTClient program from the library's examples. Edit the program to reflect the broker, port number, and topics that you are using. I used the broker.hivemq.com broker on port 1883 with the topic of "ArduinoOut". When uploading, disconnect the RX and TX pins of the Arduino. Reconnect pins once successfully uploaded.
8. Connect the computer and Arduino wirelesslyUnplug your Arduino from your computer and use an external power source to power it. Make sure that the Bluetooth module is receiving power.
Next, connect your computer to the Bluetooth module by viewing available Bluetooth devices in Settings. Select the Bluetooth module and enter the pin '1234' to connect. I noticed that I need to forget the device from Settings and reconnect with the pin number '1234' each time I disconnect the Arduino from power.
Once connected, go to the Arduino IDE and select the port as your Bluetooth module.
I used the Mosquitto MQTT client in the terminal. This requires the installation of Mosquitto. Once installed, use this command to subscribe to the ArduinoOut topic:
mosquitto_sub -h "broker.hivemq.com" -p 1883 -t "ArduinoOut"
10. Run the gateway protocol on your computerWithin the SerialToTCPBridgeProtocol-master folder, there is a program called "example.go". Run this through an IDE or through the terminal. To run in the terminal, you must first install Go (programming language) to your computer. Once installed, open a new terminal tab at the folder and enter:
go run example.go
This will start the gateway protocol.
11. Successfully connected!Once the gateway protocol has started, you should receive messages from the Arduino (alive time and confirmation messages) in the client subscribed to the ArduinoOut topic! To turn on and off the built-in LED through MQTT as the example code has written, publish these messages using Mosquitto in the terminal:
mosquitto_pub -h "broker.hivemq.com" -p 1883 -t "LedIn" -m 1
mosquitto_pub -h "broker.hivemq.com" -p 1883 -t "LedIn" -m 2
Hopefully, this tutorial helped you successfully use MQTT with an Arduino Uno and HC-05 Bluetooth module without the need for a WiFi module like ESP8266! Please let me know if I made mistakes or if any steps are unclear.
Comments
Please log in or sign up to comment.