Nice to meet you. It is s_rae. I am studying programming hard.
Recently I made a simple robot. It is a robot with two wheels and a Bluetooth module.
Let's create an interface that can operate the robot as a practice of application development! I thought.
I made a backend on RaspberryPi3B + with Django + Django Channels so that I can send Bluetooth messages with PyBluez.
It's still unfinished, but please take a look if you don't mind the process halfway through. Also, please note that it may be a little unnatural because your mother tongue is not Japanese.
--Robots and IoT devices capable of Bluetooth serial communication (The things that can be connected are still very limited, but the goal is to be able to connect various devices in the future.)
--Since it is a home server, please connect Raspberry Pi and the client side to the same network.
(Refer to the official website of Raspberry Pi for how to install the OS. You can easily install it using Raspberry Pi Imager.)
First of all, please access Raspberry Pi with ssh. I use Linux shell (bash).
$ ssh pi@IP address of Raspberry Pi
Please clone from the repository.
$ cd ~
$ git clone https://github.com/samanthanium/dullahan_local.git
$ cd dullahan_local
Please install redis.
$ wget http://download.redis.io/releases/redis-6.0.5.tar.gz
$ tar xzf redis-6.0.5.tar.gz
$ cd redis-6.0.5
$ make
Download the required package.
$ pip install -r requirements.txt
Find the project settings.py.
$ cd dullahan
Enter the Raspberry Pi's IP address in ALLOWED_HOSTS in settings.py using your favorite text editor.
settings.py
...
ALLOWED_HOSTS = ['IP address of Raspberry Pi']
...
Finally, try starting the test server.
$ cd ../ #~/dullahan_local
$ python manage.py migrate
$ redis-server &
$ python manage.py runserver IP address:8080 &
$ python manage.py runworker background-tasks
If you can do so far, the first stage will be successful! I thought.
Also, since I was just starting to study Python, I decided to make it in Python anyway.
I thought what I was trying to make was similar to a chat system (?). It feels like the Robot and the client side send and receive messages.
You may be able to use the popular WebSocket protocol here! I thought.
WebSocket is a full-duplex communication that takes place over TCP. It keeps the communication instead of disconnecting each time it sends a message like HTTP.
Django Channels adds WebSocket communication to the HTTP-based Django framework.
I've found that using Django and Django Channels allows HTTP to transfer a clean interface to clients and WebSockets to exchange messages in real time.
When you receive a WebSocket message from the client side, DjangoChannels sends the message to the consumer class (the class that processes the message). It is also possible to create a group with consumers and share messages within that group.
As a result of researching and reading various things, I thought of such a mechanism.
The client-side message-processing consumer sends the message to a background worker that has a robot consumer.
The robot consumer sends another message to the robot via Bluetooth. When receiving a message from a robot, it is done in almost the reverse pattern.
By using a background worker, you can get the sensor data of the robot continuously.
If you add the robot to the group, the same message may be sent to the robot many times, so I made it like this. This is one of many things that need to be improved.
The application looks like this.
You can see it in action at this link! (Image quality is low) https://youtu.be/UL7yFSde5Hw
Both Python and Django were new to me, so it's a shame I was messing around to make them.
I thought I should move it somehow, but it was hard to read the code again later.
I will continue to fix various things and add functions.
But when the robot moved, I was very happy!
Thank you for reading this far!