blynkkk / blynk-library

Blynk library for IoT boards. Works with Arduino, ESP32, ESP8266, Raspberry Pi, Particle, ARM Mbed, etc.
https://blynk.io
MIT License
3.81k stars 1.38k forks source link

Feature request: setting hostname #500

Closed ettom closed 3 years ago

ettom commented 3 years ago

Hi, this is a feature request to allow setting a hostname. Good arguments for hostnames are OTA updates and general clarity when multiple devices are on the same network.

I've implemented this for ESP8266 and it's working well so far.

diff --git a/src/BlynkSimpleEsp8266.h b/src/BlynkSimpleEsp8266.h
index a61552d..6c1eb9c 100644
--- a/src/BlynkSimpleEsp8266.h
+++ b/src/BlynkSimpleEsp8266.h
@@ -35,8 +35,12 @@ public:
         : Base(transp)
     {}

-    void connectWiFi(const char* ssid, const char* pass)
+    void connectWiFi(const char* ssid, const char* pass, const char* hostname)
     {
+   if (hostname != NULL) {
+            WiFi.hostname(hostname);
+            BLYNK_LOG2(BLYNK_F("Set hostname to "), hostname);
+   }
         BLYNK_LOG2(BLYNK_F("Connecting to "), ssid);
         WiFi.mode(WIFI_STA);
         if (WiFi.status() != WL_CONNECTED) {
@@ -75,9 +79,10 @@ public:
                const char* ssid,
                const char* pass,
                const char* domain = BLYNK_DEFAULT_DOMAIN,
-               uint16_t    port   = BLYNK_DEFAULT_PORT)
+               uint16_t    port   = BLYNK_DEFAULT_PORT,
+               const char* hostname = NULL)
     {
-        connectWiFi(ssid, pass);
+        connectWiFi(ssid, pass, hostname);
         config(auth, domain, port);
         while(this->connect() != true) {}
     }
@@ -86,9 +91,10 @@ public:
                const char* ssid,
                const char* pass,
                IPAddress   ip,
-               uint16_t    port   = BLYNK_DEFAULT_PORT)
+               uint16_t    port   = BLYNK_DEFAULT_PORT,
+               const char* hostname = NULL)
     {
-        connectWiFi(ssid, pass);
+        connectWiFi(ssid, pass, hostname);
         config(auth, ip, port);
         while(this->connect() != true) {}
     }
vshymanskyy commented 3 years ago

You can do this manually in your sketch. Sorry, I see no reason why this should be part of Blynk library.