probonopd / WirelessPrinting

Print wirelessly from Cura, PrusaSlicer or Slic3r to your 3D printer connected to an ESP8266 or ESP32 module
352 stars 65 forks source link

Delay sending anything to the printer until ok is received #85

Closed probonopd closed 5 years ago

probonopd commented 5 years ago

This is how I was doing it in 1.x and I had never any trouble with it.

bool okFound = true; // Set to true if last response from 3D printer was "ok", otherwise false

(...)

String sendToPrinter(String line) {

  /* Although this function does not return before okFound is true,
     somewhere else in the sketch this function might also have been called
     hence we make sure we have okFound before we start sending. */

  while (okFound == false) {
    yield();
  }

  Serial.setTimeout(240000); // How long we wait for "ok" in milliseconds

  (...)

  Serial.println(line); // Send to 3D Printer

  (...)

  lineLastSent = line;
  okFound = false;
  while (okFound == false) {
    response = Serial.readStringUntil('\n');
    parseTemperatures(response);
    lineSecondLastReceived = lineLastReceived;
    lineLastReceived = response;
    if (response.startsWith("ok")) okFound = true;
  }
  return (response);
}

Using such a logic should hopefully solve https://github.com/probonopd/WirelessPrinting/issues/72.

probonopd commented 5 years ago

In essence, we are not sending anything to the printer before the previous response started with ok. And we are waiting for a looong time for this - 240000ms.

GMagician commented 5 years ago

I usually don't like the toooo long wait for timeout... I think that we have to detect if printer is live and if communication is still live and have calibrated timeout for it. Going back to your issues during heating, they are because it seems your printer send a different message when it is heating, it sends only current temp. Since temp format is different it is not parsed and it will timeout. I prefer to add anoter timeoutreset in "unhandled" if branch. In that case I reset timer because communication is live and printer is live.... but this is just my idea

probonopd commented 5 years ago

Can you implement waiting forever on ok in a branch, for testing purposes?

probonopd commented 5 years ago

I think we need to distinguish two different things:

Does this make sense?

GMagician commented 5 years ago

I'm working on it