No message posted in TTN MQTT broker through AWS lambda

javiergraciasanti
Fri, 28 Jul 2023 10:55:53 +0000
MQTT

I am experiencing problems displaying the downlink message generated by the AWS lambda. However, I can see that TTN has subscribed to the post made through lambda. Below I show the code from the lambda using the node.js mqtt package:

import { connect } from 'mqtt';

export async function handler(event) {
    let options = {
        port:1883,
        clientId: 'mqttjs_'+ Math.random().toString(16).substr(2,8),
        username:'my-user-name',
        password: 'my-app-key',
        keepalive:60,
        reconnectPeriod:1000,
        protocolId:'MQIsdp',
        protocolVersion:3,
        //clean:true,
        encoding: 'utf8'
    };

    const mqttHost = 'eu1.cloud.thethings.network'; // La dirección del broker MQTT de TTN
    //const mqttPort = 1883; // El puerto MQTT (generalmente 1883 para MQTT y 8883 para MQTT sobre TLS)
    const appId = 'my-app-id'; // El ID de la aplicación en TTN
    const deviceId = 'my device id';

    const payload = `{
          "color": "green"
          }`;
    const topic = "v3/my-application@ttn/devices/my-device-id/down/push";
    const client = connect(`https://${mqttHost}`, options);

    return new Promise((resolve, reject) => {
  client.on('connect', () => {
  client.subscribe([topic], () => {
    console.log(`Subscribe to topic '${topic}'`);
    client.publish(topic, payload, { qos: 0, retain: true }, (error) => {
      if (error) {
        console.error(error);
      }else {
        console.log('Mensaje de downlink enviado correctamente');
        resolve('Mensaje de downlink enviado correctamente');
        //console.log(client)
        return resolve;
        }
    });
    });
    });

    client.on('message', (topic, payload) => {
      console.log('Received Message:', topic, payload.toString());
    });
    //LAMBDA MQTT.txt
    });
}

with this code I get the following message from TTN

but nothing more than this. I would like to know how to get the payload. Thanks for your contributions.

13 posts - 4 participants