Skip to content

Latest commit

 

History

History
592 lines (415 loc) · 16.4 KB

server-programming-manual.md

File metadata and controls

592 lines (415 loc) · 16.4 KB

0113-MyoCoach-DIY :
Server Programming Guide

myocoach


The MyoCoach v2.0 application allows the visualization of raw EMG signals and allows users to train by doing exercises or through video games. As it is a web application with a responsive design, it can be accessed using a web browser installed on a desktop workstation or on a mobile device such as a smartphone or a tablet.

The MyoCoach v2.0 application has been created using the following technologies :

  • Back-end development : Python Flask, SQLite, Socket-IO
  • Front-end development : TypeScript, React, Material-UI
  • Games development : Godot Engine

The original environment board chosen to host the application is a Raspberry Pi 3 model B+ but the installation process remains identical with later versions of Raspberry Pi boards and can probably be adapted for other development boards or desktop workstations running under a GNU/Linux OS.

OS setup

Download a Raspberry Pi OS Lite image available at the following address from a workstation : https://www.raspberrypi.org/software/operating-systems/#raspberry-pi-os-32-bit

Unzip the downloaded archive to get the image file (.img file extension).

From a terminal, go to the directory where the .img file is then connect a MicroSD card on the workstation (16Gb A1 minimum recommended). The MicroSD card has to be unmounted if it has been mounted automatically.

Then run the following command :

Note : Replace 2021-05-07-raspios-buster-armhf-lite by the actual .img file name.

~> sudo dd bs=1m if=2021-05-07-raspios-buster-armhf-lite.img of=/dev/rdisk2

Eject the MicroSD card from the workstation SD slot then insert it in the Raspberry Pi MicroSD slot. Connect a screen, a keyboard and an ethernet cable then power on the Raspberry Pi board. Wait for the system to be ready.

Once the system started, authenticate on the terminal with "pi" user (password: raspberry).

SSH access

On the Raspberry Pi, enable ssh access allow remote connection from other workstation. Run the following command :

pi@raspberrypi:~ $ sudo raspi-config

Select the following option on the user interface : 3 Interface Options, P2 SSH then select Yes. Finally, select Finish to save configuration and get back to the command prompt.

Keyboard layout

It may be necessary to configure the type of keyboard used. Run the following command :

pi@raspberrypi:~ $ sudo raspi-config

Select the following option on the user interface : 5 Localisation Options, L3 Keyboard then follow the instructions to configure the keyboard (for example : Logitech, French-French (legacy, alt.), The default for the keyboard layout, No compose key). Finally, select Finish to save configuration and back to the command prompt.

Pi password

To change the default password, run the following command :

pi@raspberrypi:~ $ passwd

Then type the current password, the new password and finally confirm.

Installing system packages

To install software from the Internet it is necessary to wire-up the Raspberry Pi to a router using the Ethernet interface. To do so plug an RJ45 cable on the Ethernet port of the board and connect-it to a router that has a connection to the Internet.

Before installing new components, it may be necessary to get the up-to-date packages list :

pi@raspberrypi:~ $ sudo apt udpate

As well as the packages installed by default :

pi@raspberrypi:~ $ sudo apt upgrade

The MyoCoach setup needs several packages to work properly : Pip3, VirtualEnv, Flask, NodeJs, SQLite.

First add nodesource apt repository in order to be able to get the nodejs packages :

pi@raspberrypi:~ $ curl -sL https://deb.nodesource.com/setup_16.x | sudo bash -

Then install the packages :

pi@raspberrypi:~ $ sudo apt install nodejs git python3-pip python3-flask sqlite3 virtualenv

Application setup

Get the application code

To get the latest version of the application source code, keep the board connected to the Internet using the Ethernet port and run the following commands :

pi@raspberrypi:~ $ git clone https://github.com/orthopus/01-myocoach.git

Fetch application dependencies

On the Raspberry Pi, go to the MyoCoach webapp directory :

pi@raspberrypi:~ $ cd 01-myocoach/src/software/webapp/

To get the Python packages, run the following command :

pi@raspberrypi:~/01-myocoach/src/software/webapp $ sudo pip3 install -r requirements.txt

