Resource log API - no response

Hi Raphael,

I´m using resource-log/id via API, but now I don´t get any response from server. It worked few days/week before without problem, but now I got no response if I try to connect and POST data to server. Other connection like bridge access, bridge heartbeat & bridge stop still give me 200 response.

I can post my code if you want.

Thx for answer.

MIroslav
FabLab Brno

Hi Miro,

can you give me more details? I assume you mean PUT /resource-logs/{id}? What do you mean by “no response”? Are you sure you are using the right URL?

I just checked our logs:

  • It seem like you’ve made a lot of 409 Conflict and 400 Bad Request responses on 28. and 29.04. Our logs don’t go back much further. 409 Conflict means your lockVersion was outdated (see the API doc for details).
  • I don’t see any unsuccessful (non-200) calls to PUT /resource-logs/{id} from 01.05. until today.
  • I do see some successful calls on 13.05.

Can you send me your requests and responses? You can send them to support@fabman.io if they contain any sensitive information (or you can change this thread to private, if you like).

Best regards,
Raphael

Hi Raphael,

yes, I mean PUT in /recource-logs/{id}. I´m pretty sure that I´m using right URL, because I did not make any changes from point that I made it work.

Thx for info that it worked around date 13.5 so I load backup file from 12.5.2018 to be sure that I did not touch it and result is still the same.

I made separate sketch in Arduino with one heartbeat & one send request to check that I´m not crazy and also to see if heartbeat get response from server.

I´m using ESP32 :slight_smile:

Here is my Arduino code (without sensitive data):

#include <WiFiMulti.h>
#include <HTTPClient.h>
#include <SimpleTimer.h>

WiFiMulti wifiMulti;
SimpleTimer wifiTimer;

int lockVersion = 1;
int filament_used = 17574; // dummy value
String file_name = "test.gcode"; // test name
int configVersion = 0;

const char* root_ca = \
                      "-----BEGIN CERTIFICATE-----\n" \
                      "MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/\n" \
                      "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" \
                      "DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow\n" \
                      "SjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDUxldCdzIEVuY3J5cHQxIzAhBgNVBAMT\n" \
                      "GkxldCdzIEVuY3J5cHQgQXV0aG9yaXR5IFgzMIIBIjANBgkqhkiG9w0BAQEFAAOC\n" \
                      "AQ8AMIIBCgKCAQEAnNMM8FrlLke3cl03g7NoYzDq1zUmGSXhvb418XCSL7e4S0EF\n" \
                      "q6meNQhY7LEqxGiHC6PjdeTm86dicbp5gWAf15Gan/PQeGdxyGkOlZHP/uaZ6WA8\n" \
                      "SMx+yk13EiSdRxta67nsHjcAHJyse6cF6s5K671B5TaYucv9bTyWaN8jKkKQDIZ0\n" \
                      "Z8h/pZq4UmEUEz9l6YKHy9v6Dlb2honzhT+Xhq+w3Brvaw2VFn3EK6BlspkENnWA\n" \
                      "a6xK8xuQSXgvopZPKiAlKQTGdMDQMc2PMTiVFrqoM7hD8bEfwzB/onkxEz0tNvjj\n" \
                      "/PIzark5McWvxI0NHWQWM6r6hCm21AvA2H3DkwIDAQABo4IBfTCCAXkwEgYDVR0T\n" \
                      "AQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwfwYIKwYBBQUHAQEEczBxMDIG\n" \
                      "CCsGAQUFBzABhiZodHRwOi8vaXNyZy50cnVzdGlkLm9jc3AuaWRlbnRydXN0LmNv\n" \
                      "bTA7BggrBgEFBQcwAoYvaHR0cDovL2FwcHMuaWRlbnRydXN0LmNvbS9yb290cy9k\n" \
                      "c3Ryb290Y2F4My5wN2MwHwYDVR0jBBgwFoAUxKexpHsscfrb4UuQdf/EFWCFiRAw\n" \
                      "VAYDVR0gBE0wSzAIBgZngQwBAgEwPwYLKwYBBAGC3xMBAQEwMDAuBggrBgEFBQcC\n" \
                      "ARYiaHR0cDovL2Nwcy5yb290LXgxLmxldHNlbmNyeXB0Lm9yZzA8BgNVHR8ENTAz\n" \
                      "MDGgL6AthitodHRwOi8vY3JsLmlkZW50cnVzdC5jb20vRFNUUk9PVENBWDNDUkwu\n" \
                      "Y3JsMB0GA1UdDgQWBBSoSmpjBH3duubRObemRWXv86jsoTANBgkqhkiG9w0BAQsF\n" \
                      "AAOCAQEA3TPXEfNjWDjdGBX7CVW+dla5cEilaUcne8IkCJLxWh9KEik3JHRRHGJo\n" \
                      "uM2VcGfl96S8TihRzZvoroed6ti6WqEBmtzw3Wodatg+VyOeph4EYpr/1wXKtx8/\n" \
                      "wApIvJSwtmVi4MFU5aMqrSDE6ea73Mj2tcMyo5jMd6jmeWUHK8so/joWUoHOUgwu\n" \
                      "X4Po1QYz+3dszkDqMp4fklxBwXRsW10KXzPMTZ+sOPAveyxindmjkW8lGy+QsRlG\n" \
                      "PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6\n" \
                      "KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==\n" \
                      "-----END CERTIFICATE-----\n";

const String API_key = ""; 
const String API_key_user = "";
int currentSessionId = 132913;
int test = 0;

WiFiClient client;
HTTPClient http;

void setup() {
  Serial.begin(115200);
  Serial.println("Starting..");

  wifiMulti.addAP("FabLab", "wearemakers");
  wifiMulti.addAP("FabLab", "wearemakers");

  Serial.println("Connecting Wifi...");
  if (wifiMulti.run() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("WiFi not connected");
  }
  wifiTimer.setInterval(5000, connection);

}

