ESP 32 multitasking

Hi,
I use esp32 which is connected to the firebase database in the project I have several functions such as checking the status of PIR and UV sensors and sending their status to the database, I also use temperature and soil moisture sensors and send data with measurements every minute, also uses a mini servo motor and led strip which are controlled from the database. I have a question, can I somehow increase the speed of the program use FreeRTOS or make state machine? The loop from the program is below

not sure what you mean. Do you feel the code is slow and if so, have you investigated where time is spent? the loop's code does not help much see what's going on

Most of the time takes checking and sending status to firebase , sending or getting string takes about 1 second, for example during getting status from firebase (led_strip()), UV sensor change status (led_uv) and before it sends the status it has to wait 3 seconds because I have 2 function before it (mini_servo() and ledonoff())

Put the firebase stuff in a separate FreeRTOS task that will run "in parallel" with the loop() cycle.

Check documentation for the instruction xTaskCreate()

why should these other functions delay reading data? wouldn't the code simply read input until a complete msg(?) is received?

consider


byte pinsLed [] = { 10, 11, 12, 13 };
#define N_LED   sizeof(pinsLed)

// -----------------------------------------------------------------------------
void
tglLed12 (void)
{
    digitalWrite (12, ! digitalRead (12));
}

void
tglLed13 (void)
{
    digitalWrite (13, ! digitalRead (13));
}

// -----------------------------------------------------------------------------
struct Action {
    unsigned long   period;
    void(*func)(void);
    const char *    desc;
    unsigned long   msecLst;
};

Action actions [] = {
    { 500, tglLed12 , "tglLed12" },
    { 333, tglLed13 , "tglLed13" },
};
#define N_ACT   (sizeof(actions)/sizeof(Action))

void
actChk (void)
{
    unsigned long msec = millis ();

    Action *act = actions;
    for (unsigned n = 0; n < N_ACT; n++, act++)  {
        if (msec > act->msecLst)  {
         // Serial.println (act->desc);
            act->msecLst = msec + act->period;
            act->func ();
        }
    }
}

// -----------------------------------------------------------------------------
void
msgProcess (
    char  *buf,
    int    nChar )
{
    Serial.println (buf);
}

void
msgChk (void)
{
    static char buf [80];
    static int  bufIdx;

    if (Serial.available ())  {
        char c = Serial.read ();

        if ('\n' == c)  {
            buf [bufIdx] = 0;
            msgProcess (buf, bufIdx);
            bufIdx = 0;
        }
        else
            buf [bufIdx++] = c;
    }
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    actChk ();
    msgChk ();
}

void
setup (void)
{
    Serial.begin (9600);

    for (unsigned n = 0; n < N_LED; n++)  {
        digitalWrite (pinsLed [n], HIGH);       // off
        pinMode      (pinsLed [n], OUTPUT);
    }
}

Basically, yes.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.