# nebuleair_pro_4g

Based on the Rpi4 or CM4.

# Installation

# Express

You can download the `installation_part1.sh` and run it:
```
wget http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g/raw/branch/main/installation_part1.sh
chmod +x installation_part1.sh
sudo ./installation_part1.sh
```

After reboot you can do the same with part 2.

```
wget http://gitea.aircarto.fr/PaulVua/nebuleair_pro_4g/raw/branch/main/installation_part2.sh
chmod +x installation_part2.sh
sudo ./installation_part2.sh
```


## General

Line by line installation.

```
sudo apt update
sudo apt install git gh apache2 sqlite3 php php-sqlite3 python3 python3-pip jq autossh i2c-tools python3-smbus -y
sudo pip3 install pyserial requests RPi.GPIO adafruit-circuitpython-bme280 crcmod psutil ntplib pytz gpiozero adafruit-circuitpython-ads1x15 numpy nsrt-mk3-dev --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
/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py
/usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/set_config.py
sudo chmod -R 777 /var/www/nebuleair_pro_4g/
git config --global core.fileMode false
git -C /var/www/nebuleair_pro_4g config core.fileMode false
git config --global --add safe.directory /var/www/nebuleair_pro_4g
sudo crontab /var/www/nebuleair_pro_4g/cron_jobs
sudo /usr/bin/python3 /var/www/nebuleair_pro_4g/sqlite/create_db.py
```
## 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
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 *
www-data ALL=(ALL) NOPASSWD: /bin/systemctl *
www-data ALL=(ALL) NOPASSWD: /var/www/nebuleair_pro_4g/*
```
## 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, 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.

### 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).
BME280 address is 0x76.

### RTC module (DS3231)



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

Nois sensor address is 0x48.

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


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

## Claude Code

Instructions to use claude code on the RPI.

### Install NPM

```
sudo apt install -y nodejs npm
node -v
npm -v
```

### Install Claude

```
sudo npm install -g @anthropic-ai/claude-code
```

### Run claude

```
claude
```


