Skip to content

Instantly share code, notes, and snippets.

@rohsyl
Last active January 22, 2021 20:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rohsyl/520b356fc9fb6b5bcd906f55b26d2739 to your computer and use it in GitHub Desktop.
Save rohsyl/520b356fc9fb6b5bcd906f55b26d2739 to your computer and use it in GitHub Desktop.
Quantya Raspberry PI - Install

Fast install procedure

We will install the raspberry from an existing image.

Write image on SD card

Run the follwing command to identify the device (ex. /dev/mmcblk0)

lsblk -p 

Unmount every moutned partition of the sdcard

umount /dev/mmcblk0p...

Write on the sd card:

dd bs=4M if=2019-09-26-raspbian-buster.img of=/dev/mmcblk0 conv=fsync

Boot up

Boot on the RPi

Connection with wifi

You can :

  • Follow the Configure Wi-Fi and SSH > Wifi in the complete-install.
  • Use your phone connection sharing with a specific pass and SSID and the RPi will automatically connect to it.

Configure

nano ~/homepy/.env

Set the ID to an unique value (you will use this ID to register the battery in the web interface. Set the NO_BT3 if the battery has no EXTERNAL TEMPERATURE SENSOR

Reboot

Enjoy !

Install procedure

Download raspbian image

wget https://downloads.raspberrypi.org/raspbian_latest

Write on SD card

Run the follwing command to identify the device (ex. /dev/mmcblk0)

lsblk -p 

Unmount every moutned partition of the sdcard

umount /dev/mmcblk0p...

Unzip the image:

unzip 2019-09-26-raspbian-buster.zip

Write on the sd card:

dd bs=4M if=2019-09-26-raspbian-buster.img of=/dev/mmcblk0 conv=fsync

Configure Wi-Fi and SSH

Create two files in the boot partition

Wifi

Create a file to hold wifi config :

nano wpa_supplicant.cong

With the following content :

country=CH
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
  ssid="***"
  psk="***"
  key_mgmt=WPA-PSK
}

SSH

Create a file named ssh to enable ssh:

touch ssh

Boot up

Boot the raspberry

Found the IP address (gl)...

Connect with ssh

ssh pi@192.168.1. 113

Password is raspberry

Basic configuration

Make the RPi boot in console mode with auto-login.

sudo raspi-config

Boot option > set boot to console-autologin

Install Dependencies

Waveshare SIM 800X

Configure Waveshare

Source : https://www.waveshare.com/wiki/SIM800C_GSM/GPRS_HAT#Working_with_Raspberry_Pi

Download demo codes from wiki page. Extract and copy the SIM800C folder to /home/pi of Raspber

wget https://www.waveshare.com/w/upload/3/3e/SIM800C-GSM-GPRS-HAT-Demo-Code.zip

Unzip the file

unzip SIM800C-GSM-GPRS-HAT-Demo-Code.zip

Create a directory named sim to hold the pi_gpio_init.sh and copy it into.

mkdir /home/pi/sim && cp SIM800C-GSM-GPRS-HAT-Demo-Code/Raspberry/pi_gpio_init.sh /home/pi/sim

Open Terminal,enter /home/pi/ then execute commands below

chmod 777 /home/pi/sim/pi_gpio_init.sh

Set the script to auto-run after booting.

sudo nano /etc/rc.local

Add command before exit 0

sh /home/pi/sim/pi_gpio_init.sh

The serial port of Raspberry Pi is set for shell login by default. Therefore, we need to disable shell login function and enable hardware serial

Enter configuration page

sudo raspi-config

Choose Interfacing Options->Serial->no->yes

Open /boot/config.txt file, make sure the state below is added to the file:

enable_uart=1

reboot Raspberry Pi

Intstall PPP

Source : https://gist.github.com/rohsyl/48a62ea5fd81143bebc67c2c9dbeaed3

Fistly, we need to install PPP server

sudo apt-get install ppp

Switch to root permission

sudo su

Enter the directory /etc/ppp/peers, copy the file provider and name it gprs

cd /etc/ppp/peers
cp provider gprs

Open file gprs and edit it

sudo nano gprs

Paste etc_ppp_peers_gprs into the file

Save and close

Create or edit the /etc/chatscripts/gprs

