Search code examples
arduinoembeddedesp8266arduino-esp8266

HTTP GET request using Arduino and ESP8266


I'm trying to make a HTTP GET Request using arduino Mega 2560 with ESP82660, but it's always restarting my esp8266.

this is my approach:

    char *hello = "GET /api/bus/register?code=";
    strcat(hello,test);
    strcat(hello," HTTP/1.1\r\nHost: 192.168.88.233\r\nConnection:close\r\n\r\n");

    wifi.send((const uint8_t*)hello, strlen(hello));
    uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
    if (len > 0) {
    Serial.print("Received:[");
    for(uint32_t i = 0; i < len; i++) {
        Serial.print((char)buffer[i]);
    }
    Serial.print("]\r\n");
    }

Solution

  • You need to allocate a buffer large enough to hold your entire concatenated string, e.g:

    char hello[256];
    strcpy(hello, "GET /api/bus/register?code=");
    strcat(hello, test);
    strcat(hello," HTTP/1.1\r\nHost: 192.168.88.233\r\nConnection:close\r\n\r\n");
    

    (The above code is still not safe, you will also need to check the size of test or use strncat)

    The way you are doing it will cause an array overflow and possible memory corruption. This might be the cause of resetting your ESP82660, if corrupted data is sent with wifi.send.

    Also you might need to strcat a newline after test if there isn't one there already.