GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
-
- Posts: 6
- Joined: Wed Nov 27, 2019 6:22 pm
GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
I'm done! I'm officially done!
I signed up on arduino forum and asked this question ONLY to get this answer "use http client" And NOW the arduino.cc Server seems to be down.. So.. I'm here to get an answer to my question and PLEASE READ MY QUESTION BEFORE YOU ANSWER!
thanks
So I'm running an ESP32 system and an ESP8266. Both are doing some disco light controlling stuff.
I want to send GET requests to the ESP8266 like "192.168.2.81/LEDON" from my ESP32. But at the same time I want my ESP32 to handle GET Requests from my phone or anything else.
I know what httpClient is... But it doesn't work simultaneously with the webserver.
I found ASyncWebServer for ESP32. But I don't understand it at all and there is 0% useful tutorials or readme's for it.
They do things like "request->send(200, "text/plain", "Hello, GET: " + message);" ???
I need to send a GET request like this: 192.168.2.81/BLABLA
I signed up on arduino forum and asked this question ONLY to get this answer "use http client" And NOW the arduino.cc Server seems to be down.. So.. I'm here to get an answer to my question and PLEASE READ MY QUESTION BEFORE YOU ANSWER!
thanks
So I'm running an ESP32 system and an ESP8266. Both are doing some disco light controlling stuff.
I want to send GET requests to the ESP8266 like "192.168.2.81/LEDON" from my ESP32. But at the same time I want my ESP32 to handle GET Requests from my phone or anything else.
I know what httpClient is... But it doesn't work simultaneously with the webserver.
I found ASyncWebServer for ESP32. But I don't understand it at all and there is 0% useful tutorials or readme's for it.
They do things like "request->send(200, "text/plain", "Hello, GET: " + message);" ???
I need to send a GET request like this: 192.168.2.81/BLABLA
-
- Posts: 9757
- Joined: Thu Nov 26, 2015 4:08 am
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Moved to the Arduino subforum.
There's a fair bit of documentation up on https://github.com/me-no-dev/ESPAsyncWebServer , have you looked there?
There's a fair bit of documentation up on https://github.com/me-no-dev/ESPAsyncWebServer , have you looked there?
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Use separate threads for the client and server. See for example: https://randomnerdtutorials.com/esp32-d ... duino-ide/ and https://www.hackster.io/rayburne/esp32- ... wo-cores-1kermitfuckingfrog wrote: ↑Wed Nov 27, 2019 6:29 pmI know what httpClient is... But it doesn't work simultaneously with the webserver.
Use queues, semaphores and events to communicate between the threads.
Sending requests is done with httpClient, not with ASyncWebServer. See: https://techtutorialsx.com/2017/05/19/e ... -requests/kermitfuckingfrog wrote: ↑Wed Nov 27, 2019 6:29 pmI found ASyncWebServer for ESP32. But I don't understand it at all and there is 0% useful tutorials or readme's for it.
They do things like "request->send(200, "text/plain", "Hello, GET: " + message);" ???
I need to send a GET request like this: 192.168.2.81/BLABLA
- shabtronic
- Posts: 49
- Joined: Sun Nov 03, 2019 1:33 pm
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
I've just done this on my A1S board - with a old ADF/IDF build.
It's a WifiAP and Http Server for my project - works like a charm! I just used the tcpip stuff.
I used sockets and pthreads - with a simple listener loop handling and dishing out the threads.
Absolutely no need to use complex thread communications - semaphores,mutex e.t.c..
apart from 1 static volatile bool to shutdown the main listener thread.
it's pretty simple once you get your head around sockets.
I can post part of the code here if u like?
I had to use cpp - tho to make the string handling easier.
once you have a socket it's as simple as
dprintf(ClientSocket, "HTTP/5.0 404 Not Found\r\nConnection: close\r\n");
to send txt data.
It's a WifiAP and Http Server for my project - works like a charm! I just used the tcpip stuff.
I used sockets and pthreads - with a simple listener loop handling and dishing out the threads.
Absolutely no need to use complex thread communications - semaphores,mutex e.t.c..
apart from 1 static volatile bool to shutdown the main listener thread.
it's pretty simple once you get your head around sockets.
I can post part of the code here if u like?
I had to use cpp - tho to make the string handling easier.
once you have a socket it's as simple as
dprintf(ClientSocket, "HTTP/5.0 404 Not Found\r\nConnection: close\r\n");
to send txt data.
-
- Posts: 6
- Joined: Wed Nov 27, 2019 6:22 pm
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Sounds interesting. I also think this MUST be possible without using dual core threads..shabtronic wrote: ↑Thu Nov 28, 2019 8:38 amI've just done this on my A1S board - with a old ADF/IDF build.
It's a WifiAP and Http Server for my project - works like a charm! I just used the tcpip stuff.
I used sockets and pthreads - with a simple listener loop handling and dishing out the threads.
it's pretty simple once you get your head around sockets.
I can post part of the code here if u like?
I had to use cpp - tho to make the string handling easier.
once you have a socket it's as simple as
dprintf(ClientSocket, "HTTP/5.0 404 Not Found\r\nConnection: close\r\n");
to send txt data.
But I don't want to send text. I just want to send "192.168.2.81/A" for example to make the ESP8266 turn on the lights.
Maybe you could give me a sneak peak into ur code?
-
- Posts: 6
- Joined: Wed Nov 27, 2019 6:22 pm
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
No omg wow thank you so muchESP_Sprite wrote: ↑Thu Nov 28, 2019 4:21 amMoved to the Arduino subforum.
There's a fair bit of documentation up on https://github.com/me-no-dev/ESPAsyncWebServer , have you looked there?
seriously.. Of course I have studied this forth and back!
Otherwise I wouldn't sign up on this website and ask for help.
Where in the documentary does it explain how to handle GET requests and send GET requests simultaneously? Please show me if I'm blind.
-
- Posts: 9757
- Joined: Thu Nov 26, 2015 4:08 am
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Okidoki, I see you have everything under control. Good luck!kermitfuckingfrog wrote: ↑Thu Nov 28, 2019 9:40 amNo omg wow thank you so much
seriously.. Of course I have studied this forth and back!
- shabtronic
- Posts: 49
- Joined: Sun Nov 03, 2019 1:33 pm
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Doesn't matter if u have 1 core or 2 - as long as u can create threads!kermitfuckingfrog wrote: ↑Thu Nov 28, 2019 9:37 amSounds interesting. I also think this MUST be possible without using dual core threads..shabtronic wrote: ↑Thu Nov 28, 2019 8:38 amI've just done this on my A1S board - with a old ADF/IDF build.
It's a WifiAP and Http Server for my project - works like a charm! I just used the tcpip stuff.
I used sockets and pthreads - with a simple listener loop handling and dishing out the threads.
it's pretty simple once you get your head around sockets.
I can post part of the code here if u like?
I had to use cpp - tho to make the string handling easier.
once you have a socket it's as simple as
dprintf(ClientSocket, "HTTP/5.0 404 Not Found\r\nConnection: close\r\n");
to send txt data.
But I don't want to send text. I just want to send "192.168.2.81/A" for example to make the ESP8266 turn on the lights.
Maybe you could give me a sneak peak into ur code?
No thread comms - it's all handled by the sockets - as soon as u close a socket - any thread
that uses that socket will error and exit it's thread - super simple.
Here's my code - the main parts:
Code: Select all
pthread_t ServerThread;
pthread_t OneShotThread;
static volatile int ServerCount = 0;
static volatile int server_sock;
// Client Handler Thread
volatile static int ClientThreads = 0;
void* handle_client(void* param)
{
ClientThreads++;
int ClientSocket = (int)param;
#define DEFAULT_BUFLEN 8192
char* recvbuf = (char*)malloc(DEFAULT_BUFLEN);
u_long iMode = 1;
ioctlsocket((int)socket, (long)FIONBIO, (void*)&iMode);
if (recv(ClientSocket, recvbuf, DEFAULT_BUFLEN, 0) > 0)
{
str sbuf = recvbuf; // This is a the txt u wanna decode!
str command = sbuf.substr(0, 4);
str url;
if (sbuf.find("HTTP") != std::string::npos)
{
url = sbuf.substr(4, sbuf.find("HTTP") - 4);
url = trim(urlDecode(url));
printf("Socket %d command:%s url:%s\n", ClientSocket, command.c_str(), url.c_str());
if (fileext(url, ".flac"))
{
SendFile(ClientSocket, "/sdcard" + url);
}
if (fileext(url, ".mp4"))
{
SendFile(ClientSocket, "/sdcard" + url);
}
if (fileext(url, ".webm"))
{
SendFile(ClientSocket, "/sdcard" + url);
}
if (fileext(url, ".wma"))
{
SendFile(ClientSocket, "/sdcard" + url);
}
if (fileext(url, ".mp3"))
{
SendFile(ClientSocket, "/sdcard" + url);
}
if (fileext(url, ".png") || fileext(url, ".jpg") || fileext(url, ".jpeg"))
{
SendFile(ClientSocket, "/sdcard/" + url, "image/jpeg");
}
if (fileext(url, ".ico"))
{
dprintf(ClientSocket, "HTTP/1.0 404 Not Found\r\n");
dprintf(ClientSocket, "Connection: close\r\n\r\n");
}
else if (url.find("/sdcard/") != std::string::npos)
{
GenerateSDCardPage(ClientSocket, url);
}
else
{
GenerateWebPage(ClientSocket);
}
}
}
closesocket(ClientSocket);
ClientThreads--;
free(recvbuf);
return 0;
}
void* ServerTask(void *pvParameters)
{
ServerCount++;
printf("Thread Server Started. ServerCount %d\n", ServerCount);
struct sockaddr_in server_addr, client_addr;
socklen_t sin_size = sizeof(client_addr);
bzero(&server_addr, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = INADDR_ANY;
server_addr.sin_port = htons(80);
server_sock = socket(AF_INET, SOCK_STREAM, 0);
bind(server_sock, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr));
listen(server_sock, 5);
int client_sock = -1;
do {
client_sock = accept(server_sock, (struct sockaddr *) &client_addr, &sin_size);
if (client_sock > -1)
pthread_create(&OneShotThread, NULL, handle_client, (void*)client_sock);
} while (client_sock > -1);
printf("Thread Server Quit\n");
ServerCount--;
return 0;
}
// Switch HTTP Server on or off
void WifiAPServer()
{
static bool WifiRunning = false;
if (!WifiRunning)
{
printf("Starting Wifi AP...\n");
printf("Client Threads %d\n", ClientThreads);
esp_wifi_start();
pthread_create(&ServerThread, NULL, ServerTask, 0);
gpio_set_level((gpio_num_t)BLINK_GPIO, (gpio_num_t)0);
}
else
{
printf("Stopping Wifi AP...\n");
closesocket(server_sock); // this will shut down the main thread !
esp_wifi_stop();
printf("Client Threads %d\n", ClientThreads);
gpio_set_level((gpio_num_t)BLINK_GPIO, (gpio_num_t)1);
}
WifiRunning = !WifiRunning;
}
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
The HTTP protocol requires that you send text to the server in order to access an URL. For example, to GET the URL "http://192.168.2.81/A" you need to send at least the following text after opening a socket connection to the server:kermitfuckingfrog wrote: ↑Thu Nov 28, 2019 9:37 amSounds interesting. I also think this MUST be possible without using dual core threads..
But I don't want to send text. I just want to send "192.168.2.81/A" for example to make the ESP8266 turn on the lights.
Code: Select all
GET /A HTTP/1.1
Host: 192.168.2.81
Using a library like HttpClient makes things easier as you don't need to take care of all the details of the protocol by yourself.
-
- Posts: 6
- Joined: Wed Nov 27, 2019 6:22 pm
Re: GRRRR How can I use ESP32 to SEND and RECEIVE GET Requests simultaneously?
Hey I got it working now. But I'm facing a new problem. My watchdog get's triggered while or before sending a GET request.markkuk wrote: ↑Thu Nov 28, 2019 12:17 pmThe HTTP protocol requires that you send text to the server in order to access an URL. For example, to GET the URL "http://192.168.2.81/A" you need to send at least the following text after opening a socket connection to the server:kermitfuckingfrog wrote: ↑Thu Nov 28, 2019 9:37 amSounds interesting. I also think this MUST be possible without using dual core threads..
But I don't want to send text. I just want to send "192.168.2.81/A" for example to make the ESP8266 turn on the lights.The empty line after the "Host:" header is a mandatory part of the protocol.Code: Select all
GET /A HTTP/1.1 Host: 192.168.2.81
Using a library like HttpClient makes things easier as you don't need to take care of all the details of the protocol by yourself.
E (12693) task_wdt: Task watchdog got triggered. The following tasks did not reset the watchdog in time:
E (12693) task_wdt: - async_tcp (CPU 0/1)
E (12693) task_wdt: Tasks currently running:
E (12693) task_wdt: CPU 0: IDLE0
E (12693) task_wdt: CPU 1: loopTask
E (12693) task_wdt: Aborting.
Here's my code (I marked the point where the WDT happens with "==================WATCHDOG ERROR================="
http://peerless-records.de/public_share/code.txt
Who is online
Users browsing this forum: No registered users and 78 guests