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.