I have an arduino which is using a TCPClient to post JSON messages to a web api. First, I have pasted the code below to see if people have general comments about the method I am using to do so. I am always listening for improvements as I am no expert.

My question specifically though is how do I listed for the server response code? In my code below, I tried to implement several different ways but this one seems to be the most common. However, I am not getting anything.

The goal again is to send the message to the server every xx seconds, which I am simply controlling by using a delay since this is the one and only task for this device.


void loop(){
  if(client.connect(server, tcpServerPort))

    client.println("POST /BoxLevel/api/BoxMeasurement HTTP/1.0");
    client.println("Content-Type: application/json; charset=utf-8");
    client.println("Connection: close");

        char c = client.read();

  • 2
    Sorry but its difficult to help you there isn't really enough information. I don't know what library you are using, the full code is missing, so you might have failed to set the IP address of client, or client might be a Bluetooth module. Don't even know what board you are running on, an Uno or a new fangled thing with Wifi built in. – Code Gorilla Jun 20 '17 at 11:57
  • @Code Gorilla thank you for the response. I don't have full code complete so not much more to share. I do think there is enough information for help. I did mention that I am using TCPClient, no special libraries are imported. I don't know what it matters which board it runs on, as long as it has network/internet capability and access (assume it does) it is a TCP connection which is pretty universal. Last, I can say that my outbound messages are working fine, I am getting data on my server with the client.print (json) so I know I am good and should be getting a 200 OK response. – Michael Bedford Jun 20 '17 at 16:02
  • 2
    What do you mean, you don't have the full code? What's in contentLenBuf? Your request looks malformed. Most likely issue is you aren't waiting long enough for the server to reply and even if you do get a reply immediately, you're only printing one character of it before closing the connection. – SoreDakeNoKoto Jun 20 '17 at 18:31
  • @TisteAndii Sorry, I think you misunderstood, I said I don't have full code complete. Meaning, it is not complete and is a mess. I made a decision (I guess the wrong one) to paste just the code I am having an issue with so as to avoid additional confusion that the incomplete parts of my code may cause or off topic comments about the rest of my code. Full code, in my opinion, isn't always necessary to give an answer or a clue but seems to be the default response these days before anybody will even help. That said, thank you for the response and the clues. I will look into those more. – Michael Bedford Jun 20 '17 at 18:37
  • 2
    I only ask for the full code when it seems relevant. In this case, I dont know whats in contentLenBuf, which you've made a part of your request. If the contents are not "Content-Length: ..." then you may have sent a Bad Request and you probably wont get 200 OK. – SoreDakeNoKoto Jun 20 '17 at 18:47

Assuming your request passed muster, the right way to wait for a response is:

#define TIMEOUT 2000 // at the top of your sketch

Serial.println("Server Response:");

uint32_t lastRead = millis();
while (millis() - lastRead < TIMEOUT){
  while (client.available()){
    lastRead = millis();

This ensures that the socket is polled for incoming data till 2 seconds (a reasonable amount of time) pass with no new bytes, at which point the client concludes theres nothing else to be read.

Not the answer you're looking for? Browse other questions tagged or ask your own question.