Site icon Renzo Mischianti

MicroPython with esp8266 and esp32: flashing firmware and programming with basic tools – 1

Spread the love

MicroPython is a popular implementation of the Python programming language designed to run on microcontrollers. It is an excellent way to program microcontrollers in a high-level language that is easy to learn and use. MicroPython runs on many microcontroller platforms, including the ESP8266 and ESP32.

MicroPython with esp8266 and esp32: flashing firmware and using standard tools

To get started with MicroPython on the ESP8266 and ESP32, you will need to set up your development environment. This involves installing the MicroPython firmware on the board and setting up a programming environment on your computer.

ESP32 details

I like these devices. It’s very powerful and low cost with an effective value WIFI microcontroller.

You can find It here ESP32 Dev Kit v1 - TTGO T-Display 1.14 ESP32 - NodeMCU V3 V2 ESP8266 Lolin32 - NodeMCU ESP-32S - WeMos Lolin32 - WeMos Lolin32 mini - ESP32-CAM programmer - ESP32-CAM bundle - ESP32-WROOM-32 - ESP32-S

Specs

Remember that exist a lot of variants of this device. Some have a specified killer function, so this is a generic specs description.

Function Block diagram Espressif esp32 Wi-Fi Bluetooth Microcontroller

Pinouts

ESP32 DEV KIT v1

ESP32 DevKIT v4

Esp32 dev kit v4 pinout

ESP32 WeMos LOLIN32

This device is very powerful, and you can see that there are capacitive touch pins, more than one UART various Analog pins.

How to

You must download drivers for the USB chip, probably you have an esp32 version with cp2102, but exist a variant with ch340 chip.

ESP8266 details

This board does not reach the minimum required to work MicroPython at its full potential, so use it cautiously.

You can find It here WeMos D1 mini - NodeMCU V2 V2.1 V3 - esp01 - esp01 programmer

Specs

Pinouts

Wemos D1 mini

WeMos D1 mini esp8266 pinout mischianti low resolution

WeMos D1 mini high-resolution image

NodeMCU V2

NodeMcu v2 pinout low resolution

NodeMCU v2.x high-resolution image

NodeMCU v2.1

NodeMcu v2.1 low resolution pinout

NodeMCU v2.x high-resolution image

NodeMCU v3

NodeMcu v3 low resolution pinout

I think that the interesting thing is that It has more than one Hardware Serial, so you can use Serial for communication with devices and Serial1 D4 (only Transmission) to debug.

Drivers

You can download drivers for the USB chips here, check your version of D1 mini and click on drivers.

Here is a more exhaustive list of devices.

You need python

First of all, you need to install Python and download It from here.

And remember to add It to the PATH.

Python installation on windows: add to PATH

Flashing MicroPython firmware on the board:

To install MicroPython on your ESP8266 or ESP32 board, you will need to download the firmware from the official MicroPython website. The firmware is available in binary form for a variety of microcontrollers, including the ESP8266 and ESP32.

  1. Download the firmware
  1. Install the esptool.py tool, which is a command-line utility that can be used to flash the MicroPython firmware onto the board. The tool can be installed using pip, a Python package manager.
    • For Windows users:
      • Open Command Prompt
      • Install pip by running the following command (you probably don’t need this step): python -m ensurepip
      • Install esptool.py by running the following command: pip install esptool
    • For Mac or Linux users:
      • Open Terminal
      • Install pip by running the following command: sudo easy_install pip
      • Install esptool.py by running the following command: sudo pip install esptool
C:\Users\renzo>pip install esptool
Defaulting to user installation because normal site-packages is not writeable
Collecting esptool
  Downloading esptool-4.5.1.tar.gz (252 kB)
     |████████████████████████████████| 252 kB 2.2 MB/s
Collecting bitstring>=3.1.6
  Downloading bitstring-4.0.1-py3-none-any.whl (45 kB)
     |████████████████████████████████| 45 kB 3.2 MB/s
Collecting cryptography>=2.1.4
  Downloading cryptography-39.0.2-cp36-abi3-win_amd64.whl (2.5 MB)
     |████████████████████████████████| 2.5 MB 2.2 MB/s
Collecting ecdsa>=0.16.0
  Downloading ecdsa-0.18.0-py2.py3-none-any.whl (142 kB)
     |████████████████████████████████| 142 kB 2.2 MB/s
