diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 4bd3b26..fbd9be9 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -76,17 +76,23 @@ void callback(char*topic, byte* payload,unsigned int length); #include #include WiFiClient eClient; + #ifdef MDNS_SD + #include + #endif #elif defined(ESP8266) #include #include WiFiClient eClient; + #ifdef MDNS_SD + #include + #endif #else #include 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; @@ -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); @@ -205,6 +228,8 @@ void setup() digitalWrite(led_send, HIGH); #endif + client.setCallback(callback); + delay(1500); lastReconnectAttempt = 0; @@ -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()); @@ -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 diff --git a/User_config.h b/User_config.h index e8d94a8..a625681 100644 --- a/User_config.h +++ b/User_config.h @@ -29,9 +29,10 @@ along with this program. If not, see . */ /*-------------------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 @@ -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. diff --git a/ZgatewayBT.ino b/ZgatewayBT.ino index 7c01938..35d23c7 100644 --- a/ZgatewayBT.ino +++ b/ZgatewayBT.ino @@ -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 ){ @@ -143,6 +144,7 @@ void setupBT() { delay(100); softserial.print(F("AT+RESET")); delay(100); + trc(F("ZgatewayBT HM1X setup done ")); } #ifdef ZgatewayBT_v6xx diff --git a/config_DHT.h b/config_DHT.h index 36edf7a..8b23832 100644 --- a/config_DHT.h +++ b/config_DHT.h @@ -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 diff --git a/tests/Test_config.h b/tests/Test_config.h index 68c581d..968687a 100644 --- a/tests/Test_config.h +++ b/tests/Test_config.h @@ -29,7 +29,7 @@ along with this program. If not, see . */ /*-------------------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" @@ -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. @@ -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