Micropython Esp32

Posted onby admin

George gave this link in #4589 but it's dead. There is a esp32-bluetooth.bin at the micropython download page but with no info about it. MakePython ESP32 is a powerful, generic Wifi+ BLE MCU module target to MicroPython learners & developers. It is true “breadboard compatible”, with the very detailed pins explanation, it is easy to learn & use. The MakePython ESP32 is programmed with MicroPython by default, users can begin the MicroPython developing as getting then on hand. MicroPython: Wi-Fi Manager with ESP32 (ESP8266 compatible) In this tutorial we’ll show you how to use Wi-Fi Manager with the ESP32 using MicroPython firmware. Wi-Fi Manager allows you to connect your ESP32 to different Access Points (different networks) without having to hard-code your credentials and upload new code to your board.

  1. Micropython Esp32 Tutorial Pdf
  2. Micropython Esp32 Cam
  3. Micropython Esp32-c3

The objective of this post is to explain how to parse a JSON string with MicroPython running on the ESP32.


Introduction

The objective of this post is to explain how to parse a JSON string with MicroPython running on the ESP32. If you need help setting MicroPython on the ESP32, please check this previous post for a detailed guide. The guide also explains how to connect to the Python prompt.

In order to parse a JSON string, we will use the MicroPython uJSON library. You can also check the documentation of the library at Github.

Esp32

Since we are going to use the command line for testing the code, we will need a tool to help us compress the JSON content in a single line, so we can easily paste it. So, we will use this website, which can receive a JSON string and compress it to a single line. Then, we can past it to Putty just by copying the JSON and right clicking on the terminal window.


The code

After connecting to the Python prompt, we are ready to start coding. So, the first thing we need to do is importing the uJSON module. Just type the expression bellow and hit enter.

After that, we should have access to the ujson object, which makes available a method called loads. This method receives as input the JSON string and returns an object corresponding to the parsed JSON [1]. We will start with a simple JSON structure, which is the one shown bellow.

After compressing it to a one liner, we get the JSON string shown bellow.

So, we will now pass this string as input of the previously mentioned loads method. Note that we will enclose the string between “”” in each side, in order to escape the quotes from the JSON structure. We will store the object in a variable called parsed.

Now, we will confirm that we have the content of the JSON correctly in our returning object. So we will print it. Additionally, we will print the type of the object with the type function.

After running the whole previous code, we should get an output similar to figure 1. Note that the type of our object with the parsed content is a Python dictionary, making it perfect for accessing the content in a key-value style.

Figure 1 – Parsing the JSON string.

Now, we will access the value for the key equal to “name”. It should return “John”. In order to access the value for that key in the dictionary, send the command bellow. Note that it is like accessing an array value but instead of using an index, we use a key, in the format of a string.

You should get an output similar to figure 2. Note that the name “John” is printed on the console.

Figure 2 – Accessing the parsed values of the dictionary object.

To finalize our example, we will now parse a more complex structure, as shown bellow. This could represent, for example, a message sent from an IoT device.

Micropython Esp32 Tutorial Pdf

Micropython Esp32

After compressing, we get the following:

So, we will now parse it and print all the keys existing in our JSON structure. We will also print the type of the “values” structure, to understand how it is mapped by the parser.

Micropython Esp32

You should get a result similar to figure 3. As can be seen, all the values for each key are printed correctly. In the case of the “values” key, the structure inside the dictionary is a list, rather than a string representation of the values. Naturally, this is much better since we can operate over those values with all the functions available for lists, making them easier to manipulate.

Micropython Esp32 Cam

Figure 3 – Result of the JSON content parsed.

Micropython Esp32-c3


References

[1] https://docs.micropython.org/en/latest/pyboard/library/ujson.html