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)[-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()
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.
- Otherwise they were thinking of delayed release on GitHub ↩
Hi. I am following your guide to setup a webpage with a led in my esp8266 nodemcu. i have done the following code:
log(‘start server method’)
addr = socket.getaddrinfo(‘0.0.0.0’, 80)[-1]
s = socket.socket()
log(‘listening on’+str( addr))
cl, addr = s.accept()
log(‘client connected from’+str(addr))
html = “””
Hello #%d from MicroPython!
Click here to toggle LED hooked to pin 12
the page comes up but when i press the link, the page changes to http://192.168.1.89/toggle (which is the ip of the esp) and a problem loading page appears
any ideas on how to solve that please?