Including Library error

Hi,

I am trying to use an LCD Display. For that I need two include two more librarys: Wire and the Grove LCD RGB Backlight library.

I have a webserver running on the Arduino and a external Javascript backend, that returns some paramters.

When I run the code without including one these librarys above the whole code works fine and I get the data I want. Here is what will gets displayed in the Monitor:

parsing "from=Zurich-HB&to=Bern&lastOne=false" produced 3 parameters:
param 0 name "from", param "Zurich-HB".
param 1 name "to", param "Bern".
param 2 name "lastOne", param "false".
Json Body: {"from":"Zurich-HB","to":"Bern","lastOne":"false"}
making POST request
Status code: 200
Response: {"from":"Zürich HB","to":"Bern","gleis":15,"lateInMinutes":0,"departure":"18:53","waggonsCount":19,"vias":0}
To Request: Bern

But after I put either the Wire or the Grove - LCD Library in the code stops working at one point. The Json document is not complete, if I compare it with the other from above. I can show that, what is displayed in the Monitor:

parsing "from=Zurich-HB&to=Bern&lastOne=false" produced 3 parameters:
param 0 name "from", param "Zurich-HB".
param 1 name "to", param "Bern".
param 2 name "lastOne", param "false".
Json Body: {"from":"Zurich-HB","to":"Bern"
making POST request

Here is my whole code:

#include <Wire.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <ArduinoJson.h>
#include <ArduinoHttpClient.h>
#include <stdio.h>
#include <string.h>

char ssid[] = "iPhone 14 Pro von Andrin"; // your network SSID (name)
char pass[] = "12345678"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;          // your network key index number (needed only for WEP)

char serverAddress[] = "172.20.10.2"; // server address from Wifi. You can look up this adress in the settings
int port = 8080;
WiFiServer server(80);

DynamicJsonDocument postMessage(1048);
String jsonBody;
String contentType = "application/json";

DynamicJsonDocument doc(900);

WiFiClient wifi;
HttpClient http = HttpClient(wifi, serverAddress, port);

char webpage[] = R"=====(
<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SBB Fahrplan</title>
</head>

<body>
    <script>
        const handleSubmit = () => {
            const radioButtons = document.querySelectorAll('input[name="zug"]');

            const abfahrt = document.getElementById('abfahrt').value
            const ankunft = document.getElementById('ankunft').value
            
            let selectedButton;

            for (const radioButton of radioButtons) {
                if (radioButton.checked) {
                    selectedButton = radioButton.value;
                    if (selectedButton === 'false') {
                        selectedButton = false
                    }

                    if (selectedButton === 'true') {
                        selectedButton = true
                    }
                }
            }

            var myHeaders = new Headers();
            myHeaders.append("Content-Type", "application/json");

            var raw = JSON.stringify({
                from: abfahrt,
                to: ankunft,
                lastOne: selectedButton
            });

            var requestOptions = {
                method: 'POST',
                headers: myHeaders,
                body: raw,
                redirect: 'follow'
            };

            fetch("http://localhost:8080/getDataByTrip", requestOptions)
                .then(response => response.json())
                .then(result => console.log(result)) 
                .then(fetch(`http://172.20.10.8/H?from=${abfahrt}&to=${ankunft}&lastOne=${selectedButton}`, {
                    method: "GET"
                }))
        }
    </script>
    <h2>SBB Fahrplan</h2>

    <label for="abbh">Abfahrts Bahnhof:</label>
    <input type="text" id="abfahrt" name="Abfahrts Bahnhof" placeholder="Abfahrts Bahnhof">

    <label for="anbh">Ankunfts Bahnhof:</label>
    <input type="text" id="ankunft" name="Ankunfts Bahnhof" placeholder="Ankunfts Bahnhof">

    <input type="radio" id="nzug" name="zug" value="false">
    <label for="nzug">nächster Zug</label>
    <input type="radio" id="lzug" name="zug" value="true">
    <label for="lzug">letzter Zug</label>
    <br><br>
    <button onclick="handleSubmit()">ok</button>

</body>

</html>
)=====";

void printWifiStatus()
{
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());

    // print your board's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);

    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
    // print where to go in a browser:
    Serial.print("To see this page in action, open a browser to http://");
    Serial.println(ip);
}

