# moduleair_pro_4g
Version Pro du ModuleAir avec CM4, SaraR4 et ecran Matrix LED p2 64x64.

# Installation
## General
```
sudo apt update
sudo apt install git gh apache2 php php-sqlite3 python3 python3-pip jq g++ autossh i2c-tools python3-smbus -y
sudo pip3 install pyserial requests sensirion-shdlc-sfa3x RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil --break-system-packages
sudo gh auth login
git config --global user.email "paulvuarambon@gmail.com"
git config --global user.name "PaulVua"
sudo gh repo clone aircarto/moduleair_pro_4g /var/www/moduleair_pro_4g
sudo mkdir /var/www/moduleair_pro_4g/logs
sudo touch /var/www/moduleair_pro_4g/logs/app.log /var/www/moduleair_pro_4g/logs/loop.log /var/www/moduleair_pro_4g/matrix/input_NPM.txt /var/www/moduleair_pro_4g/matrix/input_MHZ16.txt /var/www/moduleair_pro_4g/wifi_list.csv
sudo cp /var/www/moduleair_pro_4g/config.json.dist /var/www/moduleair_pro_4g/config.json
sudo chmod -R 777 /var/www/moduleair_pro_4g/
```

## Apache
Configuration of Apache to redirect to the html homepage project
```
sudo sed -i 's|DocumentRoot /var/www/html|DocumentRoot /var/www/moduleair_pro_4g|' /etc/apache2/sites-available/000-default.conf
sudo systemctl reload apache2
```

## Serial

Need to open all the uart port by modifying `/boot/firmware/config.txt`

```
enable_uart=1
dtoverlay=uart0
dtoverlay=uart1
dtoverlay=uart2
dtoverlay=uart3
dtoverlay=uart4
dtoverlay=uart5
```
And reboot !

Then we need to authorize connection over device on `/etc/ttyAMA*`
```
sudo chmod 777 /dev/ttyAMA*
```

## Sudo athorization
To make things simpler we will allow all users to use "nmcli" as sudo without entering password. For that we need to open the sudoers file with `sudo visudo` and add this to the bottom of the file:
```
ALL ALL=(ALL) NOPASSWD: /usr/bin/nmcli, /usr/sbin/reboot
www-data ALL=(ALL) NOPASSWD: /usr/bin/git pull
www-data ALL=(ALL) NOPASSWD: /usr/bin/ssh
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 *
```

## I2C

Decibel meter, BME280 and the RTC module (DS3231) is connected via I2C.
Need to activate by modifying `sudo nano /boot/firmware/config.txt`

```
dtparam=i2c_arm=on
```

And authorize access to  `/dev/i2c-1`.

```
sudo chmod 777 /dev/i2c-1
```

Attention: sometimes activation with config.txt do not work, you need to activate i2c with `sudo raspi-config` and go to "Interface" -> I2C -> enable.
It is possible to manage raspi-config only with cli: `sudo raspi-config nonint do_i2c 0`
I2C addresses: use `sudo i2cdetect -y 1` to check the connected devices

## Matrix LED

### Library

To use the Matrix LED on the RPI we will use the [rpi-rgb-led-matrix](https://github.com/hzeller/rpi-rgb-led-matrix) repository from hzeller.

Before compiling any code we need the **include** folder and the **lib** folder from the library. Then we can compile any .cc code with g++.
```
g++ -Iinclude  -Llib test.cc -o test -lrgbmatrix
sudo ./test --led-no-hardware-pulse --led-row-addr-type=3
```

### Pinout

Details of connection bewtween components can be found [here](https://docs.google.com/spreadsheets/d/1EJoq7nlJIN9nd_CbVmmozsGb-Ntp0cyxrEt9vokqc6g/edit?usp=sharing).
Some pins for the Matrix need to change because they use uart pins. For this we need to change pins inside `/lib/hardware-mapping.c` and recompile the library:
```
cd /var/www/moduleair_pro_4g/matrix/lib
make
```

### Matrix 64x32

Pour la matrix 64x32 (celle du ModuleAir mini) on utilise le pinout "regular"

Tests avec la biblio de hzeller sur un RPi4:
```
sudo examples-api-use/demo -D0 \
    --led-no-hardware-pulse \
    --led-chain=1 \
    --led-cols=64 \
    --led-rows=32 \
    --led-gpio-mapping=regular 
```

### Matrix 128x64

Pour le grand écran il faut mettre sur ground deux pins (E et D) et ajouter la commande `--led-row-addr-type=3` car il s'agit d'un panneau de type "ABC".
Il faut aussi préciser le type de chip-set avec `--led-panel-type=FM6126A`

Test avec la biblio de hzeller sur un RPi4:
```
sudo examples-api-use/demo -D0 \
    --led-no-hardware-pulse \
    --led-chain=1 \
    --led-cols=128 \
    --led-rows=64 \
    --led-gpio-mapping=regular \
    --led-row-addr-type=3  \
    --led-parallel=1  \
    --led-panel-type=FM6126A
```

Pour tester sur la CM4
```
sudo ./test_forms --led-no-hardware-pulse
```

### Fonts

Font "6x9.bdf" is 7 pixels height +1 (top) +1 (bottom) = 9 height.
Font "8x13.bdf" is 9 pixels height +2 (top) +2 (bottom) = 13 height.
Font "8x18.bdf" is 14 pixels height +2 (top) +2 (bottom) = 13 height.

### Troubleshooting

Switch off on-board sound:
* `dtparam=audio=off` in `/boot/firmware/config.txt`
* Add the file  `sudo nano /etc/modprobe.d/blacklist.conf` with `blacklist snd_bcm2835`
* Command `lsmod | grep snd_bcm2835` should anwser nothing

Add `isolcpus=3` to `/boot/firmware/cmdline.txt`

## Start matrix loop at boot

We can use systemd to create a service (better than con because Cron doesn’t monitor the script; if it fails, it won’t restart automatically.).
```
sudo nano /etc/systemd/system/matrix_display.service
```

and we add the following:
```
[Unit]
Description=Matrix Display Script
After=network.target

[Service]
ExecStart=/var/www/moduleair_pro_4g/matrix/screen_sensors_loop
WorkingDirectory=/var/www/moduleair_pro_4g/matrix
Restart=always
User=root
Group=root

[Install]
WantedBy=multi-user.target
```

Then Reload systemd and Enable the Service:
```
sudo systemctl daemon-reload
sudo systemctl enable matrix_display.service
sudo systemctl start matrix_display.service

```

You can check/restart/stop this service (restart combines stop and start)

```
sudo systemctl status matrix_display.service
sudo systemctl stop matrix_display.service
sudo systemctl restart matrix_display.service

```





