Arduino UNO + Ethernet Shield for Mosquitto

Hi everyone,

I’m trying to get running a small Mosquitto server for a college work, however I can’t seem to get the Arduino to connect to the Mosquitto Server on a local network. If someone could check the code to see if there is something that could be wrong it would be appreciated.

Setup is currently:
PC <-> Router <-> Arduino

On the PC I have an inbound rule for TCP Port 1883 and UDP Port 17500.

When I run “mosquitto -v” I keep getting the message: “New connection from 192.168.125 on port 1883” endlessly as if it has never connected.

//Libraries needed
#include "DHT.h"
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

//Defines for DHT.h
#define DHTPIN A0
#define DHTTYPE DHT22 
DHT dht(DHTPIN, DHTTYPE);

//Needed for Ethernet
byte MAC_ADDRESS[] = {  0x90, 0xA2, 0xDA, 0x0E, 0x94, 0x93 };
byte MQTT_SERVER[] = { 192, 168, 0, 25 };
byte IP_ADDRESS[] = {192, 168, 0, 125};

//Needed for DHT
char* tempC;
unsigned long time;
char message_buffer[100];

//MQTT
EthernetClient ethClient;
PubSubClient client(MQTT_SERVER, 1883, 0, ethClient);

void setup()
{
  Ethernet.begin(MAC_ADDRESS,IP_ADDRESS);
  delay(1000);
  dht.begin();
}
 
void loop()
{
  //Retry until connected is true
  while (!client.connected())
  {   
    client.connect("arduinoClient");
    delay(5000);
  }
  
  // Publish sensor reading every X milliseconds
  delay(5000);
  float tempF = dht.readTemperature();
  tempC = dtostrf(tempF,5,2,message_buffer);  
  client.publish("arduino/temperature",tempC); 
  
  // MQTT client loop processing
  client.loop();
  
}

Thanks for any tips.

Barbaroti,
Did you get anywhere with this issue? I'm looking to implement the same thing, but I'm just starting out. Is is your Arduino connecting to the MQTT broker - via a Wiznet W5100?

Is the PubSubClient your MQTT library?

Same same stick for me!

hi this is my arduino code to publish lm 35 temperature sensor data using mqttlens and mosquitto broker

#include <SPI.h>
#include <PubSubClient.h>
#include <Ethernet.h>

// Pins
// Analog 0 is the input pin

// Variables
const int Tempin = 0;
int value;
float temp,tempC;
String pubstring;
unsigned long time;
char message_buffer[100];

// Network Settings
// MAC address of ethernet shield
// Look for it on a sticket at the bottom of the shield.
// Old Arduino Ethernet Shields or clones may not have a dedicated MAC address. Set any hex values here.
byte MAC_ADDRESS = { 0x78, 0x45, 0xC4, 0xAA, 0xE5, 0x6F };

// IP address of MQTT server
byte MQTT_SERVER = {85,119,83,194};
byte ip = {192,168,1,115};
EthernetClient ethClient;
PubSubClient client;

void setup()
{
// Initilize serial link for debugging
Serial.begin(9600);

Ethernet.begin(MAC_ADDRESS,ip);
Serial.print(“Local IP=”);
Serial.println(Ethernet.localIP());
client = PubSubClient(MQTT_SERVER, 1883, callback, ethClient);
}

void loop()
{
if (!client.connected())
{
//client.connect(“clientID”, “mqtt_username”, “mqtt_password”);
client.connect(“sfo-arduino”);
client.publish(“sfo/arduino/alive”, “I’m alive!”);
}
else{
client.connect(“arduino”);
Serial.println(“here i am connected”);}

//tempC = dtostrf(tempF,5,2,message_buffer);

// Publish sensor reading every X milliseconds

if (millis() > (time + 5000)) {
time = millis();
value = analogRead(Tempin);
temp = (value/1024.0)*5000;
tempC = temp/10;
pubstring = String(tempC);
pubstring.toCharArray(message_buffer, pubstring.length() + 1);
client.publish(“arduino/temperature”,message_buffer);
//Serial.println(“published!”);
//Serial.println(message_buffer);
}

// MQTT client loop processing
client.loop();
}

// Handles messages arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
}

The topic alone gets published and not the sensor value…can someone look into it and tell me wats the prob??
C:\mosquitto>mosquitto -v
1457007893: mosquitto version 1.4.7 (build date 28/12/2015 21:28:48.57) starting

1457007893: Using default config.
1457007893: Opening ipv6 listen socket on port 1883.
1457007893: Opening ipv4 listen socket on port 1883.
1457008553: New connection from ::1 on port 1883.
1457008553: New client connected from ::1 as lens_br58qDTLlzabTCA7ykOz5KTsTbc (c
1, k120).
1457008553: Sending CONNACK to lens_br58qDTLlzabTCA7ykOz5KTsTbc (0, 0)
1457008570: Received DISCONNECT from lens_br58qDTLlzabTCA7ykOz5KTsTbc

C:\mosquitto>mosquitto_sub -h test.mosquitto.org -t “arduino/temperature” -v
arduino/temperature (null)
arduino/temperature (null)
arduino/temperature (null)

i am also getting the very same problem please help me

I know this is an old post, but maybe it will help someone else if they run across this post like I did.

I just spent over an hour trying to get my arduino connected to my local mosquitto broker with a 5100 ethernet shield and here’s what I had to fix:

  • First Issue: Connection state -2 (network issue).
    I used a random mac address generator and it didn’t jive with my network, so i copy and pasted the mac address from an example sketch (byte mac = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }:wink:

-Connection state -4 (no keep alive response): Now i was connected to the network but not to the broker, so I tried an example broker (const char* server = “broker.hivemq.com”) and everything was fine

I know this is an old post, but maybe it will help someone else if they run across this post like I did.

I just spent over an hour trying to get my arduino connected to my local mosquitto broker with a 5100 ethernet shield and here's what I had to fix:

  • First Issue: Connection state -2 (network issue).
    I used a random mac address generator and it didn't jive with my network, so i copy and pasted the mac address from an example sketch (byte mac = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }:wink:

-Next Issue: Connection state -4 (no keep alive response):
Now i was connected to the network but not to the broker, so I tried an example broker (const char* server = "broker.hivemq.com") and everything was fine

-Final Resolution: Mosquitto installed on my linux server was v 3.1 and the current PubSubClient library defaults to v 3.1.1. So i had to edit the PubSubClient.h and comment the MQTT_VERSION_3_1_1 definition out.

Hope this helps someone else.