この完全にユニットテストされたライブラリを使用すると、Arduino側のデータをエンコードして、TTN側にデコードできます。 CベースのエンコーダーとJavaScriptベースのデコーダーの両方を提供します。
バージョン2.2.0以降、TTN側のエンコーダーもあります。
Arduino側:
# include " LoraMessage.h "
LoraMessage message;
message
.addUnixtime( 1467632413 )
.addLatLng(- 33.905052 , 151.26641 );
lora_send_bytes (message.getBytes(), message.getLength());
delete message;TTN側:
function decodeUplink ( input )
{
// decoder function according to https://www.thethingsindustries.com/docs/integrations/payload-formatters/javascript/uplink/
// input has the following structure:
// {
// "bytes": [1, 2, 3], // FRMPayload (byte array)
// "fPort": 1
// }
var data = decode ( input . bytes , [ unixtime , latLng ] , [ 'time' , 'coords' ] ) ;
return { data : data }
}
// include content from src/decoder.js:
var bytesToInt = function ( bytes ) {
var i = 0 ;
for ( var x = 0 ; x < bytes . length ; x ++ ) {
i |= + ( bytes [ x ] << ( x * 8 ) ) ;
}
return i ;
} ;
...TTN側:
// include src/encoder.js
var bytes = encode ( [ timestamp , [ latitude , longitude ] ] , [ unixtime , latLng ] ) ;
// bytes is of type Buffer // include src/encoder.js
// include src/LoraMessage.js
var bytes = new LoraMessage ( encoder )
. addUnixtime ( 1467632413 )
. addLatLng ( - 33.905052 , 151.26641 )
. addBitmap ( true , true , false , true )
. getBytes ( ) ;
// bytes = <Buffer 1d 4b 7a 57 64 a6 fa fd 6a 24 04 09 d0>そして、いつものようにデコードします:
var result = decoder . decode (
bytes ,
[ decoder . unixtime , decoder . latLng , decoder . bitmap ] ,
[ 'time' , 'coords' , 'heaters' ]
) ;
// result =
// { time: 1467632413,
// coords: [ -33.905052, 151.26641 ],
// heaters:
// { a: true,
// b: true,
// c: false,
// d: true,
// e: false,
// f: false,
// g: false,
// h: false } } Unix時間(秒)をシリアル化/脱必要にします
# include " LoraEncoder.h "
byte buffer[ 4 ];
LoraEncoder encoder (buffer);
encoder.writeUnixtime( 1467632413 );
// buffer == {0x1d, 0x4b, 0x7a, 0x57}次に、TTNフロントエンドで、次の方法を使用します。
unixtime ( input . bytes . slice ( x , x + 4 ) ) // 14676324136小数の精度で座標(緯度/経度)をシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 8 ];
LoraEncoder encoder (buffer);
encoder.writeLatLng(- 33.905052 , 151.26641 );
// buffer == {0x64, 0xa6, 0xfa, 0xfd, 0x6a, 0x24, 0x04, 0x09}次に、TTNフロントエンドで、次の方法を使用します。
latLng ( input . bytes . slice ( x , x + 8 ) ) // [-33.905052, 151.26641]符号なしの8ビット整数をシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 1 ];
LoraEncoder encoder (buffer);
uint8_t i = 10 ;
encoder.writeUint8(i);
// buffer == {0x0A}次に、TTNフロントエンドで、次の方法を使用します。
uint8 ( input . bytes . slice ( x , x + 1 ) ) // 10符号なしの16ビット整数をシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 2 ];
LoraEncoder encoder (buffer);
uint16_t i = 23453 ;
encoder.writeUint16(i);
// buffer == {0x9d, 0x5b}次に、TTNフロントエンドで、次の方法を使用します。
uint16 ( input . bytes . slice ( x , x + 2 ) ) // 23453符号なしの32ビット整数をシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 4 ];
LoraEncoder encoder (buffer);
uint32_t i = 2864434397 ;
encoder.writeUint32(i);
// buffer == {0xdd, 0xcc, 0xbb, 0xaa}次に、TTNフロントエンドで、次の方法を使用します。
uint32 ( input . bytes . slice ( x , x + 4 ) ) // 2864434397-327.68から+327.67(包括的)の間の温度測定値を2小数の精度でシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 2 ];
LoraEncoder encoder (buffer);
encoder.writeTemperature(- 123.45 );
// buffer == {0xcf, 0xc7}次に、TTNフロントエンドで、次の方法を使用します。
temperature ( input . bytes . slice ( x , x + 2 ) ) // -123.450〜100(包括的)の間の湿度の読み取り値を2小数の精度でシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 2 ];
LoraEncoder encoder (buffer);
encoder.writeHumidity( 99.99 );
// buffer == {0x0f, 0x27}次に、TTNフロントエンドで、次の方法を使用します。
humidity ( input . bytes . slice ( x , x + 2 ) ) // 99.99完全な4バイトフロートをシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 4 ];
LoraEncoder encoder (buffer);
encoder.writeRawFloat( 99.99 );
// buffer == {0xe1, 0xfa, 0xc7, 0x42}次に、TTNフロントエンドで、次の方法を使用します。
rawfloat ( input . bytes . slice ( x , x + 4 ) ) // 99.990〜8種類のフラグを含むビットマップをシリアル化/脱必要にします。
# include " LoraEncoder.h "
byte buffer[ 1 ];
LoraEncoder encoder (buffer);
encoder.writeBitmap( true , false , false , false , false , false , false , false );
// buffer == {0x80}次に、TTNフロントエンドで、次の方法を使用します。
bitmap ( input . bytes . slice ( x , x + 1 ) ) // { a: true, b: false, c: false, d: false, e: false, f: false, g: false, h: false } デコーダーを使用すると、複数の値をバイト配列に書き込むことができます。
# include " LoraEncoder.h "
byte buffer[ 19 ];
LoraEncoder encoder (buffer);
encoder.writeUnixtime( 1467632413 );
encoder.writeLatLng(- 33.905052 , 151.26641 );
encoder.writeUint8( 10 );
encoder.writeUint16( 23453 );
encoder.writeUint32( 2864434397 );
encoder.writeTemperature( 80.12 );
encoder.writeHumidity( 99.99 );
encoder.writeRawFloat( 99.99 );
encoder.writeBitmap( true , false , false , false , false , false , false , false );
/* buffer == {
0x1d, 0x4b, 0x7a, 0x57, // Unixtime
0x64, 0xa6, 0xfa, 0xfd, 0x6a, 0x24, 0x04, 0x09, // latitude,longitude
0x0A, // Uint8
0x9d, 0x5b, // Uint16
0xdd, 0xcc, 0xbb, 0xaa, // Uint32
0x1f, 0x4c, // temperature
0x0f, 0x27, // humidity
0xe1, 0xfa, 0xc7, 0x42, // 4-byte float
0x80 // bitmap
}
*/ LoraMessage読みを追加できるロラメッジを表す便利なクラスがあります。
# include " LoraMessage.h "
LoraMessage message;
message
.addUnixtime( 1467632413 )
.addLatLng(- 33.905052 , 151.26641 )
.addUint8( 10 )
.addUint16( 23453 )
.addUint32( 2864434397 )
.addTemperature( 80.12 )
.addHumidity( 99.99 )
.addRawFloat( 99.99 )
.addBitmap( false , false , false , false , false , false , true , false );
send (message.getBytes(), message.getLength());
/*
getBytes() == {
0x1d, 0x4b, 0x7a, 0x57, // Unixtime
0x64, 0xa6, 0xfa, 0xfd, 0x6a, 0x24, 0x04, 0x09, // latitude,longitude
0x0A, // Uint8
0x9d, 0x5b, // Uint16
0xdd, 0xcc, 0xbb, 0xaa, // Uint32
0x1f, 0x4c, // temperature
0x0f, 0x27, // humidity
0xe1, 0xfa, 0xc7, 0x42, // 4-byte float
0xfd // Bitmap
}
and
getLength() == 28
*/decodeメソッドを備えたTTNデコーダーフロントエンドの構成decodeメソッドを使用すると、着信バイトバッファー(このライブラリによって生成された)のマスクを指定し、それに応じてデコード機能を適用できます。
decode ( byte Array , mask Array [ , mapping Array ] ) src/decoder.jsからデコーダーメソッドにすべてを貼り付け、次のように使用します。
function ( bytes ) {
// code from src/decoder.js here
return decode ( bytes , [ latLng , unixtime ] , [ 'coords' , 'time' ] ) ;
}これにより、12バイトの着信バイトバッファーが1つのlatLng (8バイト)のシーケンスと1つのunixtime (4バイト)シーケンスにマッピングされ、最初のバッティング(4バイト)シーケンスをマップし、キーcoordsに、2番目はキーtimeにマッピングします。
使用できます: 64 A6 FA FD 6A 24 04 09 1D 4B 7A 57テストのために、次のようになります。
{
"coords" : [
-33.905052 ,
151.26641
],
"time" : 1467632413
}デコーダーをコンソールにセットアップします。
デコードメソッドはすでに必要な変換のほとんどを実行しているため、ほとんどの場合、次のデータを渡すことができます。
yarn経由で依存関係をインストールしますyarn run test:cyarn testを介してユニットテスト(JavaScript)を実行しますyarn coverageを介してカバレッジ(JavaScript)を確認します( coverage/lcov-reportを参照)CIは、プルリクエストを自動的に作成するとキックオフされます。
ESP-IDFプロジェクトのmain/idf_component.ymlファイルにlora-serialization依存関係を追加します。
dependencies:
lora-serialization:
git: https://github.com/thesolarnomad/lora-serialization.git
main/CMakeLists.txtに同様に
idf_component_register( ...
REQUIRES .... lora-serialization
)