Run the following command to get the nodejs dependencies :

pi@raspberrypi:~/01-myocoach/src/software/webapp $ npm install

Environment variables and compilation

Create a copy of the .env.example file named .env :

pi@raspberrypi:~/01-myocoach/src/software/webapp $ cp .env.example .env

Then check its content and adapt it if necessary using a text editor :

pi@raspberrypi:~/01-myocoach/src/software/webapp $ nano .env

.env

NODE_ENV=production
ENDPOINT=http://app.myocoach.lan

The run the Webpack compilation with the following command :

pi@raspberrypi:~/01-myocoach/src/software/webapp $ npx webpack --config webpack.prod.js

Networking

Hostname

In objective to change the MyoCoach hostname (currently raspberrypi), open the system settings interface with the following commands :

pi@raspberrypi:~ $ sudo raspi-config

On the system settings interface, select 1 System Options, S4 Hostname then OK. Type the new name "myocoach". Select Finish to save the configuration. Finally, select Yes to reboot the system for the changes take effect.

The hostname of the hosts file has to be refreshed to save the new hostname.

pi@myocoach:~ $ less /etc/hosts

hosts

127.0.0.1       localhost
::1             localhost ip6-localhost ip6-loopback
ff02::1         ip6-allnodes
ff02::2         ip6-allrouters

127.0.1.1       myocoach

Avahi configuration

Open the avahi configuration file in order to make MyoCoach get the "myocoach.lan" name on the network :

pi@myocoach:~ $ sudo nano /etc/avahi/avahi-daemon.conf

avahi-daemon.conf

...
[server]
host-name=myocoach
domain-name=lan
...

Restart the service for the changes to take effect with the following commands :

pi@myocoach:~ $ sudo systemctl reload avahi-daemon
pi@myocoach:~ $ sudo systemctl restart avahi-daemon

Access Point - RaspAP

To enable wireless on the Raspberry Pi use the following command :

pi@myocoach:~ $ sudo rfkill unblock wlan
pi@myocoach:~ $ curl -sL https://install.raspap.com | bash

Then follow the instructions.

Once the Raspberry Pi rebooted, connect to the network raspap-webui (psk: ChangeMe) with the workstation. Go to the access point management interface from the workstation with a web browser at the following address 10.3.141.1. Authenticate to the interface with the "admin" username (password: secret) then go to the Authentication section to change the password.

Captive portal

The captive portal is managed by the Nodogsplash application.

pi@myocoach:~ $ sudo apt-get install libmicrohttpd-dev
pi@myocoach:~ $ sudo mkdir /srv/nodogsplash
pi@myocoach:~ $ sudo chown pi /srv/nodogsplash
pi@myocoach:~ $ sudo chgrp pi /srv/nodogsplash
pi@myocoach:~ $ git clone https://github.com/nodogsplash/nodogsplash.git /srv/nodogsplash

To compile Nodogsplash, run the following commands :

pi@myocoach:~ $ cd /srv/nodogsplash
pi@myocoach:~ $ make
pi@myocoach:~ $ sudo make install

Open the configuration file nodogsplash.conf :

pi@myocoach:~ $ sudo nano /etc/nodogsplash/nodogsplash.conf

Change the following lines :

GatewayInterface wlan0
GatewayAddress 10.3.141.1
RedirectURL http://10.3.141.1:2050/redirect.html

In the FirewallRuleSet users-to-router object, add the following line :

FirewallRule allow tcp port 5000

Save and close the file.

Remove nodogsplash default captive portal html files :

pi@myocoach:~ $ sudo rm -rf /etc/nodogsplash/htdocs

Then create a link to myocoach captive portal html files :

pi@myocoach:~ $ sudo ln -s /home/pi/01-myocoach/src/software/captiveportal /etc/nodogsplash/htdocs

Open the configuration file dnsmasq.conf :

pi@myocoach:~ $ sudo nano /etc/dnsmasq.conf

Add or change the following line :

address=/#/10.3.141.1

Save and close the file.

Copy the nodogsplash.service file then enable start at boot :

