I am doing a sketch using an ESP8266 as a WiFi server, I need to send a character string to the Client, for this I am using this piece of code:
char sbuf[] = "Hello world!\n\r";
size_t len = strlen(sbuf);
for (i = 0; i < MAXCLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write((char*) &sbuf, len);
delay(1);
}
}
It works OK, the client can receive the characters.
But now I want to make a function to call it the times I need it, this is the function code:
void sendDataToClient( char *sbuf[]) {
size_t len = strlen(sbuf);
for (i = 0; i < MAXCLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write((char*) &sbuf, len);
delay(1);
}
}
}
That's I'm calling the function:
char bufferMSGtoClient[] = "Hello world!\n\r";
sendDataToClient(bufferMSGtoClient)
But it doesn't work, the client does not receive anything, someone can tell me what is my error?
sizeof doesn’t do what you think it does here. You’re getting 4 because that’s the size of a pointer. Use strlen instead. It will tell you how long up to the \n. Add two if you want the \r too.
void sendDataToClient( char *sbuf[]) {
This line is also not doing what you think. It is passing an array of pointers. You either want a pointer OR an array, but certainly not an array of pointers.
void sendDataToClient( char *sbuf) {
and
serverClients[i].write((char*) &sbuf, len);
should be
serverClients[i].write(sbuf, len);
I'm betting.