I'm using Arduino Mega 2560 and Ethernet shield 2 (W5500) to send MQTT messages to my Home assistant (Raspberry Pi). The Home assistant are running a MQTT broker (Mosquitto). Arduino connects to the MQTT broker and is able to send messages for 30-90 seconds before gives an error. The error is "MQTT_CONNECTION_TIMEOUT" but I can't figure out how to fix this. Anyone have an idea??
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);
// IP and MAC adress
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x4F, 0x77 };
IPAddress ip(192, 168, 1, 214);
// Adress to MQTT server!
const IPAddress server(192, 168, 1, 140);
// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
// Constant definitions
const int ledPin = 22;
const int bortePin = 23;
const int utePin = 24;
const int sparetempPin = 25;
const int frostsikringPin = 26;
const int lysitrappPin = 27;
bool borteState = false ; // Initial setting
bool LastborteState = false ; // Initial setting Lastbortestate
bool uteState = false ; // Initial setting uteState
bool LastuteState = false; // Initial setting Lastutestate
bool hjemmeState = true ; // Initial setting hjemmeState
bool LasthjemmeState = true ; // Initial setting LasthjemmeState
bool sparetemp = false ; // Initial setting sparetemp
bool frostsikr = false ; //Initial setting frostsikr
bool LysitrappState = false ; // Initial setting LysitrappState
bool LastLysitrappState = false ; //Initial setting LastLysitrappstate
void setup()
{
//Deactivate SD-card on the SPI-bus
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);
pinMode(10, OUTPUT);
//pinMode(4, OUTPUT);
//digitalWrite(4, HIGH);
// Useful for debugging purposes
Serial.begin(9600);
// Start the ethernet connection
Ethernet.begin(mac, ip);
// Ethernet takes some time to boot!
delay(3000);
// Set the MQTT server to the server stated above ^
mqttClient.setServer(server, 1883);
// Attempt to connect to the server with the ID "Mqttuser"
if (mqttClient.connect("ArduinoMqtt", "Mqttuser", "Mqttuser"))
{
Serial.println("Connection has been established, well done");
// Establish the subscribe event
// mqttClient.setCallback(subscribeReceive);
}
else
{
Serial.println("Looks like the server connection failed...");
}
// PinMode definitions
pinMode(ledPin, OUTPUT);
pinMode(bortePin, INPUT_PULLUP);
pinMode(utePin, INPUT_PULLUP);
pinMode(lysitrappPin, INPUT_PULLUP);
}
void loop()
{
// This is needed at the top of the loop!
mqttClient.loop();
// Status connection
if (mqttClient.state()==0)
{}
else
{
Serial.println(mqttClient.state());
}
delay(1000);
// Stairlight ON/OFF for coordination with Home assistant
// Read status stairlight from Centrol (inverted PULL_UP):
LysitrappState = !digitalRead(lysitrappPin);
// Compare status stairlight with previous state
if (LysitrappState == LastLysitrappState)
{}
else
{
// If status has changed, parameter for LastLysitrappState and sendes MQTT message to Home Assistant
LastLysitrappState = LysitrappState;
// If status has changed to OFF send MQTT message
if (LastLysitrappState == false)
{
// Writing status to screen, turn checklight off
Serial.println("Lys i trapp AV");
digitalWrite(ledPin,LOW);
// Sendeing message "off" to topic "home/trapp/lights"
if(mqttClient.publish("home/trapp/lights", "off"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
}
if (LastLysitrappState == true)
{
// Writing status on screen, turn checklight on
Serial.println("Lys i trapp PÅ");
digitalWrite(ledPin,HIGH);
// Sendeing message "on" to topic "home/trapp/lights"
if(mqttClient.publish("home/trapp/lights", "on"))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
}
}
}