pi@myocoach:~ $ sudo cp /srv/nodogsplash/debian/nodogsplash.service /lib/systemd/system/
pi@myocoach:~ $ sudo systemctl enable nodogsplash.service

Enable start at boot :

pi@myocoach:~ $ sudo systemctl enable dnsmasq.service

Finally, reboot the system to save the changes.

Lighttpd configuration

In objective to access the MyoCoach web interface and the RaspAP web interface as well, Lighttpd has to be configured.

First, the RaspAP web interface must have restricted access to the raspap.myocoach.lan subdomain name.

pi@myocoach:~ $ sudo nano /etc/lighttpd/conf-available/50-raspap-router.conf

50-raspap-router.conf

server.modules += ("mod_rewrite")

$HTTP["host"] == "raspap.myocoach.lan" {
  $HTTP["url"] =~ "^/(?!(dist|app|ajax|config)).*" {
    url.rewrite-once = ( "^/(.*?)(\?.+)?$"=>"/index.php/$1$2" )
    server.error-handler-404 = "/index.php"
  }
}

Next, create a new file to declare the virtual host app.myocoach.lan.

pi@myocoach:~ $ sudo nano /etc/lighttpd/conf-available/40-myocoach.conf

40-myocoach.conf

server.modules += ( "mod_proxy" )

# proxy myocoach
$HTTP["host"] == "app.myocoach.lan" {
  proxy.debug = 1
  proxy.server = ( "" => ( ( "host" => "10.3.141.1", "port" => "5000" ) ) )
  proxy.header = ( "upgrade" => "enabled" )
}

Then, create a link to activate this new host.

pi@myocoach:~ $ cd /etc/lighttpd/conf-enabled/
pi@myocoach:~ $ sudo ln -s ../conf-available/40-myocoach.conf

At last, reboot the service for the changes to take effect.

pi@myocoach:~ $ sudo systemctl reload lighttpd
pi@myocoach:~ $ sudo systemctl restart lighttpd

MyoCoach service

Create a link named myocoach in the /srv directory to the webapp directory :

pi@myocoach:~ $ sudo ln -s /home/pi/01-myocoach/src/software/webapp /srv/myocoach

Create a link named ledbutton in the /srv directory to the ledbutton directory :

pi@myocoach:~ $ sudo ln -s /home/pi/01-myocoach/src/software/ledbutton /srv/ledbutton

Create the MyoCoach systemd service :

pi@myocoach:~ $ cd /lib/systemd/system
pi@myocoach:/lib/systemd/system $ sudo nano myocoach.service

myocoach.service

[Unit]
Description= Controls myocoach webapp service
Requires= lighttpd.service
After= lighttpd.service

[Install]
WantedBy= multi-user.target

[Service]
Type= simple
User= root
WorkingDirectory= /srv/myocoach/app
ExecStart= /bin/python3 ./app.py
ExecStartPost= /bin/bash /srv/ledbutton/set_led_color.sh 0 0 255 0
ExecStop= /bin/kill -2 $MAINPID
ExecStopPost= /bin/bash /srv/ledbutton/set_led_color.sh 255 255 255 0

To start the service run the following command :

pi@myocoah:~ $ sudo systemctl start myocoach.service

To enable the service at system boot run the following command :

pi@myocoah:~ $ sudo systemctl enable myocoach.service

The MyoCoach web interface is now reachable at the following address : http://app.myocoach.lan

And the RaspAP web interface is now reachable at the following address : http://raspap.myocoach.lan

LED Power Button Configuration (optional)

Optionally the MyoCoach can be equipped with a LED Power Button. It allows users to have information about the system state and proceed to a more graceful Power-OFF of the board (ie. cleaner than unplugging the power cable).

Create systemd services :

pi@myocoach:~ $ cd /lib/systemd/system
pi@myocoach:/lib/systemd/system $ sudo nano enlight_led.service

enlight_led.service

[Unit]
Description= Launch enlight_led.service

[Install]
WantedBy=multi-user.target

[Service]
Type= simple
User= pi
WorkingDirectory= /srv/ledbutton
ExecStart= /bin/bash ./enlight_led.sh
pi@myocoach:/lib/systemd/system $ sudo nano check_color_changed.service

