You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
// 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;
}
The text was updated successfully, but these errors were encountered:
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.
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!
import AWS from 'aws-sdk';
// import milesightDeviceDecode from "./decoder.js"
export const handler = (event, context, callback) => {
console.log("Event received:", event);
};
function decodeUplink(input) {
// Validate input structure
if (!input || !Array.isArray(input)) {
throw new TypeError('Invalid input: input must be an array');
}
}
function milesightDeviceDecode(bytes) {
var 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;
}
The text was updated successfully, but these errors were encountered: