1

I use this simple HTTP Post request to post some values on my API from Arduino using Switch. each time I push the switch it send a different values to API.

void switch_led() {
bool SwitchReading = digitalRead(SWITCH);
if (SwitchReading == HIGH) {
Serial.println("Now pressed....");
state = state + 1;
if (state % 2 != 0) {
  digitalWrite(LED, HIGH);
  char data1[] = "{""LDRValue"": 88888888}";
  Serial.println("\nStarting connection to led server...");
  if (client.connect(server, 8040)) {
    Serial.println("connected to post server");
    client.println("POST /api/sensor HTTP/1.1");
    client.println("Host: 192.168.2.116:8040");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(sizeof(data1));
    client.println();
    client.print(data1);
    Serial.println("1.finish");
  }
  Serial.println("2.finish");
}
else {
  digitalWrite(LED, LOW);
  char data2[] = "{""LDRValue"": 1111111111}";
  Serial.println("\nStarting connection to led server...");
  if (client.connect(server, 8040)) {
    Serial.println("connected to post server");
    client.println("POST /api/sensor HTTP/1.1");
    client.println("Host: 192.168.2.116:8040");
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(sizeof(data2));
    client.println();
    client.print(data2);
    Serial.println("3.finish");
  }
  Serial.println("4.finish");
}
delay(500);
   }
}

It works perfect for the first and second posts in both Arduino and API but at the third post it works fine on Arduino but it takes long time on the API(about 2 minutes to get the post). The fourth and the fifth posts also works fine but the sixth is same as the third. Knowing that my API works 100% with Raspberry Pi and it has no errors.

The same thing for PUT and GET.

After Edition: After I remove the port number from the host line and using strlen(data)instead of sizeof(data), the POST request works but only for first 7th posts. The output of serial monitor on Arduino is like that:

Attempting to connect to WPA SSID: WiFi-Repeater1

WiFi connected
SSID: WiFi-Repeater1
IP Address: 192.168.2.149
signal strength (RSSI):-66 dBm

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...
connected to post server

Starting connection to post server...

Starting connection to post server...

Starting connection to post server...

Before the Editing, it was always giving me "connected to post server" and It always posts but as I told you before It takes a lot of time at the 3rd 6th 9th 12th ..... Now after the 7th post it will not posts.

  • Escape the quotes in your strings. Don't include the port number in the HTTP host header. Use strlen() instead of sizeof(). – TisteAndii Jun 14 '16 at 3:26
  • @TisteAndii which quotes to escape ? – Bilal Sehwail Jun 14 '16 at 7:00
  • @TisteAndii please see the output in the question – Bilal Sehwail Jun 14 '16 at 7:10
0

I THINK the problem might be the client.println() statement. I believe REST messages should be terminated with a \r\n and I think println just does \n. So if you change it to client.println("\r") it should start working on every message.

Also you might want to consider moving creating the message into a function to reduce the amount of code you have.

  • It doesn't work. – Bilal Sehwail Jun 13 '16 at 12:33
  • No it doesn't because it should be client.println(sizeof(data2)); client.println(); client.print(data2); client.println("\r"); I misread that you hadn't added the new line at the end of the message. – Code Gorilla Jun 13 '16 at 12:37
  • It works but the same problem appears. First 2 posts works fine the 3rd needs a lot of time, and then 4th and 5th are ok, 6th same as 3rd. – Bilal Sehwail Jun 13 '16 at 12:47
  • 3rd, 6th, 9th, .......and so on are not working well. But all the other posts works 100% – Bilal Sehwail Jun 13 '16 at 12:48
  • The pattern that is happening sound like it may be a leak of some sort, or a lack of resources. You are not leaking memory. Do you need to disconnect or call a function to mark that you have finished? Could it be that the data1 and data2 values need Null terminating and extra space to allow for that? What does sizeof (data2) actually return, is it right, would strlen(data2) be better? Is your server expecting 8 byte characters or UTF-16, should you message define that? – Code Gorilla Jun 13 '16 at 15:24
0

I figure out the problem.

To solve the session between Arduino and the server must ended after each Post using client.stop();

void switch_led() {
      bool SwitchReading = digitalRead(SWITCH);
      if (SwitchReading == HIGH) {
        state = state + 1;
        if (state % 2 != 0) {
          digitalWrite(LED, HIGH);
          char data1[] = "{""LDRValue"": 88888888}";
          Serial.println("\nStarting connection to post server...");
          if (client.connect(server, 8040)) {
            Serial.println("connected to post server");
            client.println("POST /api/sensor HTTP/1.1");
            client.println("Host: 192.168.2.116");
            client.println("Content-Type: application/json");
            client.print("Content-Length: ");
            client.println(strlen(data1));
            client.println();
            client.print(data1);
          }
          client.stop();
        }
        else {
          digitalWrite(LED, LOW);
          char data2[] = "{""LDRValue"": 11111111111}";
          Serial.println("\nStarting connection to post server...");
          if (client.connect(server, 8040)) {
            Serial.println("connected to post server");
            client.println("POST /api/sensor HTTP/1.1");
            client.println("Host: 192.168.2.116");
            client.println("Content-Type: application/json");
            client.print("Content-Length: ");
            client.println(strlen(data2));
            client.println();
            client.print(data2);
          }
          client.stop();
        }
       }
0

you may wish to do more than just client.stop:

// ensure the client is clean
while (client.connected()) { while (client.available()) client.flush(); }
client.stop();

also; if you are using a WiFi shield; you may want to take a look at a known issue where on a failed connection; the socket isn't released. i simple patch is required to make it work flawlessly:

https://github.com/arduino-libraries/WiFi/issues/5

-1

I've observed that virtually every example of an Arduino web server posted online fails to adhere to the HTTP standard. When a "GET" request is received, the server should parse the "Content-Length: xxx" attribute to get "xxx", then read xxx characters. When sending a web page to a client, the length of the request should be calculated, omitting the HTTP headers (everything before the "html" tag) and the "Content-Length: xxx" attribute should be included in the header, where xxx is the length of the response from "html" tag to "/html" tag, inclusively.

I had the same problem described here, and nothing I did fixed it until I implemented the standard-compliant code described here.

  • How does this answer the question? The code in the question isn't missing the Content-Length header. If you are commenting on the code in the question as a solution to your problem, then this obviously isn't an answer to the question. – gre_gor Jun 21 '18 at 14:39
  • It highlights what the poster is doing right, which the original post didn't draw much attention to. I just spent lots of time figuring this out for myself, and thought highlighting the correct use of Content-Length here might be helpful to others. – Mr. Mxyxptlik Jun 26 '18 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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