check_color_changed.service

[Unit]
Description= Restarts enlight_led.service
Requires=basic.target
After=basic.target

[Install]
WantedBy=multi-user.target

[Service]
Type= oneshot
ExecStart= /usr/bin/systemctl restart enlight_led.service
pi@myocoach:/lib/systemd/system $ sudo nano check_color_changed.path

check_color_changed.path

[Unit]
Wants= check_color_changed.service

[Path]
PathChanged= /srv/ledbutton/led_color.txt

[Install]
WantedBy=basic.target
pi@myocoach:/lib/systemd/system $ sudo nano set_led_color_on_system_startup.service

set_led_color_on_system_startup.service

[Unit]
Description= Set led color on system startup
Requires= basic.target
After=basic.target

[Install]
WantedBy=multi-user.target

[Service]
Type= oneshot
WorkingDirectory= /srv/ledbutton
ExecStart= /bin/bash ./set_led_color.sh 255 255 255 0

Enable start at boot :

pi@myocoach:~ $ sudo systemctl enable enlight_led.service
pi@myocoach:~ $ sudo systemctl enable check_color_changed.service
pi@myocoach:~ $ sudo systemctl enable check_color_changed.path
pi@myocoach:~ $ sudo systemctl enable set_led_color_on_system_startup.service

Open the configuration file config.txt :

pi@myocoach:~ $ sudo nano /boot/config.txt

Add the following line :

dtoverlay=gpio-shutdown

Save and close the file. Finally, reboot the system to apply the changes.

pi@myocoach:~ $ sudo reboot

Add new Games

The MyoCoach application has been designed to host video games developed using the Godot environment and that are adapted to use EMG signals as inputs.

Further information about game development with Godot can be found on Godot Engine website : https://godotengine.org/

To integrate new games in the MyoCoach environment and make them available from the web application, here are some guide-lines :

As there are only two EMG signals, and thus two possible inputs, the gameplay has to be kept simple. These EMG signals are mapped to InputEventMouseMotion in order to keep the "analog" values of signals. Depending on the game design, the value of EMG signals can be used as proportional inputs directly or to trigger events when they exceed a defined threshold.

A folder has to be created under the public/games/ directory for each hosted game. As an exemple to add "Squash" game, run the following command :

pi@myocoach:~ $ cd /srv/myocoach/public/games/ 
pi@myocoach:/srv/myocoach/public/games $ mkdir squash

From the Godot development environment, the games must be exported using an HTML5 template. Once exported, if the name of the export chosen is game.html, then get the files game.pck and game.wasm copy them in the freshly created folder.

pi@myocoach:/srv/myocoach/public/games $ ls squash/
game.pck  game.wasm

For each game a description with its metadata must be indicated in the gamelist.json file.

Open the gamelist.json with a text editor to add the necessary info :

pi@myocoach:/srv/myocoach/public/games $ nano gamelist.json

As an example, for a MyoCoach setup with the games "FlappyPoulpy" and "Squash" installed, gamelist.json might contain the following lines :

gamelist.json

[
  {
    "name": "FlappyPoulpy",
    "url": "/flappypoulpy",
    "path": "flappypoulpy",
    "width": 340,
    "height": 256,
    "fileSizes": {
      "/public/games/flappypoulpy/game.pck": 1191008,
      "/public/games/flappypoulpy/game.wasm": 18131449
    }
  },
  {
    "name": "Squash",
    "url": "/squash",
    "path": "squash",
    "width": 640,
    "height": 400,
    "fileSizes": {
      "/public/games/squash/game.pck": 16352,
      "/public/games/squash/game.wasm": 18149419
    }
  }
]

Once the information filled in, save and close the file.

The file size information for game.pck and game.wasm can be found looking in the game.html file that has been generated during the export. It is important to fill in this information in order to make the game loader work properly.

The screen dimensions (width and height in number of pixels) are the default game screen dimensions that can be found in Godot development environment. It is important to fill in this information in order to make the MyoCoach web application keep the game's screen aspect ratio whatever the device screen size is.