Python on ESP8266 using MicroPython

You know I love ESP8266 and Python. So when the opportunity came to be part of it, I did in a small way on Kickstarter. The project team has been truly great. They send almost monthly updates on what they are working on and since they got enough funding they went open from day 11. Yesterday they released V1.8 which has pretty good support for ESP8266.

If you were part of Kicstarter you would have the firmware by email if not you can just build it. I tried the pre-built one on one of my NodeMCU. It was super easy. You need esptool installed. Check the wiki page. Connect the NodeMCU to the pc and then

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=8m -fm dio 0 esp8266-2016-05-03-v1.8.bin

That should give you the bare minimum ESP8266 running MicroPython and with REPL. You can connect using usb serial at 115200. or you can try WebRepl. This is the best part. You get access to python command prompt which you can use like the one on desktop.

I had try a simple webserver. Call this file main.py and move it to the root folder. Then reset the ESP8266.

import network
import socket

def log(msg):
    print(msg)


def do_connect():
    sta_if = network.WLAN(network.STA_IF)
    if not sta_if.isconnected():
        log('connecting to network...')
        sta_if.active(True)
        sta_if.connect('<SSID>', '<PASSWORD>')
        while not sta_if.isconnected():
            pass
    log('network config:'+str( sta_if.ifconfig()))

def start_myserver():
    log('start server method')
    addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
    s = socket.socket()
    s.bind(addr)
    s.listen(1)
    log('listening on'+str( addr))
    while True:
        cl, addr = s.accept()
        log('client connected from'+str(addr))
        cl.send(html)
        cl.close()

#main part
html = """<!DOCTYPE html>
<html>
    <head> <title>ESP8266</title> </head>
    <body> 
        Hello from Python web on ESP8266
    </body>
</html>
"""

do_connect()
start_myserver()

esp8266_python

Code is self-explanatory. You can use any tool to move the code or you can use their webrepl_clI. Now try http://192.168.4.1. That’s the ip it tries to get.

ESP8266 for Artists is not yet update. I am working on it. If you are new to ESP8266 and want a detailed how-to. Thats probably the best place to go.

  1. Otherwise they were thinking of delayed release on GitHub

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *