Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ with Arduino Uno

I'm using RabbitMQ with Arduino for the first time and I need to publish data. So I've used the PubSubCLient class. This is the code:

#include <SPI.h>
#include <PubSubClient.h>
#include <Dhcp.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
#include <EthernetServer.h>
#include <EthernetClient.h>

//declare variables
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xDE, 0xDE, 0xDD };
byte server[] = { 127, 0, 0, 1 };
byte ip[] = { 192, 168, 1, 22 };
String stringone = "localhost";

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.println(topic);
  //convert byte to char
  payload[length] = '\0';
  String strPayload = String((char*)payload);
  Serial.println(strPayload);
  int valoc = strPayload.lastIndexOf(',');
  String val = strPayload.substring(valoc+1);
  Serial.println(val);
}

EthernetClient ethClient;
PubSubClient client(server, 5672, callback, ethClient);

void setup() {
  // client is now configured for use
  Serial.begin(9600);
  Serial.println("==STARTING==");
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    // try to congifure using IP address instead of DHCP:
    Ethernet.begin(mac, ip);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print(".");
  }
  boolean con = client.connect("arduinoMQTT123");
  while(con != 1) {
    Serial.println("no con-while");
    con = client.connect("arduinoMQTT123");
  }
  if(con) {
    Serial.println("got con");
    client.subscribe("/v2/feeds/FEED_ID.csv");
  } else Serial.println("no con");
}

void loop() {
  client.loop();
}

I keep getting an error, no connection. I think that's because I don't know how to use Arduino with RabbitMQ.

like image 684
Anas Elazhar Avatar asked Jul 01 '17 21:07

Anas Elazhar


1 Answers

These two lines are the source of your troubles:

    byte server[] = { 127, 0, 0, 1 };

    ...

    PubSubClient client(server, 5672, callback, ethClient);

server[] must specify the address of your RabbitMQ server. 127.0.0.1 is the address for localhost. This is never routable.

Additionally, PubSubClient is an MQTT client, not a RabbitMQ (AMQP) client. Therefore, the AMQP port you specified, 5672, will not work.

You need to enable and configure the MQTT adapter in RabbitMQ and then use the appropriate MQTT port, typically 1883.

like image 132
C. Taylor Avatar answered Oct 13 '22 11:10

C. Taylor