Tag Archives: micropython

ESP32 – MicroPython compiling for ESP32

We begin now a series of articles about the Espressif ESP32 MCU

ESP32 Wifi-Bluetooth Module

After having analyzed in the previous articles  MicroPython  for ESP8266, in this we start to treat MicroPython on ESP32.

The following shows how to generate the MicroPython image from the source code for the ESP32 board.

The operating system is Debian 9, previously encountered for the esptool and Adafruit-ampy utilities

MicroPython – Python on embedded devices

Log on  with the operating system user on Debian 9, sviluppo in our case; under the home folder  create the Micropython folder and inside this folder the  esp32 folder

The MicroPython source code is available at the following link

Github Micropython

Instructions to build the firmware for the ESP32 are available at the link

MicroPython esp32

Clone the repository

Go into  micropython/ports/esp32 folder

Run

The command releases the git hash for the supported espidf

At the time of our compilation the result is as follows

Go into  /home/sviluppo/Micropython/esp32 folder and clone ESPIDF

Checkout with the hash obtained above

Run

We follow the instructions at the link

Cross compiler

to install the cross compiler; install the prerequisites for building

Download the cross compiler

Create the  esp folder under /home/sviluppo/Micropython/esp32

Unpack the cross compiler in esp

Add the binary folder of the cross compiler to the PATH envinronment variable

Go into the following folder

and create the makefile file with the following code inside

Go into the micropython folder available in /home/sviluppo/Micropython/esp32

Run

to add external dependencies.

Next step is the MicroPython cross compiler build

Finally we can build the ESP32  firmware with the following commands

The firmware is generated under ports/esp32/build with the name firmware.bin.

Proceed to load the firmware with the commands

In our case, the firmware upload was successful with the command

It is also possible to load the firmware.bin image with the commands

where  /dev/ttyXXX is replaced with the actual serial port to which we connect the ESP32 board.

In the next article we’ll compile the nodemcu firmware for ESP32

ESP32 – Nodemcu firmware compiling

MicroPython – MicroPython compiling for ESP8266 with additional modules

After the firmware build of the MicroPython firmware for esp8266 in the previous article

MicroPython – MicroPython compiling for ESP8266

we now show how build a module inside the firmware; this option optimizes the module execution and minimizes the size of the same on the device; the module is frozen into the firmware.

With the firmware compiled with the defaults, the following modules are available, listed in the REPL console with the command

standardmodulesNow let’s try to compile the firmware with the mqtt modules already inside. For this purpose, the module sources are copied to the umqtt folder under modules

umqttfolderWe proceed to recompile the firmware. From micropython/ports/esp8266 we run the following commands

We can see from the compilation log, that these modules are placed in the frozen state

frozenmodulesAfter uploading this firmware we obtain the following list of modules inside the firmware

firmwarewithmodulesIt is also possible to create the firmware with the modules not compiled and optimized, by copying the sources in the scripts folder under esp8266; in the specific case the sources must be placed as shown below

The modules are always visible in the firmware, but not optimized, so if you need some module it is convenient to copy it on the device during the coding  and in the last phase of the project compile the firmware with the frozen modules.

 

 

MicroPython – MicroPython compiling for ESP8266

After using in some examples the MicroPython firmware on the ESP8266 board

MicroPython – Nodes examples with DHT11, Relay and MQTT

this article shows how to generate the MicroPython firmware for the ESP8266 board.

The operating system used for this task is Debian 9, already encountered when we analyzed  the esptool utility

MicroPython – Python on embedded devices

Log on  with the operating system user on Debian 9, sviluppo in our case; under the home folder  create the Micropython folder and inside this folder the  esp8266 folder

The MicroPython source code is available at the following link

Github Micropython

Instructions to build the firmware for the ESP8266 are available at the link

MicroPython esp8266

Clone the repository

Download the cross compiler, as described here

Cross compiler

As indicated in the link above, download the source of the cross compiler always from the folder esp8266

Install the prerequisites for building

Proceed to the build of the cross compiler with the following commands

At the end of the build add the binary folder of the cross compiler to the PATH envinronment variable

Go into the micropython folder available under ~/Micropython/esp8266

Run

to add external dependencies.

Next step is the MicroPython cross compiler build

Finally we can build the ESP8266 firmware with the following commands

The firmware is generated under ports/esp8266/build folder with the name firmware_combined.bin.

