Arduino icon indicating copy to clipboard operation
Arduino copied to clipboard

WiFiClient.read() read from where

Open FTOD opened this issue 9 years ago • 1 comments

Hardware

Hardware: ESP-12E (NodeMcu)

Description

Hi guys, i'm working in a simple Iot device which can publish in a topic and subscribe a topic at the same time (subscribe to receive commands from server for update/reboot...)

I got a function here

uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
                                          int16_t timeout) {
  /* Read data until either the connection is closed, or the idle timeout is reached. */
  uint16_t len = 0;
  int16_t t = timeout;

  while (client->connected() && (timeout >= 0)) {
    //DEBUG_PRINT('.');
    while (client->available()) {
      //DEBUG_PRINT('!');
      char c = client->read();
      timeout = t;  // reset the timeout
      buffer[len] = c;
      //DEBUG_PRINTLN((uint8_t)c, HEX);
      len++;
      if (len == maxlen) {  // we read all we want, bail
        DEBUG_PRINT(F("Read data:\t"));
        DEBUG_PRINTBUFFER(buffer, len);
        return len;
      }
    }
    timeout -= MQTT_CLIENT_READINTERVAL_MS;
    delay(MQTT_CLIENT_READINTERVAL_MS);
  }
  return len;
}

using char c = client->read() ,the client here is WiFiClient

I want to set timeout to 20 or something so i don't lose too much data when reading.

So i'm wondering how the function WiFiClient.read() does his job. Do i lose the data if i'm not listening to but the server is sending to me?

i find this in WiFiClient.cpp

int WiFiClient::read()
{
    if (!available())
        return -1;

    return _client->read();
}

But in the constructor

WiFiClient::WiFiClient()
: _client(0)
{
    WiFiClient::_add(this);
}

WiFiClient::WiFiClient(ClientContext* client) : _client(client)
{
    _client->ref();
    WiFiClient::_add(this);
}

i have this to initialise Adafruit_MQTT_Client

WiFiClient wifiClient;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqttClient = Adafruit_MQTT_Client(&wifiClient, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

and it works well to simply send and receive..

So the question is
1.how the function WiFiClient.read() does his job. 2.Do i lose the data if i'm not listening to but the server is sending to me?

Thanks for your help!

FTOD avatar Apr 06 '17 21:04 FTOD

I'm pretty sure that it read data from a buffer , so i want to know : what is this buffer ? (it's a UART?) and the length?

FTOD avatar Apr 06 '17 21:04 FTOD