Add MDNS for ESP8266 (#153)

With ESP32 MDNS causes BLE to don't work properly
This commit is contained in:
Florian authored and GitHub committed 2018-01-06 09:42:27 +01:00
1 parent 9d55af871b
commit ebf82fbb38
5 files changed
+73 -8

No files matched your search

+61 -3
View File
@@ -76,17 +76,23 @@ void callback(char*topic, byte* payload,unsigned int length);
#include <WiFi.h> #include <WiFi.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
WiFiClient eClient; WiFiClient eClient;
#ifdef MDNS_SD
#include <ESPmDNS.h>
#endif
#elif defined(ESP8266) #elif defined(ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ArduinoOTA.h> #include <ArduinoOTA.h>
WiFiClient eClient; WiFiClient eClient;
#ifdef MDNS_SD
#include <ESP8266mDNS.h>
#endif
#else #else
#include <Ethernet.h> #include <Ethernet.h>
EthernetClient eClient; EthernetClient eClient;
#endif #endif
// client parameters // client link to pubsub mqtt
PubSubClient client(mqtt_server, mqtt_port, callback, eClient); PubSubClient client(eClient);
//MQTT last attemps reconnection date //MQTT last attemps reconnection date
unsigned long lastReconnectAttempt = 0; unsigned long lastReconnectAttempt = 0;
@@ -188,6 +194,23 @@ void setup()
else if (error == OTA_END_ERROR) trc(F("End Failed")); else if (error == OTA_END_ERROR) trc(F("End Failed"));
}); });
ArduinoOTA.begin(); 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 #else
//Launch serial for debugging purposes //Launch serial for debugging purposes
Serial.begin(SERIAL_BAUD); Serial.begin(SERIAL_BAUD);
@@ -205,6 +228,8 @@ void setup()
digitalWrite(led_send, HIGH); digitalWrite(led_send, HIGH);
#endif #endif
client.setCallback(callback);
delay(1500); delay(1500);
lastReconnectAttempt = 0; lastReconnectAttempt = 0;
@@ -253,7 +278,7 @@ void setup_wifi() {
IPAddress subnet_adress(subnet); IPAddress subnet_adress(subnet);
IPAddress dns_adress(Dns); IPAddress dns_adress(Dns);
WiFi.begin(wifi_ssid, wifi_password); 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: ")); trc(F("OpenMQTTGateway mac: "));
Serial.println(WiFi.macAddress()); Serial.println(WiFi.macAddress());
@@ -268,6 +293,39 @@ void setup_wifi() {
trc(F("WiFi ok")); 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 #else
void setup_ethernet() { void setup_ethernet() {
Ethernet.begin(mac, ip); //Comment and uncomment the following line if you want to use advanced network config Ethernet.begin(mac, ip); //Comment and uncomment the following line if you want to use advanced network config
+3 -1
View File
@@ -29,9 +29,10 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/*-------------------VERSION----------------------*/ /*-------------------VERSION----------------------*/
#define OMG_VERSION "0.6" #define OMG_VERSION "0.6.1"
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/ /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
//MQTT Parameters definition //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_server "192.168.1.17"
//#define mqtt_user "your_username" // not compulsory only if your broker needs authentication //#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 #define mqtt_password "your_password" // not compulsory only if your broker needs authentication
@@ -43,6 +44,7 @@
#define will_Retain true #define will_Retain true
#define will_Message "Offline" #define will_Message "Offline"
#define Gateway_AnnouncementMsg "Online" #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----------------*/ /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
// Update these with values suitable for your network. // Update these with values suitable for your network.
+2
View File
@@ -100,6 +100,7 @@ Thanks to wolass https://github.com/wolass for suggesting me HM 10 and dinosd ht
1, /* Priority of the task */ 1, /* Priority of the task */
NULL, /* Task handle. */ NULL, /* Task handle. */
taskCore); /* Core where the task should run */ taskCore); /* Core where the task should run */
trc(F("ZgatewayBT ESP32 setup done "));
} }
void coreTask( void * pvParameters ){ void coreTask( void * pvParameters ){
@@ -143,6 +144,7 @@ void setupBT() {
delay(100); delay(100);
softserial.print(F("AT+RESET")); softserial.print(F("AT+RESET"));
delay(100); delay(100);
trc(F("ZgatewayBT HM1X setup done "));
} }
#ifdef ZgatewayBT_v6xx #ifdef ZgatewayBT_v6xx
+3 -1
View File
@@ -35,8 +35,10 @@
//#define DHT_SENSOR_TYPE DHT21 //uncomment for DHT21 Sensor //#define DHT_SENSOR_TYPE DHT21 //uncomment for DHT21 Sensor
#define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility) #define DHT_SENSOR_TYPE DHT22 //uncomment for DHT22 Sensor (default for backwards compatibility)
/*-------------------PIN DEFINITIONS----------------------*/ /*-------------------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 #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 #else
#define DHT_RECEIVER_PIN 8 #define DHT_RECEIVER_PIN 8
#endif #endif
+4 -3
View File
@@ -29,7 +29,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
/*-------------------VERSION----------------------*/ /*-------------------VERSION----------------------*/
#define OMG_VERSION "0.5" #define OMG_VERSION "0.6.1"
/*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/ /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
//MQTT Parameters definition //MQTT Parameters definition
#define mqtt_server "192.168.1.17" #define mqtt_server "192.168.1.17"
@@ -43,6 +43,7 @@
#define will_Retain true #define will_Retain true
#define will_Message "Offline" #define will_Message "Offline"
#define Gateway_AnnouncementMsg "Online" #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----------------*/ /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/
// Update these with values suitable for your network. // Update these with values suitable for your network.
@@ -117,8 +118,8 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress
//#include "config_BH1750.h" //#include "config_BH1750.h"
//#define ZsensorBME280 //#define ZsensorBME280
//#include "config_BME280.h" //#include "config_BME280.h"
//#define ZsensorDHT // If you uncomment this you can't use I2C due to the fact that I2C use also D1 #define ZsensorDHT // If you uncomment this you can't use I2C due to the fact that I2C use also D1
//#include "config_DHT.h" #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 //#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" //#include "config_RFM69.h"
#else // for arduino + W5100 #else // for arduino + W5100