28
28
uint8_t protocolH2i (char c);
29
29
30
30
31
- IPAddress _brokerIp (MY_CONTROLLER_IP_ADDRESS);
31
+ #if defined MY_CONTROLLER_IP_ADDRESS
32
+ IPAddress _brokerIp (MY_CONTROLLER_IP_ADDRESS);
33
+ #endif
32
34
33
35
#if defined(MY_GATEWAY_ESP8266)
34
36
#define EthernetClient WiFiClient
@@ -46,6 +48,7 @@ IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
46
48
47
49
EthernetClient _ethClient;
48
50
PubSubClient _client (_ethClient);
51
+ bool _connecting = true ;
49
52
bool _available = false ;
50
53
char _convBuffer[MAX_PAYLOAD*2 +1 ];
51
54
char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH];
@@ -152,7 +155,13 @@ bool reconnectMQTT() {
152
155
}
153
156
154
157
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
+
156
165
_client.setCallback (incomingMQTT);
157
166
158
167
#if defined(MY_GATEWAY_ESP8266)
@@ -166,30 +175,42 @@ bool gatewayTransportInit() {
166
175
MY_SERIALDEVICE.print (" ." );
167
176
yield ();
168
177
}
169
- MY_SERIALDEVICE.print (F ( " IP: " ) );
178
+ MY_SERIALDEVICE.print (" IP: " );
170
179
MY_SERIALDEVICE.println (WiFi.localIP ());
171
180
#else
172
181
#ifdef MY_IP_ADDRESS
173
182
Ethernet.begin (_clientMAC, _clientIp);
174
- MY_SERIALDEVICE.print (F (" IP: " ));
175
- MY_SERIALDEVICE.println (Ethernet.localIP ());
176
183
#else
177
184
// 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: " );
180
192
MY_SERIALDEVICE.println (Ethernet.localIP ());
181
- #endif /* IP_ADDRESS_DHCP */
193
+ #endif
194
+
182
195
// give the Ethernet interface a second to initialize
183
196
// TODO: use HW delay
184
197
wait (1000 );
185
198
#endif
199
+ _connecting = false ;
186
200
return true ;
187
201
}
188
202
189
203
190
204
bool gatewayTransportAvailable () {
205
+ if (_connecting)
206
+ return false ;
207
+
208
+ // keep lease on dhcp address
209
+ // Ethernet.maintain();
191
210
if (!_client.connected ()) {
192
- reconnectMQTT ();
211
+ // reinitialise client
212
+ if (gatewayTransportInit ())
213
+ reconnectMQTT ();
193
214
return false ;
194
215
}
195
216
_client.loop ();
0 commit comments