Proceed to load the firmware with the command

where  /dev/ttyXXX is repalced with the actual serial port to which we connect the ESP8266 board.

In our case, the firmware upload was successful with the command

It is also possible to load the firmware_combined.bin image with the command

In the next article we’ll  analyze how to build the MicroPython firmware adding into the image additional MicroPython modules

MicroPython – MicroPython compiling for ESP8266 with additional modules

MicroPython – Nodes examples with DHT11, Relay and MQTT

In a series of articles seen previously  on Mqtt, Beaglebone and ESP8266

Mqtt with Beaglebone and ESP8266 – Articles List

we have created the infrastructure for managing the ESP8266 nodes .

In this article we program the two ESP8266 nodes  using MicroPython instead of Nodemcu and Lua.

The infrastructure remains identical; on the Beaglebone there is Mqtt Mosquitto Server and Freeboard for the management of the mqtt topics, visualization and interaction.

The ESP8266 node examples with relay and DHT11 sensor use the umqtt MicroPython libraries, available on the site

umqtt simple

In the following examples, only the umqtt simple is used; in the code several delays have been inserted to allow the board to manage the messages and not run into errors; in the case the Mqtt server is not available the code restarts the board.

Below the code used for these examples.

Node with DHT11 sensor

  • boot.py: code executed at startup

  • umqtt/simple.py:  mqtt library

  • dht11.py: module for reading the temperature and humidity on the GPIO2

  • main.py: code executed after boot

  •  client_mqtt.py: Main module for node management

  • riavvia.py: service code to restart the module from uPyCraft

 

Node with relay

  • boot.py: code executed at startup

  • umqtt/simple.py: mqtt library

    • main.py: code executed after boot

    •  client_mqtt.py: Main module for node management

  • riavvia.py: service code to restart the module from uPyCraft

Below is the filesystem on ESP8266 for the relays sample

ESPFilesystemBy accessing Freeboard we have the same informations obtained with Nodemcu and LUA on ESP8266 nodes

FreeboardMicroPythonBelow are the screens with MQTT Dashboard installed on an Android smartphone, with the possibility to check the status of the relay, read the sensor parameters, restart and sleep the two nodes

MqttPublish

MqttSubscribe

Here are the links to download the two examples

DHT11 sensor sample

Relay sample

In the next article we will compile the MicroPython firmware for ESP8266

MicroPython – MicroPython compiling for ESP8266

 

 

 

 

MicroPython – MicroPython program structure

After reviewing various development tools for MicroPython in previous articles

MicroPython – Development tools Ampy – Eric

MicroPython IDE uPyCraft

in this article we briefly describe the main features of MicroPython. In particular, we examine the documentation relating to the ESP8266, but the concepts can also be extended to the other boards.

The documentation on the ESP8266 soc and MicroPython  is available at the link

ESP8266 MicroPython Documentation

At the link we can find all the informations necessary to implement the code on the card and the features of MicroPython as well as the differences compared to Python.

Here are the main points for coding in MicroPython:

  • MicroPython has a REPL (Read Evaluate Print Loop) console through the serial port to which the device is connected on which we can give the various MicroPython commands interactively.
  • The REPL console can also be accessed via connection to the IP address of the device by enabling the WEBREPL (Web Browser Interactive prompt) console

WebREPL Documentation

importing the webrepl package, enabling it and starting it at each device restart. To access the configured device, we can use the link

WEBREPL Access

or locally cloning it  from github

  • At the start of the device the system runs boot.py and after main.py.
  • The MicroPython libraries are available on the github at the link

MicroPython libraries

  • Additional packages

The additional modules can be downloaded from the link of the libraries presented above or downloaded with the upip utility (Package Manager) from the REPL console if available in the Python Package Index (PyPI) repository

PYPI

as described below

upip

Below is a simple example that connects to the router and allows us to access the MicroPython console with the browser and WEBREPL.

  • boot.py code

At the first access it is required to enable webrepl to be executed in the REPL console with the command

we can activate the service and the access password.

The procedure generates the webrepl_cfg.py file on the device.

After setting up and restarting, we access the web console by opening the locally cloned webrepl.html file with a browser

WebreplPressing connect and after entering the password ,set in the initial setup, we access the console

WebreplConnectedNow we install from the WEBREPL console, for example, the stat module using upip

