Wating response from server -Only works first loop, following, not-

Hi,

I’m using the following code to connect to my server, send a Json and wait for the response. Everything is going ok, but in the second loop, I don’t know why there is no response information.

Any idea why I receive only first time?

Thank’s for your help

///////////////////////////////////////////////////////////////Libraries definition//////////////////////////////////////////////////////
#include <MKRGSM.h>
#include <ArduinoHttpClient.h>

// PIN Number
const char PINNUMBER[]     = ""; //blank if no pin
// APN data: check settings on mobile for sim provider or contact carrier for network settings
const char GPRS_APN[]      = "internet.easym2m.eu";
const char GPRS_LOGIN[]    = "";
const char GPRS_PASSWORD[] = "";

// get this from the wia dashboard. it should start with `d_sk`
const char* device_secret_key = "******************";

GSMClient client;
GPRS gprsAccess;
GSM gsmAccess;//(true);

//API parameters
char server[] = "***********";
char path[] = "******";
int port = ******;

HttpClient httpClient = HttpClient(client, server, port);

// Various variables used in this Sketch
String response = "";
String okmsg = "ok";
String errormsg = "error";
void setup() {
  delay(3000);
  pinMode(LED_BUILTIN, OUTPUT);
 
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
}
void loop() {
  
  digitalWrite(LED_BUILTIN, HIGH);

  

    if (gsmAccess.begin(PINNUMBER) != GSM_READY) {
      Serial.println("GSM Error");

    }


    else {
      Serial.println("GSM OK");
      // Attach GPRS and check if it works
      if (Serial) Serial.println("Attaching to GPRS with your APN...");
      if (gprsAccess.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) != GPRS_READY) {
        if (Serial) Serial.println(errormsg);
        gsmAccess.shutdown();

      } else {
        // GPRS successfully attached
        if (Serial) Serial.println(okmsg);

        // Reading temperature or humidity takes about 250 milliseconds!
        // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
        float h = 80.88;
        float t;
        t = 25.80;



        // Prepare post data
        String PostData = "{\"Temperature\":" + String(t) + ",\"Humidity\":" + String(h) + "}";

        // Connect to Soracom harvest
        if (Serial) Serial.print("Connecting and sending POST request ...");
        int res_connect;

        res_connect = client.connect(server, port);

        // If connection is successful, POST JSON data
        if (res_connect) {

          //                data.printTo(PostData);
          httpClient.beginRequest();
          httpClient.post(path);
          httpClient.sendHeader("Content-Type", "application/json");
          //          httpClient.sendHeader("Content-Length", data.measureLength());
          httpClient.sendHeader("Authorization", "Bearer "  + String(device_secret_key));
          httpClient.beginBody();
          httpClient.print(PostData);
          httpClient.endRequest();

          if (Serial) Serial.println(okmsg);

          // Read abd print the response from the server
          if (Serial) Serial.print("Receiving response...");
          boolean test = true;
          while (test) {
            // if there are incoming bytes available, print them
            if (client.available()) {
              char c = client.read();
              if (Serial) Serial.print(c);
            }

            // if the server's disconnected, stop the client:
            if (!client.connected()) {
              if (Serial) Serial.println("Shuttign down GSM connection: disconnected");
              client.stop();
              test = false;
            }
          }
        } else {
          // if we didn't get a connection to the server
          if (Serial) Serial.println(errormsg);
        }
      }
    }
//  }


  digitalWrite(LED_BUILTIN, LOW);
  delay(900000);

Didn't you post already questions on that code or very similar ? why don't you follow up there ?

Your code is missing a closing curly brace at the end - anything else missing?

what does all the printing tells you in the second loop? is res_connect successful ? --> you have stuff going through the console, share that... do you have access to the server logs? do you see the incoming connexion successful?

HI @J-M-L

About the missing brace is because I have to erase part of the code because there is code that if I publish everyone could enter in the server.

No, is not the same code, I had to change it because the library was with a bug and the developer is updating when he will have time to do it.

About the problem, I have no access to server logs right now, I'm going to demand to the master

The reason why I want to see the response is that I miss some messages and I don't know why? for example the console give me the following response

I just made a question is somebody has the same problem.

Console:

GSM OK
Attaching to GPRS with your APN...
ok
Connecting and sending POST request to ..ok
Receiving response...Shuttign down GSM connection: disconnected
GSM OK
Attaching to GPRS with your APN...
ok
Connecting and sending POST request to ..ok
Receiving response...Shuttign down GSM connection: disconnected
GSM OK
Attaching to GPRS with your APN...
ok
Connecting and sending POST request to ..ok
Receiving response...Shuttign down GSM connection: disconnected

Thank's for your help

How did you get the impression that even one connection was successful? If above text is the serial output you never get an answer back from the server.

Post a link to the HttpClient library you're using as it seems to be a different one than the library available in the library manager of the IDE.

Hi #pylon

I'm using the following library:

I got the impression because I have a post in my Node telling me the connection was ok

Why not I will never have the response via serial, I had it, but only in the first connection

Thank's for your help

Why not I will never have the response via serial, I had it, but only in the first connection

That's why:

            // if there are incoming bytes available, print them
            if (client.available()) {
              char c = client.read();
              if (Serial) Serial.print(c);
            }

Any response from the server should be printed to the serial interface. As you got nothing, it's strange you're telling us that you get a response.

I got the impression because I have a post in my Node telling me the connection was ok

... and "node" means "server" in this context?

What's the reason for not using the simple post() method to send the request?

In your version no content length is submitted and an additional cr/lf is sent at the end. If you do it as in the SimplePost.ino example these problems would be omitted.