Requirement already satisfied: pyserial>=3.0 in c:\users\renzo\appdata\roaming\python\python39\site-packages (from esptool) (3.0.1)
Collecting reedsolo<=1.6.0,>=1.5.3
  Downloading reedsolo-1.6.0.tar.gz (278 kB)
     |████████████████████████████████| 278 kB 2.2 MB/s
Collecting cffi>=1.12
  Downloading cffi-1.15.1-cp39-cp39-win_amd64.whl (179 kB)
     |████████████████████████████████| 179 kB 2.2 MB/s
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
     |████████████████████████████████| 118 kB 2.2 MB/s
Collecting six>=1.9.0
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Using legacy 'setup.py install' for esptool, since package 'wheel' is not installed.
Using legacy 'setup.py install' for reedsolo, since package 'wheel' is not installed.
Installing collected packages: pycparser, six, cffi, reedsolo, ecdsa, cryptography, bitstring, esptool
    Running setup.py install for reedsolo ... done
    Running setup.py install for esptool ... done
Successfully installed bitstring-4.0.1 cffi-1.15.1 cryptography-39.0.2 ecdsa-0.18.0 esptool-4.5.1 pycparser-2.21 reedsolo-1.6.0 six-1.16.0

If you add the Script folder to the PATH variable, you can directly call the esptool.py.

Add Script folder to the PATH of windows
  1. Connect the board to your computer via USB.
    To find the new Serial COM port, go to Device Manager from windows menù and check which port is born.
Device Manager find the Serial COM port
  1. Erase the flash memory of the board by running the following command:
    For ESP8266 and esp32:
esptool.py --port COM5 erase_flash

If you don’t have esptool.py in path use this command

python -m esptool --port COM5 erase_flash

Replace COM5 with the correct serial port of your board.

D:\projects\micropython\testesp>python -m esptool --port COM5 erase_flash
esptool.py v4.5.1
Serial port COM5
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: 50:02:91:74:fd:2f
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 8.6s
Hard resetting via RTS pin...
  1. Flash the MicroPython firmware onto the board by running the following command:

For ESP8266:

esptool.py --port COM5 --baud 460800 write_flash --flash_size=detect 0 <firmware.bin>

or

python -m esptool --port COM5 --baud 460800 write_flash --flash_size=detect 0 <firmware.bin>

For ESP32:

esptool.py --port COM5 --baud 460800 write_flash -z 0x1000 <firmware.bin>

or

python -m esptool --port COM5  --baud 460800 write_flash -z 0x1000 <firmware.bin>

Replace COM5 with the correct serial port of your board and <firmware.bin> with the path to the MicroPython firmware file that you downloaded earlier.

