Work with strings and regex in decode function issue, why?

Hello,
I’m new to LoRa and ChirpStack especially, I have a LoRa node that sends some sensor data in a specific type “t19.11h43.39p931.14g397.29f8000a10s10”. I send it like that because sometimes the data I send should look like this “t19.11h43.39p931.14g397.29fnansn” - n stands for None, so I just don’t add this field in my JSON. The problem is that my code works perfectly in the browser, but on the server I get errors. My function looks like this:

function Decode(fPort, bytes, variables) {
    var jsonObject = new Object();
    var temp;
    var humidity;
    var pressure;
    var gas;
    var frequency;
    var amplitude;
    var seconds;
    var message = atob(bytes);
    message = message.split(/(?=[thpgfas])/g);

    var i;
    for (i of message) {
        switch(i[0]) {
            case 't':
                var str = i.slice(1);
                temp = parseFloat(str);
                break;
            case 'h':
                var str = i.slice(1);
                humidity = parseFloat(str);
                break;
            case 'p':
                var str = i.slice(1);
                pressure = parseFloat(str);
                break;
            case 'g':
                var str = i.slice(1);
                gas = parseFloat(str);
                break;
            case 'f':
                var str = i.slice(1);
                frequency = parseFloat(str);
                break;
            case 'a':
                var str = i.slice(1);
                amplitude = parseFloat(str);
                break;
            case 's':
                var str = i.slice(1);
                seconds = parseFloat(str);
                break;
          }
    }   

    if (!isNaN(temp)) {
        jsonObject.temp = temp;
    }
    if (!isNaN(humidity)) {
        jsonObject.humidity = humidity;
    }
    if (!isNaN(pressure)) {
        jsonObject.pressure = pressure;
    }
    if (!isNaN(gas)) {
        jsonObject.gas = gas;
    }
    if (!isNaN(frequency)) {
        jsonObject.frequency = frequency;
    }
    if (!isNaN(amplitude)) {
        jsonObject.amplitude = amplitude;
    }
    if (!isNaN(seconds)) {
        jsonObject.seconds = seconds;
    }
    return jsonObject;
}

So, does someone have an idea of what is getting wrong with my code?
Best regards!

Please do not mix multiple questions within one topic to keep the forum organized.

Okay, I removed the second question.

Is the JS-engine used by the App-Server

(?=) // Lookahead (positive), currently a parsing error

I would suggest using a binary representation.
Example:
HEADER - Byte
PAYLOAD - Variable length depending on the header

You have 7 fields - so a “full” message would be HEADER 01111111 (0thpgfas)
Depending on the header you then slice the payload. Each of your values is float, so you’d need 4 bytes per field.

a full message would then be: 1 Byte + 7x4 Bytes = 29 Bytes Max
a single field message would be: 1 Byte + 1x4 Bytes = 5 Bytes

might sound a bit trickier to implement than regex, but your messages will be “lighter”
Your string for a full message equals to around 37 bytes.

1 Like

Okay, thank you very much I will try what you suggested.