Saturday, March 25, 2023

Micropython on ESP8266 - WLAN/DHCP setup and common issues

First, the minimal example that shall work with most hotspots you create on your mobile phone:

import network
 
# configure your hotspot like this or change this
WIFI_SSID = 'mySSID'
WIFI_PASSWORD = 'myWIFIPASSWORD'

# start the wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID , WIFI_PASSWORD)

# test if the wlan is connected
wlan.isconnected()

If everything works fine, sooner or later the test will return true.

If not, continue here (for firmware version esp8266-20220618-v1.19.1.bin):

# print debugging information
import esp
esp.osdebug(0)

Then reconnect again and watch  the logs.

wlan.connect(WIFI_SSID , WIFI_PASSWORD)

In my case it was like this:

>>> sl
scandone
usl
 
This is not one of the common errors like 'wrong password' or something I could find at google. But maybe it can help in your case.

Besides the test if the wlan is connected there's also a command to check the status of the wlan, that I executed next:

wlan.status()

In my case it returned 1, equal to 'STAT_CONNECTING'. My WLAN router showed the device, along with a note 'The device doesn't need an IP address'. For me, DHCP didn't work, and to make it work I first configured a static IP address on the device, then connected to the WLAN and then configured my WLAN router to always assign a static address to my ESP8266. The last step is optional, though.
For the next example my WLAN router is configured with the IP address 192.168.1.1, the netmask 255.255.255.0 and the address 192.168.1.123 is a free address in my network.

# preparation
wlan.active(False)
wlan.active(True)

# set a static IP address
wlan.ifconfig(('192.168.1.123', '255.255.255.0', '192.168.1.1', '8.8.8.8'))

# start the wlan
wlan.connect(WIFI_SSID , WIFI_PASSWORD)

# test if the wlan is connected
wlan.isconnected()

The ESP8266 is now connected to my WLAN router. And after a reboot it would connect again, and DHCP would magically work.


Sunday, March 12, 2023

Micropython on ESP8266 and ESP32 (Windows 7)

Identify the ESP Module
https://www.espressif.com/en/products/modules

Install USB to serial port drivers
https://www.silabs.com/developers/usb-to-uart-bridge-vcp-drivers

Download the latest version of MicroPython
https://micropython.org/download/
Select your board and download the corresponding .bin file. Only this is needed, there is no need to download the complete source code.

Setup a Python environment on Windows

You can download and install python from the Windows store or from a website, or follow this guide that will use Anaconda.

Download and install Anaconda (a python distribution)
Manual download ind installation: https://www.anaconda.com/
Using choccolately: choco install -y anaconda

Download and install nano (a simple text editor) or Visual Studio Code
Nano:

Manual download ind installation: https://www.npackd.org/p/gnunano
Unpack nano.exe and all dlls into a folder in the path, so it can be started by typing in 'nano'.
Using choccolately: choco install -y nano
Visual Studio Code
Manual download ind installation: https://code.visualstudio.com/
Using choccolately: choco install -y vscode

Create a virtual python environment with Anaconda
Start the Anaconda PowerShell prompt, then type:
(base) PS C:> conda update -n base -c defaults conda
(base) PS C:> conda create --name micropython
(base)
PS C:> conda activate micropython
(micropython)
PS C:> conda install python=3.8 #3.9 is not supported
(micropython)
PS C:> pip install terminal-s
(micropython) PS C:> pip install esptool
(micropython)
PS C:> pip install rshell
(micropython)
PS C:> pip install adafruit-ampy

 

Install MicroPython on the ESP module

Identify the serial port
Disconnect the ESP module (if it was already connected), then type:
(micropython) C:> terminal-s

Press Ctrl+C to abort (or if this does not work use the Windows Task Manager to kill the process). Then connect the ESP module, repeat the command and there should be an additional port listed. Again press Ctrl+C to abort. If there is no additional port listed there might be an error in the board, USB port, cable or drivers. Fix the error before you continue. In the following guide I'll use 'COM10', most likely you'll have a different port.

Note: In the next step, if 'Connecting...' is followed by more dots and underscores you might have a broken board or you'll have to press and/or hold one or more buttons to bring the board in a state where it can be flashed. E.g. if there are both an 'EN' and 'BOOT' button: Press and hold 'EN', then press and release 'BOOT' - or if the labels are printed wrong, : Press and hold 'BOOT', then press and release 'EN'.

Erase the flash
Type:
(micropython) PS C:> $port='COM1'
(micropython) PS C:> esptool --port $port erase_flash
esptool.py v4.5.1
Serial port COM1
Connecting....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:0d:8e:8c:84:e5
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 8.7s
Hard resetting via RTS pin...

Install the firmware on ESP8266:
Type:
(micropython) PS C: > esptool --chip ESP8266 --port $port --b
aud 460800 write_flash --flash_mode dio --flash_size detect 0x0 .\esp8266-20220618-v1.19.1.bin
esptool.py v4.5.1
Serial port COM1
Connecting....
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:0d:8e:8c:84:e5
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash will be erased from 0x00000000 to 0x0009afff...
Flash params set to 0x0240
Compressed 634844 bytes to 419808...
Wrote 634844 bytes (419808 compressed) at 0x00000000 in 9.5 seconds (effective 5
36.4 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

Install the firmware on ESP8266:
Type:
(micropython) PS C: > esptool --chip ESP32 --port $port --b
aud 460800 write_flash --flash_mode dio --flash_size detect 0x0 .\esp32-
20220618-v1.19.1.bin

Install notes
- The option '--baud 460800' makes the transfer faster than usual
- The option '--flash_mode dio' makes the flashing slower than usual, but safer supporting all types of flash modules

Connect to the ESP Module
Type:
(micropython) PS C: > rshell -p $port -a -e nano
Using buffer-size of 32
Connecting to COM1 (buffer-size 32)...
Trying to connect to REPL  connected
Retrieving sysname ... esp8266
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /boot.py/
Setting time ... Mar 12, 2023 16:35:17
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
Welcome to rshell. Use the exit command to exit rshell.
C:>

Note
If rshell throws the error 'AttributeError: module 'collections' has no attribute 'Callable'' then search the local file py3k_compat.py and change line 8 to 'return isinstance(x, collections.abc.Callable)' (Python version 3.10.0 throws this error).

Note
When using Visual Studio Code, type '
rshell -p $port -a -e code'.

Using the editor
Now you can edit files on the ESP module with 'edit filename' and they will be opened in either nano or Visual Studio Code. However there's no backup to your local machine, the files are stored only on the ESP module. The two most common files you would edit:
C:> edit /pyboard/boot.py
C:> edit /pyboard/main.py

Using the interactive python interpreter
Type:
C:> repl
Entering REPL. Use Control-X to exit.
>
MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266
Type "help()" for more information.
>>>
>>>

To exit repl press Ctrl+X, to exit rshell type 'exit'.

Using rsync to transfer files
In rshell you can use rsync to syncronize files between a local folder on your computer and the ESP module:
C:> rsync /local/computer/folder /pyboard

Using ampy to transfer files
To transfer individual files, e.g. main.py, type:
ampy --port $port put main.py

Using rshell to start repl automatically:
rshell -p $port -a -e nano repl

That's it. For an introduction to Python, MicroPython look somewhere else. And if you want to use Adafruit CircuitPython forget everything on this page.