# Thingspeak connection code question

**URL:** <https://forum.arduino.cc/t/thingspeak-connection-code-question/1408438>\
**Category:** Programming\
**Created:** [October 5, 2025, 4:19am UTC](https://forum.arduino.cc/t/thingspeak-connection-code-question/1408438 "2025-10-05T04:19:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![brice3010](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/brice3010/32/912680_2.png) [@brice3010](https://forum.arduino.cc/u/brice3010)\
**Post date:** [October 5, 2025, 4:19am UTC](https://forum.arduino.cc/t/thingspeak-connection-code-question/1408438/1 "2025-10-05T04:19:44Z")

</div>

I use Thingspeak without its library, with following code. The code is working for years but I recently found out there are a few things that need correction.

1. is a delay needed after client.stop() ? How much ?
2. is it advisable (or not) to use client.flush() before client.stop() ?
3. is the use of \r and \n correct in my code ?
4. should I use `client.print(postStr.length());` or `client.print(String(postStr.length()));` ?

My code:

```arduino
    if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
    {
      String postStr = apiKey;
      postStr += "&field1=";
      postStr += String(valveState);
      postStr += "&field2=";
      postStr += String(hiLvlState);
      postStr += "&field3=";
      postStr += String(loLvlState);
      postStr += "&field4=";
      postStr += String(valveDelayedClosureExecution);
      postStr += "&field5=";
      postStr += String(alarmState);
      postStr += "&field6=";
      postStr += String(get3231Temp());
      postStr += "&field7=";
      postStr += String(valveOpenTime);
      postStr += "&field8=";
      postStr += String(waterUsage);
      postStr += "\r\n\r\n";

      client.print("POST /update HTTP/1.1\n");
      client.print("Host: api.thingspeak.com\n");
      client.print("Connection: close\n");
      client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
      client.print("Content-Type: application/x-www-form-urlencoded\n");
      client.print("Content-Length: ");
      // client.print(String(postStr.length()));
      client.print(postStr.length());
      client.print("\n\n");
      client.print(postStr);
      Serial.println(F("Thingspeak executed"));
      delay(4000);
    }
    client.flush();
    client.stop();
    delay(5):

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 3, 2026, 4:20am UTC](https://forum.arduino.cc/t/thingspeak-connection-code-question/1408438/2 "2026-04-03T04:20:29Z")

</div>

Closed on April 3, 2026, 4:20am UTC

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