MicroPython on ESP8266

January 31, 2017 by
 

This is an example post, extracted from my OneNote Notebook with specifics on getting micropython working on ESP8266.

Please excuse any typos below… there are my notes after all, and I only am publishing them as an example of what is possible with OneNote.

Micropython on Esp8266 Tutorial https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/index.html

http://micropython-on-wemos-d1-mini.readthedocs.io/en/latest/setup.html

Using Wemos D1 with esp8266MOD The WROOM v2 (aka ESP-13, aka ESP8266MOD

From <https://esp8266hints.wordpress.com/tag/esp8266mod/>

With 4MB SPI flash

Using wemos d1 pro No module, 16M SPI flash

Install / load / reinstall micropython on the Wemos D1

Install the driver-

The wemos d1 uses a cheap

CH340 USB to UART driver

From <https://www.wemos.cc/downloads/>

<<CH341SER_win.zip>>
After installing python on pc, need to installpython and then use the ESPtool to install or reflash / update the firmware to the wemos d1. it may ship with NODEMCU or AT commands.
For help using python pip install and packages https://packaging.python.org/installing/
Install Python.

This guide uses 2.7 as of the writing of this install. This is installed on the PC you are using. Do not use python 3 .

Make sure that it installs / registers itself in the local path so that you can use the reguar “cmd” prompt. Else it might cause issues.

If things don’t work restart.

Then open the python command line



Install esptool from the python command line.

Help:

https://github.com/themadinventor/esptool

Commands

Pip install esptool

If this command does not work, then you need to figure out how to get python installed properly. You should be able to use the windows terminal for this command.

Try restarting


Here are the esptool github details if you need to do it manually, or refer to instructions. https://github.com/espressif/esptool
using esptool

Commands

Esptool.py -h Shows all available commands
esptool.py –port COM6 –baud 115200 read_flash 0x0000000 4096 dump.dmp Reads out the first 4096 bytes to a file called dump to the current director (c:/users/jhavell)


View hex dump file using a hex capable editor (notepad++ with hex edit plugin installed



Alt method… use the GUI for esp8266 Windows use nodemcu-flasher (https://github.com/nodemcu/nodemcu-flasher) under Win32

From <https://forum.micropython.org/viewtopic.php?t=2166>

Clear the esp8266, install image.

From windows Cmd prompt

:

Commands

Esptool.py -h Shows all available commands
esptool.py –port COM6 –baud 115200 erase_flash Erases the flash
<<esp8266-20161110-v1.8.6.bin>>

Download (http://micropython.org/download#esp8266) and then copy the image file to the working drectory

esptool.py –port COM6 write_flash -fm dio -fs 32m -ff 40m 0x0000000 esp8266-20161110-v1.8.6.bin Flashes the new image. The image is in the directory that you are in so in this case c:/esp/lfasfile.bin

(optional… dum it again and verify the memory is all ff ff ff ff)





Testing to see if it installed.

Check device manager for com port – Open com port

Open Putty (serial / console software) settings

Connect using (usb) serial console

115200 / 8 / no parity /1 stop/no flow ctl

Should show up on wifi. Don’t connect yet.




You will now be using the Putty Terminal to type commands.

This is called the REPL (Read Execute Python Language)

Once you open the connection:

Hit the Enter key at the cursor and wait for the prompt >>>

Try the hello world program

Commands

Print (“Hello World”!) Returns hello world mesage
Import esp Imports esp
Esp.check_fw() Checks firmware.

If the last output value is True, the firmware is OK. Otherwise, it’s corrupted and need to be reflashed correctly.

From <https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html>



Now lets set up WEBREPL so that we can do this from a browser

commands

Import webrepl_setup Imports the repl setup
Enable and Set the web console password and then reboot

After a fresh install and boot the device configures itself as a WiFi access point (AP) that you can connect to. The ESSID is of the form MicroPython-xxxxxx where the x’s are replaced with part of the MAC address of your device (so will be the same everytime, and most likely different for all ESP8266 chips). The password for the WiFi is micropythoN (note the upper-case N). Its IP address will be 192.168.4.1 once you connect to its network. WiFi configuration will be discussed in more detail later in the tutorial.

From <https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html>

Restart

[redacted]

[redacted]


The Wifi connection of the device is

SSID Wifi password
MicroPython-xxx micropythoN
IP address 192.168.4.1

There is no webpage. It will go nowhere. You can only connect by WebRepl, for now

Next, we install the webrepl client on your Computer.

Here is an example of what it would look like once installed. You could use this if your computer is hard wired or connected to the same wifi. Otherwise, we will install this on your local machine.

Functional example:

http://micropython.org/webrepl/

Install it locally using this link

https://github.com/micropython/webrepl

Extract the files to a folder

Open the html file

Click the connect button

Enter the password you setup earlier of the serial console




Play with the simple LED on the board

Commands

>>> import
machine
>>> pin
=
machine.Pin(2, machine.Pin.OUT)
>>> pin.high()
>>> pin.low()

From <https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/repl.html>


You cannot simply copy and paste code into the repl console. It will throw errors.

To enter a special “paste mode” hit

CTRL E

This will allow you to paste in chunks of text.

Use CTRL D

to exit this mode and execute the code as entered

https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/filesystem.html

The code you enter at the REPL does not persist past reboot unless you save it to a file. Say for example the entered the led toggle code above. After unplugging and re-pugging in the code, you cant call the toggle function. If you have your board connected over serial and hot the reset button, you will see it reload in the console window.

To have your code persist, you would need to start creating files to make the code permanent.

To view the files on the esp8266 , type the following:

Commands

Import os
Os.listdir() This is like the “dir” or “ls” commands on a computer
Os.mkdir(‘cats’) Makes a folder called cats
Os.rmdir(‘cats’) Deletes the folder cats
Os.remove(fine.txt) Deletes a file called fine.txt

Additionally, you can upload a file from your computer using the webrepl console.

3.3. Start up scripts

There are two files that are treated specially by the ESP8266 when it starts up: boot.py and main.py. The boot.py script is executed first (if it exists) and then once it completes the main.py script is executed. You can create these files yourself and populate them with the code that you want to run when the device starts up.

From <https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/filesystem.html>



Esptool.py -h Shows all available commands


Comments