2

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.

MBS
  • 181
  • 1
  • 8

4 Answers4

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.

Code Gorilla
  • 5,652
  • 1
  • 17
  • 31
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();
        }
       }
MBS
  • 181
  • 1
  • 8
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

Aaron Ardiri
  • 121
  • 1
  • 2
-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.