Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More MQTT Ethernet configuration - timeout #1453

Open
wants to merge 3 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions MyConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,32 @@
*/
//#define MY_MQTT_CLIENT_KEY

/**
* @def MY_MQTT_ETH_INIT_DELAY
* @brief Set a delay for Ethernet initialisation.
*
* This define is useful if you want to change default Ethernet chip initialisation
* delay time to other value. By default, it is 1000ms
* Example: @code #define MY_MQTT_ETH_INIT_DELAY 1 @endcode
*/
#ifndef MY_MQTT_ETH_INIT_DELAY
#define MY_MQTT_ETH_INIT_DELAY 1000
#endif
/**
* @def MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT
* @brief Set a MQTT broker socket connection timeout time.
*
* This define is useful if you want to change default MQTT TCP/IP broker
* connection timeout. By default, it is 1000ms.
*
* Note that this is not supported in ESP8266 and ESP32 platforms, sorry.
*
* Example: @code #define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT 1000 @endcode
*/
#ifndef MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT
#define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT 1000
#endif

/**
* @def MY_IP_ADDRESS
* @brief Static ip address of gateway. If not defined, DHCP will be used.
Expand Down Expand Up @@ -2323,6 +2349,8 @@
#define MY_MQTT_CA_CERT
#define MY_MQTT_CLIENT_CERT
#define MY_MQTT_CLIENT_KEY
#define MY_MQTT_ETH_INIT_DELAY
#define MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT
#define MY_SIGNAL_REPORT_ENABLED
// general
#define MY_WITH_LEDS_BLINKING_INVERSE
Expand Down
11 changes: 9 additions & 2 deletions core/MyGatewayTransportMQTTClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,11 @@ bool reconnectMQTT(void)

return true;
}
#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
delay(1000);
#else
delay(MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT);
#endif
GATEWAY_DEBUG(PSTR("!GWT:RMQ:FAIL\n"));
return false;
}
Expand Down Expand Up @@ -194,7 +198,7 @@ bool gatewayTransportConnect(void)
Ethernet.localIP()[0],
Ethernet.localIP()[1], Ethernet.localIP()[2], Ethernet.localIP()[3]);
// give the Ethernet interface a second to initialize
delay(1000);
delay(MY_MQTT_ETH_INIT_DELAY);
#endif
return true;
}
Expand Down Expand Up @@ -247,7 +251,10 @@ bool gatewayTransportInit(void)
#else
_MQTT_client.setServer(MY_CONTROLLER_URL_ADDRESS, MY_PORT);
#endif /* End of MY_CONTROLLER_IP_ADDRESS */

// ESP platform doesn't support connection timeout
#if !defined(MY_GATEWAY_ESP8266) && !defined(MY_GATEWAY_ESP32)
_MQTT_ethClient.setConnectionTimeout(MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT);
#endif
_MQTT_client.setCallback(incomingMQTT);

#if defined(MY_GATEWAY_ESP8266) || defined(MY_GATEWAY_ESP32)
Expand Down
11 changes: 9 additions & 2 deletions hal/architecture/Linux/drivers/core/EthernetClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@
#include <errno.h>
#include "log.h"

EthernetClient::EthernetClient() : _sock(-1)
EthernetClient::EthernetClient() : _sock(-1), socketTimeout(1000)
{
}

EthernetClient::EthernetClient(int sock) : _sock(sock)
EthernetClient::EthernetClient(int sock) : _sock(sock), socketTimeout(1000)
{
}

Expand Down Expand Up @@ -89,6 +89,13 @@ int EthernetClient::connect(const char* host, uint16_t port)
continue;
}

// Sets the socket timeout
struct timeval timeout;
timeout.tv_sec = 0;
timeout.tv_usec = socketTimeout * 1000000;
setsockopt(_sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
setsockopt(_sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));

break;
}

Expand Down
9 changes: 9 additions & 0 deletions hal/architecture/Linux/drivers/core/EthernetClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,21 @@ class EthernetClient : public Client
{
return !this->operator==(rhs);
};
/**
* @brief Set socket timeout.
*
*/
void setConnectionTimeout(uint16_t timeoutInMilis)
{
socketTimeout = timeoutInMilis;
};

friend class EthernetServer;

private:
int _sock; //!< @brief Network socket file descriptor.
IPAddress _srcip; //!< @brief Local ip to bind to.
uint16_t socketTimeout; //!< @brief Socket timeout in miliseconds.
};

#endif
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,8 @@ MY_MQTT_PASSWORD LITERAL1
MY_MQTT_PUBLISH_TOPIC_PREFIX LITERAL1
MY_MQTT_SUBSCRIBE_TOPIC_PREFIX LITERAL1
MY_MQTT_USER LITERAL1
MY_MQTT_ETH_CLIENT_CONNECTION_TIMEOUT LITERAL1
MY_MQTT_ETH_INIT_DELAY LITERAL1
MY_W5100_SPI_EN LITERAL1
MY_WIFI_SSID LITERAL1
MY_WIFI_BSSID LITERAL1
Expand Down