Raspberry Pi a RabbitMQ

Warning : This article has been automatically translated by Google Translate




With the proliferation of computers at home and the arrival of the Raspberry, There are many data collection projects, of measures, etc. Today we will see how to use a broker to allow all these machines to simply exchange information through messages via RabbitMQ.

rabbit_header_logo

RabbitMQ implements among other protocol AMQP which allows to transport messages containing information. This transport is done from point to point or on the principle of subscribing to a message type. For some this will make you think to work in companies often found the IBM Websphere MQ solution.
There is also the Protocol MQTT who is best suited for data capture for all that is sensors and measures.

Once installed on the Raspberry, It turns into communication centre, will receive messages and send back them to the right recipients. Then what will it serve us ?
And though I'd say everything that a machine has as information and that you do not want to keep it or who must trigger one action on another. For example, an image captured and then sent on another machine to check it, a measure that should trigger actions on other servers, etc.

How to use the service ? By program or script. On the official website you will find clients in Java, .NET or Erlang (the language used to developed RabbitMQ). You also have opportunities with Python (see here) in the case of the Raspberry can be more efficient. For my tests as a client, I'm on a Mac so I took the Java. Examples are at the end of article.

You can go even further by using several Raspberry Pi in cluster to improve your performance. Mais c’est déjà à un autre niveau pour moi 🙂

So let's see how to install RabbitMQ on our Raspberry Pi (me a 256Mb model)

  • Update the system
    apt-get update && apt-get -y upgrade
  • Download archive. The latest version is the 3.1.5.
    wget http://www.rabbitmq.com/ releases/rabbitmq-server/v3.1.5/rabbitmq-server_3.1.5-1_all.deb
  • Install additional packages
    apt-get install –y erlang logrotate
  • Install RabbitMQ
    dpkg -i rabbitmq-server_3.1.5-1_all.deb

    If you have an error try the command

    apt-get -f install

    It will install the missing dependencies and RabbitMQ.

  • Activate the plugins from the server management
    rabbitmq-plugins enable rabbitmq_management

    And then restart the service

    rabbitmq-server service restart
  • Access Pi Raspberry to the http address://<IP of the raspberry>:15672/#/
    RabbitMQ_ManagementThe web server, even on a 256Mb, is really fast.
  • Everything should be green.
    RabbitMQ_ManagementIf necessary create a file /etc/rabbitmq/rabbitmq.config by adding the following lines :
    For a minimum limit of 25Mb of disk space :

    [{Rabbit, [{disk_free_limit, 25000000}]}].

    For the memory limit to 50% the capacity of the Raspberry (I've not had need for testing) :

    [{Rabbit, [{vm_memory_high_watermark, 0.5}]}].

    If you have other problems, You can find more information on this page.

  • For the client part, install Java on an another Raspberry, the same Raspberry, a Mac, Windows, etc. In my case here is the program to send a message to the server on the Raspberry, don't forget to change the IP of your server RabbitMQ :
    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    
    public class Send {
    
      private final static String QUEUE_NAME = "hello";
    
      public static void main(String[] argv) throws Exception {
    
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("localhost");
        factory.setHost("192.168.1.25");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");
    
        channel.close();
        connection.close();
      }
    }

    Et le programme pour aller écouter les messages sur une file. Yet once do not forget to change the IP of your server RabbitMQ :

    import com.rabbitmq.client.ConnectionFactory;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.QueueingConsumer;
    
    public class Recv {
    
        private final static String QUEUE_NAME = "hello";
    
        public static void main(String[] argv) throws Exception {
    
        ConnectionFactory factory = new ConnectionFactory();
        //factory.setHost("localhost");
        factory.setHost("192.168.1.25");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
    
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        System.out.println(" [*] Waiting for messages. To exit press CTRL   C");
    
        QueueingConsumer consumer = new QueueingConsumer(channel);
        channel.basicConsume(QUEUE_NAME, true, consumer);
    
        While (true) {
          QueueingConsumer.Delivery delivery = consumer.nextDelivery();
          String message = new String(delivery.getBody());
          System.out.println(" [x] Received '" + message + "'");
        }
      }
    }

    To compile everything, You must first download the JAR :

    wget http://www.rabbitmq.com/ releases/rabbitmq-java-client/v3.1.5/rabbitmq-java-client-bin-3.1.5.tar.gz

    Unpack the archive and put your Java files in the directory that was just created.
    Then compile with the command

    javac - cp rabbitmq - client.jar Send.java Recv.java

    And finally're you ready to run your examples. To send :

    java - cp .:Commons-io - 1.2.jar:Commons-cli - 1.1.jar:rabbitmq - client.jar Send

    You see the message that has not been consumed in the administration console :
    RabbitMQ_Management

    And now to consume the message, you start the reception program :

    java - cp .:Commons-io - 1.2.jar:Commons-cli - 1.1.jar:rabbitmq - client.jar Recv

    [*] Waiting for messages. To exit press CTRL+C
    [x] Received 'Hello World!'

This is your first message was exchanged. There is more to imagine the possibilities to interconnect your systems. For me this solution is really simple and quick to set up and to easily switch between complex scripts for the scp or another command to send data.

Like this article ? Bear with me on patreon!

You may also like...