E
E
Egor Lepikhin2019-12-17 12:33:19
Arduino
Egor Lepikhin, 2019-12-17 12:33:19

Why Arduino loses connection after multiple POST requests?

I upload a sketch to the Arduino, which sends a POST request to the local server every second. The first 8 requests come, and then the connection is lost. It is not clear why this happens
. When sending GET requests every second, no problems arise.
PS There are no errors or exceptions on the server

Sketch Code
#include <SPI.h>
#include <Ethernet2.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 0, 228);  // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

IPAddress ip(192, 168, 0, 229);

void setup() {
  Serial.begin(9600);
  Ethernet.init(4);
  while (!Serial) {
  }

  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip);
  }
  delay(1000);
  Serial.println("connecting...");

}

void loop()
{

  EthernetClient client;

String data = "{\"login\":\"admin\",\"password\":\"qwerty\",\"deviceName\":\"ard\",\"deviceType\":\"nano\",\"sensorsValue\":{\"temp\":36.0,\"light\":1023.0,\"humiditu\":233.0}}";
  
  if (client.connect(server, 8080)) {
    Serial.println("connected");
    client.println("POST /intler_iot_war_exploded/send-device-data HTTP/1.1");
    client.println("Host: 192.168.0.228:8080");
    client.println("Content-Type: application/json");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
    client.println();
    Serial.println(client);
  }
  else {
    Serial.println("connection failed");
  }

  delay(1000);
}

Output to the port
connecting...
connected
1
connected
1
connected
1
connected
1
connected
1
connected
1
connected
1
connected
1
connected
1
connection failed
connection failed
connection failed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Lepikhin, 2019-12-17
@LepikhinEgor

I found an error, in the end it turned out that the servlet dispatcher (server in Java + Spring) sent a 500 error in response, since I forgot to add the @ResponceBody annotation and it could not find the view for the response. Why requests worked the first 8 times is still not clear ... Well, okay

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question