ESP32 MicroPython: Passing arguments to a thread function

The objective of this post is to explain how to pass parameters to a thread function, on MicroPython running on a ESP32. The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board.

 

Introduction

The objective of this post is to explain how to pass parameters to a thread function, on MicroPython. For an introduction on creating threads, please check this previous post.

The tests were performed using a DFRobot’s ESP-WROOM-32 device integrated in a ESP32 FireBeetle board. The MicroPython IDE used was uPyCraft.

 

The code

The code for this tutorial will be very similar to the previous one where we introduced MicroPython threads. So we start by importing the _thread module, for having available the thread related functionality.

import _thread

Next we will declare our thread function. In this case, we will specify that it receives as input two arguments. For this example, the first argument will be a thread function description and the second will be a counter.

def threadFunction(description, count):
## Thread function code

Our thread function logic will be very simple. We will first print the description argument and then do a loop printing a message. The number of iterations of the loop will be equal to the counter that is the second argument of our function.

def threadFunction(description, count):

  print(description)

  i = 0

  while i < count:
    print("Iteration: " + str(i) )
    i=i+1

Remember from the previous post that the thread will exit when the function returns.

Finally we will start our thread by calling the start_new_thread function of the _thread module. As first argument it receives the thread function we defined early and as second it receives a tuple with the arguments of the function.

So, as defined, the first element of the tuple will be a description string and the second a counter. In this case I’m using 5 as counter and thus I will obtain 5 iterations on the loop, but you can use another value.

_thread.start_new_thread(threadFunction, ("Thread test function", 5))

The final complete source code for the script can be seen below.

import _thread

def threadFunction(description, count):

  print(description)

  i = 0

  while i < count:

    print("Iteration: " + str(i) )
    i=i+1

_thread.start_new_thread(threadFunction, ("Thread test function", 5))


Testing the code

To test the code, simply upload it to your ESP32 and run it. Upon execution, you should get an output similar to figure 1 on the MicroPython console, which shows the both the description string and the output of the iterations of the loop being printed. As expected, the thread function performs 5 iterations on the loop, printing a message for each one.

ESP32 MicroPython passing arguments to thread.png

Figure 1 – Output of the script.


Related posts

Advertisements
This entry was posted in ESP32 and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s