Skip to content

Instantly share code, notes, and snippets.

@martinholub
Last active July 15, 2018 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinholub/e42e59461f7847fc9e5eac06caeac098 to your computer and use it in GitHub Desktop.
Save martinholub/e42e59461f7847fc9e5eac06caeac098 to your computer and use it in GitHub Desktop.
Talking Bytes with The Things Network
void add_buffer_gps(double lat, double lon){
// offset by 90(180) to make positive and scale to 0..1
latB = ((lat + 90) / 180.0) * pow(256,3);
lonB = ((lon + 180) / 360.0) * pow(256,3);
// byte shifting
dataBuffer[0] = ( latB >> 16 ) & 0xFF;
dataBuffer[1] = ( latB >> 8 ) & 0xFF;
dataBuffer[2] = latB & 0xFF;
dataBuffer[3] = ( lonB >> 16 ) & 0xFF;
dataBuffer[4] = ( lonB >> 8 ) & 0xFF;
dataBuffer[5] = lonB & 0xFF;
}
{"_id": "5b377f54e4d28c0006c1ab93",
"deviceId":"ttn-uno",
"receivedAt":"2018-06-30T13:02:12.565Z",
"metadata":{
"time":"2018-06-30T13:02:12.339076452Z",
"frequency":868.1,
"modulation":"LORA",
"data_rate":"SF7BW125",
"airtime":66816000,
"coding_rate":"4/5",
"gateways":[
{"gtw_id":"eui-0000024b080310b1",
"timestamp":628068507,
"time":"2018-06-30T13:02:11.414067Z",
"channel":0,
"rssi":-117,
"snr":1.5,
"rf_chain":1,
"latitude":47.37787,
"longitude":8.53909,
"altitude":433
}
]
},
"dist":0,
"speed":0,
"temp-inside":16.99981689453125,
"temp-outside":23.09722900390625,
"tiltx":0.1875,
"tilty":-0.203125
}
function Decoder (bytes, port) {
var decoded = {}
if (port === 1) {
if (bytes.length === 10) {
var lat = bytesToInt(bytes.slice(0, 3)) / Math.pow(256, 3) * 180 - 90
var lon = bytesToInt(bytes.slice(3, 6)) / Math.pow(256, 3) * 360 - 180
if (lat !== 0 && lon !== 0) {
decoded['lat'] = lat
decoded['lon'] = lon
}
var tiltx = bytesToInt(bytes.slice(10, 11)) / Math.pow(256, 1) * 4 - 2
decoded['tiltx'] = tiltx
var tilty = bytesToInt(bytes.slice(11, 12)) / Math.pow(256, 1) * 4 - 2
decoded['tilty'] = tilty
}
}
return decoded
}
function bytesToInt (bytes) {
var integer = 0
for (var n = 0; n < bytes.length; n++) {
integer += bytes[n]
if (n < bytes.length - 1) {
integer = integer << 8
}
}
return integer
}
#include <TheThingsNetwork.h>
#include <private_keys.h> // imports appKey and appEUI from external file
#define loraSerial Serial1
#define freqPlan TTN_FP_EU868 // Use european freq plan
#define COUNT_OF(array) (sizeof(array) / sizeof(array[0])) // gets array size
TheThingsNetwork ttn(loraSerial, Serial, freqPlan);
bool is_connected = false;
uint8_t dataBuffer[12] = {0}; // uint8_t is byte, buffer can hold 12 bytes
void send_ttn_message(uint8_t* data, size_t data_size, port_t prt = 1) {
if(is_connected){
ttn.sendBytes(data, data_size);// sizeof(data)
} else {
Serial.print("ttn message: ");
uint8_t i = 0;
for (i = 0; i < data_size; i++){ // prints byte message in HEX encoding
if (data[i] < 16) {
Serial.print("0");
Serial.print(data[i], HEX);
} else {
Serial.print(data[i], HEX);
}
}
Serial.println("");
}
}
void setup(){
Serial.print("--- TTN STATUS ---"); ttn.showStatus();
ttn.provision(appEui, appKey);
is_connected = ttn.join(10); // try to connect up to 10 times
if (!is_connected){
Serial.print("Join to TTN failed.");
}
}
void loop(){
// ...
// Messaging the TTN Application
if (iteration % send_per == 0){ // send only infrequently
send_ttn_message(dataBuffer, COUNT_OF(dataBuffer));
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment