Skip to content

Commit 921e603

Browse files
NicolasdejeanFokkeZB
authored andcommitted
Protocol Buffer (nanopb) integration (#108)
1 parent 7714097 commit 921e603

20 files changed

+3310
-2
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ cache:
99
- "~/.platformio"
1010

1111
env:
12-
- PLATFORMIO_CI_SRC=test
12+
- PLATFORMIO_CI_SRC=test/TheThingsNetwork.ino
13+
- PLATFORMIO_CI_SRC=test/TheThingsMessage.ino
1314

1415
install:
1516
- pip install -U platformio

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
PROTOC = $(NANOPB)/generator-bin/protoc --proto_path=src/api --nanopb_out="-v:src"
2+
3+
.PHONY: proto
4+
5+
proto:
6+
$(PROTOC) src/api/*.proto

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ This is an [Arduino Library](https://www.arduino.cc/en/Guide/Libraries) for Ardu
1313
## Documentation
1414

1515
* [The Things Network Documentation / Arduino](https://www.thethingsnetwork.org/docs/arduino/)
16-
* [API Reference](API.md)
16+
* API References:
17+
* [TheThingsNetwork](docs/TheThingsNetwork.md)
18+
* [TheThingsMessage](docs/TheThingsMessage.md)
1719

1820
## Examples
1921

docs/TheThingsMessage.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# API Reference
2+
The `TheThingsMessage` class provides structs for sensor and application data you can encode and decode as bytes.
3+
4+
## Class: TheThingsMessage
5+
6+
```c
7+
#include <TheThingsMessage.h>
8+
```
9+
10+
## Type: sensordata_t
11+
12+
Create a struct of this type using `api_SensorData_init_default` as defaults.
13+
14+
```c
15+
sensordata_t data = api_SensorData_init_default;
16+
```
17+
18+
Then in your `setup()` function select what fields of the struct should be included when encoding it as bytes:
19+
20+
```c
21+
data.has_motion = true;
22+
data.has_water = false;
23+
data.has_temperature_celcius = true;
24+
data.has_temperature_fahrenheit = true;
25+
data.has_humidity = true;
26+
```
27+
28+
In your `loop()` function read your sensors and set the fields of the struct:
29+
30+
```c
31+
data.motion = true;
32+
data.water = 682;
33+
data.temperature_celcius = 30
34+
data.temperature_fahrenheit = 86;
35+
data.humidity = 97;
36+
```
37+
38+
### Additional analog readings
39+
40+
You can also add other analog readings.
41+
42+
> **TODO:** Document how this works and include in example.
43+
44+
## Method: encodeSensorData
45+
Encode the message you want to send.
46+
47+
```c
48+
static void encodeSensorData(sensordata_t *data, byte **buffer, size_t *size);
49+
```
50+
51+
- `sensordata_t *data`: Structure containing all the message we can send.
52+
- `const byte **buffer`: Bytes received.
53+
- `size_t *size`: The number of bytes.
54+
55+
Usage:
56+
57+
```c
58+
byte *buffer;
59+
size_t size;
60+
TheThingsMessage::encodeSensorData(&data, &buffer, &size);
61+
```
File renamed without changes.

examples/Message/Send/Send.ino

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <TheThingsNetwork.h>
2+
#include <TheThingsMessage.h>
3+
4+
// Set your AppEUI and AppKey
5+
const char *appEui = "0000000000000000";
6+
const char *appKey = "00000000000000000000000000000000";
7+
8+
#define loraSerial Serial1
9+
#define debugSerial Serial
10+
11+
TheThingsNetwork ttn(loraSerial, debugSerial, /* TTN_FP_EU868 or TTN_FP_US915 */);
12+
13+
sensordata_t data = api_SensorData_init_default;
14+
15+
void setup() {
16+
loraSerial.begin(57600);
17+
debugSerial.begin(9600);
18+
19+
// Wait a maximum of 10s for Serial Monitor
20+
while (!debugSerial && millis() < 10000);
21+
22+
debugSerial.println("-- STATUS");
23+
ttn.showStatus();
24+
25+
debugSerial.println("-- JOIN");
26+
ttn.join(appEui, appKey);
27+
28+
// Select what fields to include in the encoded message
29+
data.has_motion = true;
30+
data.has_water = false;
31+
data.has_temperature_celcius = true;
32+
data.has_temperature_fahrenheit = true;
33+
data.has_humidity = true;
34+
}
35+
36+
void loop() {
37+
debugSerial.println("-- LOOP");
38+
39+
// Read the sensors
40+
data.motion = true;
41+
data.water = 682;
42+
data.temperature_celcius = 30;
43+
data.temperature_fahrenheit = 86;
44+
data.humidity = 97;
45+
46+
// Encode the selected fields of the struct as bytes
47+
byte *buffer;
48+
size_t size;
49+
TheThingsMessage::encodeSensorData(&data, &buffer, &size);
50+
51+
ttn.sendBytes(buffer, size);
52+
53+
delay(10000);
54+
}

keywords.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#######################################
88

99
TheThingsNetwork KEYWORD1
10+
TheThingsMessage KEYWORD1
1011

1112
#######################################
1213
# Methods and Functions (KEYWORD2)
@@ -20,6 +21,8 @@ personalize KEYWORD2
2021
sendBytes KEYWORD2
2122
poll KEYWORD2
2223

24+
encodeSensorData KEYWORD2
25+
2326
#######################################
2427
# Constants (LITERAL1)
2528
#######################################

src/TheThingsMessage.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <TheThingsMessage.h>
2+
3+
void TheThingsMessage::encodeSensorData(sensordata_t *data, byte **buffer, size_t *size) {
4+
byte message[51];
5+
pb_ostream_t sendStream = pb_ostream_from_buffer(message, sizeof(message));
6+
pb_encode(&sendStream, api_SensorData_fields, data);
7+
*buffer = message;
8+
*size = sendStream.bytes_written;
9+
}

src/TheThingsMessage.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef _THETHINGSMESSAGE_H_
2+
#define _THETHINGSMESSAGE_H_
3+
4+
#include <TheThingsNetwork.h>
5+
#include <pb.h>
6+
#include <pb_encode.h>
7+
#include <pb_decode.h>
8+
#include "sensorData.pb.h"
9+
10+
typedef api_SensorData sensordata_t;
11+
12+
class TheThingsMessage
13+
{
14+
public:
15+
static void encodeSensorData(sensordata_t *data, byte **buffer, size_t *size);
16+
};
17+
18+
#endif

src/api/sensorData.proto

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
syntax = "proto2";
2+
3+
package api;
4+
5+
message SensorData {
6+
optional bool motion = 1;
7+
optional uint32 water = 2;
8+
optional float temperature_celcius = 3;
9+
optional float temperature_fahrenheit = 4;
10+
optional float humidity = 5;
11+
12+
repeated uint32 analog_readings = 110;
13+
}

0 commit comments

Comments
 (0)