Skip to content

Commit e95b75e

Browse files
committed
Added ability to connect to mqtt server by url. Fixed mqtt client
connect bug. arduino#355
1 parent 0d4efb1 commit e95b75e

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

libraries/MySensors/MySensor.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@
179179
#endif
180180
// GATEWAY - COMMON FUNCTIONS
181181
// We only support MQTT Client using W5100 and ESP8266 at the moment
182-
#if !defined(MY_CONTROLLER_IP_ADDRESS)
183-
#error You must specify MY_CONTROLLER_IP_ADDRESS (MQTT broker address)
182+
#if !(defined(MY_CONTROLLER_URL_ADDRESS) || defined(MY_CONTROLLER_IP_ADDRESS))
183+
#error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS
184184
#endif
185+
185186
#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
186187
#error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client
187188
#endif

libraries/MySensors/core/MyGatewayTransportEthernet.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ bool gatewayTransportSend(MyMessage &message)
156156
ret = _ethernetServer.endPacket();
157157
#else
158158
EthernetClient client;
159-
if (client.connect(_ethernetControllerIP, MY_PORT)) {
159+
#if defined(MY_CONTROLLER_URL_ADDRESS)
160+
if (client.connect(MY_CONTROLLER_URL_ADDRESS, MY_PORT)) {
161+
#else
162+
if (client.connect(_ethernetControllerIP, MY_PORT)) {
163+
#endif
160164
client.write(_ethernetMsg, strlen(_ethernetMsg));
161165
client.stop();
162166
}

libraries/MySensors/core/MyGatewayTransportMQTTClient.cpp

+30-9
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
uint8_t protocolH2i(char c);
2929

3030

31-
IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
31+
#if defined MY_CONTROLLER_IP_ADDRESS
32+
IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
33+
#endif
3234

3335
#if defined(MY_GATEWAY_ESP8266)
3436
#define EthernetClient WiFiClient
@@ -46,6 +48,7 @@ IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
4648

4749
EthernetClient _ethClient;
4850
PubSubClient _client(_ethClient);
51+
bool _connecting = true;
4952
bool _available = false;
5053
char _convBuffer[MAX_PAYLOAD*2+1];
5154
char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH];
@@ -152,7 +155,13 @@ bool reconnectMQTT() {
152155
}
153156

154157
bool gatewayTransportInit() {
155-
_client.setServer(_brokerIp, MY_PORT);
158+
_connecting = true;
159+
#if defined(MY_CONTROLLER_IP_ADDRESS)
160+
_client.setServer(_brokerIp, MY_PORT);
161+
#else
162+
_client.setServer(MY_CONTROLLER_URL_ADDRESS, MY_PORT);
163+
#endif
164+
156165
_client.setCallback(incomingMQTT);
157166

158167
#if defined(MY_GATEWAY_ESP8266)
@@ -166,30 +175,42 @@ bool gatewayTransportInit() {
166175
MY_SERIALDEVICE.print(".");
167176
yield();
168177
}
169-
MY_SERIALDEVICE.print(F("IP: "));
178+
MY_SERIALDEVICE.print("IP: ");
170179
MY_SERIALDEVICE.println(WiFi.localIP());
171180
#else
172181
#ifdef MY_IP_ADDRESS
173182
Ethernet.begin(_clientMAC, _clientIp);
174-
MY_SERIALDEVICE.print(F("IP: "));
175-
MY_SERIALDEVICE.println(Ethernet.localIP());
176183
#else
177184
// Get IP address from DHCP
178-
Ethernet.begin(_clientMAC);
179-
MY_SERIALDEVICE.print(F("IP: "));
185+
if (!Ethernet.begin(_clientMAC))
186+
{
187+
MY_SERIALDEVICE.print("DHCP FAILURE...");
188+
_connecting = false;
189+
return false;
190+
}
191+
MY_SERIALDEVICE.print("IP: ");
180192
MY_SERIALDEVICE.println(Ethernet.localIP());
181-
#endif /* IP_ADDRESS_DHCP */
193+
#endif
194+
182195
// give the Ethernet interface a second to initialize
183196
// TODO: use HW delay
184197
wait(1000);
185198
#endif
199+
_connecting = false;
186200
return true;
187201
}
188202

189203

190204
bool gatewayTransportAvailable() {
205+
if (_connecting)
206+
return false;
207+
208+
//keep lease on dhcp address
209+
//Ethernet.maintain();
191210
if (!_client.connected()) {
192-
reconnectMQTT();
211+
//reinitialise client
212+
if (gatewayTransportInit())
213+
reconnectMQTT();
193214
return false;
194215
}
195216
_client.loop();

libraries/MySensors/examples/GatewayW5100MQTTClient/GatewayW5100MQTTClient.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@
103103
#define MY_IP_GATEWAY_ADDRESS 192,168,178,1
104104
#define MY_IP_SUBNET_ADDRESS 255,255,255,0
105105

106-
107-
// MQTT broker ip address.
106+
// MQTT broker ip address or url. Define one or the other.
107+
//#define MY_CONTROLLER_URL_ADDRESS "m20.cloudmqtt.com"
108108
#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
109109

110110
// The MQTT broker port to to open

0 commit comments

Comments
 (0)