forked from me-no-dev/AsyncTCP
Try fix #26 again
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
-D CONFIG_ASYNC_TCP_MAX_ACK_TIME=3000
|
||||
-D CONFIG_ASYNC_TCP_PRIORITY=10
|
||||
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=128
|
||||
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1
|
||||
-D CONFIG_ASYNC_TCP_STACK_SIZE=4096
|
||||
*/
|
||||
#include <Arduino.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#define HOST "homeassistant.local"
|
||||
#define PORT 8123
|
||||
|
||||
size_t permits = CONFIG_LWIP_MAX_ACTIVE_TCP;
|
||||
|
||||
void makeRequest() {
|
||||
if (!permits)
|
||||
return;
|
||||
|
||||
Serial.printf("** permits: %d\n", permits);
|
||||
|
||||
AsyncClient* client = new AsyncClient;
|
||||
|
||||
client->onError([](void* arg, AsyncClient* client, int8_t error) {
|
||||
Serial.printf("** error occurred %s \n", client->errorToString(error));
|
||||
client->close(true);
|
||||
delete client;
|
||||
});
|
||||
|
||||
client->onConnect([](void* arg, AsyncClient* client) {
|
||||
permits--;
|
||||
Serial.printf("** client has been connected: %" PRIu16 "\n", client->localPort());
|
||||
|
||||
client->onDisconnect([](void* arg, AsyncClient* client) {
|
||||
Serial.printf("** client has been disconnected: %" PRIu16 "\n", client->localPort());
|
||||
client->close(true);
|
||||
delete client;
|
||||
|
||||
permits++;
|
||||
makeRequest();
|
||||
});
|
||||
|
||||
client->onData([](void* arg, AsyncClient* client, void* data, size_t len) {
|
||||
Serial.printf("** data received by client: %" PRIu16 "\n", client->localPort());
|
||||
// Serial.write((uint8_t*)data, len);
|
||||
});
|
||||
|
||||
client->write("GET / HTTP/1.1\r\nHost: " HOST "\r\nUser-Agent: ESP\r\nConnection: close\r\n\r\n");
|
||||
});
|
||||
|
||||
if (client->connect(HOST, PORT)) {
|
||||
} else {
|
||||
Serial.println("** connection failed");
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
continue;
|
||||
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin("IoT");
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
}
|
||||
Serial.println("** connected to WiFi");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// 16 slots on esp32 (CONFIG_LWIP_MAX_ACTIVE_TCP)
|
||||
for (size_t i = 0; i < CONFIG_LWIP_MAX_ACTIVE_TCP; i++)
|
||||
makeRequest();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
delay(500);
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <AsyncTCP.h>
|
||||
|
||||
#include "config.h"
|
||||
|
||||
static void replyToServer(void* arg) {
|
||||
AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);
|
||||
|
||||
// send reply
|
||||
if (client->space() > 32 && client->canSend()) {
|
||||
char message[32];
|
||||
client->add(message, strlen(message));
|
||||
client->send();
|
||||
}
|
||||
}
|
||||
|
||||
/* event callbacks */
|
||||
static void handleData(void* arg, AsyncClient* client, void *data, size_t len) {
|
||||
Serial.printf("\n data received from %s \n", client->remoteIP().toString().c_str());
|
||||
Serial.write((uint8_t*)data, len);
|
||||
|
||||
}
|
||||
|
||||
void onConnect(void* arg, AsyncClient* client) {
|
||||
Serial.printf("\n client has been connected to %s on port %d \n", SERVER_HOST_NAME, TCP_PORT);
|
||||
replyToServer(client);
|
||||
}
|
||||
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
delay(20);
|
||||
|
||||
AsyncClient* client = new AsyncClient;
|
||||
client->onData(&handleData, client);
|
||||
client->onConnect(&onConnect, client);
|
||||
client->connect(SERVER_HOST_NAME, TCP_PORT);
|
||||
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
|
||||
/*
|
||||
* This example demonstrate how to use asynchronous client & server APIs
|
||||
* in order to establish tcp socket connections in client server manner.
|
||||
* server is running (on port 7050) on one ESP, acts as AP, and other clients running on
|
||||
* remaining ESPs acts as STAs. after connection establishment between server and clients
|
||||
* there is a simple message transfer in every 2s. clients connect to server via it's host name
|
||||
* (in this case 'esp_server') with help of DNS service running on server side.
|
||||
*
|
||||
* Note: default MSS for ESPAsyncTCP is 536 byte and defualt ACK timeout is 5s.
|
||||
*/
|
||||
|
||||
#define SSID "ESP-TEST"
|
||||
#define PASSWORD "123456789"
|
||||
|
||||
#define SERVER_HOST_NAME "esp_server"
|
||||
|
||||
#define TCP_PORT 7050
|
||||
#define DNS_PORT 53
|
||||
|
||||
#endif // CONFIG_H
|
||||
Reference in New Issue
Block a user