knolleary / pubsubclient

A client library for the Arduino Ethernet Shield that provides support for MQTT.
http://pubsubclient.knolleary.net/
MIT License
3.85k stars 1.48k forks source link

Arduino Mega with Ethernet Shield #180

Open Luis-Filipe opened 8 years ago

Luis-Filipe commented 8 years ago

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?

The pubsubclient nor support Arduino Mega? Who is wrong with my code?

Thanks in advance

knolleary commented 8 years ago

Do you see the client connect to mosquitto at all? Have you tried adding some debug to show what is happening? Have you tried the basic example the library includes to prove that part is working?

knolleary commented 8 years ago

Any update?

carlosrodriguesIT commented 8 years ago

Hello Knolleary, soon I post more for that, because I was not succeed and had to change the design . But I will return to this implementation briefly.

Uppermill commented 7 years ago

I am having a similar problem. I have just moved from a Uno to a Mega and am starting to have problems. I've gone back to the basic example and am getting a return code of 2 when I attempt to connect to MQTT. Can you help?

Uppermill commented 7 years ago

I backtracked a bit to ensure that I could run a modified version of the basic example on my network using a Uno and then tried again on the Mega and although it gave a return code of -2 on the first attempt, it was OK subsequently.