bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
549 stars 208 forks source link

getting too many timeout and could not write to socket 3 error #150

Open amrithmmh opened 6 years ago

amrithmmh commented 6 years ago

my code in arduino uno + nodemcuw (with AT firmware baudrate 115200)

#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#include "SoftwareSerial.h"

#define WIFI_AP "xxxxxx"
#define WIFI_PASSWORD "xxxxxx"

#define TOKEN "token" 

char thingsboardServer[] = "192.168.1.101";

// Initialize the Ethernet client object
WiFiEspClient espClient;

PubSubClient client(espClient);

SoftwareSerial soft(10, 11); // RX, TX

int status = WL_IDLE_STATUS;
int t;
unsigned long lastSend;

void getAndSendTemperatureAndHumidityData()
{
  Serial.println("Collecting temperature data.");

  // Reading temperature or humidity takes about 250 milliseconds!
 t=1000;

  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.print(" *C ");

  String temperature = String(t);

  // Just debug messages
  Serial.print( "Sending temperature  : [" );
  Serial.print( temperature );
  Serial.print( "]   -> " );

  // Prepare a JSON payload string
  String payload = "{";
  payload += "\"temperature\":"; payload += temperature;
  payload += "}";

  // Send payload
  char attributes[100];
  payload.toCharArray( attributes, 100 );
  client.publish( "v1/devices/me/telemetry", attributes );
  Serial.println( attributes );
}

void InitWiFi()
{
  // initialize serial for ESP module
  soft.begin(115200);
  // initialize ESP module
  WiFi.init(&soft);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  Serial.println("Connecting to AP ...");
  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(WIFI_AP);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
    delay(500);

  }
  Serial.println("Connected to AP");

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");
  Serial.println(ip);

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Connecting to Thingsboard node ...");
    // Attempt to connect (clientId, username, password)
    if ( client.connect("arduino", TOKEN, NULL) ) {
      Serial.println( "[DONE]" );
    } else {
      Serial.print( "[FAILED] [ rc = " );
      Serial.print( client.state() );
      Serial.println( " : retrying in 5 seconds]" );
      // Wait 5 seconds before retrying
      delay( 5000 );
    }
  }
}
void setup() {
  // initialize serial for debugging
  Serial.begin(9600);

  InitWiFi();
  client.setServer( thingsboardServer, 1883 );
  lastSend = 0;
}

void loop() {
  status = WiFi.status();
  if ( status != WL_CONNECTED) {
    while ( status != WL_CONNECTED) {
      Serial.print("Attempting to connect to WPA SSID: ");
      Serial.println(WIFI_AP);
      // Connect to WPA/WPA2 network
      status = WiFi.begin(WIFI_AP, WIFI_PASSWORD);
      delay(500);
    }
    Serial.println("Connected to AP");
     IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");
  Serial.println(ip);
  }

  if ( !client.connected() ) {
    reconnect();
     IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");
  Serial.println(ip);
  }

  if ( millis() - lastSend > 1000 ) { // Update and send only after 1 seconds
    getAndSendTemperatureAndHumidityData();
    lastSend = millis();
  }

  client.loop();
}
amrithmmh commented 6 years ago

[WiFiEsp] Data packet send error (2) [WiFiEsp] Failed to write to socket 3 [WiFiEsp] Disconnecting 3

boylesg commented 6 years ago

You need to get into EspDrv.cpp and increase the timeout values such that send and receive events have longer to complete.

With my wifi irrigation controller I have noticed regularly that send and receive events are very snappy when the controller is operating at some of my clients premises. But it is quite slow (and still times out even after I hacked the library) when operating at home.

I suspect you have the same problem that I have in my home in that the Wifi coverage is not optimal that leads to these timeout errors

omerfarukaran commented 4 years ago

[WiFiEsp] Data packet send error (1) [WiFiEsp] Failed to write to socket 3 [WiFiEsp] Disconnecting 3

samoyedfranek commented 2 years ago

Musisz wejść do EspDrv.cpp i zwiększyć wartości limitu czasu, tak aby zdarzenia wysyłania i odbierania miały więcej czasu do ukończenia.

Dzięki mojemu kontrolerowi nawadniania Wi-Fi regularnie zauważyłem, że zdarzenia wysyłania i odbierania są bardzo szybkie, gdy kontroler działa w niektórych lokalizacjach moich klientów. Ale jest dość powolny (i nadal się kończy, nawet po zhakowaniu biblioteki) podczas pracy w domu.

Podejrzewam, że masz ten sam problem, który mam w moim domu, ponieważ zasięg Wifi nie jest optymalny, co prowadzi do tych błędów przekroczenia limitu czasu

Can you give me your discord?