Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AM 107 decoder Error #46

Open
ByroneIntel opened this issue Jul 22, 2024 · 1 comment
Open

AM 107 decoder Error #46

ByroneIntel opened this issue Jul 22, 2024 · 1 comment

Comments

@ByroneIntel
Copy link

ByroneIntel commented Jul 22, 2024

Good day, GitHub community!

I hope everything is going well on your end.

I’ve been facing an issue with the AM107 Decoder function. Despite successfully running my decoder on a Lambda function and confirming data transfer (the uplink time is accurate), I can’t seem to retrieve accurate payload data.

I’m using an E-Link device to measure temperatures, and my goal is for the Lambda function to display the exact temperatures as shown on the E-Link device. I’ve followed the integration guide provided, (https://support.milesight-iot.com/support/solutions/articles/73000514172-aws-iot-core-for-lorawan-milesight-sensors-integration).

I would greatly appreciate any assistance or insights to achieve accurate data display from my Lambda function. I have attached screenshots for your reference and the Milesight IoT Am107 repo with the code (https://github.com/Milesight-IoT/SensorDecoders/blob/main/AM_Series/AM107/AM107_Decoder.js)

Thank you in advance!

image

import AWS from 'aws-sdk';
// import milesightDeviceDecode from "./decoder.js"

export const handler = (event, context, callback) => {
console.log("Event received:", event);

// Validate event.PayloadData
if (!event || !event.PayloadData) {
    console.error("Invalid event: PayloadData is missing or undefined.");
    callback(new TypeError("Invalid event: PayloadData is missing or undefined."));
    return;
}


try {
    var data = Buffer.from(event.PayloadData, 'base64');
    var chars = [...data];
    console.log("Decoded bytes:", chars);

    var params = decodeUplink(chars);
    console.log("Decoded parameters:", params);

    var iotdata = new AWS.IotData({ endpoint: '123456789-ats.iot.eu-west-1.amazonaws.com' });
    var response = {
        topic: event.WirelessMetadata.LoRaWAN.DevEui.concat("/project/sensor/decoded"),
        payload: JSON.stringify(params),
        qos: 0
    };

    console.log("Publishing to IoT topic:", response.topic);
    iotdata.publish(response, function(err, data) {
        if (err) {
            console.error("IoT publish error:", JSON.stringify(err));
            callback(err);
        } else {
            console.log("Published data:", response);
            callback(null, {
                statusCode: 200,
                body: JSON.stringify(params)
            });
        }
    });
} catch (error) {
    console.error("Error processing event:", error);
    callback(error);
}

};

function decodeUplink(input) {
// Validate input structure
if (!input || !Array.isArray(input)) {
throw new TypeError('Invalid input: input must be an array');
}

console.log("Input for decoding:", input);
var decoded = milesightDeviceDecode(input);
return { PayloadData: decoded };

}

function milesightDeviceDecode(bytes) {
var decoded = {};

for (var i = 0; i < bytes.length; ) {
    var channel_id = bytes[i++];
    var channel_type = bytes[i++];
    // BATTERY
    if (channel_id === 0x01 && channel_type === 0x75) {
        decoded.battery = bytes[i];
        i += 1;
    }
    // TEMPERATURE
    else if (channel_id === 0x03 && channel_type === 0x67) {
        // ℃
        decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10;
        i += 2;

        // ℉
        // decoded.temperature = readInt16LE(bytes.slice(i, i + 2)) / 10 * 1.8 + 32;
        // i +=2;
    }
    // HUMIDITY
    else if (channel_id === 0x04 && channel_type === 0x68) {
        decoded.humidity = bytes[i] / 2;
        i += 1;
    }
    // PIR
    else if (channel_id === 0x05 && channel_type === 0x6a) {
        decoded.activity = readUInt16LE(bytes.slice(i, i + 2));
        i += 2;
    }
    // LIGHT
    else if (channel_id === 0x06 && channel_type === 0x65) {
        decoded.illumination = readUInt16LE(bytes.slice(i, i + 2));
        decoded.infrared_and_visible = readUInt16LE(bytes.slice(i + 2, i + 4));
        decoded.infrared = readUInt16LE(bytes.slice(i + 4, i + 6));
        i += 6;
    }
    // CO2
    else if (channel_id === 0x07 && channel_type === 0x7d) {
        decoded.co2 = readUInt16LE(bytes.slice(i, i + 2));
        i += 2;
    }
    // TVOC
    else if (channel_id === 0x08 && channel_type === 0x7d) {
        decoded.tvoc = readUInt16LE(bytes.slice(i, i + 2));
        i += 2;
    }
    // PRESSURE
    else if (channel_id === 0x09 && channel_type === 0x73) {
        decoded.pressure = readUInt16LE(bytes.slice(i, i + 2)) / 10;
        i += 2;
    } else {
        break;
    }
}
console.log('Decoded data:', decoded);

return decoded;

}

function readUInt16LE(bytes) {
var value = (bytes[1] << 8) + bytes[0];
return value & 0xffff;
}

function readInt16LE(bytes) {
var ref = readUInt16LE(bytes);
return ref > 0x7fff ? ref - 0x10000 : ref;
}

@milesight-sway
Copy link
Contributor

milesight-sway commented Jul 24, 2024

Based on the information you provided, it seems that event.PayloadData is not defined. Please examine the data contained in the console.log("Event received:", event); output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants