I'm using ESPAsyncTCP with an ESP8266. I want to modify the program to send a larger amount of data. currently it is setup to send a single string but i want to send an array or struct. I need a way to orginize the data. I have a list of variables i would like to send to another esp module but how would i do this?
this is the example code im using i added a structer called sampleStruct this is the data i would like to send over tcp.
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
extern "C" {
#include <osapi.h>
#include <os_type.h>
}
#include "config.h"
struct sampleStruct {
int var1;
float var2;
byte Array[20];
unsigned long var3;
unsigned long var4;
unsigned long var5;
unsigned long var6;
unsigned long var7;
unsigned long var8;
bool var9;
};
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() {
}
I've had a hard time trying to find documentation on this AsyncTCP server. Would i send each variable 1 at a time? How can i achieve what im trying to do?
I really need some advice please.
Looks to me like you would change:
char message[32];
sprintf(message, "this is from %s", WiFi.localIP().toString().c_str());
client->add(message, strlen(message));
to:
client->add(ArrayOfStructs, sizeof ArrayOfStructs);
johnwasser:
Looks to me like you would change:
char message[32];
sprintf(message, "this is from %s", WiFi.localIP().toString().c_str());
client->add(message, strlen(message));
to:
client->add(ArrayOfStructs, sizeof ArrayOfStructs);
I tried "client->add(sampleStruct, sizeof sampleStruct);"
when i try to compile if gives me this error,
expected primary-expression before ',' token
so i tried "client->add(&sampleStruct, sizeof sampleStruct);"
and got the same result. I have not found any documentation how to use the library either
I also tried this,
struct sampleStruct {
int var1;
byte Array[20];
float var2;
unsigned long var3;
unsigned long var4;
unsigned long var5;
unsigned long var6;
unsigned long var7;
unsigned long var8;
bool var9;
};
sampleStruct st;
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();
client->add(&st, sizeof(sampleStruct));
}
}
i get a different error,
exit status 1
no matching function for call to 'AsyncClient::add(sampleStruct*, unsigned int)'
Yes. Apparently you have to cast the pointer:
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->add((char *)&st, sizeof(sampleStruct));
client->send();
}
}
The error message tells you the line and character at which the error was detected.
Your previous error ("expected primary-expression before ',' token") was caused by trying to use a data type as a variable. The type 'enum sampleStruct' doesn't have an address. The variable 'st' does.
johnwasser:
Yes. Apparently you have to cast the pointer:
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->add((char *)&st, sizeof(sampleStruct));
client->send();
}
}
The error message tells you the line and character at which the error was detected.
Your previous error ("expected primary-expression before ',' token") was caused by trying to use a data type as a variable. The type 'enum sampleStruct' doesn't have an address. The variable 'st' does.
Thankyou that did the trick. I really really really appreciate the help