Added ability to connect to mqtt server by url. Fixed mqtt client

connect bug. #355
This commit is contained in:
Henrik Ekblad
2016-01-27 20:25:52 +01:00
parent 0d4efb167e
commit e95b75e062
4 changed files with 40 additions and 14 deletions
+3 -2
View File
@@ -179,9 +179,10 @@
#endif #endif
// GATEWAY - COMMON FUNCTIONS // GATEWAY - COMMON FUNCTIONS
// We only support MQTT Client using W5100 and ESP8266 at the moment // We only support MQTT Client using W5100 and ESP8266 at the moment
#if !defined(MY_CONTROLLER_IP_ADDRESS) #if !(defined(MY_CONTROLLER_URL_ADDRESS) || defined(MY_CONTROLLER_IP_ADDRESS))
#error You must specify MY_CONTROLLER_IP_ADDRESS (MQTT broker address) #error You must specify MY_CONTROLLER_IP_ADDRESS or MY_CONTROLLER_URL_ADDRESS
#endif #endif
#if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX) #if !defined(MY_MQTT_PUBLISH_TOPIC_PREFIX)
#error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client #error You must specify a topic publish prefix MY_MQTT_PUBLISH_TOPIC_PREFIX for this MQTT client
#endif #endif
@@ -156,7 +156,11 @@ bool gatewayTransportSend(MyMessage &message)
ret = _ethernetServer.endPacket(); ret = _ethernetServer.endPacket();
#else #else
EthernetClient client; 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.write(_ethernetMsg, strlen(_ethernetMsg));
client.stop(); client.stop();
} }
@@ -28,7 +28,9 @@
uint8_t protocolH2i(char c); 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) #if defined(MY_GATEWAY_ESP8266)
#define EthernetClient WiFiClient #define EthernetClient WiFiClient
@@ -46,6 +48,7 @@ IPAddress _brokerIp(MY_CONTROLLER_IP_ADDRESS);
EthernetClient _ethClient; EthernetClient _ethClient;
PubSubClient _client(_ethClient); PubSubClient _client(_ethClient);
bool _connecting = true;
bool _available = false; bool _available = false;
char _convBuffer[MAX_PAYLOAD*2+1]; char _convBuffer[MAX_PAYLOAD*2+1];
char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH]; char _fmtBuffer[MY_GATEWAY_MAX_SEND_LENGTH];
@@ -152,7 +155,13 @@ bool reconnectMQTT() {
} }
bool gatewayTransportInit() { 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); _client.setCallback(incomingMQTT);
#if defined(MY_GATEWAY_ESP8266) #if defined(MY_GATEWAY_ESP8266)
@@ -166,30 +175,42 @@ bool gatewayTransportInit() {
MY_SERIALDEVICE.print("."); MY_SERIALDEVICE.print(".");
yield(); yield();
} }
MY_SERIALDEVICE.print(F("IP: ")); MY_SERIALDEVICE.print("IP: ");
MY_SERIALDEVICE.println(WiFi.localIP()); MY_SERIALDEVICE.println(WiFi.localIP());
#else #else
#ifdef MY_IP_ADDRESS #ifdef MY_IP_ADDRESS
Ethernet.begin(_clientMAC, _clientIp); Ethernet.begin(_clientMAC, _clientIp);
MY_SERIALDEVICE.print(F("IP: "));
MY_SERIALDEVICE.println(Ethernet.localIP());
#else #else
// Get IP address from DHCP // Get IP address from DHCP
Ethernet.begin(_clientMAC); if (!Ethernet.begin(_clientMAC))
MY_SERIALDEVICE.print(F("IP: ")); {
MY_SERIALDEVICE.print("DHCP FAILURE...");
_connecting = false;
return false;
}
MY_SERIALDEVICE.print("IP: ");
MY_SERIALDEVICE.println(Ethernet.localIP()); MY_SERIALDEVICE.println(Ethernet.localIP());
#endif /* IP_ADDRESS_DHCP */ #endif
// give the Ethernet interface a second to initialize // give the Ethernet interface a second to initialize
// TODO: use HW delay // TODO: use HW delay
wait(1000); wait(1000);
#endif #endif
_connecting = false;
return true; return true;
} }
bool gatewayTransportAvailable() { bool gatewayTransportAvailable() {
if (_connecting)
return false;
//keep lease on dhcp address
//Ethernet.maintain();
if (!_client.connected()) { if (!_client.connected()) {
reconnectMQTT(); //reinitialise client
if (gatewayTransportInit())
reconnectMQTT();
return false; return false;
} }
_client.loop(); _client.loop();
@@ -103,8 +103,8 @@
#define MY_IP_GATEWAY_ADDRESS 192,168,178,1 #define MY_IP_GATEWAY_ADDRESS 192,168,178,1
#define MY_IP_SUBNET_ADDRESS 255,255,255,0 #define MY_IP_SUBNET_ADDRESS 255,255,255,0
// MQTT broker ip address or url. Define one or the other.
// MQTT broker ip address. //#define MY_CONTROLLER_URL_ADDRESS "m20.cloudmqtt.com"
#define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68 #define MY_CONTROLLER_IP_ADDRESS 192, 168, 178, 68
// The MQTT broker port to to open // The MQTT broker port to to open