0

$USD

$USD
TUTORIALS ESP32MicroPythonESP8266

ESP32 MicroPython Tutorial: Passing arguments to a thread function

DFRobot Oct 12 2017 1973

The objective of this MicroPython Tutorial 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 development board.

Introduction

The objective of this MicroPython Tutorial 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.

Figure 1 – Output of the script.

NOTE: This article is written by Nuno Santos who is an kindly Electronics and Computers Engineer. live in Lisbon, Portugal. you could check the original article here.
He had written many useful tutorials and projects about ESP32, ESP8266, If you are interested, you could check his blog to know more.

DFRobot supply lots of esp32 arduino tutorials and esp32 projects for makers to learn.