NodeMCU Config

From Network Security Wiki


  • Documentation:


  • Flashing Micropython

Download firmware file from

sudo pip install esptool --port /dev/ttyUSB0 erase_flash --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 ~/Desktop/esp8266-20171101-v1.9.3.bin
  • Check the firmware integrity:
import esp

Console Access

  • Via Terminal:
sudo apt install picocom
picocom /dev/ttyUSB0 -b115200

Exiting from picocom:

Cntrl + A 
Cntrl + X
  • Via REPL(web):
import webrepl_setup
  • If you disabled automatic start-up on boot, you may run configured daemon on demand using:
import webrepl
  • Upload data using Serial Port
sudo pip install adafruit-ampy
ampy --port /dev/ttyUSB0 put ~/Desktop/
ampy --port /dev/ttyUSB0 ls

Error Codes

Error Code Details:

File System

  • Checking filesystem:
import os
  • Create directories:
  • Remove Files:
  • Reading data:
f = open('data.txt')
  • Writing Data to files:
f = open('data.txt', 'w')
f.write('some data')
  • Execute a script called
import foo


  • Checking Machine Frequency & Overclocking:
import machine
machine.freq()                # get the current frequency of the CPU
machine.freq(160000000)       # set the CPU frequency to 160 MHz
  • Diagnostics
import port_diag


AP Mode

  • Check AP Name:
import network
ap = network.WLAN(network.AP_IF)
  • Change AP name and password:
import network
ap = network.WLAN(network.AP_IF)
ap.config(essid='MyESP8266', authmode=network.AUTH_WPA_WPA2_PSK, password='mypassword')    # Password should be min 8 char
  • Turning off AP:
ap = network.WLAN(network.AP_IF)

Station Mode

Connecting to WIFI AP:

sta_if = network.WLAN(network.STA_IF)
sta_if.connect('<your ESSID>', '<your password>')

To check if the connection is established:


Once established you can check the IP address:


You can then disable the access-point interface if you no longer need it:

Disconnecting from Station:

sta_if = network.WLAN(network.STA_IF)

Here is a function you can run (or put in your file) to automatically connect to your WiFi network:

def do_connect():
    import network
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        print('connecting to network...')
        sta_if.connect('<essid>', '<password>')
        while not sta_if.isconnected():
    print('network config:', sta_if.ifconfig())

Boot Process

  1. As a final step of boot procedure, is executed from filesystem
  2. This file is a hook to start up a user application each time on boot (instead of going to REPL).
  3. For small test applications, you may name them directly as
  4. But instead it’s recommended to keep your application(s) in separate files, and have just the following in
import my_app
my_app.main() File

# This file is executed on every boot (including wake-boot from deepsleep)
import esp
import uos
import machine
import gc
import webrepl
import network
import ntptime
#from machine import RTC
import micropython
import time

#uos.dupterm(None, 1)    # disable REPL on UART(0)


# Determine if waking from Deep Sleep    
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
    print('Woke from a deep sleep')

# Machine Stats
print("Freq: ", machine.freq()/1000000, "MHz")
#print("ESP Flash size: ", esp.flash_size())
#print("ESP Free Mem: ", esp.freemem())
print("Free Mem: ", gc.mem_free()/1000,"KB")
#print("Micropython Info: ") 

# Flash Space stats
space = uos.statvfs('/')
total_space = (space[1] * space[3])/1000000
used_space = (space[2] * space[4])/1000000
free_space = total_space - used_space
print("Total:", total_space, "MB Used:", used_space,"MB Free:", free_space,"MB")

# WiFi Config
sta_if = network.WLAN(network.STA_IF) # allow auto reconnect

if not sta_if.isconnected():
    print('Connecting to WiFi...')
    sta_if.connect('TP-Link_6A31', 'Kirat#291011')
    while not sta_if.isconnected():
print('STA Config: ', sta_if.config('essid'), sta_if.ifconfig())

# Wifi Access Point config
ap = network.WLAN(network.AP_IF)
ap.config(essid='MicroPythonAP', authmode=network.AUTH_WPA_WPA2_PSK, password='Admin@123')   # Password should be min 8 char
print('AP Config: ', ap.config('essid'), ap.ifconfig())

# synchronize with ntp need to be connected to wifi
rtc = machine.RTC()
    ntptime.settime() # set the rtc datetime from the remote server
    print("Time: ", rtc.datetime())    # get the date and time in UTC
except OSError:
    print("Time: Unable to get data from Server")

# Deep Sleep config
def deep_sleep(delta):
    delay = delta*1000*60
    rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
    rtc.alarm(rtc.ALARM0, delay)
    print("Deep Sleep for: ", delta," min")

# Flash Onboard LED
def flash_led(n,x,y):
    pin = machine.Pin(2, machine.Pin.OUT)
    for i in range(n):


{{#widget:DISQUS |id=networkm |uniqid=NodeMCU Config |url= }}