nano /etc/chatscripts/gprs

Paste etc_chatscripts_gprs to the file

Save and close

Execute this to start the connection

pppd call gprs &

Add route

route add -net 0.0.0.0 ppp0

Try to ping one website

ping google.com

If it works, then stop the pppd process (ps aux and kill)

Create a service to auto-start call

Create a new file

sudo nano /etc/systemd/system/pppd-gprs.service

Paste this into it

[Unit]
Description=pppd gprs service
After=network.target
StartLimitIntervalSec=0

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=pppd call gprs

[Install]
WantedBy=multi-user.target

Make it start automatically

sudo systemctl enable pppd-gprs

Start it

sudo systemctl start pppd-gprs
MySQL (MariaDB)

Install MariaDB

sudo apt install mariadb-server

Finish the installation

sudo mysql_secure_installation

Create the database

CREATE DATABASE `quantya`;
CREATE USER 'myuser' IDENTIFIED BY 'mypassword';
GRANT USAGE ON *.* TO 'myuser'@localhost IDENTIFIED BY 'mypassword';
GRANT USAGE ON *.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;

Create the table

CREATE TABLE `jobs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `queue` varchar(191) DEFAULT NULL,
  `payload` longtext NOT NULL,
  `attempt` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1361 DEFAULT CHARSET=utf8mb4;
Install HomePy Tools

Source : https://github.com/rohsyl/homepy

Clone the repository

Go to your home

cd /home/pi

Clone the repository

git clone git@github.com:rohsyl/homepy.git

Don't forget to add the ssh key to ~/.ssh and to configure ssh using it for github.com host

Create the .ssh directory in your home

cd ~ && mkdir .ssh

Create a file that will contain the ssh key and past the key into it.

nano github

Create a config file

nano config

Past the config into the file

Host github.com
IdentityFile ~/.ssh/github

Set the permission to 600

chmod -R 600 ~/.ssh
Dependencies

Install the pyserial, python-dotenv, mysql-connector, websockets and asyncio libraries

pip install pyserial mysql-connector websockets asyncio python-dotenv
Configuration

All configurations are done in the .env file.

Serial port

EMUS BMS
EMUS_SERIAL_PORT=/dev/ttyUSB1

Database

DB_HOST=localhost
DB_NAME=homepy
DB_USER=user
DB_PASS=pass

CRON Job

Edit the cron

crontab -e

Add the following lines at the end of the file

*/2 * * * * /usr/bin/python3.7 /home/pi/homepy/emus_broadcast.py
*/5 * * * * /usr/bin/python3.7 /home/pi/homepy/jobs_process.py

emus_broadcast script will connect to the EMUS BMS every minutes, get data, and store them in a local database.

jobs_process will process every data in the local database and will try to send them to the server.

Auto process job

Set up a script that will start jobs_process when the pi is connected to a Wi-Fi

Source : https://raspberrypi.stackexchange.com/questions/96050/run-a-script-when-wifi-is-connected-to-a-particular-wifi-network

Create a new script into /home/pi/wifi/process_job.sh

nano /home/pi/wifi/process_job.sh

And past this into it

#!/bin/sh
# redirect all output into a logfile
exec 1>> /home/pi/wifi/event.log 2>&1

case "$1" in
wlan0)
    case "$2" in
    CONNECTED)
        # sync when wifi up
        echo wlan0 connected
        python3.7 /home/pi/homepy/jobs_process.py
        ;;
    DISCONNECTED)
        # do stuff on disconnect with wlan0
        echo wlan0 disconnected
        ;;
    *)
        >&2 echo empty or undefined event for wlan0: "$2"
        exit 1
        ;;
    esac
    ;;
*)
    >&2 echo empty or undefined interface: "$1"
    exit 1
    ;;
esac

Make the script executable

sudp chmod +x /home/pi/wifi/process_job.sh

Register the script to wpa service

sudo wpa_cli -i wlan0 -a /home/pi/wifi/process_job.sh -B

Avoid dead lock

We have a script homepy/clear_lock.sh that clear the .LOCK files. We need this to run after booting.

Set the script to auto-run after booting.

sudo nano /etc/rc.local

Add command before exit 0

sh /home/pi/homepy/clear_lock.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment