2

Can I program my ESP32 to send a string MQTT data while also blinking the InBuilt LED?

If yes, then how to do so

Thanks in advance

3
  • Sure you can. You can use something like the BlinkWithoutDelay example included in the Arduino IDE so that your program can run (for example sending MQTT data) while the LED is turned on and off with the correct frequency. As long as sending the MQTT data does not take longer than the on- or off period of the LED, this will work. – StarCat Jun 22 at 14:27
  • Take a look at the blink-without-delay example to get an idea of how "multitasking" can be implemented. But as @HigorMaiaConcessa mentioned, the ESP supports FreeRTOS where you don't have to care much about how it works. – Sim Son Jun 23 at 18:14
  • 1
    My opinion is that using multiple cores for extremely simple tasks such as blinking an LED is unnecessarily complex. – StarCat Jun 24 at 10:08
1

The ESP32 have two core, each core can do diferents tasks. Then, you can send data with one core and blink the LED with another core.

To do this, you need to use a multicore programming. I recommend to use the FreeRTOS (ESP32 support this). See more.

Look the example "Let's multitask: Blink with Hello" here, is that what you want.

Read the documentation about the FreeRTOS in ESP32.

Example in Arduino IDE:

#include <Arduino_FreeRTOS.h>

// define two tasks for Blink & AnalogRead
void TaskBlink( void *pvParameters );
void TaskAnalogRead( void *pvParameters );

// the setup function runs once when you press reset or power the board
void setup() {

  // Now set up two tasks to run independently.
  xTaskCreate(
    TaskBlink
    ,  (const portCHAR *)"Blink"   // A name just for humans
    ,  128  // Stack size
    ,  NULL
    ,  2  // priority
    ,  NULL );

  xTaskCreate(
    TaskAnalogRead
    ,  (const portCHAR *) "AnalogRead"
    ,  128 // This stack size can be checked & adjusted by reading Highwater
    ,  NULL
    ,  1  // priority
    ,  NULL );

  // Now the task scheduler, which takes over control of scheduling individual tasks, is automatically started.
}

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;

  // initialize digital pin 13 as an output.
  pinMode(13, OUTPUT);

  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
    digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    vTaskDelay( 1000 / portTICK_PERIOD_MS ); // wait for one second
  }
}

void TaskAnalogRead(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);

  for (;;)
  {
    // read the input on analog pin 0:
    int sensorValue = analogRead(A0);
    // print out the value you read:
    Serial.println(sensorValue);
    vTaskDelay(1);  // one tick delay (15ms) in between reads for stability
  }
}

Reference.

5
  • Thank you for the help. Are there any Arduino IDE Examples available, since it is more understandable in it – P_K Jun 24 at 15:48
  • Look this example. I added the example in the answer. – Higor Maia Concessa Jun 24 at 16:16
  • While true, it seems like a large increase in complexity for a relatively low gain (and there are multiple task schedulers for Arduino that don't involve an RTOS). It's also my understanding that unless tasks are specifically pinned to a core there's no guarantee regarding multicore-ness. – Dave Newton Jun 24 at 16:29
  • So I guess giving same priority to 2 small tasks will make them execute simultaneously. Am I right? – P_K Jun 28 at 13:12
  • If run in diferents cores, then will execute simultaneously. If run in the same core with the same priority, will run merging each one. – Higor Maia Concessa Jun 28 at 13:17

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.