Maven java mqttproject

Thanks Aaron. Could you guide how to open 1883 port ?

You mean on subscriber too, but it is there.


Hi, After turning on Internet, the code is again showing java.nio. File. InvalidPathException. aAlthough the slash / is removed now. Why is it?

Hi, I am coping the code and sending here
package com.mycompany.mqttproject1;

import java.io.BufferedReader;
import java.io.FileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import javax.json.Json;
import javax.json.JsonObject;

public class Aarhus2 {

private static final String MQTT_BROKER = "tcp://mqtt.eclipseprojects.io:1883";
private static final String MQTT_TOPIC = "ub20-Aarhus2";
private static final String CSV_FILE_PATH = "../..data/PollutionData180601.csv";
private static final int SLEEP_DURATION = 1000;

public Aarhus2() {
    try {
        MqttClient client = new MqttClient(MQTT_BROKER, MqttClient.generateClientId());
        client.connect();

        Path csvFilePath = Paths.get(Arhus1.class.getProtectionDomain().getCodeSource().getLocation().getPath())
                .resolve(CSV_FILE_PATH);

        try (BufferedReader br = new BufferedReader(new FileReader(csvFilePath.toString()))) {
            String strLine;
            int i = 0;
            while ((strLine = br.readLine()) != null) {
                i++;
                String[] tempLine = strLine.split(",");
                if (i == 1 || tempLine.length != 8) {
                    continue;
                }
                Timestamp timeSend = new Timestamp(System.currentTimeMillis());
                JsonObject value = Json.createObjectBuilder()
                        .add("particulate_matter", tempLine[1])
                        .add("timestamp", tempLine[7])
                        .add("time_send", timeSend.toString())
                        .build();
                
                MqttMessage message = new MqttMessage();
                message.setQos(0);
                System.out.println(value.toString());
                message.setPayload(value.toString().getBytes());
                client.publish(MQTT_TOPIC, message);
                Thread.sleep(SLEEP_DURATION);
            }
        }
        client.disconnect();
    } catch (MqttException | InterruptedException err) {
        System.out.println(err);
    } catch (Exception err) {
        System.out.println(err);
    }
}

public static void main(String[] args)
{
    new Aarhus2();
}

}

package com.mycompany.mqttproject1;

import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;

public class Pollutionsubscriber {

public static void main(String args) {
try {
MqttClient client = new MqttClient(
“tcp://mqtt.eclipseprojects.io:1883”,
MqttClient.generateClientId()
);
client.setCallback((MqttCallback) new Pollutioncallback());
client.connect();
client.subscribe(“ub20-Arhus1”);
//client.subscribe(“ub20-Arhus2”);
} catch (MqttException err) {
System.out.println(err);
}

}

}

package com.mycompany.mqttproject1;

import java.io.StringReader;
import java.sql.Timestamp;
import java.util.Arrays;
import javax.json.Json;
import javax.json.JsonObject;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;

public class Pollutioncallback implements MqttCallback {

	 public Pollutioncallback() {}

    @Override
    public void connectionLost(Throwable thrwbl) {
        System.out.println("Connection to MQTT broker lost!");
    }

    @Override
    public void messageArrived(String string, MqttMessage mm) throws Exception {
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        
        JsonObject valueObject = Json.createReader(new StringReader(new String(mm.getPayload()))).readObject();
        String value = valueObject.getString("particulate_matter", "");
        String time = valueObject.getString("timestamp", "");
        String time_send = valueObject.getString("time_send", "");
        
        String station = "Aarhus2";
        if (string.equals("ub20-Arhus1")) {
            station = "Arhus1";
        }
        
        System.out.println(
            "Receiced " + station + "(" +timestamp.toString() +"):: particulate_matter: " 
            + value + ", timestamp: " + time + ", time_send: " + time_send);
    }
    @Override
    public void deliveryComplete(IMqttDeliveryToken imdt){
        System.out.println(Arrays.toString(imdt.getTopics()));
            
    }     
}

Hi, after putting repository on pom dependency it is dhowing error

Hi th pom dendencybis not accepting the whole

<project ...>
<repositories>
    <repository>
        <id>Eclipse Paho Repo</id>
        <url>%REPOURL%</url>
    </repository>
</repositories>
...
<dependencies>
    <dependency>
        <groupId>org.eclipse.paho</groupId>
        <artifactId>org.eclipse.paho.client.mqttv3</artifactId>
        <version>%VERSION%</version>
    </dependency>
</dependencies>
</project>

Hello @neema,

Thank you for the additional details.

The HiveMQ community forum is intended to function as support for HiveMQ products, and associated connections. Based on the information that has been provided so far, there does not appear to be an issue with any HiveMQ related components. Specifically, the clients and brokers being utilized are supported and maintained by the Eclipse Paho team. We are happy to assist with resolving questions or issues relating to MQTT, but it is generally out of our scope to troubleshoot Java IDE (Netbeans) errors, or third party clients when using third party brokers.

Based on the details provided, it looks like this project is outside of the scope of the support of this forum, and I believe that you may best receive the information you are looking for from the Eclipse Paho team. If you have any further questions pertaining to HiveMQ specifically, please feel free to let us know, and we will do our best to assist further.

Best,
Aaron from HiveMQ Team

Hi Aaron, since you know the problem is related to eclipse, my query is could you write a short code under hive mqtt either in python or java maven, based on sending data row by row? That can be up and running.
Thanks again,

Hello @Neema ,

Custom work, such as developing a client to parse CSV data to line-by-line messages, is unfortunately outside of the scope of this community forum. I can certainly pass the interest to our product team for potential creation of a tool fitting these criteria in the future.

With that in mind, some client examples and additional documentation for the HiveMQ Java client can be found on the page previously linked to the HiveMQ Java client.

Best,
Aaron from the HiveMQ Team

Hi Aaron,

If you could solve this problem for me, then it can be mutual benefit for the future tasks in using Hive MQTT for the usage of IoT in retail business.

Thanks again,

Regards,

Neema

Hello @neema,

As mentioned previously, creating a custom client implementation for a particular use-case is outside of the scope of support on the community forum. I will be more than happy to provide details of the configuration and request to our product team to review as a potential tool or example implementation for HiveMQ clients.

If you have any questions relating to HiveMQ, HiveMQ products, or the MQTT protocol, we are more than happy to assist further.

Best,
Aaron from HiveMQ Team