Verze s opravou unacked
This commit is contained in:
62
examples/ClientServer/Client/Client.ino
Normal file
62
examples/ClientServer/Client/Client.ino
Normal file
@ -0,0 +1,62 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESPAsyncTCP.h>
|
||||
|
||||
extern "C" {
|
||||
#include <osapi.h>
|
||||
#include <os_type.h>
|
||||
}
|
||||
|
||||
#include "config.h"
|
||||
|
||||
static os_timer_t intervalTimer;
|
||||
|
||||
static void replyToServer(void* arg) {
|
||||
AsyncClient* client = reinterpret_cast<AsyncClient*>(arg);
|
||||
|
||||
// send reply
|
||||
if (client->space() > 32 && client->canSend()) {
|
||||
char message[32];
|
||||
sprintf(message, "this is from %s", WiFi.localIP().toString().c_str());
|
||||
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);
|
||||
|
||||
os_timer_arm(&intervalTimer, 2000, true); // schedule for reply to server at next 2s
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// connects to access point
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(SSID, PASSWORD);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
Serial.print('.');
|
||||
delay(500);
|
||||
}
|
||||
|
||||
AsyncClient* client = new AsyncClient;
|
||||
client->onData(&handleData, client);
|
||||
client->onConnect(&onConnect, client);
|
||||
client->connect(SERVER_HOST_NAME, TCP_PORT);
|
||||
|
||||
os_timer_disarm(&intervalTimer);
|
||||
os_timer_setfn(&intervalTimer, &replyToServer, client);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
}
|
23
examples/ClientServer/Client/config.h
Normal file
23
examples/ClientServer/Client/config.h
Normal file
@ -0,0 +1,23 @@
|
||||
#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