이 완전 단위 테스트 라이브러리를 사용하면 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 ) ) // 1467632413정밀도의 정밀도로 좌표 (위도/경도) 직렬화/사제화.
# 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 (포함) 사이의 온도 판독 값을 직렬화/제외합니다.
# 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.45정밀도의 정밀도로 0과 100 (포함) 사이의 습도 판독 값을 직렬화/제외합니다.
# 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다음에 읽을 수있는 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 에서 decoder 방법에 모든 것을 붙여 넣고 다음과 같이 사용하십시오.
function ( bytes ) {
// code from src/decoder.js here
return decode ( bytes , [ latLng , unixtime ] , [ 'coords' , 'time' ] ) ;
} 이것은 12 바이트의 들어오는 바이트 버퍼를 하나의 latLng (8 바이트) 및 하나의 unixtime (4 바이트) 시퀀스의 시퀀스에 맵핑하고 첫 번째 것을 주요 coords 에, 두 번째는 키 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
)