TTN v3 ESP32 MQTT Message don't arrive

Using the PubSubClient Arduino library I can connect to TTN v3 using MQTT.
But I don’t receive any message.
Don’t know what I’m doing wrong. Using mosquito_sub I do receive messages using the same parameters.
I’m using the following scketch:

#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = ".......";
const char* password = "............";

const char* ttn_server = "eu1.cloud.thethings.network";
const char* ttn_user = ".....@ttn";
const char* ttn_password = "NNSXS......;
const int ttn_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.println("Setup MQTT connection");
    // Attempt to connect
    if (client.connect("ESP32Client", ttn_user, ttn_password )) {
      Serial.println("MQTT connected");
      client.subscribe("#");
      Serial.println("MQTT subscribed, waiting for message...");
    } else {
      Serial.print("MQTT failed, rc= ");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);

  Serial.print("Setup WiFi network ");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.println("WiFi connected!");

  Serial.println("Setup MQTT");
  client.setServer(ttn_server, ttn_port);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

Output is:

Setup WiFi network ......
WiFi connected!
Setup MQTT
Setup MQTT connection
MQTT connected
MQTT subscribed, waiting for message...

Any idea what’s missing?

i think this line must be changed into something like this:

client.subscribe(“v3/your_application_id@ttn/devices/your_End_device_id/up”);

https://www.thethingsindustries.com/docs/integrations/mqtt/#subscribing-to-upstream-traffic

And the buffer Size is by default to small for the TTN Message - try this:

client.setBufferSize(2048);

before client.subscribe

In my applications, I use wildcards for the application name and device name, so they subscribe to
“v3/+/devices/+/up”.
The access key you’re using to log in to MQTT is already limited to a specific application anyway, and I assume you probably want to listen on application level, so receive uplink data from any device linked to that application.

Thnx, but this solution doesn’t work for me.
Also @ttn3 shouldn’t be in the application id (according the documentation)

Thnx, but this didn’t solve the problem.

Thnx, tried this solution but it didn’t work for me.

I think you should try to see if your Arduino code works at all, so instead of trying to make the entire stack work in one go, just take it step-by-step.

So publish to your own topic on a test server, like test.mosquitto.org for example. Start with a small MQTT message, then if that works, publish a copy of an actual representative MQTT message (which is typically much larger for TTN v3 than it was for TTN v2!). Then if that works, connect it to the MQTT server of TheThingsNetwork.

I cant find “@ttn3” - neither in my post nor in the linked documentation

I just checked a TTNv3 MQTT message I recorded earlier this year: it’s actually 2771 bytes, so even 2048 is not enough.

if (client.connect("ESP32Client", ttn_user, ttn_password )) {
  Serial.println("MQTT connected");
  client.setBufferSize(2048);
  client.subscribe("v3/your_application_id@ttn/devices/your_End_device_id/up");
  Serial.println("MQTT subscribed, waiting for message...");
}

This is working for me.

1 Like

Oh thanx! Than this value must be increased.

Yes, this is working for me too, only increased the buffer to 4096 bytes.
Thnx!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.