void percentDecode(char *src)
{
    char *dst = src;

    while (*src)
    {
        if (*src == '+')
        {
            src++;
            *dst++ = ' ';
        }
        else if (*src == '%')
        {
            // handle percent escape

            *dst = '\0';
            src++;

            if (*src >= '0' && *src <= '9')
            {
                *dst = *src++ - '0';
            }
            else if (*src >= 'A' && *src <= 'F')
            {
                *dst = 10 + *src++ - 'A';
            }
            else if (*src >= 'a' && *src <= 'f')
            {
                *dst = 10 + *src++ - 'a';
            }
            
            *dst <<= 4;

            if (*src >= '0' && *src <= '9')
            {
                *dst |= *src++ - '0';
            }
            else if (*src >= 'A' && *src <= 'F')
            {
                *dst |= 10 + *src++ - 'A';
            }
            else if (*src >= 'a' && *src <= 'f')
            {
                *dst |= 10 + *src++ - 'a';
            }

            dst++;
        }
        else
        {
            *dst++ = *src++;
        }
    }
    *dst = '\0';
}

int parseUrlParams(char *queryString, char *results[][2], int resultsMaxCt, boolean decodeUrl)
{
    int ct = 0;

    while (queryString && *queryString && ct < resultsMaxCt)
    {
        results[ct][0] = strsep(&queryString, "&");
        results[ct][1] = strchrnul(results[ct][0], '=');
        if (*results[ct][1])
            *results[ct][1]++ = '\0';

        if (decodeUrl)
        {
            percentDecode(results[ct][0]);
            percentDecode(results[ct][1]);
        }

        ct++;
    }

    return ct;
}

int status = WL_IDLE_STATUS;

void setup()
{ 
    Serial.begin(9600); // initialize serial communication
    pinMode(9, OUTPUT); // set the LED pin mode
    
    // check for the WiFi module:
    if (WiFi.status() == WL_NO_MODULE)
    {
        Serial.println("Communication with WiFi module failed!");
        // don't continue
        while (true)
            ;
    }

    String fv = WiFi.firmwareVersion();
    if (fv < WIFI_FIRMWARE_LATEST_VERSION)
    {
        Serial.println("Please upgrade the firmware");
    }

    // attempt to connect to WiFi network:
    while (status != WL_CONNECTED)
    {
        Serial.print("Attempting to connect to Network named: ");
        Serial.println(ssid); // print the network name (SSID);

        // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
        status = WiFi.begin(ssid, pass);
        // wait 10 seconds for connection:
        delay(10000);
    }
    server.begin();    // start the web server on port 80
    printWifiStatus(); // you're connected now, so print out the status
}

char url[100];

boolean done = false;

char responseCopy = "";

