---
# System prepended metadata

title: TP3

---

# TP3


## 1. Objectives


## 2. What is TTN




## 3. Hello World



### 3.1. Setup TTN

WARNING: With ABP Arduino resets its own counter while keeping always the same session in TTN. Which makes TTN think it is a replay attack and discard packets instead of sending them.


### 3.2. Setup an end-device
1. https://www.thethingsindustries.com/docs/devices/abp-vs-otaa/

**Activation by Personlization**

En Activation by Personalization (ABP), il n'y a pas de demande à rejoindre un réseau (pas de procédure de join). Toutes les clés (DevAddr et les clés de chiffrement) sont directement écrites en dur dans le code source du noeud

**Over-The-Air Activation**

Over-the-Air Activation (OTAA) est la façon attendue de se connecter à un réseau LoRaWAN. Le device procède à une procédure de join durant laquelle une DevAddr est fixée et où les clés de chiffrement (NetSKey et AppSKey) sont négociées... over the air justement. Un peu comme une navigation en HTTPS.

Which parameters are different
when comparing the ABP code with the OTAA code and why?


In the code we can find that


How often are the packets sent? Why?
```cpp!
// Schedule TX every this many seconds (might become longer due to duty
// cycle limitations).
const unsigned TX_INTERVAL = 60;
```

### 3.3. Decoding the payload

```javascript!
function decodeUplink(input) {
  
  data = {}
  data.message = String.fromCharCode.apply(null, new Uint8Array(input.bytes));
  
  
  
  return {data}

}
```

## 4. Collect temperature and humidity

We want to send temp and humidity in 2bytes format each to have a payload that looks like
```
0...1...2....3...
hum hum temp temp
```

We first try encoding in c and decoding in js with sample value.
To get a 2 bytes encoding we reduce precision to 2 digits by doing `(int) val * 100`

Value of humidity and temps will fit in the range of possible values with 2 bytes

encode.c
```c!
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// takes float as input
// returns 2 bytes array as int
// uses a factor of 100 for int conversion
// array has to be allocated first
uint8_t *floatTo2BytesArrayInt(float f, uint8_t *bytes)
{
    // reset memory
    memset(bytes, 0, sizeof(uint8_t) * 2);
    uint16_t val_int = (uint16_t)(f * 100);
    bytes[0] = (val_int);
    bytes[1] = (val_int >> 8);
    return bytes;
}

// main
int main(int argc, char const *argv[])
{
    // create a float var
    float hum = 24.5969;

    // encode the float var in 2 bytes array as int
    uint8_t *hum_bytes = (uint8_t *)malloc(sizeof(uint8_t) * 2);
    floatTo2BytesArrayInt(hum, hum_bytes);

    // print each array element in hex format
    for (int i = 0; i < 2; i++)
    {
        printf("hum_bytes[%d]: %x\n", i, hum_bytes[i]);
    }
    return 0;
}
```

decode.js
```javascript!
function decodeUplink(input) {
  
  data = {}
  
  data.humidity = decodeBytesArrayToInt(reverse(input.bytes.slice(0,4)));
  data.temperature = decodeBytesArrayToInt(reverse(input.bytes.slice(4,8)));
  
  return {data}
}

// reverse bytes
function reverse(bytes) {
  var result = [];
  for (var i = bytes.length - 1; i >= 0; i--) {
    result.push(bytes[i]);
  }
  return result;
}

// decode function
// use 100 as factor
function decodeBytesArrayToInt(bytes) {
  var val = 0;
  var len = bytes.length;
  for (var i = 0; i < len; i++) {
    val = val * 256 + bytes[i];
  }
  return val / 100;
}

```

Then we use these function in the arduino code and as js formatter in TTN console.

## 5. Visualize data

`mosquitto_sub -h eu1.cloud.thethings.network -t "#" -u "rld-tp3-tom@ttn" -P "NNSXS.PYHDX664NLVPFHPBVERARVHO2KLCQ26WUIPWJ5A.LC7LSUEARRT6FBSC6N4KG6D5XZHPYUNTSJ3VBXGCFLDR5YVRX36A" -d`





# PingPong log parser

## Input
This program use the experiment configurations defined in asset/config.json and the log files generated after experimentations.

For each log file, it will parse the content and retrieve the rssi and the snr values. And it will calculate the mean and standard deviation 

## Run mode

`python pingpong_parser.py`

## Output

After execution, a csv file (experimentation.csv) is generetad with the following values : 
* experimentation_name
* mean_snr
* stdev_snr
* mean_rssi
* stdev_rssi
* bw(bandwidth)
* cr(coding rate)
* sf(spreading factor)


![](https://i.imgur.com/yM89t6a.png)


