# nebuleair_pro_4g

Based on the Rpi4 or CM4.

# Installation

Installation can be made with Ansible or the classic way.

## Ansible (WORK IN PROGRESS)
Installation with Ansible will use a playbook  `install_software.yml`.

## General

See `installation.sh`

```
sudo apt update
sudo apt install git gh apache2 php python3 python3-pip jq autossh i2c-tools python3-smbus -y
sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod --break-system-packages
sudo mkdir -p /var/www/.ssh
sudo ssh-keygen -t rsa -b 4096 -f /var/www/.ssh/id_rsa -N ""
sudo ssh-copy-id -i /var/www/.ssh/id_rsa.pub -p 50221 airlab_server1@aircarto.fr
sudo git clone http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g.git /var/www/nebuleair_pro_4g
sudo mkdir /var/www/nebuleair_pro_4g/logs
sudo touch /var/www/nebuleair_pro_4g/logs/app.log /var/www/nebuleair_pro_4g/logs/loop.log /var/www/nebuleair_pro_4g/wifi_list.csv
sudo cp /var/www/nebuleair_pro_4g/config.json.dist /var/www/nebuleair_pro_4g/config.json
sudo chmod -R 777 /var/www/nebuleair_pro_4g/
git config --global core.fileMode false
git config --global --add safe.directory /var/www/nebuleair_pro_4g
sudo crontab /var/www/nebuleair_pro_4g/cron_jobs
```
## Apache
Configuration of Apache to redirect to the html homepage project
```
sudo sed -i 's|DocumentRoot /var/www/html|DocumentRoot /var/www/nebuleair_pro_4g|' /etc/apache2/sites-available/000-default.conf
sudo systemctl reload apache2
```

## 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

```
## Serial

Need to open all the uart port by modifying `sudo nano /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*
```

## I2C

Decibel meter and BME280 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.

### BME280

The python script is triggered by the main loop every minutes to get instant temp, hum and press values (no need to have a minute average).

### Noise sensor

As noise varies a lot, we keep the C program running every seconds to create a moving average for the last 60 seconds (we also gather max and min values).
To keep the script running at boot and stay on we create a systemd service

Create the service with `sudo nano /etc/systemd/system/sound_meter.service` and add:
```
[Unit]
Description=Sound Meter Service
After=network.target

[Service]
ExecStart=/var/www/nebuleair_pro_4g/sound_meter/sound_meter_moving_avg
Restart=always
User=airlab
WorkingDirectory=/var/www/nebuleair_pro_4g/sound_meter

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

Then start the service:
```
sudo systemctl daemon-reload
sudo systemctl enable sound_meter.service

sudo systemctl start sound_meter.service
```


## SSH Tunneling

To have a remote access to the RPI we can start a SSH tunneling at boot with the command:
```
ssh -p 50221 -R <device_sshTunelPort>:localhost:22 airlab_server1@aircarto.fr
```

### To make things simpler we need to connect via a ssh key.

```
ssh-keygen -t rsa -b 4096
```

And add the key to the server with `ssh-copy-id -p 50221 airlab_server1@aircarto.fr`


## Crontabs

Attention, authorization for uart are reinitialized after reboot. Need to add the command to   `sudo crontab -e `
```
@reboot chmod 777 /dev/ttyAMA* /dev/i2c-1
```

And start the Hotspot check:
```
@reboot /var/www/nebuleair_pro_4g/boot_hotspot.sh >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
```

And set the base URL for Sara R4 communication:
```
@reboot sleep 30 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
```

### With only 1 NPM

Loop every minutes to get the PM values and send it to the server:

```
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1

```

All in one:

```
@reboot chmod 777 /dev/ttyAMA*
@reboot /var/www/nebuleair_pro_4g/boot_hotspot.sh >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
@reboot sleep 30 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/1_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
0 0 */2 * * > /var/www/nebuleair_pro_4g/logs/loop.log
```

### With 3 NPM
Loop every minutes to get the PM values and send it to the server:

```
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/3_NPM/get_data_closest_pair.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
* * * * * sleep 5 && /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/3_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
```

All in one:

```
@reboot chmod 777 /dev/ttyAMA* /dev/i2c-1
@reboot /var/www/nebuleair_pro_4g/boot_hotspot.sh >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
@reboot sleep 30 && /usr/bin/python3 /var/www/nebuleair_pro_4g/SARA/sara_setURL.py ttyAMA2 data.nebuleair.fr >> /var/www/nebuleair_pro_4g/logs/app.log 2>&1
* * * * * /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/3_NPM/get_data_closest_pair.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
* * * * * sleep 5 && /usr/bin/python3 /var/www/nebuleair_pro_4g/loop/3_NPM/send_data.py >> /var/www/nebuleair_pro_4g/logs/loop.log 2>&1
0 0 */2 * * > /var/www/nebuleair_pro_4g/logs/loop.log
```

# Notes

## Wifi Hotspot (AP)

To connect the device to the internet we need to create a Hotspot using nmcli.

Command to create a AP with SSI: nebuleair_pro and PASS: nebuleaircfg:

```
sudo nmcli device wifi hotspot ifname wlan0 ssid nebuleair_pro password nebuleaircfg

#we also need to set IP addresses
sudo nmcli connection modify Hotspot ipv4.addresses 192.168.4.1/24
sudo nmcli connection modify Hotspot ipv4.method shared
```

This will create a new connection called "Hotspot" using device "wlan0". You can connect to this network 
via wifi and access to the self-hosted webpage on 192.168.4.1 (to get the IP `ip addr show wlan0` or `nmcli device show wlan0`).

Only problem is that you cannot perform a wifi scan while wlan0 is in AP mode. So you need to scan the available networks just before creating the Hotspot.

Second issue: hotspot need to be lauched at startup only if it cannot connected to the selected wifi network.

Wifi connection check, wifi scan and activation of the Hotspot need to be lauched at every startup.

This can be doned with script boot_hotspot.sh.

```
@reboot chmod 777 /dev/ttyAMA* /dev/i2c-1
@reboot /var/www/nebuleair_pro_4g/boot_hotspot.sh
```