void loop()
{
  
    WiFiClient client = server.available(); // listen for incoming clients

    char buf[100];
    char *params[5][2];

    if (client)
    {                                 // if you get a client,
        Serial.println("new client"); // print a message out the serial port
        String currentLine = "";      // make a String to hold incoming data from the client
        String httpGetUrl = "";
        while (client.connected())
        { // loop while the client's connected
            if (client.available())
            {                           // if there's bytes to read from the client,
                char c = client.read(); // read a byte, then
                                        // Serial.write(c);                    // print it out the serial monitor
                if (c == '\n')
                { // if the byte is a newline character

                    // if the current line is blank, you got two newline characters in a row.
                    // that's the end of the client HTTP request, so send a response:
                    if (currentLine.length() == 0)
                    {
                        // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
                        // and a content-type so the client knows what's coming, then a blank line:
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type:text/html");
                        client.println();

                        // the content of the HTTP response follows the header:
                        // client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
                        client.print(webpage);

                        // The HTTP response ends with another blank line:
                        client.println();
                        // break out of the while loop:
                        break;
                    }
                    else
                    { // if you got a newline, then clear currentLine:
                        currentLine = "";
                    }
                }
                else if (c != '\r')
                {                     // if you got anything else but a carriage return character,
                    currentLine += c; // add it to the end of the currentLine
                }
                else
                {

                    if (currentLine.startsWith("GET /H?from="))
                    {

                        httpGetUrl += currentLine;

                        httpGetUrl.remove(0, 7);
                        int http = httpGetUrl.indexOf(" HTTP/1.1");
                        httpGetUrl.remove(http, 9);

                        httpGetUrl.toCharArray(url, 100);

                        char *tests[] = {
                            url,
                        };

                        for (int i = 0; i < sizeof(tests) / sizeof(*tests); i++)
                        {
                            Serial.print("parsing \"");
                            Serial.print(tests[i]);

                            // copy test[i] into the buffer
                            // because the parser overwrites what is i the string it is passed.
                            strcpy(buf, tests[i]);

                            // parse the buffer into params[][]
                            int resultsCt = parseUrlParams(buf, params, 5, true);

                            // print off the results;

                            Serial.print("\" produced ");
                            Serial.print(resultsCt);
                            Serial.print(" parameters:");
                            Serial.println();

                            for (int i = 0; i < resultsCt; i++)
                            {
                                Serial.print("param ");
                                Serial.print(i);
                                Serial.print(" name \"");
                                Serial.print(params[i][0]);
                                Serial.print("\", param \"");
                                Serial.print(params[i][1]);
                                Serial.print("\".");
                                Serial.println();
                                postMessage["from"] = params[0][1];
                                postMessage["to"] = params[0][3];
                                postMessage["lastOne"] = params[0][5];
                            }

                            serializeJson(postMessage, jsonBody);

                            Serial.print("Json Body: ");
                            Serial.println(jsonBody);

                            Serial.println("making POST request");

                            done = true;
                        }
                    }
                }

                // Check to see if the client request was "GET /H" or "GET /L":
                //  if (currentLine.endsWith("GET /H")) {
                while (done)
                {
                    http.post("/getDataByTrip", contentType, jsonBody);

                    // read the status code and body of the response
                    int statusCode = http.responseStatusCode();
                    String responseRequest = http.responseBody();

                    Serial.print("Status code: ");
                    Serial.println(statusCode);
                    Serial.print("Response: ");
                    Serial.println(responseRequest);

                    DeserializationError error = deserializeJson(doc, responseRequest);

                    if (error) {
                      Serial.print(F("deserializeJson() failed: "));
                      Serial.println(error.f_str());
                      return;
                    }

                    const char* from = doc["from"];
                    const char* toTrain = doc["to"];
                    int gleis = doc["gleis"];
                    int lateInMinutes = doc["lateInMinutes"];
                    const char* departure = doc["departure"];
                    int waggonsCount = doc["waggonsCount"];

                    Serial.print("To Request: ");
                    Serial.println(toTrain);

                    jsonBody = "";
               
                    
                    if (responseRequest.length() > 10)
                    {
                        done = false;
                    }
                    else
                    {

                        done = false;
                    }
                    responseRequest = "";
                }
                //}
            }
        }
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
    }
}

which arduino are you using?

Arduino UNO Wifi Rev2

There is only 6KB of SRAM on this, are you trying to use too much?

I have not had the time to go though all the code, but I do see a few odd things:

You are clearly going out of range on the 2nd index value, although it does still fall within the actual array.

    char *params[5][2];
   ...
   ...
   ...

                                postMessage["from"] = params[0][1];
                                postMessage["to"] = params[0][3];
                                postMessage["lastOne"] = params[0][5];
                            }

This creates a single-element array. Not sure why tests is being declared as an array unless you were planning on future expansion.
sizeof(tests)/sizeof(*tests) is very unusual, more common would be sizeof(tests)/sizeof(tests[0])


                        char *tests[] = {
                            url,
                        };

                        for (int i = 0; i < sizeof(tests) / sizeof(*tests); i++)
                        {

Does just including the Grove library cause the problem, without adding any actual code that uses the library?
Can you successfully run the examples from the library on the UNO WiFi rev2? There is a possibility the library is not compatible with that board.

It’s quite frequent actually (the parenthesis are not needed)

Shows my lack of experience, I've just never seen it in code posted here on the forum, although there are probably lots of times I've just not noticed. As for the parenthesis, I'm aware that they are optional, but personally I find it easier to read with the parenthesis.

Alright thanks for the answer. I will try it again. But yes it does fail, when I just include the library at the top. Without any code for that library.

And yes the example sketch of that library works perfectly fine

Though I had posted this earlier in the day, but must have lost the wifi connection on my phone.

Does it help if you declare webpage as const char? With the amount of memory you are allocating with DynamicJsonDocument, and the use of String for jsonBody and contentType, this may well be a memory problem.

char webpage[] = R"=====(

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