Go Down

Topic: Arduino MEGA2560 with Ethernet Shield and MQTT  (Read 157 times) previous topic - next topic

Luis_Filipe

Hello guys,

I´m play around with my arduino mega with a ethernet shield and MQTT protocol, but since I´have some issues i´hope you guys can help me to solve the problem.

I work with the mosquitto in my PC to make my MQTT broker up and running, look:

C:\Program Files (x86)\mosquitto>mosquitto -v
1468107949: mosquitto version 1.4.9 (build date 08/06/2016 11:59:29.51) starting
1468107949: Using default config.
1468107949: Opening ipv6 listen socket on port 1883.
1468107949: Opening ipv4 listen socket on port 1883.
1468107949: New connection from 192.168.0.2 on port 1883.
1468107949: New client connected from 192.168.0.2 as lens_p4tRRdv0C3DTmZ00JAoxawHqi
cM (c1, k120, u'iotUser').
1468107949: Sending CONNACK to lens_p4tRRdv0C3DTmZ00JAoxawHqicM (0, 0)
1468107975: Received PUBLISH from lens_p4tRRdv0C3DTmZ00JAoxawHqicM (d0, q0, r0, m0,
 'arduino/topic', ... (24 bytes))


I test with the MQTTLens to publish one message and the result is succeeded, so the MQTT protocol is working fine!

The problem is that after i grab some basic example from PubSubClient and work around a bit I see that this code is not working, I not receive any message from the arduino to the broker.

The code is very simple, but not work well, who can I fix this code to put the arduino publish and subcribe some topic, look the code:

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

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 200);
IPAddress server(192, 168, 0, 2);

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(server, 1883, callback, ethClient);

void setup()
{
     Serial.begin(9600); 
     Ethernet.begin(mac, ip);

     client.publish("arduino/topic","simple message");
     client.subscribe("arduino/topic");
}

void loop()

     client.loop();       
}



Who is missing or is wrong?

I change many things the code and search a lot before I request help to the community, OK

Thanks in advance

rw950431

Firstly- please use the code tags </> to post code- makes it much more readable.


Did you try to run the original version from here?  Your version lacks the code that calls reconnnect() so will never get connected to the MQTT broker and it allows for no delay() to enable the ethernet link to be established.

If the published example code for a library doesnt work for you its probably something to do with your hardware or setup, not a problem with the example code.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy