How to Install the Open Source Energy Management System MyEMS on Raspberry Pi

The MyEMS open source energy management system is suitable for energy data collection, analysis, and reporting of electricity, water, gas, etc., in buildings, factories, shopping malls, hospitals, and parks. It also has optional features such as photovoltaic, energy storage, charging piles, microgrids, equipment control, fault diagnosis, work order management, and artificial intelligence optimization.

In this guide, you will deploy MyEMS on a Raspberry Pi.

Prerequisites

  • Raspberry Pi 5 or Raspberry Pi 4 Model B

  • Raspberry Pi OS (64-bit) A port of Debian Bookworm with the Raspberry Pi Desktop Released:2024-07-04

Clone the Source Code

sudo apt install git
sudo apt install pip
sudo apt install ufw
cd ~ && git clone https://gitee.com/myems/myems

Step 1: Database

Set up the MySQL server

sudo apt update
sudo apt upgrade
sudo apt install mariadb-server

By default, MySQL is installed without any password set, which means you can access the MySQL server without any authentication. Run the following command to begin the MySQL security process.

sudo mysql_secure_installation
Enter current password for root (enter for none): [Enter key or return key]Switch to unix_socket authentication [Y/n] YChange the root password? [Y/n] YNew password: !MyEMS1Re-enter new password: !MyEMS1Remove anonymous users? [Y/n] YDisallow root login remotely? [Y/n] nRemove test database and access to it? [Y/n] YReload privilege tables now? [Y/n] Y

Install the database schema and scripts for MyEMS.

See https://myems.io/docs/installation/database

Step 2: myems-api

Install the myems-api service:

sudo cp -r ~/myems/myems-api /myems-api
cd /myems-api

To avoid ‘error: externally-managed-environment’, create a virtual environment configuration directory:

sudo python -m venv venv

Start using the virtual environment

source venv/bin/activate

Install the dependencies

sudo venv/bin/pip install -r requirements.txt

Deactivate the virtual environment

deactivate

Create the .env file based on example.env and edit .env as needed:

sudo cp /myems-api/example.env /myems-api/.env
sudo nano /myems-api/.env

Change the gunicorn path in myems-api.service to

/myems-api/venv/bin/gunicorn

sudo nano /myems-api/myems-api.service
ExecStart=/myems-api/venv/bin/gunicorn -b 0.0.0.0:8000 --pid /run/myems-api/pid --timeout 600 --workers=4 app:api

Add the port to the firewall:

sudo ufw allow 8000

Install the systemd configuration files:

sudo cp /myems-api/myems-api.service /lib/systemd/system/
sudo cp /myems-api/myems-api.socket /lib/systemd/system/
sudo cp /myems-api/myems-api.conf /usr/lib/tmpfiles.d/

Next, enable these services so that they start automatically on boot:

sudo systemctl enable myems-api.socket
sudo systemctl enable myems-api.service

Start the services:

sudo systemctl start myems-api.socket
sudo systemctl start myems-api.service

Step 3: myems-admin

Install the NGINX server

Reference http://nginx.org/en/linux_packages.html#Debian

sudo apt install curl gnupg2 ca-certificates lsb-release debian-archive-keyring
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg
keyring.gpg] http://nginx.org/packages/debian `lsb_release -cs` nginx" | sudo tee /etc/apt/sources.list.d/nginx.list
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" | sudo tee /etc/apt/preferences.d/99nginx
sudo apt update
sudo apt install nginx

Configure NGINX

sudo nano /etc/nginx/nginx.conf

Add some directives to the ‘http’ block:

http {    client_header_timeout 600;    client_max_body_size 512M;    gzip on;    gzip_min_length 512;    gzip_proxied any;    gzip_types *;    gzip_vary on;    proxy_buffering off;}

Add a new ‘server’ block with the following directives:

