Reading HTTP Response on an Embedded system

44 Views Asked by At

I am using MK20 microcontroller with Quectel EC25-E 4G communication module, and I am trying to send and receive HTTP requests to my own server over cellular network. The issue that I am facing is in reading the full HTTP post response.

I am coding on Visual Studio basic and and using only C and C++.

I am able to read part of the response but not the full thing, and how matter I increase the buffer or the char array that I am using to store the response in, I can seam to read the full response.

I am using serial terminal to output the value of the response.

I tried the HTTP POST request on postman and it is working as intended.

The weird thing about it is that it doesnt always ignore the same part. Sometimes it ignores the end of the response and sometimes a middle part of the body of the JSON.

I tried to find a reliable library that can help in solving this issue but with no luck.

I tried changing the size of all the variables that I can change but still didnt work.

This is the code I am using to read the response:

void EC25::HTTP_read_response_JSON(char *buf, size_t buf_len, bool multiline)
{
    size_t counter = 0;
    if (sendCheckReply(F("AT+QHTTPREAD=10"), F("CONNECT"), 10000))
    {
        readline(10000,multiline);

        if (buf != nullptr)
        {
            while (--buf_len)
            {
                buf[counter] = replybuffer[counter];
                //Serial.println(buf);
                //delay(100);
                ++counter;
            }
        }
    }
}

where readline funtction is:

uint8_t EC25::readline(uint16_t timeout, bool multiline)
{
    uint16_t replyidx = 0;

    while (timeout--)
    {
        if (replyidx >= sizeof(replybuffer) - 1)
        {
            break;
        }

        while ((cmSerial->available()) && (replyidx < sizeof(replybuffer) - 1))
        {
            char c = cmSerial->read();
            if (c == '\r') continue;
            if (c == 0xA)
            {
                if (replyidx == 0)   // the first 0x0A is ignored
                    continue;

                if (!multiline)
                {
                    timeout = 0;         // the second 0x0A is the end of the line
                    break;
                }
            }
            replybuffer[replyidx] = c;
            replyidx++;
        }

        if (timeout == 0)
        {
            break;
        }
        vTaskDelay(pdMS_TO_TICKS(1));
    }
    replybuffer[replyidx] = 0;  // null term
    Serial.println(replybuffer);
    return replyidx;
}

If you need more information please let me know, this is my first question so apoligies for any messing info.

0

There are 0 best solutions below