D:\projects\micropython\testesp>python -m esptool --port COM5 --baud 460800 write_flash --flash_size=detect 0 esp8266-20220618-v1.19.1.bin
esptool.py v4.5.1
Serial port COM5
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: 50:02:91:74:fd:2f
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 0x0040
Compressed 634844 bytes to 419808...
Wrote 634844 bytes (419808 compressed) at 0x00000000 in 10.0 seconds (effective 509.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
  1. Once the firmware has been flashed onto the board, you can connect to the board using a serial terminal program, such as PuTTY (Windows) or screen (Mac/Linux). The baud rate should be set to 115200.

Start programming in an interactive console (REPL)

Now we are going to program in python immediately. To do so, we can use a Serial monitor to connect to the interactive console of the device.

Use a serial monitor to view the output of your MicroPython code. There are several options for serial monitors, including:

screen COM5 115200

Writing and running Python code with an interactive session

Once you have MicroPython installed on your board, you can use the REPL (Read-Eval-Print Loop) to interact with the board and run Python code. The REPL is a simple way to enter Python commands and see their output in real-time. You can connect to the board’s REPL using a serial terminal program, like PuTTY or screen, over a USB connection.

Hello from CoolTerM MicroPython on esp8266 and esp32

To enter the REPL on an ESP8266 or ESP32 board, open a serial terminal program and connect to the board’s serial port at a baud rate of 115200. You should see a prompt like this:

MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266
Type "help()" for more information.
>>>

This is the Python prompt, where you can enter Python commands. For example, you can enter the following command to print “Hello, world!” to the console:

>>> print("Hello from www.mischianti.org")

You should see the following output:

Hello from www.mischianti.org

You can also write Python code on your computer and upload it to the board using a tool like ampy or rshell. These tools allow you to transfer Python files to the board and run them from the board’s file system.

Interfacing with the board’s hardware peripherals:

MicroPython on the ESP8266 and ESP32 provide access to the board’s hardware peripherals, like GPIO pins, SPI and I2C buses, and analog inputs. You can use Python code to interact with these peripherals and build projects that interact with the physical world.

You must install the package “machine” with pip.

You probably get this error.

D:\projects\micropython\testesp>pip install machine
Defaulting to user installation because normal site-packages is not writeable
Collecting machine
  Downloading machine-0.0.1-py2.py3-none-any.whl (4.2 kB)
Collecting pycrypto
  Downloading pycrypto-2.6.1.tar.gz (446 kB)
     |████████████████████████████████| 446 kB 2.2 MB/s
Using legacy 'setup.py install' for pycrypto, since package 'wheel' is not installed.
Installing collected packages: pycrypto, machine
    Running setup.py install for pycrypto ... error
    ERROR: Command errored out with exit status 1:
     command: 'c:\program files\python39\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\renzo\\AppData\\Local\\Temp\\pip-install-svmkdo9_\\pycrypto_ee48e2c475ff47aaa23d619983f3b5e3\\setup.py'"'"'; __file__='"'"'C:\\Users\\renzo\\AppData\\Local\\Temp\\pip-install-svmkdo9_\\pycrypto_ee48e2c475ff47aaa23d619983f3b5e3\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\renzo\AppData\Local\Temp\pip-record-qctz6vm0\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\renzo\AppData\Roaming\Python\Python39\Include\pycrypto'
         cwd: C:\Users\renzo\AppData\Local\Temp\pip-install-svmkdo9_\pycrypto_ee48e2c475ff47aaa23d619983f3b5e3\
    Complete output (183 lines):
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-3.9
    creating build\lib.win-amd64-3.9\Crypto
    copying lib\Crypto\pct_warnings.py -> build\lib.win-amd64-3.9\Crypto
    copying lib\Crypto\__init__.py -> build\lib.win-amd64-3.9\Crypto
    creating build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\hashalgo.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\HMAC.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\MD2.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\MD4.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\MD5.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\RIPEMD.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\SHA.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\SHA224.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\SHA256.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\SHA384.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\SHA512.py -> build\lib.win-amd64-3.9\Crypto\Hash
    copying lib\Crypto\Hash\__init__.py -> build\lib.win-amd64-3.9\Crypto\Hash
    creating build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\AES.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\ARC2.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\ARC4.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\blockalgo.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\Blowfish.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\CAST.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\DES.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\DES3.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\PKCS1_OAEP.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\PKCS1_v1_5.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\XOR.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    copying lib\Crypto\Cipher\__init__.py -> build\lib.win-amd64-3.9\Crypto\Cipher
    creating build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\asn1.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\Counter.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\number.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\py3compat.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\randpool.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\RFC1751.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\winrandom.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\_number_new.py -> build\lib.win-amd64-3.9\Crypto\Util
    copying lib\Crypto\Util\__init__.py -> build\lib.win-amd64-3.9\Crypto\Util
    creating build\lib.win-amd64-3.9\Crypto\Random
    copying lib\Crypto\Random\random.py -> build\lib.win-amd64-3.9\Crypto\Random
    copying lib\Crypto\Random\_UserFriendlyRNG.py -> build\lib.win-amd64-3.9\Crypto\Random
    copying lib\Crypto\Random\__init__.py -> build\lib.win-amd64-3.9\Crypto\Random
    creating build\lib.win-amd64-3.9\Crypto\Random\Fortuna
    copying lib\Crypto\Random\Fortuna\FortunaAccumulator.py -> build\lib.win-amd64-3.9\Crypto\Random\Fortuna
    copying lib\Crypto\Random\Fortuna\FortunaGenerator.py -> build\lib.win-amd64-3.9\Crypto\Random\Fortuna
    copying lib\Crypto\Random\Fortuna\SHAd256.py -> build\lib.win-amd64-3.9\Crypto\Random\Fortuna
    copying lib\Crypto\Random\Fortuna\__init__.py -> build\lib.win-amd64-3.9\Crypto\Random\Fortuna
    creating build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    copying lib\Crypto\Random\OSRNG\fallback.py -> build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    copying lib\Crypto\Random\OSRNG\nt.py -> build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    copying lib\Crypto\Random\OSRNG\posix.py -> build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    copying lib\Crypto\Random\OSRNG\rng_base.py -> build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    copying lib\Crypto\Random\OSRNG\__init__.py -> build\lib.win-amd64-3.9\Crypto\Random\OSRNG
    creating build\lib.win-amd64-3.9\Crypto\SelfTest
    copying lib\Crypto\SelfTest\st_common.py -> build\lib.win-amd64-3.9\Crypto\SelfTest
    copying lib\Crypto\SelfTest\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\common.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_AES.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_ARC2.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_ARC4.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_Blowfish.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_CAST.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_DES.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_DES3.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_pkcs1_15.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_pkcs1_oaep.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\test_XOR.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    copying lib\Crypto\SelfTest\Cipher\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Cipher
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\common.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_HMAC.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_MD2.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_MD4.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_MD5.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_RIPEMD.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_SHA.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_SHA224.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_SHA256.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_SHA384.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\test_SHA512.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    copying lib\Crypto\SelfTest\Hash\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Hash
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    copying lib\Crypto\SelfTest\Protocol\test_AllOrNothing.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    copying lib\Crypto\SelfTest\Protocol\test_chaffing.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    copying lib\Crypto\SelfTest\Protocol\test_KDF.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    copying lib\Crypto\SelfTest\Protocol\test_rfc1751.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    copying lib\Crypto\SelfTest\Protocol\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Protocol
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    copying lib\Crypto\SelfTest\PublicKey\test_DSA.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    copying lib\Crypto\SelfTest\PublicKey\test_ElGamal.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    copying lib\Crypto\SelfTest\PublicKey\test_importKey.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    copying lib\Crypto\SelfTest\PublicKey\test_RSA.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    copying lib\Crypto\SelfTest\PublicKey\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\PublicKey
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Random
    copying lib\Crypto\SelfTest\Random\test_random.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random
    copying lib\Crypto\SelfTest\Random\test_rpoolcompat.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random
    copying lib\Crypto\SelfTest\Random\test__UserFriendlyRNG.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random
    copying lib\Crypto\SelfTest\Random\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Random\Fortuna
    copying lib\Crypto\SelfTest\Random\Fortuna\test_FortunaAccumulator.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\Fortuna
    copying lib\Crypto\SelfTest\Random\Fortuna\test_FortunaGenerator.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\Fortuna
    copying lib\Crypto\SelfTest\Random\Fortuna\test_SHAd256.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\Fortuna
    copying lib\Crypto\SelfTest\Random\Fortuna\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\Fortuna
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\test_fallback.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\test_generic.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\test_nt.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\test_posix.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\test_winrandom.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    copying lib\Crypto\SelfTest\Random\OSRNG\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Random\OSRNG
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    copying lib\Crypto\SelfTest\Util\test_asn1.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    copying lib\Crypto\SelfTest\Util\test_Counter.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    copying lib\Crypto\SelfTest\Util\test_number.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    copying lib\Crypto\SelfTest\Util\test_winrandom.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    copying lib\Crypto\SelfTest\Util\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Util
    creating build\lib.win-amd64-3.9\Crypto\SelfTest\Signature
    copying lib\Crypto\SelfTest\Signature\test_pkcs1_15.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Signature
    copying lib\Crypto\SelfTest\Signature\test_pkcs1_pss.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Signature
    copying lib\Crypto\SelfTest\Signature\__init__.py -> build\lib.win-amd64-3.9\Crypto\SelfTest\Signature
    creating build\lib.win-amd64-3.9\Crypto\Protocol
    copying lib\Crypto\Protocol\AllOrNothing.py -> build\lib.win-amd64-3.9\Crypto\Protocol
    copying lib\Crypto\Protocol\Chaffing.py -> build\lib.win-amd64-3.9\Crypto\Protocol
    copying lib\Crypto\Protocol\KDF.py -> build\lib.win-amd64-3.9\Crypto\Protocol
    copying lib\Crypto\Protocol\__init__.py -> build\lib.win-amd64-3.9\Crypto\Protocol
    creating build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\DSA.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\ElGamal.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\pubkey.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\RSA.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\_DSA.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\_RSA.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\_slowmath.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    copying lib\Crypto\PublicKey\__init__.py -> build\lib.win-amd64-3.9\Crypto\PublicKey
    creating build\lib.win-amd64-3.9\Crypto\Signature
    copying lib\Crypto\Signature\PKCS1_PSS.py -> build\lib.win-amd64-3.9\Crypto\Signature
    copying lib\Crypto\Signature\PKCS1_v1_5.py -> build\lib.win-amd64-3.9\Crypto\Signature
    copying lib\Crypto\Signature\__init__.py -> build\lib.win-amd64-3.9\Crypto\Signature
    Skipping optional fixer: buffer
    Skipping optional fixer: idioms
    Skipping optional fixer: set_literal
    Skipping optional fixer: ws_comma
    running build_ext
    warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
    building 'Crypto.Random.OSRNG.winrandom' extension
    creating build\temp.win-amd64-3.9
    creating build\temp.win-amd64-3.9\Release
    creating build\temp.win-amd64-3.9\Release\src
    C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isrc/ -Isrc/inc-msvc/ -Ic:\program files\python39\include -Ic:\program files\python39\include -IC:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\cppwinrt /Tcsrc/winrand.c /Fobuild\temp.win-amd64-3.9\Release\src/winrand.obj
    winrand.c
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(27): error C2061: errore di sintassi: identificatore 'intmax_t'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(28): error C2061: errore di sintassi: identificatore 'rem'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(28): error C2059: errore di sintassi: ';'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(29): error C2059: errore di sintassi: '}'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(31): error C2061: errore di sintassi: identificatore 'imaxdiv_t'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(31): error C2059: errore di sintassi: ';'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(41): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(42): error C2146: errore di sintassi: ')' mancante prima dell'identificatore '_Number'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(42): error C2061: errore di sintassi: identificatore '_Number'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(42): error C2059: errore di sintassi: ';'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(43): error C2059: errore di sintassi: ')'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(46): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(47): error C2146: errore di sintassi: ')' mancante prima dell'identificatore '_Numerator'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(47): error C2061: errore di sintassi: identificatore '_Numerator'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(47): error C2059: errore di sintassi: ';'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(47): error C2059: errore di sintassi: ','
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(49): error C2059: errore di sintassi: ')'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(51): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(57): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(64): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(70): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(77): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(83): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(90): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\ucrt\inttypes.h(96): error C2143: errore di sintassi: '{' mancante prima di '__cdecl'
    error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools\\VC\\Tools\\MSVC\\14.16.27023\\bin\\HostX86\\x64\\cl.exe' failed with exit code 2
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'c:\program files\python39\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\renzo\\AppData\\Local\\Temp\\pip-install-svmkdo9_\\pycrypto_ee48e2c475ff47aaa23d619983f3b5e3\\setup.py'"'"'; __file__='"'"'C:\\Users\\renzo\\AppData\\Local\\Temp\\pip-install-svmkdo9_\\pycrypto_ee48e2c475ff47aaa23d619983f3b5e3\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\renzo\AppData\Local\Temp\pip-record-qctz6vm0\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\renzo\AppData\Roaming\Python\Python39\Include\pycrypto' Check the logs for full command output.

