This article focuses how to connect a NodeMCU , which has a MicroPython Firmware installed on it,to a Wifi connection. For installtion of MicroPython, please my previous post.
First things first, if you have successfully flashed your NodeMCU with MicroPython, then now you need to know about how to work with the REPL prompt. Haven’t you got your NodeMCU yet ? Click here to get it for the best price from the store.
The REPL prompt
Though not that scary as the heading looks, the REPL (Read Evaluate Print Loop) prompt is used to directly interact with the MicroPython hosted on NodeMCU. It is the best way to test out your code and run commands. You can connect to this prompt by just plugging your USB cable between your PC and NodeMCU. Now run the picocom command as mentioned here and you should be able to see the REPL prompt right on your screen.
NodeMCU is a ESP8266 based board, which means it has a WiFi connectivity and it does nt make sense if we do not use it. So, lets get our hands dirty!
WebREPL – a prompt over WiFi
Now here we will discuss connecting to Wifi on NodeMCU using MicroPython. Once connected, we will have a way to access WebREPL.
WebREPL is a prompt which can be accessed from a browser (Chrome/FireFox). You can connect via MicroPython’s webREPL URL or you can download the HTML to your machine and connect over it. I recommend you the later.
The default address of the NodeMCU is “192.168.4.1” at the port “8266”. Before connecting to WebREPL, you should set a password and enable it via a normal serial connection.
So, in the Serial REPL prompt type the following and set the webREPL up:
>>> import webrepl_setup
Once the setup process is completed, restart your NodeMCU. DONT KNOW how? just press the small button on the left of the USB port on the NodeMCU or unplug your serial USB cable and plug it in, even this works.
Done ? You are now ready to get connected to MicroPython via WebREPL. Keep the seat belts fastened, still we have some more things to do.
As every one has their own local network and want to connect to NodeMCU via the same, I am directly going to mention how to connect to your own network and then access WebREPL.
Connecting to WiFi :
NodeMCU has a good thing, it remembers to what network it has connected earlier . So, here is how you can connect to the WiFi on MicroPython:
>>> import network #Import the Network Module
>>> conn = network.WLAN(network.STA_IF) #Initate Network
>>> conn.active(True) #Change the connection status to active
>>> conn.connect('<your ESSID>', '<your password>') #Connect to network, give your network values and wait for 10 seconds
>>> if conn.isconnected():
Hurray !! You are connected to the WiFi network. Now type the following to know your network details.
>>> <span class="go">('192.168.168.2', '255.255.255.0', '192.168.168.1', '184.108.40.206')</span>
This will list the details of your NodeMCU network configuration
Connect to WebREPL
As you are now connected to the same network as of NodeMCU you can open up WebREPL window and type in your IP address and hit “Connect” button. For example in my case IP address would be “192.168.168.2:8266“.
Once connected, WebREPL asks you to give your “Password” created when you have setup WebREPL using “import webrepl_setup”. You should see the REPL prompt and thats it you are now officially connected to your NodeMCU via WebREPL
By the way if you come across any other ways or other tools to help us out in connecting to Wifi via NodeMCU please leave us a comment, we will share it with world.