{"id":103189,"date":"2021-04-22T10:28:48","date_gmt":"2021-04-22T10:28:48","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=103189"},"modified":"2021-04-22T10:28:51","modified_gmt":"2021-04-22T10:28:51","slug":"esp8266-nodemcu-set-custom-hostname-arduino","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-set-custom-hostname-arduino\/","title":{"rendered":"ESP8266 NodeMCU Setting a Custom Hostname (Arduino IDE)"},"content":{"rendered":"\n<p>By default, the hostname of an ESP8266 NodeMCU board is <span class=\"rnthl rntliteral\">ESP-XXXXXX<\/span> where the Xs represents the last six characters of its MAC address. In this tutorial, you&#8217;ll learn how to set a custom hostname for your board.<\/p>\n\n\n\n<p class=\"rntbox rntclgreen\">To set a custom hostname for your board, call <span class=\"rnthl rntliteral\">WiFi.hostname(YOUR_NEW_HOSTNAME);<\/span> before <span class=\"rnthl rntliteral\">WiFi.begin();<\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU Setting a Custom Hostname Arduino IDE\" class=\"wp-image-103195\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?w=1280&amp;quality=100&amp;strip=all&amp;ssl=1 1280w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Setting an ESP8266 NodeMCU Hostname<\/h2>\n\n\n\n<p>The default ESP8266 hostname is <span class=\"rnthl rntliteral\">ESP-XXXXXX<\/span>. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-default-hostname.png?resize=812%2C28&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Setting ESP8266 Hostname\" class=\"wp-image-103190\" width=\"812\" height=\"28\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-default-hostname.png?w=959&amp;quality=100&amp;strip=all&amp;ssl=1 959w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-default-hostname.png?resize=300%2C11&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-default-hostname.png?resize=768%2C27&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 812px) 100vw, 812px\" \/><\/figure><\/div>\n\n\n\n<p>There is a method provided by the <span class=\"rnthl rntliteral\">ESP8266WiFi.h<\/span> library that allows you to set a custom hostname. <\/p>\n\n\n\n<p>First, start by defining your new hostname. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String newHostname = \"ESP8266Node\";<\/code><\/pre>\n\n\n\n<p>Then, call the <span class=\"rnthl rntliteral\">WiFi.hostname()<\/span> function before calling <span class=\"rnthl rntliteral\">WiFi.begin()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFi.hostname(newHostname.c_str());<\/code><\/pre>\n\n\n\n<p>You can copy the complete example below:<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\n  Rui Santos\n  Complete project details at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-set-custom-hostname-arduino\/\n  \n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files.\n  \n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n*\/\n\n#include &lt;ESP8266WiFi.h&gt;\n\n\/\/ Replace with your network credentials (STATION)\nconst char* ssid = &quot;REPLACE_WITH_YOUR_SSID&quot;;\nconst char* password = &quot;REPLACE_WITH_YOUR_PASSWORD&quot;;\n\nString newHostname = &quot;ESP8266Node&quot;;\n\nvoid setup() {\n  Serial.begin(115200);\n  WiFi.mode(WIFI_STA);\n\n  \/\/Get Current Hostname\n  Serial.printf(&quot;Default hostname: %s\\n&quot;, WiFi.hostname().c_str());\n\n  \/\/Set new hostname\n  WiFi.hostname(newHostname.c_str());\n\n  \/\/Get Current Hostname\n  Serial.printf(&quot;New hostname: %s\\n&quot;, WiFi.hostname().c_str());\n  \n  \/\/Init Wi-Fi\n  WiFi.begin(ssid, password);\n  Serial.print(&quot;Connecting to WiFi ..&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print('.');\n    delay(1000);\n  }\n  Serial.println(WiFi.localIP());\n  Serial.print(&quot;RRSI: &quot;);\n  Serial.println(WiFi.RSSI());\n}\n\nvoid loop() {\n  \/\/ put your main code here, to run repeatedly:\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Random-Nerd-Tutorials\/raw\/master\/Projects\/ESP8266\/ESP8266_Set_Hostname.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>After uploading the code to your board, open the Serial Monitor at a baud rate of 115200.<\/p>\n\n\n\n<p>It should print the previous hostname and the new hostname.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"831\" height=\"515\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Custom-Hostname-Serial-Monitor.png?resize=831%2C515&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Custom Hostname Serial Monitor Arduino IDE\" class=\"wp-image-103193\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Custom-Hostname-Serial-Monitor.png?w=831&amp;quality=100&amp;strip=all&amp;ssl=1 831w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Custom-Hostname-Serial-Monitor.png?resize=300%2C186&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Custom-Hostname-Serial-Monitor.png?resize=768%2C476&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 831px) 100vw, 831px\" \/><\/figure><\/div>\n\n\n\n<p>You can use this previous snippet of code in your projects to set a custom hostname to the ESP8266 NodeMCU boards.<\/p>\n\n\n\n<p class=\"rntbox rntcred\"><strong>Important: <\/strong> you may need to restart your router for the changes to take effect on the router settings.<\/p>\n\n\n\n<p>After this, if you go to your router settings, you&#8217;ll see the ESP8266 with the custom hostname.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"956\" height=\"35\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-New-Custom-Hostname.png?resize=956%2C35&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Custom Hostname Arduino IDE\" class=\"wp-image-103192\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-New-Custom-Hostname.png?w=956&amp;quality=100&amp;strip=all&amp;ssl=1 956w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-New-Custom-Hostname.png?resize=300%2C11&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-New-Custom-Hostname.png?resize=768%2C28&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 956px) 100vw, 956px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ve learned how to set up a custom hostname for your ESP8266 NodeMCU boards. This can be useful to identify the devices connected to your network easily. For example, if you have multiple boards connected simultaneously, it will be easier to identify them if they have a custom hostname.<\/p>\n\n\n\n<p>We hope you\u2019ve found this tutorial useful.<\/p>\n\n\n\n<p>Learn more about the ESP8266 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build ESP8266 Web Servers with Arduino IDE (eBook)<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation using ESP8266<\/a><\/li><li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 Projects and Tutorials\u2026<\/a><\/li><\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By default, the hostname of an ESP8266 NodeMCU board is ESP-XXXXXX where the Xs represents the last six characters of its MAC address. In this tutorial, you&#8217;ll learn how to &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU Setting a Custom Hostname (Arduino IDE)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-set-custom-hostname-arduino\/#more-103189\" aria-label=\"Read more about ESP8266 NodeMCU Setting a Custom Hostname (Arduino IDE)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":1,"featured_media":103195,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[265,214,246,300,240,264],"tags":[],"class_list":["post-103189","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266-project","category-esp8266","category-esp8266-arduino-ide","category-0-esp8266","category-esp8266-projects","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/04\/ESP8266-Set-Custom-Hostname.jpg?fit=1280%2C720&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/103189","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=103189"}],"version-history":[{"count":0,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/103189\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/103195"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=103189"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=103189"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=103189"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}