###### tags: `rpi`
# Tutorial about P2P connection using Lora Module and ad-hoc Wifi on Raspberry Pi 3
## 1. TestBed
Laptop
Rasperry Pi 3 (B, B+) x2 (Ubuntu 22.04)
Lora module (lora-sx1276) x2
### SSH (optional)
#### Connect a PC and two Raspberry PIs with Ethernet, and connect the PC to a router. So you can use SSH to control both Raspberry PIs on the computer.
<!--
IP:
Computer: 10.0.1.10
PiB: 10.0.0.1
PiB+: 10.0.1.1
<!-->
## 2. Lora
### Connect LoRa modules to PIs
<!--->
<!-->



:::success
Pin connection on the Raspberry Pi and the mini LoRa chip.
:::
### Enable SPI interface
sudo raspi-config

#Select 3 Interface Options

#Select I4 SPI

#Enter
### Download the sample code from Github to both Raspberry PIs
https://github.com/piepie-tw/lora-sx1276
### Test if the pin connection work
cd /lora-sx1276
sudo python3 test_lora.py

### Display the parameters
##### Frequency, Bandwidth, and Spreading Factor must be the same on 2 Raspberry Pis for P2P to work.
sudo python3 lora_util.py

### P2P connection
#### For receiver
cd 02-p2p/
sudo python3 p2p_recv.py
#### For sender
cd 02-p2p/
sudo pytonh3 p2p_sender.py
### Change LoRaWAN settting
SX127x /
| --- Lora.py # Change setting manually
### Change Tx power
set_pa_config(pa_select = 1)
set_pa_dac(True)
set_pa_config(max_power=0x0f, output_power=0x0f) # Max power
set_pa_config(max_power=0x00, output_power=0x00) # Min power
#### Test Tx power
Compare the difference between max power and min power, and change the setting in p2p_recv.py and p2p_send.py
Max power: 15dBm RSSI: -101
Min power: -4.2dBm RSSI: -30
### Test Throughput, Latency, Packet loss rate, RSSI and SNR
Run lora_test_ssh.py to remotely connect to rpis. Test and copy the log files
Avg Throughput: 3.6769099248991153 kbps
Avg Latency: 217.57399999999998 ms
Packet Loss Rate: 0.0 %
Avg RSSI: -123.0
Avg SNR: 58.0
## 3. WiFi
### Change wifi into ad-hoc mode
essid need to be the same on Pi B and Pi B+
:::info
Attached to external Wi-Fi dongle is required
:::
sudo ifconfig wlan1 down
sudo iwconfig wlan1 mode ad-hoc
sudo iwconfig wlan1 essid Pi_adhoc
sudo iwconfig wlan1 channel 1
### Set IP on Pi B
sudo ifconfig wlan1 up
sudo ifconfig wlan1 192.168.1.5
### Set IP on Pi B+
sudo ifconfig wlan1 up
sudo ifconfig wlan1 192.168.1.6
### Use ping and traceroute to check whether they are connected
ping 192.168.1.6
traceroute 192.168.1.6
### Test throughout: iperf
sudo apt install iperf
Server (192.168.1.6)
iperf -s
Client (192.168.1.5)
iperf -c 192.168.1.6 -t 10 -i 1
Result: 21Mb/s
[ 1] local 192.168.0.108 port 5001 connected with 192.168.0.80 port 55132
[ ID] Interval Transfer Bandwidth
[ 1] 0.0000-10.5462 sec 26.4 MBytes 21.0 Mbits/sec
### Test latency and packet loss rate: mtr
sudo apt install mtr
Client (192.168.0.80)
mtr --no-dns --report --report-cycle 10 192.168.0.108
Result: 14.5 ms
HOST: Pi3B Loss% Snt Last Avg Best Wrst StDev
1.|-- 192.168.0.108 0.0% 10 7.8 14.5 6.9 42.4 10.7
## Flow to get Figs
Run wifi_setting_1.sh on client (192.168.1.5)
Run wifi_setting_2.sh on server (192.168.1.6)
Run T3_wifi_test_server.py on server to activate iperf.
Run T3_wifi_test_client.py on client.
Retrive the log file.
```
cd wifi_log
```
Use scp command to copy the log file to the local host.
```
scp rpi@192.168.0.108:/home/rpi/smartagr/wifi_log/T3_wifi_send_0.log ~/Desktop/
```
Run `T3_parser_new.py` to get figs.
:::info
Modify to multiple distances is required (todo)
:::
## Experiment result
Test with differnet distance between two nodes: 10cm, 20cm, 40cm, 80cm, 160cm
### Wifi
Throughput

RSSI

### LoRa
Throughput

RSSI

SNR
