首页 文章

如何将数据从MQTT发送到AMQP

提问于
浏览
-2

提前致谢!

我在Raspberry-Pi上安装了Mqtt,并在端口1883上将传感器数据发布到mqtt . 使用Paho客户端发布的数据在接收器端进行验证 .

现在我在Windows7上安装了RabbitMQ . 如何将数据从mqtt发送到rabbitmq .

String brokerUrlRpi_Mqtt = "tcp://localhost:1883";
String clientId="ExamplePublish";
String channel = "SensorIntegratedData";
int qos=0;

public void publish(String data) throws MqttPersistenceException, MqttException {
  String time = new Timestamp(System.currentTimeMillis()).toString();
  System.out.println("Publishing at: "+time+ " to topic \""+channel+"\" qos "+qos);
  MqttMessage message = new MqttMessage(data.getBytes());
  message.setQos(qos);
  message.setRetained(false);
  client.publish(channel, message);
  // Disconnect the client
}

Mqtt Connection是没有凭据的dafault . 现在我如何将数据发送到RabbitMQ .

2 回答

  • 0

    您所描述的是MQTT桥 . 您可以在pi上配置mosquitto实例以连接到RabbitMQ代理(假设它配置为支持MQTT) .

    有关如何配置网桥的详细信息,请参见mosquitto.conf手册页here .

    基本上你需要在配置中添加如下内容 .

    connection rabbitmq
    address ip-of-rabbitmq
    cleansession true
    topic # out 0
    
  • 1

    通过从命令提示符运行命令在rabbitmq上启用mqtt插件OPen cmd提示路径到rabbitmq / sbin默认wiil是C:\ Program Files \ RabbitMQ Server \ rabbitmq_server-3.6.9 \ sbin在windows中然后运行命令 .

    “rabbitmq-plugins启用rabbitmq_mqtt”

    我们需要设置运行rabbitmq的服务器系统的Topic,url . 随机发送clientId . 将Qos设置为0 OR 1 OR 2

    String topic = "amq.topic"; String brokerUrlRpi_Mqtt = "tcp://192.168.xxx.xxx:1883/"; String clientId=UUID.randomUUID().toString();

    如果从不同系统发送mqtt数据,用户名和密码应该是在rabbitmq中创建的新用户 . 如果mqtt和rabbitmq在本地运行,那么访客用户名和密码就可以了 .

    `

    public  SendDataUsingMQTT() throws MqttException {
                String tmpDir = System.getProperty("java.io.tmpdir");
    //          MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(tmpDir);
                try {
                  conOpt = new MqttConnectOptions();
                  conOpt.setUserName("username");
                  conOpt.setPassword(new String("password").toCharArray());
                  conOpt.setCleanSession(true);
                client = new MqttClient(brokerUrlRpi_Mqtt,clientId, datastore);
    
     client.setCallback(new MqttCallback() { 
    
    // add all anonymous implemented methods of connect
     public void messageArrived(String arg0, MqttMessage arg1) throws Exception {
    
    //
    }
    public void deliveryComplete(IMqttDeliveryToken arg0) { }
     public void connectionLost(Throwable arg0) {}
    }
    catch (MqttException e) {
                e.printStackTrace();
                System.out.println("Unable to set up client: "+e.toString());
                System.exit(1);
              }               
              }
    

相关问题