// 向服务器发送数据 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(); } }
第四步,定义TCP服务器事件回调函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 处理数据 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); }