Mudanças entre as edições de "Message Broker MQTT"

De MediaWiki do Campus São José
Ir para navegação Ir para pesquisar
Linha 12: Linha 12:
  
 
==Comandos==
 
==Comandos==
*Subscrever: mosquitto_sub -h host -t subject
+
*Subscrever
 +
**Genérico: mosquitto_sub -h host -t subject
 
**Exemplo: mosquitto_sub -h test.mosquitto.org -t testCleber
 
**Exemplo: mosquitto_sub -h test.mosquitto.org -t testCleber
*Publicar: mosquitto_pub -h host -t subject -m message
+
**Exemplo: mosquitto_sub -t test.mqtt.topic
**Exemplo: mosquitto_pub -h dweet.io -t testeCleber -m OláMundo!
+
*Publicar  
 +
**Genérico: mosquitto_pub -h host -t subject -m message
 +
**Exemplo: mosquitto_pub -h dweet.io -t testeCleber -m OlaMundo!
 +
*Subscrever em todos os tópicos para depuração (modo verboso -v), observar qeue -h foi omitido pois neste exemplo está rodando no próprio servidor mosquitto:
 +
**mosquitto_sub -v -t \$SYS/#
 +
 
 +
==Rotas Camel==
 +
*Publicar:
 +
<syntaxhighlight lang=java>
 +
from("timer:test?period=2500").process(new Processor() {
 +
  public void process(Exchange exchange) throws Exception {
 +
    exchange.getIn().setBody("OlaMundo!");
 +
  }
 +
}).to("mqtt:camelArtifact?host=tcp://192.168.0.113:1883&publishTopicName=test.mqtt.topic");
 +
</syntaxhighlight>
 +
*Subscrever:
 +
<syntaxhighlight lang=java>
 +
from("mqtt:camelArtifact?host=tcp://192.168.0.113:1883&subscribeTopicName=test.mqtt.topic")
 +
  .transform(body().convertToString())
 +
  .to("log:CamelArtifactLoggerOut?level=info");
 +
</syntaxhighlight>
  
 
==Exemplo de geração de mensagens de teste==
 
==Exemplo de geração de mensagens de teste==

Edição das 10h48min de 27 de julho de 2016

Sobre

Mosquitto

Requisitos

  • Instalar o mosquitto server
$ sudo apt-get install mosquitto
  • Para subscrever e publicar, instalar o mosquitto-clients
$ sudo apt-get install mosquitto-clients
  • Liberar porta de entrada 1883

Comandos

  • Subscrever
    • Genérico: mosquitto_sub -h host -t subject
    • Exemplo: mosquitto_sub -h test.mosquitto.org -t testCleber
    • Exemplo: mosquitto_sub -t test.mqtt.topic
  • Publicar
    • Genérico: mosquitto_pub -h host -t subject -m message
    • Exemplo: mosquitto_pub -h dweet.io -t testeCleber -m OlaMundo!
  • Subscrever em todos os tópicos para depuração (modo verboso -v), observar qeue -h foi omitido pois neste exemplo está rodando no próprio servidor mosquitto:
    • mosquitto_sub -v -t \$SYS/#

Rotas Camel

  • Publicar:
from("timer:test?period=2500").process(new Processor() {
  public void process(Exchange exchange) throws Exception {
    exchange.getIn().setBody("OlaMundo!");
  }
}).to("mqtt:camelArtifact?host=tcp://192.168.0.113:1883&publishTopicName=test.mqtt.topic");
  • Subscrever:
from("mqtt:camelArtifact?host=tcp://192.168.0.113:1883&subscribeTopicName=test.mqtt.topic")
  .transform(body().convertToString())
  .to("log:CamelArtifactLoggerOut?level=info");

Exemplo de geração de mensagens de teste

  • Gerando mensagens a cada 5 segundos (script counter_mqtt.sh)

  1. !/bin/bash
  2. /etc/rc.d/init.d/
  1. Source function library

. /etc/init.d/functions

COUNTING=true

start() { echo "Publishing in mosquitto test server"

       #Starting counter in ZERO
       COUNTER=0
       #Get current datetime put it in a variable
       CURRENTDATETIME="$(date +'%d/%m/%Y-%T')"
       #Publish in mosquitto server (must be running in the svr and TCP 1883 port opened)
       mosquitto_pub -h 192.168.0.113 -m "$COUNTER-$CURRENTDATETIME" -t mqtt_test
       while [ COUNTING ]
       do

sleep 5 COUNTER=$[$COUNTER + 1] CURRENTDATETIME="$(date +'%d/%m/%Y-%T')" mosquitto_pub -h 192.168.0.113 -m "$COUNTER-$CURRENTDATETIME" -t mqtt_test

       done
       return

}

stop() {

       echo -n "Shutting down : "

echo "Stopping mosquitto test pushish messages"

       COUNTING=false
       return

}

case "$1" in

   start)
       start
       ;;
   stop)
       stop
       ;;
   status)
       ;;
   restart)

echo "Restarting mosquitto test pushish messages"

       COUNTING=true
       ;;
   *)
       echo "Usage:  {start|stop|status|reload|restart[|probe]"
       exit 1
       ;;

esac exit $? </syntaxhighlight> Fonte: https://www.linux.com/learn/managing-linux-daemons-init-scripts


  • Colocando o gerador de mensagens na inicialização do linux
sudo mv counter_mqtt.sh /etc/init.d/
sudo chmod +x /etc/init.d/counter_mqtt.sh
sudo update-rc.d counter_mqtt.sh defaults 

Fonte: http://stackoverflow.com/questions/7221757/run-automatically-program-on-startup-under-linux-ubuntu

Servidores públicos