kermitfuckingfrog wrote: ↑Thu Nov 28, 2019 9:37 am
shabtronic wrote: ↑Thu Nov 28, 2019 8:38 am
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.
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.
Sounds 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.
Maybe you could give me a sneak peak into ur code?
Doesn't matter if u have 1 core or 2 - as long as u can create threads!
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;
}