To paste commands  execute Ctrl+A e Ctrl+V

WebreplupipIn this case the package was taken from the PYPI repository, unpacked and installed on the device.

We have noticed that some modules, although available on the repository, give an error during installation with upip; these modules are installed directly without using the upip utility,but by downloading the library from the site listed above and transferring the code to the device.

Here is the link of the code shown above

Webreplsample

Another option available is to build  the firmware with the required modules.

In the next article we will examine examples similar to what was done with LUA on ESP8266 in previous articles

MicroPython – Nodes examples with DHT11, Relay and MQTT

 

 

 

Micropython – Development tools Ampy – Eric

After the  uPyCraft  intallation in the previous article

MicroPython – IDE MicroPython uPyCraft

let’s now look at other tools available for the development in MicroPython for various platforms.

This article shows the operations to be performed on Windows and Linux.

First Python package to install is Ampy (Adafruit MicroPython Tool)

ampy

The tool is available in the python repositories, so it can be installed using the  pip tool.

In Windos run in a dos command prompt

Similarly in Linux run in a shell

The available commands are listed running

ampyAmpy allows the transfer, creation, viewing, and execution of files on the device.

As for the IDE you can install Eric, generic Python IDE

Eric

Eric does not allow communication from the IDE with the device as seen with uPyCraft; this task is delegated to Ampy. For the installation proceed as follows:

  • For Linux Debian 9 just run

The tool is available among the development tools; at the first execution it requires default settings

ericSettingsWe define the workspace location

ericWorkspaceand we have the access to the IDE

eric

  • For Windows download the zip

Eric

Unzip the zip into a folder from which we run it; install the prerequisites

Perform the eric installation at this point by double clicking on install.py

ericInstallWindowsThe program checks the prerequisites and completes the installation

ericWindowsProcedure

Clicking twice on eric6.py or running eric6 from a dos command prompt we run the program in the same way as seen for Linux Debian

ericWindows

In the next article we will briefly examine how a MicroPython program is structured

MicroPython – MicroPython program structure

 

Micropython – MicroPython IDE uPyCraft

After installing the firmware on the Nodemcu esp8266 DevKit board

MicroPython Firmware

let us now examine a development tool can be suitable for the development of Python code.

A tool that allows both to write code and upload it to the device is uPyCraft

uPyCraft

The tool currently works for Windows and from version 0.30 is also available for Linux; the source code is available at the link

uPyCraft Source

Here are the steps for Windows and Linux.

  • Windows

Once downloaded the package we must  run it as an administrator; the installation of the Monaco font is required

monaco-font

After the font installation,  we have the access to the IDE

upycraft-v0-29We set the Location in Tools->Preferences->Language Location

upycraftlocation

and the serial port paramenters in Tools->Preferences->Serial

upycraftserialsettingsWe plug the device into an USB port and set the serial port on uPyCraft

upycraftserialWe choose the device type; in this case it is ESP8266

upycraftdeviceBy clicking on the connection icon we access to the device and view the files on it

upycraftconnectionThe firmware installation expands on the device only the Python file boot.py

upycraftfilesystemThe tool allows to download examples of MicroPython code for the board, blink.py for example

examplesClicking the “Download and Run” button we load the code onto the device and run it

blinkIn this example the led of the Nodemcu DevKit blinks.

  • Linux

The tool is similar in the Linux version; once downloaded the package to which we have to assign  the execution right we run it. We must assign to the user the access to the serial port with the command, already seen above

In our case, on Debian 9, we have the following screens similar to what we saw on Windows.

In the uPyCraft IDE

upycraft-v0-30linuxWe set the Location in Tools->Preferences->Language Location

upycraftlocationlinux

and the serial port paramenters in Tools->Preferences->Serial

upycraftserialsettingslinuxWe plug the device into an USB port and set the serial port on uPyCraft

upycraftseriallinuxWe choose the device type; in this case it is ESP8266

upycraftdevicelinuxBy clicking on the connection icon we access to the device and view the files on it

upycraftconnectionlinuxThe firmware installation expands on the device only the Python file boot.py

upycraftfilesystemlinuxThe tool allows to download examples of MicroPython code for the board, blink.py for example

exampleslinuxClicking the “Download and Run” button we load the code onto the device and run it

blinklinuxAs in Windows the led of the Nodemcu DevKit blinks..