From the log, you can get the path of Microsoft Visual Studio BuildTools, and then execute these commands:

cd "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build"
vcvarsall.bat x86_amd64
cd %VCINSTALLDIR%
for /R %f in (*stdint.h) do set CL=-FI"%f"
pip install machine
D:\projects\micropython\testesp>cd "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build"

D:\projects\micropython\testesp>c:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build>vcvarsall.bat x86_amd64
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.0
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x86_x64'

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build>cd %VCINSTALLDIR%

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC>for /R %f in (*stdint.h) do set CL=-FI"%f"

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC>set CL=-FI"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Tools\MSVC\14.16.27023\include\stdint.h"
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC>pip install pycrypto
Defaulting to user installation because normal site-packages is not writeable
Collecting pycrypto
  Using cached pycrypto-2.6.1.tar.gz (446 kB)
Using legacy 'setup.py install' for pycrypto, since package 'wheel' is not installed.
Installing collected packages: pycrypto
    Running setup.py install for pycrypto ... done
Successfully installed pycrypto-2.6.1
C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC>pip install machine
Defaulting to user installation because normal site-packages is not writeable
Collecting machine
  Using cached machine-0.0.1-py2.py3-none-any.whl (4.2 kB)
Requirement already satisfied: pycrypto in c:\users\renzo\appdata\roaming\python\python39\site-packages (from machine) (2.6.1)
Installing collected packages: machine
  WARNING: The script sample.exe is installed in 'C:\Users\renzo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed machine-0.0.1

