Decoding sensor data

Hello,

Im having trouble decoding the binary sensor data in the payload formatter.

I have tried various snippets of code and different variations.

This is the code in my Things Uno:

#include <Wire.h>

#include <BH1750.h>

#include <TheThingsNetwork.h>

// Set your AppEUI and AppKey
const char *appEui = "hidden";
const char *appKey = "hidden";

#define loraSerial Serial1
#define debugSerial Serial

// Replace REPLACE_ME with TTN_FP_EU868 or TTN_FP_US915
#define freqPlan TTN_FP_EU868

TheThingsNetwork ttn(loraSerial, debugSerial, freqPlan);
BH1750 lightMeter;

void setup()
{
  loraSerial.begin(57600);
  debugSerial.begin(9600);

  // Wait a maximum of 10s for Serial Monitor
  
  while (!debugSerial && millis() < 10000)
    ;

  debugSerial.println("-- STATUS");
  ttn.showStatus();

  debugSerial.println("-- JOIN");
  ttn.join(appEui, appKey);
  
  Wire.begin();
  lightMeter.begin();
}

void loop()
{
  debugSerial.println("-- LOOP");

  lux = lightMeter.readLightLevel() * 100;
  
  byte payload[2];
  payload[0] = highByte(lux);
  payload[1] = lowByte(lux);
  
  debugSerial.print("Light: ");
  debugSerial.print(lux);
  debugSerial.println(" lx");

  ttn.sendBytes(payload, sizeof(payload));

  delay(1000);
}

The payload f

ormatter looks like:


function decodeUplink(input) {
  var data = {};
  var light = (input.bytes[0] << 8) | input.bytes[1];
  return {
    data: light / 100,
  };
}

The error I get is
could not convert struct value 59.16 to map[string]interface {} for field Data: could not convert 59.16 to map[string]interface {}

When I try other variations that work, I get an empty output - {}
Im not sure where I am going wrong and have read through and tried to interpret different examples.

I appreciate your support in advance

Kind Regards
Zhivko

Check out the documentation as you’ve got a stray comma in your return structure.

And check out https://www.thethingsnetwork.org/docs/devices/bytes/ to see how you are OR’ing the two bytes rather than adding them - which may in theory work but could be ambiguous for the wonders of JavaScript.

And check out the </> on the toolbar for formatting code in your posts.

Than you for your response @descartes

I have read through the documentation which you provided a link to. It gives a very informative and detailed breakdown of bytes with references to further reading. I appreciate this.

However, it doesnt bring me any closer to figuring out what the problem is, rather it raises more questions and I have deviated further from the answer.

The main reason for posting a topic regarding this is that my head is about to explode and I’m probably missing the simplest detail, which is probably clearly obvious to many people.

I am just starting out with working with this kind of encoding/decoding. If I can see a working example, then it is easier to understand how the code is being manipulated to get a better understanding of how it works.

Right now I am going round in circles and not really understanding where I am making mistakes and just guessing.

function decodeUplink(input) {
  var light = (input.bytes[0] << 8) + input.bytes[1];
  return {
    data: {
     light: input.bytes
    }
  }
}

returns

{
  "light": [
    62,
    128
  ]
}

modified the following line:

     light: input.bytes

to

     light: light

Now it works

For refrence, I found this regarding “bitwise OR” “|”
I believe you are referring to this line

var light = (input.bytes[0] << 8) | input.bytes[1];

which should look like (bitwise AND) “+”

var light = (input.bytes[0] << 8) + input.bytes[1];

You may benefit from some of the coding tutorials online as that’s not a bitwise AND, that is an addition - you are bit shifting the first byte to make it an integer and then adding the byte. A bitwise AND is & and would be worse than doing the OR |

And the output of

light: input.bytes

given that the input.bytes is an array of bytes, is the expected array of bytes.

A little time refreshing your C skills will help with head exploding issues - no amount of working examples here can compensate for that as it usually involves CopyNPasta programming without understanding.

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