In version 0.30 for Windows we noticed some bugs, so for this operating system we continue to use the uPyCraft  version 0.29.

Further informations about uPyCraft can be found at the following site

uPyCraft Documentation

In the next article we will discuss another Python development tool

MicroPython – Development tools Ampy – Eric

MicroPython – Python on embedded devices

In this article we begin to treat MicroPython

MicroPython

an implementation of Python3

Python

for embedded devices.

Various images are available on the MicroPython website for different devices, including the modules esp8266 and esp32

MicroPython Dowloads

We start our tests with a Nodemcu esp8266 DevKit

Nodemcu Devkit

We proceed to load the firmware  on this card.

The Micropython site also refers how to load the firmware on the cards; these informations are available at the link

MicroPython Tutorial for esp8266

To load the firmware we always use the tool esptool, also available on the Python repository.

To be able to use it, we need to install Python3 on our development platform. Here are the steps to follow for Windows and Linux.

  • Windows

For windows we must first install the Python3 environment, available at the following link

Python for Windows

In our case, we downloaded the  version 3.6.4 for 64-bit systems. We installed the package  with administrative rights

setuppythonAfter proceeding to add Python in the Path we click “Install Now”

pythonsetupendAfter the installation, we open a dos command propmt and check the installed Python version

pythonversionAlways from the dos command prompt we proceed to the installation of esptool with the command

esptoolinstallationAt this point we can proceed to the MicroPython firmware upload on the Nodemcu esp8266 DevKit. Of course, it is necessary to have the serial-ttl drivers installed for your DevKit model on Windows. Now go into  the folder where the firmware has been downloaded  and run the following  commands from the dos command prompt

erase_flashProceed to load the firmware

firmware_flashOnce the firmware is installed  we log on the Python console using, in our case with the nodemcu card DevKit, the same port already used to load the firmware.

On Windows we can use Putty

Putty

and set the serial connection to 115200 baud rates

repl_putty

  • Linux

For Linux we use the Debian 9 distribution, but the procedure can be extended to other distributions. After installing Debian 9 , similar to version 8

Debian 8

we proceed to the installation of Python3 and esptool from a Linux shell

Add the user used on Debian to the dialout group

In our case, using  sviluppo as a user, the command is as follows

Let’s move on to install the Python esptool package

We can then proceed with the installation of the firmware in the same way as done on Windows.

We check on which serial port the card is available (ttyUSBX)

ttylinuxWe perform a flash erase and firmware upload

To log on the DevKit we use screen

Here is the manual of the tool

Screen Manual

To detach from screen press

instead to resume run

To quit screen

Once connected you get the same result as Windows

screenIn the next article we will begin to see which IDEs can be used to create and load our Python code

MicroPython IDE uPyCraft

 

ESP32 Wifi-Bluetooth Module

In this article we describe briefly the ESP32  chip from Espressif

ESP32

This lower power SOC  contains a 32 bit MCU  with bluetooth and wifi modules. At the Espressif web site we can find the detailed informations about the ESP32

ESP32 Resources

The device datasheet is available in the following pdf document

ESP32 Datasheet

The ESP32 modules are available individually or in a development kits; the last system simplifies its usage as the development kit contains all the hardware to use rapidly the soc device; we are already ready to load the firmware and use the device.

On internet various web sites describe home automation or iot projects using this soc.

A reference site for the ESP32  is

esp32

At above link we can find the various development platforms utilized  for the ESP32. The most relevant  are

  • Arduino Ide

ESP32 Arduino

  • ESP-IDF

ESP32 IDF

  • Micropython

MicroPython

  • PlatformIO

PlatformIO

  • Firmware Lua

LuaNode

Lua-RTOS-ESP32

Nodemcu Development

We can buy the ESP32 modules/dev kits from various internet online websites, for example ebay

ESP32 on ebay

Different ESP32  modules are available from different vendors

Espressif esp-wroom-32

AI-Thinker esp32s

At the link

http://esp32.net/

different hardware configurations available are listed.

As development kits we report

AI-Thinker NodeMCU-32S

DoIT ESP32 Dev Kit

There are development kits based on esp32 that also integrate a LORA wireless  module internally

Lora

We report for example the following

Heltec Wifi LoRa 32

TTGO LoRa32

In the next articles we’ll describe how to build firmwares and use the ESP32 MCU.