Controlling Led Connected to GPIO Pin Via Bluetooth of VisionFive
by Random Creations in Circuits > Raspberry Pi
2048 Views, 3 Favorites, 0 Comments
Controlling Led Connected to GPIO Pin Via Bluetooth of VisionFive
Bluetooth is a wireless technology that can transfer data over a short range of distance between seven devices. Today, every mobile device, stereo system, mp4 player, laptop, computer, and many more things are equipped with an On-Board Bluetooth module.
In the field of robotics and Electronics, we are also using Bluetooth technology for controlling our robots, our robotic cars, less, and many more things. We can make a whole automation system based on Bluetooth. We can control anything we want with our mobile devices via Bluetooth.
Some famous boards like Raspberry pi, and VisionFive are single-board computers and they are equipped with Bluetooth modules. Arduino is a microcontroller development board, although its most famous variants don't have an onboard Bluetooth module, options like HC-05 and HC-06 can be utilized in this case. In nutshell, Bluetooth is a feasible and widely available tool to control things.
In my case, I have VisionFive, which is a RISC-V computer designed to run Linux. It can be the best option for robotics. Luckily VisionFive comes with Bluetooth 4.2 (BLE). So I am going to use its Bluetooth module to control GPIOs.This includes some file manipulation so we need to be careful otherwise good luck with finding solutions to the problems you created, on stack overflow.
Note: The process is the same for Raspberry pi. All you will have to do will be to change the gpio pin numbers and some commands like Dnf and so.
Supplies
- VisionFive Board
- LEDs
- BreadBoard
- Jumper Wires
- Resistors (220 ohms, 330ohm or any value less than 1k )
Software& Apps
- Fedora Os running on my board. (Instructions remain almost the same on all Linux distros even on Raspbian)
- Serial Terminal [Here] (I'll recommend you to use it for the first time because it gives you a better interface for connecting to devices, settings of terminals and even setting up a few buttons)
- Arduino Serial Monitor (Or any app which gives you the best user interface)[optional]
Configuring Bluetooth for Serial Communication
The serial port is not configured on both, VisionFive and Raspberry Pi, by default. So we have to enable Serial Port Profile manually.
SPP(Serial Port Profile)
The original purpose of Bluetooth was to replace the RS-232 cables. But by emulating the SPP we can configure the Bluetooth as they were just using RX and TX lines for data transfer. Moreover, it is the most fundamental profile. And is great for sending bursts of data between devices.
So how to do it?
Let me help you with that.
Since SPP is not available by default but we can use Linux's Sdptool.
Enabling SPP
Execute the following command in your terminal.
sudo nano /etc/systemd/system/dbus-org.bluez.service
this will take you to a file that you have to edit now.
Look for a line starting with "ExecStart", at the end of this line add "-C". Press Enter and you will be on a new line. There you have to add this line:
ExecStartPost=/usr/bin/sdptool add SP
That's it. Now press Ctrl + S to save the file. Once it is saved, we can close it by pressing Ctrl + Z.
Now if you have successfully saved your file, we can reload it. o do so Execute the following command:
sudo systemctl daemon-reload
after executing this command, it may take 1 or 2 seconds, after that you will be back on your serial terminal line.
Now we can restart the services.
sudo systemctl restart bluetooth.service
Great! We have successfully enabled the SPP on our VisionFive.
Now we can pair our devices with our VisionFive.
For that either do it manually by clicking on the Bluetooth icon. Or execute the following commands in your serial terminal.
bluetoothctl
discoverable on
Turn your mobile's Bluetooth on and pair it with your VisionFive. In my case, its name was "RISCV". [ScreenShot]
sudo rfcomm watch hci0
The upper command will help you to see whether the connection was successful or not.
If the connection is successful then you will be able to see the connected device's ID.
If it is done then we can move on to viewing and monitoring our data. Open a separate terminal for the following procedure.
For that, there are several tools available on Linux. We will use the Minicom tool. In my case, it was pre-installed, but if you don't find it by executing this command you can install it with:
sudo dnf install minicom -y
But I'll skip it since I already have it.
Execute the following command in this new terminal that you have opened separately. [Check The Screenshot]
sudo minicom -b 9600 -o -D /dev/rfcomm0
Baudrate can be changed with 115200 or something else if you get garbage values. But in most cases, 9600 will do the work since it is a standard value.
If you have everything done correctly, then until now, you have two serial Terminals opened on your desktop. On the first one, you will be seeing that your device has been paired up with your VisionFive, and on the second Your Minicom screen is up and running. And your mobile device is also in your hand.
Now if you type and send anything from the serial terminal of your App. You will be able to see it on your minicom screen.
[ScreenShot added]
Ta-Da............... It's done.
Now time for some Python Coding.
Interfacing Bluetooth With Python
Python code is really simple. All we have to do is to import the Bluetooth library and connect.
The data we will be receiving will be in the following format:
b'Hello world'\r\n
We can remove \r\n which is a carriage return and new line feed. For that simply go to the setting of your App and there select the option:
none
That's it. Now our data will be in byte format, i.e,
b'Hello World'
This can be cleared by decoding our data.
And now we can use our data with python code.
I have already discussed how to use GPIOs on VisionFive so I'll not cover that.
Download the file from the Github account (Link is given). and run it.
Note: Before Running the python script make sure Both devices are paired. And also no need to do anything else.
Just:
- Pair both Devices
- Run the Sketch
- Connect the app or Refresh the status by connecting and reconnecting the serial Terminal of the App while the script is running.
- If any error related to {already in use} appears, simply restart the Bluetooth service.
Circuit Diagram
Connect everything according to the circuit diagram.
It's Done
Now if you have done everything correctly then this project will be up and running. If you have missed anything or any error is popping up then reread everything carefully and execute every command carefully. If this still doesn't solve your problem then try to understand the statement of error. Google it if necessary. Go the Stackoverflow if necessary and that solves everything.
You can watch my video for results and for clear instructions.