I'm trying to get MQQT to work on a Due but am finding it keeps disconnecting.
( -3 : MQTT_CONNECTION_LOST - the network connection was broken)
I know it's not a duplicate ip or the network and suspect it's to do with the connection between the Due and the shield and the SPI 10. Any ideas?
14:17:51.601 -> My IP address: 192.168.3.167.
14:17:53.105 -> MQQT connect failed, error code = -1
14:17:53.105 -> Attempting MQTT connection...
14:17:53.105 -> MQTT Connected
14:17:53.105 -> loop
14:17:54.103 -> loop
14:17:55.102 -> MQQT connect failed, error code = -3
14:17:55.102 -> Attempting MQTT connection...
14:17:55.150 -> MQTT Connected
14:17:55.150 -> loop
14:17:56.100 -> loop
14:17:57.100 -> MQQT connect failed, error code = -3
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress server(192, 168, 3, 217);
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
//EthernetClient client;
EthernetClient ethClient;
PubSubClient client(ethClient);
long lastReconnectAttempt = 0;
void callback(char* topic, byte* payload, unsigned int length)
{
// handle message arrived
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
boolean reconnect()
{
if (!client.connected())
{
Serial.print("MQQT connect failed, error code = ");
Serial.println (client.state());
Serial.println("Attempting MQTT connection...");
}
if (client.connect("ArduinoFrequency"))
{
// Once connected, publish an announcement...
//client.publish("Frequency","Frequency 1 on");
Serial.println("MQTT Connected");
// ... and resubscribe
client.subscribe("Pylontech/+/+");
//client.subscribe("ai/+/alexa");
}
return client.connected();
}
void setup()
{
//SPI.begin(10); // initialize the bus for a device on pin 10 doesnt work with it in
Ethernet.init(10); // makes no difference
client.setServer(server, 1883);
client.setCallback(callback);
// Open serial communications and wait for port to open:
Serial.begin(115200);
// this check is only needed on the Leonardo:
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
//Serial.println("Start");
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for(;;)
;
}
// print your local IP address:
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
delay(1500);
lastReconnectAttempt = 0;
}
void loop()
{
if (!client.connected())
{
long now = millis();
if (now - lastReconnectAttempt > 5000)
{
lastReconnectAttempt = now;
// Attempt to reconnect
if (reconnect())
{
lastReconnectAttempt = 0;
}
}
}
else
{
// Client connected
client.loop();
}
Serial.println("loop");
delay (1000);
}