The Things Network republish into another topic

0

Hello all, I am extremely new to the AWS community as well as working with LoRa integrations. I have integrated my LoRa application to my AWS IoT core. I am now trying to republish specific data from my application into another topic. I followed a tutorial on youtube and created a Lambda function to grab that specific data. When I try to verify its functionality in the MQTT broker I get nothing

function Decoder(bytes, port) {
    var decoded = {};
    
  var sensorRead = bytes[2] & 0x7f;
  var BatteryPerc = bytes[2] & 0x64;
  var Hour = bytes[2] & 0x00;
  var Min = bytes[2] & 0x64;
  var Sec = bytes[2] & 0x00;
  var lat = bytes[4] & 0x00;
  var lon = bytes[4] & 0x00;
  var alt = bytes[4] & 0x00;
  var time = ((Hour) + ":" + (Min) + ":" + (Sec));
  var Sensor = ((sensorRead) / 240.0625);
  var Battery = ((BatteryPerc) / 256);
   
   decoded.Sensor = Sensor;
   decoded.Battery = Battery
   decoded.time = time;
   decoded.lat = lat;
   decoded.lon = lon;
   decoded.alt = alt;
   
   return decoded;
}
  
exports.handler = async (event) => {
    console.log(event);
    return {
        "device" : event.DeviceId,
        "payload" : Decoder(Buffer.from(event.PayloadData, 'base64'), 2)
    };
};

this is my Lambda function. I have no idea how to fix my problem or what resources to read to help.

3 Answers
0
Accepted Answer

Hi. What you appear to be trying to achieve with the Lambda would be a typical approach if you were using AWS IoT Core for LoRaWAN instead of The Things Network. Can you please clarify your setup? Do you mean you have your devices and gateways registered in The Things Stack V3? And you installed the AWS IoT integration? https://www.thethingsindustries.com/docs/integrations/cloud-integrations/aws-iot/deployment-guide/ . If so, you would not normally need a Lambda to decode the payloads. In that architecture, your payload decoding would normally be done in The Things Stack, with decoded messages published to a topic in IoT Core.

More generally, if a Lambda is not performing as expected, you should check the CloudWatch logs for the Lambda.

profile pictureAWS
EXPERT
Greg_B
answered 2 years ago
  • I had to post my response to your question as an answer because it was too long

0

Hi. OK thanks for the clarification. I have version 1.1.6 of this integration installed from long ago. As per the architecture shown in the video, uplink messages from TTS reach AWS on thethings/lorawan/<stack-name>/uplink. The HandleUplinkFunction Lambda function processes these. This topic is where we see the large JSON document you quoted in your last answer. If TTS defines a decoder, this JSON document will include the decoded_payload field, else it will not. I think we are all good to this point.

Now, the Lambda decoder you are trying to use is for the lorawan/+/uplink topic. As shown on the video, the decrypted payload is republished to this topic. This is also shown here: https://www.thethingsindustries.com/docs/integrations/cloud-integrations/aws-iot/architecture/

The trick here is I think this decryption, and republish to this topic, will only occur if you deployed the CloudFormation template of this integration with the Enable End-to-End Encryption set to Enabled. In my case, it's Disabled, so nothing is ever published on lorawan/+/uplink. Do you have this Enabled or Disabled? If Disabled, I think your rule is never going to trigger.

