C4D_TTN
(C4 D Ttn)
March 22, 2022, 5:20pm
1
I need to update my application JAVA to receive data from TTN V3. On the old version V2 was available a Client class from org.thethingsnetwork.data.Mqtt.Client, but now necessary to develop from standard mqttv3.
I try but connection fail with this code
options = new MqttConnectOptions();
options.setUserName(username);
options.setPassword(password.toCharArray());
client = new MqttClient(serverUrl, clientId, null);
client.connect(options);
on console I have this message
Resource limit {limit}
exceeded for resource with key `{resource}
someone can help me?
thanks
ninno
(Ninno)
March 23, 2022, 8:53am
2
A simple demo client / taken from: stackoverflow / just adopted for ttn user / pw + subscribe to “#”:
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
public class PahoDemo implements MqttCallback {
MqttClient client;
public PahoDemo() {
}
public static void main(String[] args) {
new PahoDemo().doDemo();
}
public void doDemo() {
try {
String username = "xxxxx@ttn";
String password = "yyyyy";
String serverurl = "tcp://eu1.cloud.thethings.network:1883";
String clientId = MqttClient.generateClientId();
MqttConnectOptions options = new MqttConnectOptions();
options.setCleanSession(true);
options.setUserName(username);
options.setPassword(password.toCharArray());
client = new MqttClient(serverurl, clientId, null);
client.connect(options);
client.setCallback(this);
client.subscribe("#");
} catch (MqttException e) {
e.printStackTrace();
}
}
@Override
public void connectionLost(Throwable cause) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
System.out.println(message);
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
// TODO Auto-generated method stub
}
}
using:
paho mqttv3 v1.2.5
compile/run:
javac -cp org.eclipse.paho.client.mqttv3-1.2.5.jar PahoDemo.java
java -cp org.eclipse.paho.client.mqttv3-1.2.5.jar:. PahoDemo
C4D_TTN
(C4 D Ttn)
March 23, 2022, 1:08pm
3
Thank you, now the receiving work!
C4D_TTN
(C4 D Ttn)
April 7, 2022, 4:07pm
4
thanks for the last demo, I used it to receive, but I would like also send some info to my endNode
I try to use the same client with publish method:
String myTopic = “v3/”;
myTopic += username;
myTopic += ("/devices/" + devId + “/down/replace”); // also with push
try {
client.publish(myTopic, payload, 0, false);
System.out.println(myTopic);
resSend = true;
} catch (MqttException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
it is correct?
it is correct to use the same object “client” to subscribe and publish?
thanks in advance
Stefano
ninno
(Ninno)
April 11, 2022, 6:10am
5
Never tried this in java / but for downlinks this is what i found to work for me last year:
v3/appname@ttn/devices/devicename/down/push
See this thread:
https://www.thethingsnetwork.org/forum/forum/t/downlink-formatter/45359/11
(almost accidentially deleted my post now trying to create a link to it; forum software doesnt like me)
C4D_TTN
(C4 D Ttn)
April 11, 2022, 2:19pm
6
String myTopic = “v3/”;
myTopic += username;
myTopic += ("/devices/" + devId + “/down/push”);
List sList = new ArrayList();
JSONObject jsonmsg = new JSONObject();
JSONObject jdownlinks = new JSONObject();
jdownlinks.put(“f_port”, 1);
jdownlinks.put(“frm_payload”, Base64.getEncoder ().encodeToString(payload));
jdownlinks.put(“priority”, “NORMAL”);
sList.add(jdownlinks);
jsonmsg.put(“downlinks”,sList);
System.out .print("sendDownlink ");
System.out .println(jsonmsg);
try {
client.publish(myTopic, jsonmsg.toString().getBytes(“utf-8”), 0, false );
C4D_TTN
(C4 D Ttn)
April 11, 2022, 2:22pm
7
now seems ok, the solution on JSON object instead of simple payload as bates array. On publish calling must pass JSON object like see on thread linked before.
thanks ninno.
bye
Stefano