Skip to content

Commit

Permalink
Add MDNS for ESP8266 (#153)
Browse files Browse the repository at this point in the history
With ESP32 MDNS causes BLE to don't work properly
  • Loading branch information
1technophile authored Jan 6, 2018
1 parent 9d55af8 commit ebf82fb
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
64 changes: 61 additions & 3 deletions OpenMQTTGateway.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,23 @@ void callback(char*topic, byte* payload,unsigned int length);
#include <WiFi.h>
#include <ArduinoOTA.h>
WiFiClient eClient;
#ifdef MDNS_SD
#include <ESPmDNS.h>
#endif
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
WiFiClient eClient;
#ifdef MDNS_SD
#include <ESP8266mDNS.h>
#endif
#else
#include <Ethernet.h>
EthernetClient eClient;
#endif

// client parameters
PubSubClient client(mqtt_server, mqtt_port, callback, eClient);
// client link to pubsub mqtt
PubSubClient client(eClient);

//MQTT last attemps reconnection date
unsigned long lastReconnectAttempt = 0;
Expand Down Expand Up @@ -188,6 +194,23 @@ void setup()
else if (error == OTA_END_ERROR) trc(F("End Failed"));
});
ArduinoOTA.begin();

#ifdef MDNS_SD
trc(F("Connecting to MQTT by mDNS without mqtt hostname"));
connectMQTTmdns();
#else
#ifdef mqtt_server_name // if name is defined we define the mqtt server by its name
trc(F("Connecting to MQTT with mqtt hostname"));
IPAddress mqtt_server_ip;
WiFi.hostByName(mqtt_server_name, mqtt_server_ip);
client.setServer(mqtt_server_ip, mqtt_port);
trc(mqtt_server_ip.toString());
#else // if not by its IP adress
trc(F("Connecting to MQTT by IP adress"));
client.setServer(mqtt_server, mqtt_port);
trc(String(mqtt_server));
#endif
#endif
#else
//Launch serial for debugging purposes
Serial.begin(SERIAL_BAUD);
Expand All @@ -205,6 +228,8 @@ void setup()
digitalWrite(led_send, HIGH);
#endif

client.setCallback(callback);

delay(1500);

lastReconnectAttempt = 0;
Expand Down Expand Up @@ -253,7 +278,7 @@ void setup_wifi() {
IPAddress subnet_adress(subnet);
IPAddress dns_adress(Dns);
WiFi.begin(wifi_ssid, wifi_password);
//WiFi.config(ip_adress,gateway_adress,subnet_adress); //Uncomment this line if you want to use advanced network config
//WiFi.config(ip_adress,gateway_adress,subnet_adress,dns_adress); //Uncomment this line if you want to use advanced network config

trc(F("OpenMQTTGateway mac: "));
Serial.println(WiFi.macAddress());
Expand All @@ -268,6 +293,39 @@ void setup_wifi() {

trc(F("WiFi ok"));
}

#ifdef MDNS_SD
void connectMQTTmdns(){
if (!MDNS.begin("ESP_MQTT")) {
trc(F("Error setting up MDNS responder!"));
while(1){
delay(1000);
}
}
trc(F("Browsing for service MQTT "));
int n = MDNS.queryService("mqtt", "tcp");
if (n == 0) {
trc(F("no services found"));
}else {
trc(String(n));
trc(" service(s) found");
for (int i=0; i < n; ++i) {
// Print details for each service found
trc(String(i + 1));
trc(String(MDNS.hostname(i)));
trc(MDNS.IP(i).toString());
trc(String(MDNS.port(i)));
}
if (n==1) {
trc(F("One MQTT server found setting parameters"));
client.setServer(MDNS.IP(0), int(MDNS.port(0)));
}else{
trc(F("Several MQTT servers found, please deactivate mDNS and set your default server"));
}
}
}
#endif

#else
void setup_ethernet() {
Ethernet.begin(mac, ip); //Comment and uncomment the following line if you want to use advanced network config
Expand Down
4 changes: 3 additions & 1 deletion User_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*-------------------VERSION----------------------*/
#define OMG_VERSION "0.6"
#define OMG_VERSION "0.6.1"
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
//MQTT Parameters definition
//#define mqtt_server_name "www.mqtt_broker.com" // instead of defining the server by its IP you can define it by its name, uncomment this line and set the correct MQTT server host name
#define mqtt_server "192.168.1.17"
//#define mqtt_user "your_username" // not compulsory only if your broker needs authentication
#define mqtt_password "your_password" // not compulsory only if your broker needs authentication
Expand All @@ -43,6 +44,7 @@
#define will_Retain true
#define will_Message "Offline"
#define Gateway_AnnouncementMsg "Online"
//#define MDNS_SD //comment if you don't want to use mdns for discovering automatically your ip server, please note that MDNS with ESP32 can cause the BLE to not work

/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
// Update these with values suitable for your network.
Expand Down
2 changes: 2 additions & 0 deletions ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
1, /* Priority of the task */
NULL, /* Task handle. */
taskCore); /* Core where the task should run */
trc(F("ZgatewayBT ESP32 setup done "));
}

void coreTask( void * pvParameters ){
Expand Down Expand Up @@ -143,6 +144,7 @@ void setupBT() {
delay(100);
softserial.print(F("AT+RESET"));
delay(100);
trc(F("ZgatewayBT HM1X setup done "));
}

#ifdef ZgatewayBT_v6xx
Expand Down
4 changes: 3 additions & 1 deletion config_DHT.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
//#define DHT_SENSOR_TYPE DHT21 //uncomment for DHT21 Sensor
#define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility)
/*-------------------PIN DEFINITIONS----------------------*/
#if defined(ESP8266) || defined(ESP32)
#if defined(ESP8266)
#define DHT_RECEIVER_PIN D1 // you can put D5 if you don't use HCSR501 sensor and the RFM69
#elif defined(ESP32)
#define DHT_RECEIVER_PIN 35
#else
#define DHT_RECEIVER_PIN 8
#endif
Expand Down
7 changes: 4 additions & 3 deletions tests/Test_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*-------------------VERSION----------------------*/
#define OMG_VERSION "0.5"
#define OMG_VERSION "0.6.1"
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
//MQTT Parameters definition
#define mqtt_server "192.168.1.17"
Expand All @@ -43,6 +43,7 @@
#define will_Retain true
#define will_Message "Offline"
#define Gateway_AnnouncementMsg "Online"
#define MDNS_SD //comment if you don't want to use mdns for discovering automatically your ip server

/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
// Update these with values suitable for your network.
Expand Down Expand Up @@ -117,8 +118,8 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
//#include "config_BH1750.h"
//#define ZsensorBME280
//#include "config_BME280.h"
//#define ZsensorDHT // If you uncomment this you can't use I2C due to the fact that I2C use also D1
//#include "config_DHT.h"
#define ZsensorDHT // If you uncomment this you can't use I2C due to the fact that I2C use also D1
#include "config_DHT.h"
//#define ZgatewayRFM69 // If you uncomment this you can't use RF and BT due to the fact that RF use also D8 and BT use also D6/D7
//#include "config_RFM69.h"
#else // for arduino + W5100
Expand Down

0 comments on commit ebf82fb

Please sign in to comment.