I have designed a small project in which i have two sensor nodes(stm32) with each sensing temp, humidity and soil moisture and actuating the relay based on some threshold of moisture value. These sensor nodes are transmitting sensed data to base station(Rpi) through zigbee and UART. Also the Rpi can send some integer value based on which sensor nodes will change their transmission rate (delay between two transmission). Now I want to implement this system on an RTOS based system. My question is what should be different tasks (should I give different tasks for each temp, humidity and moisture or only one task for all sensors)? What priority i should assign to the different tasks?


1 Answer 1


I'd suggest to keep the task priority at the same level unless you have a task that really needs to be prioritised (shouldn't be the case in this application). Splitting your tasks(code) into different threads(RTOS tasks) has two main benefits

  1. Parallelisation (of course if you have just 1 core this is just pseudo parallel) can help to smooth your system. E.g. if you have some time consuming calculations and a display showing some live values it makes sense to split these tasks into separate threads to have the display updated frequently.
  2. Program Structure (Maintainablitly) that means if you have a large system performing various tasks it's really helpful to completely separate them (e.g. calculations, communications, display)

But having different tasks has also a downside. One is the context switch that consumes process time that means if you have too many tasks your processor is spending more time in switching between the separate tasks than actually perform the task. The other is predictability due to parallelisation compared to a strict sequential code.

So to come back to your specific example I'd not create more than two tasks. One handling the sensor data of all sensors and do whatever has to be done with it and the other task handling the communication to have these two things separated.

  • \$\begingroup\$ So like actuation can be done in first task itself? i thought of making actuation a separate task along with two tasks for transmitting and sensing data! also should i make synchronization between different sensors data (eg. apply barrier synchronization to wait for all sensor data then only transmit). \$\endgroup\$ Apr 26, 2019 at 9:33
  • \$\begingroup\$ What do you mean by actuation in this context? As these sensors are measuring slow changing values I don't see any benefit in synchronizing data, I'd rather prefer a fix sending interval and just send what ever information I have at that point. \$\endgroup\$
    – po.pe
    Apr 26, 2019 at 9:46
  • \$\begingroup\$ actuating a relay i mean ! Should i need to use semaphore or mutex to use UART buffer(as sensor task is putting data into buffer and communication task is transmitting that data) to ensure only current updated sensed value is transmitted ! please suggest further thanks \$\endgroup\$ Apr 26, 2019 at 10:10
  • \$\begingroup\$ If you have critical code sections you need to protect them but a buffer shouldn't be a problem. \$\endgroup\$
    – po.pe
    Apr 26, 2019 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.