Pages: 1 [2]   Go Down
Author Topic: Strings, bytes, char arrays MQTT arghgh  (Read 2386 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As long as you are confident that this is OK. That payload and length are passed to you. Modifying one past the end of the payload may be corrupting memory.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As long as you are confident that this is OK. That payload and length are passed to you. Modifying one past the end of the payload may be corrupting memory.

ahh OK - your way does may sense.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Actually, when I try to do it with:

Code:
lcd.print(payload,length);

I see this error:

"call of overloaded 'print(byte* &, unsigned int&)' is ambiguous"
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Assuming lcd is derived from Print, try:

Code:
lcd.write(payload,length);
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 66
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As far as I understand the MQTT format, the content of the payload variable is not defined (by MQTT) but can be anything. You seem to opt for an ASCII based protocol which is easy to debug and easily portable but not as efficient as a binary format would be.
If you choose an ASCII format, I would still strongly suggest to define standard structure of such a string, like "TYPE:POSITION:VALUE", so in your example it would not be "PUMP:ON" but "PUMP:1:1" or "VALVE:1:0" instead of "VALVE:1:OPEN". This makes it much easier to parse the incoming message and organise the call distribution. You can use strtok() then to split the message into the type, position and value. Position and value can be fed to atoi immediately after the split up and you already have integers.

The conversion from a byte array to a character array is simply a type cast:

Code:
char *cstring = (char *) payload;

I know this is an old thread now but I have resurrected this project and I am interested in the binary based format you mentioned above. I cant work out how to implement this. 
Logged

Pages: 1 [2]   Go Up
Jump to: