timum-viw / socket.io-client

A socket.io-client implementation for ESP8266 and Arduino
228 stars 90 forks source link

Concatenate variable int in emit #22

Closed OmarGC closed 6 years ago

OmarGC commented 6 years ago

How to concatenate variable double or int to socket.emit ("event name", "\" CHAIN ​​TO SEND \ "")

Achieve send the normal chain, but the ideal is to send the variable that comes from the sensor. the question is that I put the structure + String (dataInteger) + but that sends me error.

example

`int dis = 88;`
`webSocket.emit ( "Esp8866_1" , "\" Cadena a enviar "+String(dis)+"\" " );`
timum-viw commented 6 years ago

What error exactly are you getting?

OmarGC commented 6 years ago

/home/omargc/Escritorio/NodeMCU/funcinales/SocketIO_BasicExample/SocketIO_BasicExample.ino: In function 'void setup()': SocketIO_BasicExample:46: error: no matching function for call to 'SocketIoClient::emit(const char [10], StringSumHelper&)' webSocket.emit ( "Esp8866_1" , "\" Cisterna: "+String(dis)+"\" " ); ^ /home/omargc/Escritorio/NodeMCU/funcinales/SocketIO_BasicExample/SocketIO_BasicExample.ino:46:70: note: candidate is: In file included from /home/omargc/Escritorio/NodeMCU/funcinales/SocketIO_BasicExample/SocketIO_BasicExample.ino:2:0: /home/omargc/Arduino/libraries/SocketIoClient/SocketIoClient.h:39:7: note: void SocketIoClient::emit(const char*, const char*) void emit(const char* event, const char * payload = NULL); ^ /home/omargc/Arduino/libraries/SocketIoClient/SocketIoClient.h:39:7: note: no known conversion for argument 2 from 'StringSumHelper' to 'const char*' exit status 1 no matching function for call to 'SocketIoClient::emit(const char [10], StringSumHelper&)'

OmarGC commented 6 years ago

Then I converted to char and sent me this error. what I want is to send sensor variable by emit

/home/omargc/Escritorio/NodeMCU/funcinales/SocketIO_BasicExample/SocketIO_BasicExample.ino: In function 'void setup()': SocketIO_BasicExample:46: error: invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+' webSocket.emit ( "Esp8866_1" , "\" Cisterna: "+char(dis)+"\" " ); ^ exit status 1 invalid operands of types 'const char*' and 'const char [3]' to binary 'operator+'

timum-viw commented 6 years ago

The concatenation results in a String object. You have to convert it to char *. Try webSocket.emit ( "Esp8866_1" , ("\" Cisterna: "+String(dis)+"\" ").c_str() );

OmarGC commented 6 years ago

Perfect now he does not send me error Thank you

timum-viw commented 6 years ago

You're welcome!