Hello Experts,
I am using ENC28J60 ethernet shield with Arduino UNO board and want to run mqtt example.
Using the below example for mqtt subscribing.
#include <UIPEthernet.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 0, 110);
IPAddress server(192, 168, 0, 123);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!mqttClient.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (mqttClient.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
//mqttClient.publish("outTopic","hello world");
// ... and resubscribe
mqttClient.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(mqttClient.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
Serial.begin(9600);
mqttClient.setServer(server, 1883);
mqttClient.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
}
void loop()
{
if (!mqttClient.connected()) {
reconnect();
}
mqttClient.loop();
}
After uploaded the above code into arduino board and tried to send data via mqtt publishing.
Getting "Connection refused" error for publishing but subscriber is worked in linux.
titus@titus-PC:~/workdir$
titus@titus-PC:~/workdir$ mosquitto_pub -t inTopic -h 192.168.0.110 -m "."
Error: Connection refused
titus@titus-PC:~/workdir$
titus@titus-PC:~/workdir$