diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 05753f0..c562a2a 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -179,9 +179,10 @@ #endif // GATEWAY - COMMON FUNCTIONS // We only support MQTT Client using W5100 and ESP8266 at the moment - #if !defined(MY_CONTROLLER_IP_ADDRESS) - #error You must specify MY_CONTROLLER_IP_ADDRESS (MQTT broker address) + #if !(defined(MY_CONTROLLER_URL_ADDRESS) || defined(MY_CONTROLLER_IP_ADDRESS)) + #error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS #endif + #if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX) #error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client #endif diff --git a/libraries/MySensors/core/MyGatewayTransportEthernet.cpp b/libraries/MySensors/core/MyGatewayTransportEthernet.cpp index 3bbeb3d..c5b581f 100644 --- a/libraries/MySensors/core/MyGatewayTransportEthernet.cpp +++ b/libraries/MySensors/core/MyGatewayTransportEthernet.cpp @@ -156,7 +156,11 @@ bool gatewayTransportSend(MyMessage &message) ret = _ethernetServer.endPacket(); #else EthernetClient client; - if (client.connect(_ethernetControllerIP, MY_PORT)) { + #if defined(MY_CONTROLLER_URL_ADDRESS) + if (client.connect(MY_CONTROLLER_URL_ADDRESS, MY_PORT)) { + #else + if (client.connect(_ethernetControllerIP, MY_PORT)) { + #endif client.write(_ethernetMsg, strlen(_ethernetMsg)); client.stop(); } diff --git a/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp b/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp index 7a38e88..3dad8d1 100644 --- a/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp +++ b/libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp @@ -28,7 +28,9 @@ uint8_t protocolH2i(char c); -IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS); +#if defined MY_CONTROLLER_IP_ADDRESS + IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS); +#endif #if defined(MY_GATEWAY_ESP8266) #define EthernetClient WiFiClient @@ -46,6 +48,7 @@ IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS); EthernetClient _ethClient; PubSubClient _client(_ethClient); +bool _connecting = true; bool _available = false; char _convBuffer[MAX_PAYLOAD*2+1]; char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH]; @@ -152,7 +155,13 @@ bool reconnectMQTT() { } bool gatewayTransportInit() { - _client.setServer(_brokerIp, MY_PORT); + _connecting = true; + #if defined(MY_CONTROLLER_IP_ADDRESS) + _client.setServer(_brokerIp, MY_PORT); + #else + _client.setServer(MY_CONTROLLER_URL_ADDRESS, MY_PORT); + #endif + _client.setCallback(incomingMQTT); #if defined(MY_GATEWAY_ESP8266) @@ -166,30 +175,42 @@ bool gatewayTransportInit() { MY_SERIALDEVICE.print("."); yield(); } - MY_SERIALDEVICE.print(F("IP: ")); + MY_SERIALDEVICE.print("IP: "); MY_SERIALDEVICE.println(WiFi.localIP()); #else #ifdef MY_IP_ADDRESS Ethernet.begin(_clientMAC, _clientIp); - MY_SERIALDEVICE.print(F("IP: ")); - MY_SERIALDEVICE.println(Ethernet.localIP()); #else // Get IP address from DHCP - Ethernet.begin(_clientMAC); - MY_SERIALDEVICE.print(F("IP: ")); + if (!Ethernet.begin(_clientMAC)) + { + MY_SERIALDEVICE.print("DHCP FAILURE..."); + _connecting = false; + return false; + } + MY_SERIALDEVICE.print("IP: "); MY_SERIALDEVICE.println(Ethernet.localIP()); - #endif /* IP_ADDRESS_DHCP */ + #endif + // give the Ethernet interface a second to initialize // TODO: use HW delay wait(1000); #endif + _connecting = false; return true; } bool gatewayTransportAvailable() { + if (_connecting) + return false; + + //keep lease on dhcp address + //Ethernet.maintain(); if (!_client.connected()) { - reconnectMQTT(); + //reinitialise client + if (gatewayTransportInit()) + reconnectMQTT(); return false; } _client.loop(); diff --git a/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino b/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino index 34b5a9a..37c8f26 100644 --- a/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino +++ b/libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino @@ -103,8 +103,8 @@ #define MY_IP_GATEWAY_ADDRESS 192,168,178,1 #define MY_IP_SUBNET_ADDRESS 255,255,255,0 - -// MQTT broker ip address. +// MQTT broker ip address or url. Define one or the other. +//#define MY_CONTROLLER_URL_ADDRESS "m20.cloudmqtt.com" #define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68 // The MQTT broker port to to open