void loop() {
  // put your main code here, to run repeatedly:
  wifiTimer.run();

}

void connection() {
  int status = WiFi.status();
  if ((test != 1) && (status == WL_CONNECTED)) {
    heartBeat();
    log_resources();
  }
  if (status != WL_CONNECTED) {
    wifiMulti.run();
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
}

void heartBeat() {

  String heartMessage = "";
  heartMessage += "{\"configVersion\":";
  heartMessage += configVersion;
  heartMessage += "}";
  char heartMessageChar[heartMessage.length() + 1];
  heartMessage.toCharArray(heartMessageChar, heartMessage.length() + 1);

  http.begin("https://fabman.io/api/v1/bridge/heartbeat", root_ca );  //Specify destination for HTTP request
  http.addHeader("Content-Type", "application/json");             //Specify content-type header
  http.addHeader("Accept", "application/json");
  http.addHeader("Authorization", API_key);

  int httpResponseCode = http.POST(heartMessageChar);   //Send the actual POST request
  Serial.println();
  Serial.println("Sending hearbeat..");
  Serial.println(heartMessageChar);
  String payload = http.getString();
  Serial.println("JSON from server:");
  Serial.println(payload);
  Serial.println();

  if (httpResponseCode > 0) {
    Serial.print("Responde code: ");
    Serial.println(httpResponseCode);   //Print return code
  } else {
    Serial.print("Error on sending request: ");
    Serial.println(httpResponseCode);
  }
  http.end();  //Free resources
}

void log_resources() {

  String dataMessage = "";
  dataMessage += "{\"metadata\":{\"Filament used\":";
  dataMessage += filament_used;
  dataMessage += ",\"File name\":\"";
  dataMessage += file_name;
  dataMessage += "\"},\"lockVersion\":";
  dataMessage += lockVersion;
  dataMessage += "}";
  char dataMessageChar[dataMessage.length() + 1];
  dataMessage.toCharArray(dataMessageChar, dataMessage.length() + 1);
  Serial.println();
  Serial.println("Section: log_resources");
  Serial.println(dataMessage);

  http.begin("https://fabman.io/api/v1/resource-logs/" + int(currentSessionId), root_ca );  //Specify destination for HTTP request
  http.addHeader("Content-Type", "application/json");             //Specify content-type header
  http.addHeader("Accept", "application/json");
  http.addHeader("Authorization", API_key_user);

  int httpResponseCode = http.PUT(dataMessage);

  if (httpResponseCode > 0) {
    Serial.print("Responde code: ");
    Serial.println(httpResponseCode);   //Print return code
    Serial.println();
  }
  String payload = http.getString();
  Serial.println("JSON from server:");
  Serial.println(payload);
  Serial.println();
  test = 1;
}

Here is response from device when this code run (without sensitive data):

⸮х⸮ѥ⸮⸮⸮.
Connecting Wifi...
E (93) wifi: esp_wifi_scan_start 967 wifi not start
WiFi not connected

WiFi connected
IP address: 
0.0.0.0

WiFi connected
IP address: 
172.18.15.193

Sending hearbeat..
{"configVersion":0}
JSON from server:
{"config":{"type":1,"name":"Test","maxOfflineUsage":null,"deadManIntervalBusy":180,"deadManIntervalIdle":0,"lightWarningSeconds":30,"soundWarningSeconds":15,"inputConfig":[{"type":"none","state":true},{"type":"none","state":true},{"type":"none","state":true}],"auxEquipment":"none","exhaustErrorShutdown":false,"safetyMessage":"","offlineKeys":,"idlePowerThreshold":0,"preventPowerOffWhileBusy":false,"muteDeadMan":false,"stopAfterBusy":false,"configVersion":4261890858}}

Responde code: 200

Section: log_resources
{"metadata":{"Filament used":17574,"File name":"test.gcode"},"lockVersion":3}
JSON from server:

Sorry if I overlooked something.

Thanks for your help.

Miroslav

Hi Miro,

you don’t print the return code if it’s <= 0, so you don’t see the error cause.
Try this instead:

int requestResult = http.PUT(dataMessage);
if (requestResult > 0) {
	Serial.print("Response code: ");
}
else {
	Serial.print("Request error: ");
}
Serial.println(requestResult);
Serial.println();

… and then compare the output against this list to see what’s actually happening.

Best regards,
Raphael

Hi,

I added your code to my skech and I get this:

Section: log_resources
{"metadata":{"Filament used":17574,"File name":"test.gcode"},"lockVersion":3}
Responde code: -1

JSON from server:

which is: HTTPC_ERROR_CONNECTION_REFUSED (-1) .

Do you see somethink that I don´t?

Thanks
Miroslav

There’s a lot that can go wrong in connect. Try increasing the core log level to DEBUG to see why it’s failing the second time around.

I increased core log level to verbose and get this result:

Section: log_resources
{"metadata":{"Filament used":17574,"File name":"test.gcode"},"lockVersion":2}
[V][HTTPClient.cpp:140] beginInternal(): url: 
[E][HTTPClient.cpp:146] beginInternal(): failed to parse protocol
[D][HTTPClient.cpp:832] connect(): HTTPClient::begin was not called or returned error
[W][HTTPClient.cpp:1124] returnError(): error(-1): connection refused
Responde code: -1

[W][HTTPClient.cpp:1124] returnError(): error(-4): not connected
JSON from server:

so I tryed few things and did find out that HTTP client will not take integer as variable at the line where I make URL for server. When I put String as variable it works.

Anyway, many thanks for your greate support & help Raphael ;-).

This is SOLVED!

1 Like