Skip to content

Commit 5f385cb

Browse files
NicolasdejeanFokkeZB
authored andcommitted
Created calculateAirtime and made airtime public (TheThingsNetwork#162)
* created a calculateAirtime to inform the user and let him work with the airtime, called calculateAirtime in Trackaritime, added calculateAirtime in keywords.txt and in docs * fix docs
1 parent 9f19c4c commit 5f385cb

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

docs/TheThingsNetwork.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,14 @@ bool provision(const char *appEui, const char *appKey);
133133

134134
- `const char *appEui`: Application Identifier for the device.
135135
- `const char *appKey`: Application Key assigned to the device.
136+
137+
## Method: calculateAirtime
138+
Calculate the uplink time of a message.
139+
140+
```c
141+
float calculateAirtime(size_t payloadSize);
142+
```
143+
144+
- `size_t payloadSize`: number of bytes you want to send.
145+
146+
Returns the uplink time of the message in seconds.

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ join KEYWORD2
2020
personalize KEYWORD2
2121
sendBytes KEYWORD2
2222
poll KEYWORD2
23+
calculateAirtime KEYWORD2
24+
airtime KEYWORD2
2325

2426
encodeSensorData KEYWORD2
2527
decodeAppData KEYWORD2

src/TheThingsNetwork.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,17 @@ int8_t TheThingsNetwork::poll(port_t port, bool confirm) {
494494
return sendBytes(payload, 1, port, confirm);
495495
}
496496

497+
float TheThingsNetwork::calculateAirtime(size_t payloadSize) {
498+
payloadSize = 13 + payloadSize;
499+
500+
float Tsym = pow(2, this->info.sf) / this->info.band;
501+
float Tpreamble = (this->info.ps + 4.25) * Tsym;
502+
uint16_t payLoadSymbNb = 8 + (max(ceil((8 * payloadSize - 4 * this->info.sf + 28 + 16 - 20 * this->info.header) / (4 * (this->info.sf - 2 * this->info.de))) * (this->info.cr + 4), 0));
503+
float Tpayload = payLoadSymbNb * Tsym;
504+
float Tpacket = Tpreamble + Tpayload;
505+
return (Tpacket / 1000);
506+
}
507+
497508
void TheThingsNetwork::fillAirtimeInfo() {
498509
this->info = {0, 0, 0, 0, 0, 0};
499510

@@ -516,14 +527,7 @@ void TheThingsNetwork::fillAirtimeInfo() {
516527
}
517528

518529
void TheThingsNetwork::trackAirtime(size_t payloadSize) {
519-
payloadSize = 13 + payloadSize;
520-
521-
float Tsym = pow(2, this->info.sf) / this->info.band;
522-
float Tpreamble = (this->info.ps + 4.25) * Tsym;
523-
uint16_t payLoadSymbNb = 8 + (max(ceil((8 * payloadSize - 4 * this->info.sf + 28 + 16 - 20 * this->info.header) / (4 * (this->info.sf - 2 * this->info.de))) * (this->info.cr + 4), 0));
524-
float Tpayload = payLoadSymbNb * Tsym;
525-
float Tpacket = Tpreamble + Tpayload;
526-
this->airtime = this->airtime + (Tpacket / 1000);
530+
this->airtime = this->airtime + calculateAirtime(payloadSize);
527531
}
528532

529533
void TheThingsNetwork::showStatus() {

src/TheThingsNetwork.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class TheThingsNetwork
4242
Stream* debugStream;
4343
bool model_EU;
4444
airtime_t info;
45-
float airtime;
4645
ttn_fp_t fp;
4746
uint8_t sf;
4847
uint8_t fsb;
@@ -74,6 +73,8 @@ class TheThingsNetwork
7473

7574
public:
7675
TheThingsNetwork(Stream& modemStream, Stream& debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
76+
float airtime;
77+
float calculateAirtime(size_t payload);
7778
void showStatus();
7879
void onMessage(void (*cb)(const byte* payload, size_t length, port_t port));
7980
bool provision(const char *appEui, const char *appKey);

0 commit comments

Comments
 (0)