{"id":2571,"date":"2017-07-19T07:48:01","date_gmt":"2017-07-19T06:48:01","guid":{"rendered":"http:\/\/www.robertprice.co.uk\/robblog\/?p=2571"},"modified":"2017-07-19T07:48:01","modified_gmt":"2017-07-19T06:48:01","slug":"nodemcu-wifi-setup","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/","title":{"rendered":"NodeMCU WiFi setup"},"content":{"rendered":"<p>NodeMCU devices have WiFi built in, and it&#8217;s easy to configure using Lua.<\/p>\n<p>One of the easiest examples of setting this up was on <a href=\"http:\/\/www.limpkin.fr\/index.php?post\/2016\/04\/17\/A-Small-Collection-of-NodeMCU-Lua-Scripts-for-Data-Collection\">Limpkin&#8217;s blog<\/a>. I took this and tweaked it slightly so it works with the more recent Lua <a href=\"https:\/\/nodemcu.readthedocs.io\/en\/master\/en\/modules\/wifi\/\">wifi<\/a> module.<\/p>\n<pre>\r\nstation_cfg={}\r\nstation_cfg.ssid=\"Rob's iPhone\"\r\nstation_cfg.pwd=\"secret password\"\r\nwifi.setmode(wifi.STATION)\r\nwifi.sta.config(station_cfg)\r\nwifi.sta.connect()\r\ntmr.alarm(1, 1000, 1, function()\r\n    if wifi.sta.getip() == nil then\r\n        print(\"IP unavailable, Waiting...\")\r\n    else\r\n        tmr.stop(1)\r\n        print(\"ESP8266 mode is: \" .. wifi.getmode())\r\n        print(\"The module MAC address is: \" .. wifi.ap.getmac())\r\n        print(\"Config done, IP is \"..wifi.sta.getip())\r\n    end\r\nend)\r\n<\/pre>\n<p>Upload this using <a href=\"https:\/\/github.com\/4refr0nt\/luatool\">luatool<\/a> or similar as the init.lua file that runs when the NodeMCU powers up.<\/p>\n<p>In this example, the script tries to connect to my iPhone hotspot. We use tmr.alarm to check once a second if we have an IP address. If we don&#8217;t, we let the user know. If we do, we stop the timer from continually executing, and print out some of our wifi configuration details.<\/p>\n<p>It&#8217;s at this point we could start using the connection to talk to the internet if we needed to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>NodeMCU devices have WiFi built in, and it&#8217;s easy to configure using Lua. One of the easiest examples of setting this up was on Limpkin&#8217;s blog. I took this and tweaked it slightly so it works with the more recent Lua wifi module. station_cfg={} station_cfg.ssid=&#8221;Rob&#8217;s iPhone&#8221; station_cfg.pwd=&#8221;secret password&#8221; wifi.setmode(wifi.STATION) wifi.sta.config(station_cfg) wifi.sta.connect() tmr.alarm(1, 1000, 1, function() &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;NodeMCU WiFi setup&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2572,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2,84],"tags":[92,91],"class_list":["post-2571","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev","category-hardware","tag-lua","tag-nodemcu"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>NodeMCU WiFi setup - Robert Price<\/title>\n<meta name=\"description\" content=\"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"NodeMCU WiFi setup - Robert Price\" \/>\n<meta property=\"og:description\" content=\"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2017-07-19T06:48:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"NodeMCU WiFi setup\",\"datePublished\":\"2017-07-19T06:48:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/\"},\"wordCount\":141,\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/wifi-header-screen.jpg\",\"keywords\":[\"Lua\",\"NodeMCU\"],\"articleSection\":[\"Dev\",\"Hardware\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/\",\"name\":\"NodeMCU WiFi setup - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/wifi-header-screen.jpg\",\"datePublished\":\"2017-07-19T06:48:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"description\":\"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/wifi-header-screen.jpg\",\"contentUrl\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/assets\\\/wifi-header-screen.jpg\",\"width\":1200,\"height\":600,\"caption\":\"Connecting a NodeMCU to WiFi\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/nodemcu-wifi-setup\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"NodeMCU WiFi setup\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"NodeMCU WiFi setup - Robert Price","description":"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/","og_locale":"en_GB","og_type":"article","og_title":"NodeMCU WiFi setup - Robert Price","og_description":"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/","og_site_name":"Robert Price","article_published_time":"2017-07-19T06:48:01+00:00","og_image":[{"width":1200,"height":600,"url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg","type":"image\/jpeg"}],"author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"NodeMCU WiFi setup","datePublished":"2017-07-19T06:48:01+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/"},"wordCount":141,"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg","keywords":["Lua","NodeMCU"],"articleSection":["Dev","Hardware"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/","name":"NodeMCU WiFi setup - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#primaryimage"},"image":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#primaryimage"},"thumbnailUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg","datePublished":"2017-07-19T06:48:01+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"description":"How to setup and configure NodeMCU wifi. This includes working example Lua code using the wifi module.","breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/"]}]},{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#primaryimage","url":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg","contentUrl":"https:\/\/www.robertprice.co.uk\/robblog\/assets\/wifi-header-screen.jpg","width":1200,"height":600,"caption":"Connecting a NodeMCU to WiFi"},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/nodemcu-wifi-setup\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"NodeMCU WiFi setup"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=2571"}],"version-history":[{"count":1,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2571\/revisions"}],"predecessor-version":[{"id":2573,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/2571\/revisions\/2573"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media\/2572"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=2571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=2571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=2571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}