Open boylesg opened 7 years ago
Hello @boylesg , I am reading through the changes you introduced into the library (the one you've uploaded in #112). I got a question, did you implement the delay(200) you mentioned in the library or sketch itself? I am asking because the part presented below in the original and your libraries are the same. I am also having a similar problem with the timeouts and I am trying to "write" a small "hack" as well.
AFAIK the author of the library is using strings written to flash memory when sending anything to ESP to workaround these issues. I however am in need of having a mutable string getting send through - trying to controll Philips HUE lamps with the help of ESP. While my code is working it is a little annoying that when I try to send a PUT Request that contains a standardish String my ESP isn't able to read the response from the other device because of the timeout - I even tried to put the timing to 15000 miliseconds, it doesn't help.
size_t WiFiEspClient::write(const uint8_t *buf, size_t size) { if (_sock >= MAX_SOCK_NUM or size==0) { setWriteError(); return 0; } bool r = EspDrv::sendData(_sock, buf, size); if (!r) { setWriteError(); LOGERROR1(F("Failed to write to socket"), _sock); delay(4000); stop(); return 0; } return size; }
This is what I am getting after trying to send a PUT request that contains a standard string.
[WiFiEsp] Connecting to 192.168.1.100
Connecting...
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Data packet send error (1)
[WiFiEsp] Failed to write to socket 3
[WiFiEsp] Disconnecting 3
From what I've digged up these errors come out when the readUntil(...) function doesn't return a 5.
int EspDrv::readUntil(int timeout, const char* tag, bool findTags)
Here's some "proof" from EspDrv.cpp:
bool EspDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
{
LOGDEBUG2(F("> sendData:"), sock, len);
char cmdBuf[20];
sprintf_P(cmdBuf, PSTR("AT+CIPSEND=%d,%u"), sock, len);
espSerial->println(cmdBuf);
int idx = readUntil(/*1000*/8000, (char *)">", false);
if(idx!=NUMESPTAGS)
{
LOGERROR(F("Data packet send error (1)"));
return false;
}...
nlhnt I ended up creating a new class member called m_nDelay (or something lime that), gave it a large value like 8000 and then replaced all the literals in the calls to readUntil(...) with m_nDelay. This was in EspDrv.cpp/h.
In the code you have been examining above I have replaced '8000' with m_nDelay meaning that if I wanted to experiment with the delay value I only had to change it in one place in the class constructor.
As far as I can tell the original delay values used in readUntil(...) only work if you have ideal Wifi coverage around the house. If that is not the case, e.g. the adsl modem is in a back room in the house, then the delay values used in the calls to readUntil(...) are too small and you often (not always) get these annoying timeout and data send errors. Increasing these delay values in readUntill(....) has largely eliminated these timeout and data send errors for me.
Here is my version of the WifiEsp library. Drop the .zip part of the file name - I had to add that so that it would be accepted as an upload.
As well as the alterations to the calls to readUntil(...) in EspDrv.cpp I have added an WifiEspNTPClient and SMTPClient classes for convenience. There are a couple of other convenient minor functions like getRemotePort() that I have added to WifiEspServer. There is also an ugly hack in WifiEspServer ::available() that prevents this class from 'picking' up UDP requests and presenting them to your sketch as an http request containing invalid data. This is a problem when you want to receive both http and UDP requests in the same sketch. Unfortunately the hack assumes that you will setup your UDP sender to use a predetermined set of port numbers commonly used for UDP according to wikipedia. A better solution would be to determine what port the requester made the request on, i.e. 80, 21, 22,...... Because that number determines what protocol the requester is expecting. Then create a http client or a UDP client accordingly within WifiEsp. But as far as I can tell there is no way of obtaining this particular information from either WifiEsp or the firmware running on the ESP8266.
Maby there is a problem in the connection of the circuit. First I use a resistor to connect the VCC and EN pin on the bakery then I got "data packet send error" .Finnally I remove the bakery then the error disappeared.
i have the same problem my arduino code
#include <WiFiEspClient.h>
#include <WiFiEsp.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#include "SoftwareSerial.h"
#define WIFI_AP "wifi"
#define WIFI_PASSWORD "pass"
#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();
}
@boylesg hello, I miss the same problem with you, and I change the function sendData() just like you saying. After this, I problem has gone. Think you very much!
No worries matey!
my problem was because of damn esp8266 baudrate it should be 9600 not 115200 for softwareserial
Works just fine with a baud rate of 115200 for me.....with the increased timeout values in espdrv.cpp In fact my home WiFi network is not ideal since I have the ADSL modem in the back office and not the recommended central location. In other WiFi networks with better coverage the ESP8266 shield rarely times out and the response times are quite snappy, with a baud rate of 115200, with relatively large html files stored on an SD card.
One more unusual thing I have discovered about the library is that it seems to be using the <> for something when sending text via UDP. Because when I send html files, with the html tags that also use <>, there are chunks of html code proceeding the < symbols missing from the file at the other end of the UDP pipe.
My work around is to offset all the ASCII codes, for each text character, into the extended ASCII codes. I.E. Add 128 to each ASCII value read from my html file, send these through the UDP pipe in WiFiEsp library, and then subtract 128 from the received decimal value and restore the correct ASCII character.
Works without a hitch.
hello @boylesg i am having the same issue but when i checked my wifi library i dont have the EspDrv.cpp in my library and when i try replacing your SCR with the one in my library there was no changes. can i get any help on this plz
Hi, I am not familiar with the code of ESP8266, but I tested the timeout settings, I hope it is useful for someones. I save the values sent by DHT22 to SQL which is on the same WiFi network. See delays:
If server response is about not delayed: 08:48:31.099 -> [WiFiEsp] Connecting to 192.168.50.48 08:48:31.167 -> Connecting... 08:48:32.478 -> [WiFiEsp] >>> TIMEOUT >>> 08:48:32.478 -> [WiFiEsp] Data packet send error (1) 08:48:32.478 -> [WiFiEsp] Failed to write to socket 3 08:48:36.468 -> [WiFiEsp] Disconnecting 3
If server response delayed with 0.065s: 08:47:59.227 -> [WiFiEsp] Connecting to 192.168.50.48 08:47:59.293 -> Connecting... 08:48:00.606 -> [WiFiEsp] >>> TIMEOUT >>> 08:48:00.606 -> [WiFiEsp] Data packet send error (1) 08:48:00.606 -> [WiFiEsp] Failed to write to socket 3
If server response delayed with 0.07s: 08:47:09.885 -> [WiFiEsp] Connecting to 192.168.50.48 08:47:09.953 -> Connecting... 08:47:12.281 -> [WiFiEsp] >>> TIMEOUT >>> 08:47:12.281 -> [WiFiEsp] Data packet send error (2) 08:47:12.281 -> [WiFiEsp] Failed to write to socket 3 08:47:16.308 -> [WiFiEsp] Disconnecting 3
If server response delayed with 0.08s: 08:50:01.713 -> [WiFiEsp] Connecting to 192.168.50.48 08:50:01.781 -> Connecting... 08:50:02.185 -> HTTP/1.0 204 NO CONTENT 08:50:02.252 -> Content-Type: text/html; charset=utf-8 08:50:02.286 -> Server: Werkzeug/1.0.1 Python/3.8.2 08:50:02.320 -> Date: Mon, 13 Jul 2020 06:50:02 GMT
I found the cause in my code I was connecting to port 80 for http when attempting an https connection. Try connecting to port 443 for https instead.
@koger23 Thanks a lot. This seems to solve the issue! I added a 1sec sleep time before sending the response and the error :
08:47:12.281 -> [WiFiEsp] Data packet send error (2)
08:47:12.281 -> [WiFiEsp] Failed to write to socket 3
was removed !!
I hope others will see this conversation...
@lucgerrits where exactly in the library did you add the sleep time? I'm trying to overcome the same issue but haven't solved it yet.
I have de same question
if you can update the AT firmware to AT 1.7.5 (SDK 3.0.5), you can use my WiFiEspAT library, which is doesn't have these problems
I have de same question
AT firmware to AT 1.7.5 (SDK 3.0.5)
Do you have any link where I can download the AT firmware to AT 1.7.5 (SDK 3.0.5)?
I have de same question
AT firmware to AT 1.7.5 (SDK 3.0.5)
Do you have any link where I can download the AT firmware to AT 1.7.5 (SDK 3.0.5)?
I have de same question
AT firmware to AT 1.7.5 (SDK 3.0.5)
Do you have any link where I can download the AT firmware to AT 1.7.5 (SDK 3.0.5)?
yes, I downloaded that one but I don't know the memory addresses or the files to load from the flashing tool, ESP 01S 8266
I have de same question
AT firmware to AT 1.7.5 (SDK 3.0.5)
Do you have any link where I can download the AT firmware to AT 1.7.5 (SDK 3.0.5)?
I was finally able to update the firmware
but your library gives error to others that I do not see where it requests the name of my wifi and the password , he never conect
and this one too, even placing my wifi and password
did you change the baud rate? https://github.com/jandrassy/WiFiEspAT#getting-started
@koger23 Thanks a lot. This seems to solve the issue! I added a 1sec sleep time before sending the response and the error :
08:47:12.281 -> [WiFiEsp] Data packet send error (2) 08:47:12.281 -> [WiFiEsp] Failed to write to socket 3
was removed !!
I hope others will see this conversation...
Can you send in what file you change it?
[WiFiEsp] Data packet send error (2) [WiFiEsp] Failed to write to socket 1 [WiFiEsp] Disconnecting 1
My problem got solved after I used GET in the path. I was missing it before. Serial.print("GET /abc.php?humidity="); client.print("GET /abc.php?humidity="); //YOUR URL
You can add the library ThingSpeak by MathWorks and base you sketch of the example called WriteSingleField. It works for me.
As far as I can tell these errors are generated because EspWifi is transmitting data to the ESP8266 chip faster than the chip can transmit data across the network. My best guess is that this is due to the wifi coverage at some locations being better and faster than in others.
In the function EspDrv::sendData(...) I increased the timeout values to 8000 rather than 1000 and 2000. In fact I added another static variable to the class called _Timeout (value 8000) and then used this class member as my timeout value in ::sendData(...), and in other functions.
As well as this I slowed down the rate at which I was calling EspDrv::sendData(...) via WiFiEspClient::write(const uint8_t *buf, size_t size) by introducing a delay(200).
In my particular circumstances this seems to have eliminated "data packet send error" so far.