I'm having problem with that pubsubclient example. I have arduino uno and ethernet shield. I have mosquitto running in my backroung with port 1883. So I am having problem with callback function. It doesn't print anything. When I compile that script and open serial monitor, it shows me:
Attempting MQTT connection.....connected...so that works! But then it doesn't show anything more. It wont print Message arrived from callback function. What is wrong? Please help me, I need to understand mqtt for my degree. Thanks in advance!
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress server(192, 168, 1, 71);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*);*
- }*
- Serial.println();*
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {- // Loop until we're reconnected*
- while (!client.connected()) {*
- Serial.print("Attempting MQTT connection...");*
- // Attempt to connect*
- if (client.connect("arduinoClient")) {*
- Serial.println("connected");*
- // Once connected, publish an announcement...*
- client.publish("outTopic","hello world");*
- // ... and resubscribe*
- client.subscribe("inTopic");*
- } else {*
- Serial.print("failed, rc=");*
- Serial.print(client.state());*
- Serial.println(" try again in 5 seconds");*
- // Wait 5 seconds before retrying*
- delay(5000);*
- }*
- }*
}
void setup()
{- Serial.begin(9600);*
- client.setServer(server, 1883);*
- client.setCallback(callback);*
- Ethernet.begin(mac, ip);*
- // Allow the hardware to sort itself out*
- delay(1500);*
}
void loop()
{- if (!client.connected()) {*
- reconnect();*
- }*
- client.loop();*
}[/quote]