Control a LED

To control an LED connected to pin 2 on an ESP8266 board (It’s the builtin led for WeMos D1 mini), you can enter the following commands:

import machine
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()
pin.off()

The first line imports the machine module, which contains classes and functions for interacting with the hardware of the ESP8266 or ESP32 board.

The second line creates a new Pin object, which represents a GPIO pin on the board. In this case, the Pin object is created with 2 as the pin number and machine.Pin.OUT as the mode, which sets the pin as an output. The Pin object is assigned to the variable pin.

CoolTerM and MicroPython: control a led on esp8266 and esp32

This turns on the output of the pin object, which will cause the corresponding physical pin on the board to output a high voltage.

WeMos d1 mini (esp8266): led light

This turns off the output of the pin object, which will cause the corresponding physical pin on the board to output a low voltage.

Writing blink-led sketch

Here is a complete MicroPython sketch, the simple program that blink a built-in led.

Take your preferred editor, create a main.py file, and add these lines.

import machine
import time
pin = machine.Pin(2, machine.Pin.OUT)
while True:
    pin.value(0)
    time.sleep(0.5)
    pin.value(1)
    time.sleep(0.5)

Naturally, you can also test it in a realtime session.

Putty MicroPython: test blink led program on esp8266 esp32

Upload the code and view the output using a serial monitor

