"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"

Intro

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.

Trial

Things necessary

--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.)

Installation method

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

Creation process

setting a goal

  1. Connect Raspberry Pi and robot with Bluetooth
  2. Show a beautiful operation screen on the client side
  3. Somehow operate the robot in real time

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.

Investigation stage

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.

IMG_20200702_192959120.jpg

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.

How it works

As a result of researching and reading various things, I thought of such a mechanism.

IMG_20200702_172226007.jpg

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.

What I noted here is:

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.

result

The application looks like this.

Home Screen

11-dul.png

Device registration screen

3-dul.png

Control screen

6-dul.png

+ Robot

You can see it in action at this link! (Image quality is low) https://youtu.be/UL7yFSde5Hw

Impressions

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!

Recommended Posts

"1/2" I am making a simple web application for robot operation. "Raspberry Pi 3B + and Django Channels"
I made a WEB application with Django
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
Create a web surveillance camera with Raspberry Pi and OpenCV
I tried connecting Raspberry Pi and conect + with Web API
I made a resource monitor for Raspberry Pi with a spreadsheet
(For beginners) Try creating a simple web API with Django
I made a web server with Raspberry Pi to watch anime
Make a simple CO2 incubator using Raspberry PI and CO2 sensor (MH-Z14A)
Launched a web application on AWS with django and changed jobs
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
I want to make a web application using React and Python flask
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
[Raspberry Pi] Add a thermometer and a hygrometer
I tried benchmarking a web application framework
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server --2 PHP introduction
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Source compile Apache2.4 + PHP7.4 with Raspberry Pi and build a Web server ―― 1. Apache introduction
I tried running Flask on Raspberry Pi 3 Model B + using Nginx and uWSGI
The story of making a web application that records extensive reading with Django
I tried to create a button for Slack with Raspberry Pi + Tact Switch
Try creating a web application with Vue.js and Django (Mac)-(1) Environment construction, application creation