Custom JavaScript codec functions for RAK1901

I’ve set up a ChirpStack gateway and have connected a WisBlock Node and Sensor with the Environment_Monitoring.ino installed but even if I can correctly see in my Serial Monitor the data being transmitted like so:

result: Tem:28.25C Hum:29.71% Pres:979.11KPa Gas:18378Ohms
lmh_send ok count 933

my Applications / app /Devices / my_device receives incorrectly decoded data like such:

  • DecodeDataHex:“0x01, 0x0b, 0x11, 0x0b, 0x8e, 0x00, 0x01, 0x7e, 0x76, 0x00, 0x00, 0x48, 0x0b”
  • DecodeDataString:"Ž~vH"

I can assume that I am not using the correct Javascript codec functions in my Device-profiles / my_current_device so can you tell me what should I use?

Try this encoder in Chirpstack:

// Decode decodes an array of bytes into an object.
//  - fPort contains the LoRaWAN fPort number
//  - bytes is an array of bytes, e.g. [225, 230, 255, 0]
//  - variables contains the device variables e.g. {"calibration": "3.5"} (both the key / value are of type string)
// The function must return an object, e.g. {"temperature": 22.5}
function Decode(fPort, bytes, variables) {
	var decoded = {};
	switch (bytes[0])
	{
		case 0x01: // Environment sensor data
			decoded.temperature = (bytes[1] << 8 | bytes[2]) / 100;
			decoded.humidity = (bytes[3] << 8 | + bytes[4]) / 100;
			decoded.pressure = (bytes[8] | (bytes[7] << 8) | (bytes[6] << 16) | (bytes[5] << 24)) / 100;
			decoded.gas = bytes[12] | (bytes[11] << 8) | (bytes[10] << 16) | (bytes[9] << 24);
			decoded.cnt = bytes[16] | (bytes[15] << 8) | (bytes[14] << 16) | (bytes[13] << 24);
			break;
		case 0x02: // Temperature and humidity sensor data
			decoded.temperature = (bytes[1] << 8 | bytes[2]) / 100;
			decoded.humidity = (bytes[3] << 8 | + bytes[4]) / 100;
			decoded.cnt = bytes[8] | (bytes[7] << 8) | (bytes[6] << 16) | (bytes[5] << 24);
			break;
		case 0x03: // Ambient light sensor data
			decoded.light = (bytes[4] | (bytes[3] << 8) | (bytes[2] << 16) | (bytes[1] << 24)) / 100;
			decoded.cnt = bytes[8] | (bytes[7] << 8) | (bytes[6] << 16) | (bytes[5] << 24);
			break;
		case 0x04: // No sensor data, just counter
			decoded.cnt = bytes[4] | (bytes[3] << 8) | (bytes[2] << 16) | (bytes[1] << 24);
			break;
		case 0x30: // Accelerometer sensor
        	if (bytes[1] == 0) {
				decoded.x_move = "no";
            } else {
				decoded.x_move = "yes";
            }
        	if (bytes[2] == 0) {
				decoded.y_move = "no";
            } else {
				decoded.y_move = "yes";
            }
        	if (bytes[3] == 0) {
				decoded.z_move = "no";
            } else {
				decoded.z_move = "yes";
            }
			break;
		default:
			decoded.unknown = "Unknown data format";
			break;
	}
	return decoded;
}
2 Likes

I know I can just use the “like” button but can’t help it not to thank you in words :heart_eyes:

You’re welcome.
When I started to test all our WisBlock solution examples, I felt like I should write an encoder for all of them. But then I forgot to publish it. :rofl:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.