If it's enabled, some things to try:

  1. Use the IoT Core MQTT test client and make sure decrypted messages are republished to lorawan/+/uplink.
  2. If they are, enable AWS IoT logging (https://docs.aws.amazon.com/iot/latest/developerguide/configure-logging.html) and check that you're getting the correct RuleExecution log entry in CloudWatch to confirm that your rule is running.
  3. If your rule is running, check the Lambda logs in CloudWatch (https://docs.aws.amazon.com/lambda/latest/dg/monitoring-functions-access-metrics.html) to see if your function is running and whether it's suffering any errors.
profile pictureAWS
EXPERT
Greg_B
answered 2 years ago
  • So I checked to see is my decrypted messages are being republished to lorawan/+/uplink and it is. I tried to enable logging and even when I created a logging role I am still unable to.

  • When you say "unable to", what does that mean? Do you get an error? If you successfully enabled the logging, then CloudWatch will have a new log group called AWSIotLogsV2. That is where you should find the RuleExecution log entries.

    And what about the Lambda monitoring and logs? If the Lambda is being invoked, then the rule would appear to be triggering. Then you can check the Lambda logs for errors.

0

I am following a V3 tutorial from YouTube https://www.youtube.com/watch?v=RZXxzPWMne8&t=1s in which the data being sent is not decoded on The Things Network but is instead decoded via AWS IoT core Lambda function. My application from The Things Network has a payload decoder and decoded my data when I subscribe to the topic " lorawan/#", however this gives me this output

{
  "end_device_ids": {
    "device_id": "eui-70b3d57ed004c725",
    "application_ids": {
      "application_id": "dem-test-app"
    },
    "dev_eui": "70B3D57ED004C725",
    "join_eui": "0000000000000000",
    "dev_addr": "260CAD79"
  },
  "correlation_ids": [
    "as:up:01G19CHV5RVE6QANM1QF81NPET",
    "gs:conn:01G15P8JZPQR7ZGB4FDKWQRG7B",
    "gs:up:host:01G15P8K4DHA8QWYKP0X6WBYVZ",
    "gs:uplink:01G19CHTZ58GDRRSGJ7D70NAAC",
    "ns:uplink:01G19CHTZ65FVWM0A4A137PK2C",
    "rpc:/ttn.lorawan.v3.GsNs/HandleUplink:01G19CHTZ6CM9F143G89052DPQ",
    "rpc:/ttn.lorawan.v3.NsAs/HandleUplink:01G19CHV5QV4YEEZFVXCZZ6N7J"
  ],
  "received_at": "2022-04-22T19:39:01.176954347Z",
  "uplink_message": {
    "session_key_id": "AX/XUM3NMUAf2UAOY5vIfg==",
    "f_port": 2,
    "f_cnt": 131037,
    "frm_payload": "/+sAZAAAAAAAAAAAAAAAAAAAAAAAAA==",
    "decoded_payload": {
      "Battery": 100,
      "Sensor": -21.331944806040095,
      "alt": 0,
      "lat": 0,
      "lon": 0,
      "time": "0:0:0"
    },
    "rx_metadata": [
      {
        "gateway_ids": {
          "gateway_id": "dem-prod-gateway",
          "eui": "B827EBFFFE4914B7"
        },
        "time": "2022-04-22T19:39:00.977253913Z",
        "timestamp": 3775152971,
        "rssi": -34,
        "channel_rssi": -34,
        "snr": 11.25,
        "location": {
          "latitude": 38.78975021901311,
          "longitude": -77.07604676485062,
          "altitude": 62,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "Ch4KHAoQZGVtLXByb2QtZ2F0ZXdheRIIuCfr//5JFLcQy4aRiA4aDAjUiIyTBhD39o7MAyD4uZvF75kcKgwI1IiMkwYQmez+0QM="
      }
    ],
    "settings": {
      "data_rate": {
        "lora": {
          "bandwidth": 125000,
          "spreading_factor": 7
        }
      },
      "coding_rate": "4/5",
      "frequency": "902500000",
      "timestamp": 3775152971,
      "time": "2022-04-22T19:39:00.977253913Z"
    },
    "received_at": "2022-04-22T19:39:00.966183711Z",
    "confirmed": true,
    "consumed_airtime": "0.077056s",
    "locations": {
      "user": {
        "latitude": 38.789745253724085,
        "longitude": -77.07604676485062,
        "source": "SOURCE_REGISTRY"
      }
    },
    "network_ids": {
      "net_id": "000013",
      "tenant_id": "ttn",
      "cluster_id": "ttn-nam1"
    }
  }
}

In the video while using the Lambda function to decode the payload, the data only returned the sensor data, (which is what I am trying to replicate). Below is what the tutorial received from their Lambda function decoder.

{
     "output": {
          "device":   "menpedro-device-4",
          "payload": {
                "temp_board":  23,
                "batt": 3
           }
      }
}

When I tried to remove my decoder on The Things Network and utilize the Lambda Function decoder. I do not receive any messages when I subscribe to the topic I created. I am trying to send my data to a DynamoDB Database and I would like to only send the sensors readings. My original decoder is in the main question thread and below is the Rule query statement. Im very new to using AWS IoT and the error could (most likely) be on my end, but If not is there any support to fix my problem?

SELECT aws_lambda("arn:aws:lambda:us-east-1:722524886663:function:LoRadecoder", {"DeviceId":end_device_ids.device_id, "PayloadData":uplink_message.frm_payload}) as output FROM 'lorawan/+/uplink' WHERE uplink_message.f_port=2
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions