New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use the async client? #18
Comments
here is a little example: static AsyncClient * aClient = NULL;
void runAsyncClient(){
if(aClient)//client already exists
return;
aClient = new AsyncClient();
if(!aClient)//could not allocate client
return;
aClient->onError([](void * arg, AsyncClient * client, int error){
Serial.println("Connect Error");
aClient = NULL;
delete client;
}, NULL);
aClient->onConnect([](void * arg, AsyncClient * client){
Serial.println("Connected");
aClient->onError(NULL, NULL);
client->onDisconnect([](void * arg, AsyncClient * c){
Serial.println("Disconnected");
aClient = NULL;
delete c;
}, NULL);
client->onData([](void * arg, AsyncClient * c, void * data, size_t len){
Serial.print("\r\nData: ");
Serial.println(len);
uint8_t * d = (uint8_t*)data;
for(size_t i=0; i<len;i++)
Serial.write(d[i]);
}, NULL);
//send the request
client->write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");
}, NULL);
if(!aClient->connect("www.google.com", 80)){
Serial.println("Connect Fail");
AsyncClient * client = aClient;
aClient = NULL;
delete client;
}
} Serial:
|
Cool! Thank You! :-) |
I don't suppose you are working on an async HTTP client are you? I wish I had the time to do that! |
I wish someone had it too :) I do not right now :) once ESP32 is more stable-ish I can have time to play with Async and add more :) |
I added some code to my repo. Hopefully it can serve as a starting point, or if anyone needs something similar... |
Thank you very much for the example! It looks like easy enough :) Could anybody answer my question? Where should I execute "runAsyncClient()"-function? I execute it after connecting to my AP. However it tries to connect to my TCP-server before connecting to AP. It goes out from my Serial output:
Connect Faillllll - is a text message like your "Serial.println("Connect Fail");"
What's wrong with my code? :)
|
Its simple. When you call wifi.begin it starts to connect but the function returns immediately, so you need to wait for it to connect or use events to start async on connect |
@judge2005 I suggest to make" _parseLine(); " virtual in your local lib and make override function similar to |
I have one more small question about AsyncClient. Is it possible to check client connection status synchronously? For example in |
you need to have a reference to the client static AsyncClient * client = NULL;
void someMethodThatCreatesClient(){
//.....
client = createdAsyncClient;
//......
}
void onThatClientDisconnect(){
client = NULL;
}
void loop(){
if(client){
Serial.printf("ClientState: %u", client->state());
}
} |
Sounds like "If you haven't defined client you can't check it's state" :) Thank you. It's clear. |
Hey @me-no-dev, Thank you very much for the great library. I have one question to you about the example that you gave earlier. The problem that I have is after client onData is called, it waits almost 10 seconds to disconnect so that I can make another request eventually.
So to prevent that I added stop inside the initial conditions like below:
But this caused unexpected crashes sometimes:
I also tried to null and delete the client So I altered to code from stop to close:
Now it seems the previous exceptions are gone. But I am not sure if this is the correct and safe approach to solve this problem. I am hoping you would direct me to the correct path. |
it's up to you to close the connection when needed :) if the other end does not close it, then it's your responsibility to do so :) |
@me-no-dev Thanks for quick reply awesome 👍 |
@me-no-dev in this comment you used aClient inside "onError" event, and it doesn't work in Arduino IDE. Maybe it's a bug. It probably says something like 'this' not being captured. Even a local variable inside the method runAsyncClient() (in the same comment) is not captured and so can't be used inside the function. I found a way out and it's by making the variables I wish to use outside the class! And it worked. |
@me-no-dev Another question: |
??? how do you know that "GET..." is a String class? It's ordinary character array and not an object so it is given as const char * when passing to function as a parameter... |
Regarding the example used by me_no_dev
Do we have to manually free memory occupied by arg, c, or data pointer arguments? Or is it made automatically? |
Today, after a lot of time trying to find the reason for a crash when no WiFi connection was present, I found the following: The sample by me-no-dev has the following line: it seems the error type is wrong. The following fixes it: (Note the "err_t" instead of int) now the callback is executed and the crash goes away. I hope this helps someone! |
Another complication:
Is this because a DNS lookup is not performed? |
@adrionics
Shouldn't onError be triggered as well in this case? |
Yes, after 18 seconds an error Connection aborted (-13). |
hi what is the size of data received? I get my received data in 2 packets Data: 536 {"data":{"schedule":{"fri":{"start":"07:30","finish":"17:30"},"mon":{"start":"07:30","finish":"19:30"},"sat":{"start":"07:30","finish":"17:30"},"sun":{"start":"07:30","finish":"17:30"},"tur":{"start":"07:30","finish":"17:30"},"tus":{"start":"07:30","finish":"18:30"}," Data: 71 |
Hello there! Greetings from Argentina! Thank you very much in advance! |
... i´ll forgot, i´m using client->write() to send the http request string: POST /hello HTTP/1.1 serial=CL-TAP-P1-000002517826&firmware=v1.2.0.3 |
Not really the place for the question, but I will try to help you in that your problem is likely the content length is wrong. It should be 47 (do not include the line feeds after the headers). |
Excelent! That Works! |
My current code, which actually not works....
AsyncClient async_client;
.
.
.
String req ;
int8_t connected = async_client.connected();
Serial.print("connected: ");
Serial.println(connected) ;
What is missing? How to make it work? HEEEELP PLEEASE!!
The text was updated successfully, but these errors were encountered: