- Prolog
- Know your options
- The first script about board information
- The second script about disc space and memory
Before you start you should always check which features your microcontroller and the MircoPython firmware support. If you have flashed the firmware for your microcontroller that is not 100% suitable, errors can occur. Therefore, test with the REPL first before you upload and run your code.
The local directory structure and files after all examples:
$ tree .
|____firmware
| |____esp32-20230426-v1.20.0.bin
|____examples
| |____board
| |____esp32_memory.py
| |____esp32_information.py
| |____mpy
| |____example_module.py
|____venv
| |____bin
...Connect your microcontroller to your local device, start the local environment and connect via rshell.
# change into project root directory
$ cd ~/Projects/ESP
# activate python virtual environment
$ source venv/bin/activate
# connect and start REPL
(venv) $ rshell -p [SERIAL-PORT] replHave the modules displayed in the repl.
# list MicroPython modules
>>> help('modules')The firmware information can be displayed very easily via the usys module.
# import platform and version from usys
>>> from usys import platform, version
# print platform information
>>> print(platform)
# print version information
>>> print(version)Show the CPU frequency in Hertz.
# import unique_id and freq from machine
>>> from machine import unique_id, freq
# import hexlify from ubinascii
>>> from ubinascii import hexlify
# print unique id trough hexlify
>>> print(hexlify(unique_id(), ':'))
# print frequency information
>>> print(f"{freq()} Hz")Show information about hall sensor and current temperature in Fahrenheit.
# import hall_sensor and raw_temperature from esp32
>>> from esp32 import hall_sensor, raw_temperature
# print internal hall sensor
>>> print(hall_sensor())
# print internal temperature of the MCU, in Fahrenheit
>>> print(f"{raw_temperature()} F")I hope no issues occur till here! To know more about MicroPython libraries, read the content of this link. To leave the REPL press keys Control + x.
The assumption is that you are already in the root folder of your project.
# create new subdirectory
$ mkdir -p ~/Projects/ESP/examples/board
# create script
$ touch ~/Projects/ESP/examples/board/esp32_information.pySource Code for
esp32_information.py
Now connect via rshell to your microcontroller, copy the file as main.py and execute.
# start serial connection
(venv) $ rshell -p [SERIAL-PORT]
# list content of pyboard (optional)
/your/current/path> ls /pyboard/
# copy file into pyboard as main.py
/your/current/path> cp examples/board/esp32_information.py /pyboard/main.py
# change into REPL
/your/current/path> replPress the keys Control + d and observe the output. Inside the repl you also can press the reset button of your microcontroller. If you want to leave the REPL, press keys Control + x.
# exit rshell connection
/your/current/path> exitThe second script should output information about the current disc space and memory.
# create script
$ touch ~/Projects/ESP/examples/board/esp32_memory.pySource code for
esp32_memory.py
# copy file into pyboard as main.py
(venv) $ rshell -p [SERIAL-PORT] cp examples/board/esp32_memory.py /pyboard/main.py
# start repl
(venv) $ rshell -p [SERIAL-PORT] replPress the keys Control + d or the reset button and observe the output. If you want to leave the REPL, press keys Control + x.