Upload the MicroPython code to your ESP8266 or ESP32 board using the adafruit-ampy command-line tool and view the output using a serial monitor:

  1. Connect your ESP8266 or ESP32 board to your computer via USB.
  2. Install the adafruit-ampy package using pip, which is a package manager for Python. You can install this package by opening a command prompt (Windows) or terminal (macOS/Linux) and running the following command:
pip install adafruit-ampy
D:\projects\micropython\testesp>pip install adafruit-ampy
Defaulting to user installation because normal site-packages is not writeable
Collecting adafruit-ampy
  Downloading adafruit_ampy-1.1.0-py2.py3-none-any.whl (16 kB)
Collecting click
  Using cached click-8.1.3-py3-none-any.whl (96 kB)
Requirement already satisfied: pyserial in c:\users\renzo\appdata\roaming\python\python39\site-packages (from adafruit-ampy) (3.0.1)
Collecting python-dotenv
  Downloading python_dotenv-1.0.0-py3-none-any.whl (19 kB)
Collecting colorama
  Downloading colorama-0.4.6-py2.py3-none-any.whl (25 kB)
Installing collected packages: python-dotenv, colorama, click, adafruit-ampy
  WARNING: The script dotenv.exe is installed in 'C:\Users\renzo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
  WARNING: The script ampy.exe is installed in 'C:\Users\renzo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed adafruit-ampy-1.1.0 click-8.1.3 colorama-0.4.6 python-dotenv-1.0.0
  1. You can also see a warning in the installation process
  WARNING: The script ampy.exe is installed in 'C:\Users\renzo\AppData\Roaming\Python\Python39\Scripts' which is not on PATH.

So we are going to add the Script PATH also to the path variable.

Add Script folder to the PATH of windows
  1. Write your MicroPython code in a file on your computer.
  2. Use the ampy command-line tool to upload the file to the board. For example, if your MicroPython code is in a file called main.py and your board is connected to the serial port COM5, you can run the following command:
ampy --port COM5 put main.py

Note: Replace COM5 with the correct serial port of your board, and main.py with the name of your MicroPython code file.

  1. Once your serial monitor is set up, reset the board by pressing the reset button or unplugging and plugging in the USB cable. The board will automatically start running the code that you uploaded, and you should see the output of your MicroPython code in the serial monitor.

Thanks

Mastering esp8266 programming with MicroPython

  1. MicroPython with esp8266 and esp32: flashing firmware and using standard tools
  2. MicroPython with esp8266 and esp32: flashing firmware and using Thonny IDE
  3. MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE

Mastering ESP32 programming with MicroPython

  1. MicroPython with esp8266 and esp32: flashing firmware and using standard tools
  2. MicroPython with esp8266 and esp32: flashing firmware and using Thonny IDE
  3. MicroPython with esp8266 and esp32: flashing firmware and using PyCharm IDE


Spread the love
Exit mobile version