how to implement a function with char array on arduino

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?

Note, I am completely out of my pay scale here, but try this:

void sendDataToClient( const char *sbuf) {
  size_t len = sizeof(sbuf);
  for (i = 0; i < MAXCLIENTS; i++) {
    if (serverClients[i] && serverClients[i].connected()) {
      serverClients[i].write((char*) &sbuf, len);
      delay(1);
    }
  }
}

size_t len = sizeof(sbuf); <- I don't think this is doing what you think its doing. I'm pretty sure its just going to give you the size of a char. Could be wrong though..

int len = strLen(sbuf); <- may work better. Giving you the number of chars in the string. Not including the trailing '\0'.

-jim lee

SteveMann and jimLee,

I already made the change of the sizeof by strlen and *sbuf, now I can already get the number of characters but still I still the client cannot receive the characters

Well, can't help because, I've no idea what..

serverClients_.write((char*) &sbuf, len);_
serverClients write wants.
Well, I'll take a guess..
possibly?..
serverClients_.write((char*) &(sbuf[0]), len+1); // +1 in case you forgot the '\0'_
-jim lee

jimLee,

using this 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);
   }
 }

I have no problems, it works OK, the client receives the string, the problem occurs when I try to implement the function to call it when I need it

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);
  }
 }
}

void sendDataToClient( char *sbuf[]) {

I'd write it..

void sendDataToClient( char* sbuf) {

Also, if your thing expects a c string..

size_t len = strlen(sbuf)+1;

IF its only expecting the chars.. then don't bother on that.

-jim lee

void sendDataToClient( char *sbuf[]) {

You are passing a character pointer OR an array of characters. You are telling the function to expect an array of character pointers. I'm surprised you didn't get a compiler warning.

In Preferences, set compiler warnings to "All". Look at the warnings that show up in the text box below your sketch when you do a compile. Often they will point out mistaken assumptions.