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