vshymanskyy / TinyGSM

A small Arduino library for GSM modules, that just works
GNU Lesser General Public License v3.0
1.91k stars 709 forks source link

Implement peek() #396

Open WeightScale opened 4 years ago

WeightScale commented 4 years ago

Implement peek() like this int peek() override { return (uint8_t)rx.peek();//add to TinyGsmFifo.h }

TinyGsmFifo.h uint8_t peek(){ return _b[_r];
}

SRGDamia1 commented 4 years ago

Could you please write this into a pull request?

WeightScale commented 4 years ago

// TODO(SRGDamia1): Implement peek int peek() override {

IoTThinks commented 3 years ago

I intend to raise an issue, too. https://github.com/vshymanskyy/TinyGSM/blob/master/src/TinyGsmTCP.tpp#L245

Then I see this issue. When I use TinyGSM to download a bin to do OTA by esp32 Updater. esp32 updater keeps complaining the first bit is not 0xe9. When I or the Updater do the peek() to check the first bit, it is always FF.

Now, I know it may due to the -1 here. -1 mean 255 or FF.

IoTThinks commented 3 years ago

Implement peek() like this int peek() override { return (uint8_t)rx.peek();//add to TinyGsmFifo.h }

TinyGsmFifo.h uint8_t peek(){ return _b[_r]; }

Let me try if this works in my program. If works, then I will create pull request.

IoTThinks commented 3 years ago

I created a pull request based on the suggestion. https://github.com/vshymanskyy/TinyGSM/pull/554

It works for my case. I can peek() correctly the first bit of the binary file when downloading via TinyGSM.

emielch commented 2 years ago

This implementation seems to give issues for me. I'm using TinyGSM on a ESP32 TTGO T-SIM7000G board. For the client object I'm using the GsmClientSecureSIM7000SSL class. I'm using it in combination with ArduinoJSON as I'm sending JSON encoded message between the Arduino and the server. In between the JSON messages the server also sends individual heartbeat characters (in this example i'm using '*' ) so I can keep track of whether the connection is still alive. I'm trying to use the peek() function to check whether the next character is a HB character and remove it from the stream by doing a normal read() before passing the client to the deserializeJson() function. Here is a snippet of code from my main loop in which the problem appears:

if (client.available()) {
  Serial.print((char)client->peek());
  Serial.print("  ||  ");
  Serial.println(HB_char);

  if (client.peek() == HB_char) {  // don't try to parse the HB character as JSON
    Serial.println("SKIP HB CHAR");
    client->read();
  } else {
    StaticJsonDocument<2500> doc;
    ReadLoggingStream loggingStream(client, Serial);
    DeserializationError err = deserializeJson(doc, loggingStream);

    if (err) {
      SerialMon.print(F("deserializeJson() on client failed with code "));
      SerialMon.println(err.c_str());
      return;
    }
  }
}

I added ReadLoggingStream so we can see what data deserializeJson is reading before it errors.

The expected output on Serial when a HB character is received is:

*  ||  *
SKIP HB CHAR

But what I'm seeing most of the time is:

I  ||  *
*deserializeJson() on client failed with code InvalidInput
�  ||  *
*deserializeJson() on client failed with code InvalidInput
�  ||  *
*deserializeJson() on client failed with code InvalidInput
$  ||  *
*deserializeJson() on client failed with code InvalidInput

As you can see just before deserializeJson() errors, it reads , which means the first character in the buffer was but this is not what peek() returned just before that.