# Software

## Step by Step setup

You don't have to program the compute module yourself, but you have to set it up. The following steps help you to setup your Tracker.

{% hint style="success" %}
If you want to have a deeper insight in the code itself, you can find the documented code in the github repository: <https://github.com/oliverheisel/DataDrivenSailing>
{% endhint %}

### &#x20;A\_Basic Raspberry Pi Firmware

1. Install Raspberry **Pi OS Lite 64bit** with the [Raspberry Pi Imager](https://www.raspberrypi.com/software/) with the following settings:
   1. Hostname: boat1.local (replace number accordingly)
   2. Username and Password: As you like
   3. Wlan: Use the hub network
   4. Time/location: As you need
2. Power up the Raspberry Pi
3. Connect via SSH - <https://marketplace.visualstudio.com/items?itemName=Kelvin.vscode-sshfs>) - this extension in visual studio code cold make you life easier.
4. Update and upgrade:  <mark style="color:red;">`sudo apt-get update && sudo apt-get -y upgrade`</mark>
5. Reboot with: <mark style="color:red;">`sudo reboot`</mark>

### B\_Create a virtual environment

1. Install the python3-venv:

```bash
sudo apt install python3-venv
python3 -m venv codeenv --system-site-packages
```

2. Activate the environment with: <mark style="color:red;">`source codeenv/bin/activate`</mark>

{% hint style="info" %}
→ you should see a “(env)” in front of your Terminal name

→ for deactivate the env -> <mark style="color:red;">`deactivate`</mark>
{% endhint %}

### C\_Activate pigiod

**pigpiod** itself is a daemon that provides low-level access to Raspberry Pi’s GPIO pins. Paste the following code in the terminal:

```
sudo systemctl enable pigpiod
sudo systemctl start pigpiod
```

### D\_Activate i2c

1. Open rasp-config with: <mark style="color:red;">`sudo raspi-config`</mark>
2. Select: “Interface options” > “I2c” > “Activate i2c” \_> yes
3. Exit raspi-config

### E\_Change i2c speed

1. Open the firmware configuration file with: <mark style="color:red;">`sudo nano /boot/firmware/config.txt`</mark>
2. Search for the "**dparam**" line
3. Uncomment the line by removing the hashtag
4. Add the following, so it looks like this: → changing from 400khz to 100khz:\ <mark style="color:blue;">`dtparam=i2c_arm=on,i2c_arm_baudrate=100000`</mark>
5. Reboot with: <mark style="color:red;">`sudo reboot`</mark>

### F\_Copy Code from GitHub

1. Install git with: <mark style="color:red;">`sudo apt-get install git`</mark>
2. Clone the repository: <mark style="color:red;">`git clone https://github.com/oliverheisel/DataDrivenSailing.git ddsrepo`</mark>
3. Copy the code that we need to where we need it: <mark style="color:red;">`cp -r ddsrepo/code /home/globaladmin/code`</mark>&#x20;
4. Remove the rest: <mark style="color:red;">`rm -rf ddsrepo`</mark>
5. Install all required packages:
   1. Activate the virtual environment: <mark style="color:red;">`source codeenv/bin/activate`</mark>
   2. Go to the folder with: <mark style="color:red;">`cd code`</mark>
   3. Install the packages: <mark style="color:red;">`pip install -r requirements.txt`</mark>

### G\_Change the configuration

1. Open the code configuration file (it is a python script, so use your ide):\ <mark style="color:blue;">`/home/globaladmin/code/config/config.py`</mark>
2. Change the hostname number accordingly (it needs to be the same as you defined in the firmware - see Task 1)
3. Make sure that <mark style="color:blue;">devicetype = "boat"</mark> and the others are commented out
4. Save and Reboot with: <mark style="color:red;">`sudo reboot`</mark>

### H\_Create a Service

1. Create new service file with: <mark style="color:red;">`sudo nano /etc/systemd/system/startmain.service`</mark>
2. Paste the following in the file and exit it with "Ctrl"+"X" > "Y" > "Enter"

```python
[Unit]
Description=Run Main.py on Boot
After=network.target

[Service]
User=globaladmin
WorkingDirectory=/home/globaladmin/code
ExecStart=/bin/bash -c 'source /home/globaladmin/codeenv/bin/activate && /home/globaladmin/codeenv/bin/python /home/globaladmin/code/main.py'
Restart=always
RestartSec=2
Environment=VIRTUAL_ENV=/home/globaladmin/codeenv
Environment=PATH=/home/globaladmin/codeenv/bin:$PATH
ExecStartPre=/bin/sleep 5

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

3. Enable service:

```python
sudo systemctl daemon-reload
sudo systemctl enable startmain.service
sudo systemctl start startmain.service
```

{% hint style="info" %}
The service will start the main script each time the Raspberry Pi boots.

If you have to stop or restart the service (in case you changed the code) you can do it with:

**STOP**: <mark style="color:red;">`sudo systemctl stop startmain.service`</mark>

**RESTART**: <mark style="color:red;">`sudo systemctl restart startmain.service`</mark>
{% endhint %}

### I\_Copy SSH Keys

Rsync uses SSH Keys to transfer the data securly.

1. SSH into the Hub
2. Create an SSH Key: ssh key authentification: <mark style="color:red;">`ssh-keygen -t rsa -b 2048`</mark>\
   !!! You only need to do this for the first device! It will be saved on the hub !!!
3. Copy the key to the Tracker (change the host accordingly): <mark style="color:red;">`ssh-copy-id globaladmin@boat1.local`</mark>
4. Test it (change the host accordingly): <mark style="color:red;">`ssh globaladmin@boat1.local`</mark> \
   -> it should work without entering a password

**Congrats, you made it 🎉**
