Can anyone please show me how i can send and receive a Struct with ESPAsyncTCP server? I have the Client/Server examples running but i'm really struggling.
Here is a link to their github
Can anyone please show me how i can send and receive a Struct with ESPAsyncTCP server? I have the Client/Server examples running but i'm really struggling.
Here is a link to their github
My client example code,
#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 1 %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() {
}
In the part,
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 1 %s", WiFi.localIP().toString().c_str());
client->add(message, strlen(message));
client->send();
}
}
I have tried to do this,
struct test{
int var0 =21;
int var1 =55;
bool val2 =true;
};
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 1 %s", WiFi.localIP().toString().c_str());
client->add(test, sizeof(test));
client->send();
}
}
but it wont compile. How can i send that structure?
Am i limited to sending strings? this dont sound right. please forgive my knowledge. I have searched for documentation on the AsyncTCP Server but i didnt find any.