server {      listen                 8001;      server_name     myems-admin;      location / {          root    /var/www/myems-admin;          index index.html index.htm;      }      ## To avoid CORS issue, use Nginx to proxy myems-api to path /api      ## Add another location /api in 'server' and replace demo address http://127.0.0.1:8000/ with actual url      location /api {          proxy_pass http://127.0.0.1:8000/;          proxy_connect_timeout 75;          proxy_read_timeout 600;          send_timeout 600;      }  }

Install myems-admin:

sudo mkdir /var/www
sudo cp -r ~/myems/myems-admin  /var/www/myems-admin
sudo chmod 0755 -R /var/www/myems-admin

Check the configuration file and make changes as necessary:

sudo nano /var/www/myems-admin/app/api.js

Warning

The “upload” folder is for user-uploaded files. Do not delete/move/overwrite the “upload” folder when upgrading myems-admin.

/var/www/myems-admin/upload

Start Nginx:

sudo systemctl start nginx

Add the port to the firewall:

sudo ufw allow 8001

Step 4: myems-modbus-tcp

In this step, you will install the myems-modbus-tcp service.

sudo cp -r ~/myems/myems-modbus-tcp /myems-modbus-tcp
cd /myems-modbus-tcp

To avoid ‘error: externally-managed-environment’, create a virtual environment configuration directory:

sudo python -m venv venv

Start using the virtual environment

source venv/bin/activate

Install the dependencies

sudo venv/bin/pip install -r requirements.txt

Deactivate the virtual environment

deactivate

Copy the example.env file to .env and modify the .env file:

sudo cp /myems-modbus-tcp/example.env /myems-modbus-tcp/.env
sudo nano /myems-modbus-tcp/.env

Modify the python path in myems-modbus-tcp.service

sudo nano /myems-modbus-tcp/myems-modbus-tcp.service
[Unit]Description=myems-modbus-tcp daemonAfter=network.target
[Service]User=rootGroup=rootExecStart=/myems-modbus-tcp/venv/bin/python3 /myems-modbus-tcp/main.pyExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPIDPrivateTmp=trueRestart=always
[Install]WantedBy=multi-user.target

Install the systemd service:

sudo cp myems-modbus-tcp.service /lib/systemd/system/

Enable the service:

sudo systemctl enable myems-modbus-tcp.service

Start the service:

sudo systemctl start myems-modbus-tcp.service

Monitor the service:

sudo systemctl status myems-modbus-tcp.service

View logs:

cat /myems-modbus-tcp.log

Step 5: myems-cleaning

In this step, you will install the myems-cleaning service.

sudo cp -r ~/myems/myems-cleaning /myems-cleaning
cd /myems-cleaning

To avoid ‘error: externally-managed-environment’, create a virtual environment configuration directory:

sudo python -m venv venv

Start using the virtual environment

source venv/bin/activate

Install the dependencies

sudo venv/bin/pip install -r requirements.txt

Deactivate the virtual environment

deactivate

Copy the example.env file to .env and modify the .env file:

sudo cp /myems-cleaning/example.env /myems-cleaning/.env
sudo nano /myems-cleaning/.env

Modify the python path in myems-cleaning.service

sudo nano /myems-cleaning/myems-cleaning.service
[Unit]Description=myems-cleaning daemonAfter=network.target
[Service]User=rootGroup=rootExecStart=/myems-modbus-tcp/venv/bin/python3 /myems-cleaning/main.pyExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPIDPrivateTmp=trueRestart=always
[Install]WantedBy=multi-user.target

Install the systemd service:

sudo cp /myems-cleaning/myems-cleaning.service /lib/systemd/system/

Enable the service:

sudo systemctl enable myems-cleaning.service

Start the service:

sudo systemctl start myems-cleaning.service

Monitor the service:

sudo systemctl status myems-cleaning.service

View logs:

cat /myems-cleaning.log

Step 6: myems-normalization

In this step, you will install the myems-normalization service.

sudo cp -r ~/myems/myems-normalization /myems-normalization
cd /myems-normalization

To avoid ‘error: externally-managed-environment’, create a virtual environment configuration directory:

sudo python -m venv venv

Start using the virtual environment

source venv/bin/activate

Install the dependencies

sudo venv/bin/pip install -r requirements.txt

Deactivate the virtual environment

deactivate

Copy the example.env file to .env and modify the .env file:

sudo cp /myems-normalization/example.env /myems-normalization/.env
sudo nano /myems-normalization/.env

Modify the python path in myems-normalization.service

sudo nano myems-normalization.service
[Unit]Description=myems-normalization daemonAfter=network.target
[Service]User=rootGroup=rootExecStart=/myems-normalization/venv/bin/python3 /myems-normalization/main.pyExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPIDPrivateTmp=trueRestart=always
[Install]WantedBy=multi-user.target

Install the systemd service:

sudo cp /myems-normalization/myems-normalization.service /lib/systemd/system/

Enable the service:

sudo systemctl enable myems-normalization.service

Start the service:

sudo systemctl start myems-normalization.service

Monitor the service:

sudo systemctl status myems-normalization.service

View logs:

cat /myems-normalization.log

Step 7: myems-aggregation

In this step, you will install the myems-aggregation service.

sudo cp -r ~/myems/myems-aggregation /myems-aggregation
cd /myems-aggregation

To avoid ‘error: externally-managed-environment’, create a virtual environment configuration directory:

sudo python -m venv venv

Start using the virtual environment

source venv/bin/activate

Install the dependencies

sudo venv/bin/pip install -r requirements.txt

Deactivate the virtual environment

deactivate

Copy the example.env file to .env and modify the .env file:

sudo cp /myems-aggregation/example.env /myems-aggregation/.env
sudo nano /myems-aggregation/.env

Modify the python path in myems-aggregation.service

sudo nano myems-aggregation.service
[Unit]Description=myems-aggregation daemonAfter=network.target
[Service]User=rootGroup=rootExecStart=/myems-aggregation/venv/bin/python3 /myems-aggregation/main.pyExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s TERM $MAINPIDPrivateTmp=trueRestart=always
[Install]WantedBy=multi-user.target

Install the systemd service:

sudo cp /myems-aggregation/myems-aggregation.service /lib/systemd/system/

Enable the service:

sudo systemctl enable myems-aggregation.service

Start the service:

sudo systemctl start myems-aggregation.service

Monitor the service:

sudo systemctl status myems-aggregation.service

View logs:

cat /myems-aggregation.log

Step 8: myems-web

In this step, you will install the myems-web service.

  • Install the NGINX server (if already installed in myems-admin, you can ignore) Reference http://nginx.org/en/docs/install.html

  • Configure NGINX

sudo nano /etc/nginx/nginx.conf

Add some directives to the ‘http’ block (if already configured in myems-admin, you can ignore)

http {    client_header_timeout 600;    client_max_body_size 512M;    gzip on;    gzip_min_length 512;    gzip_proxied any;    gzip_types *;    gzip_vary on;    proxy_buffering off;    ...}

Add a new ‘server’ block in the ‘http’ block with the following directives:

server {      listen                 80;      server_name     myems-web;      location / {          root    /var/www/myems-web;          index index.html index.htm;          # add try_files directive to avoid 404 error while refreshing pages          try_files $uri  /index.html;      }      ## To avoid CORS issue, use Nginx to proxy myems-api to path /api      ## Add another location /api in 'server'      ## NOTE: replace default address http://127.0.0.1:8000/ with actual IP or URL      location /api {          proxy_pass http://127.0.0.1:8000/;          proxy_connect_timeout 75;          proxy_read_timeout 600;          send_timeout 600;      }  }
  • Install MyEMS Web UI:

Install NodeJS:

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
sudo apt-get install -y nodejs

Modify the configuration file:

Note

Get mapboxToken from https://mapbox.com then set showOnlineMap to true. If you want to disable online map functionality, set showOnlineMap to false.

cd ~/myems/myems-web
sudo nano src/config.js

Compile and compress:

sudo npm i --unsafe-perm=true --allow-root --legacy-peer-deps
sudo npm run build

Install

sudo mv build  /var/www/myems-web

Restart NGINX

sudo systemctl restart nginx

Add the port to the firewall:

sudo ufw allow 80

After Installation

Congratulations, you can now log in to MyEMS Admin UI and Web UI.

Default Ports

MyEMS Web UI: 80

MyEMS API: 8000

MyEMS Admin UI: 8001

Default Password

Admin UI

administrator
!MyEMS1

Web UI

[email protected]
!MyEMS1

Leave a Comment

×