0

How to publish a uint8_t value?
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

How to publish a uint8_t value?

by NIF on Sat Jan 06, 2018 4:21 am

Im publishing a uint8_t array with some values that are not character ( 0x01 , 0x04). when im subscribing to that feed im not getting the same values back?

NIF
 
Posts: 4
Joined: Tue Dec 26, 2017 12:13 pm

Re: How to publish a uint8_t value?

by abachman on Mon Jan 08, 2018 1:02 pm

Hi NIF!


How are you publishing the values? Are you encoding it at all before publishing?

IO stores all values as Unicode (utf8 binary encoded) strings, so you'll need to encode your data in a compatible format if you want it eventually use it in a particular format.

If you have some example code, that would be helpful for suggesting a strategy in your case.


- adam

abachman
 
Posts: 230
Joined: Mon Feb 01, 2010 12:48 pm

Re: How to publish a uint8_t value?

by NIF on Wed Jan 10, 2018 1:19 pm

Hi Adam...
Im just reading data from the serial port, puting it in a uint8_t array and sending it away using this
Adafruit_MQTT_Publish::publish(uint8_t *payload, uint16_t bLen).

NIF
 
Posts: 4
Joined: Tue Dec 26, 2017 12:13 pm

Re: How to publish a uint8_t value?

by abachman on Thu Jan 11, 2018 4:27 pm

Hey NIF,


I'm seeing the issue you're describing. After some thorough digging through our code and platform, it looks like this may not end up working for you as cleanly as I'd like. The root issue is that we're converting all data to UTF-8 string before storing it.

Unfortunately, since we're using UTF-8 as our internal storage representation for feed data, we are unable to store and forward some byte sequences since some byte sequences (like you're finding) aren't representable as UTF-8 strings. If we were just storing binary blobs what you're describing would work, but then we'd lose the ability to easily do things like aggregation and charting for numerical data and I suspect it would require an extra amount of configuration for every feed. The case you're facing is the unfortunate tradeoff.

The quickest solution I can see is to come up with a string-compatible representation that you can push to IO that's easy to read out. If you control both ends and your requirements are relatively fixed, you could use String-based join/split functions to manage the arrays (full sketch here):
Code: Select all | TOGGLE FULL SIZE
// join the given uint8_t array into a string separated by the given character
String join(uint8_t vals[], char sep, int items) {
  String out = "";
 
  for (int i=0; i<items; i++) {
    out = out + String(vals[i]);
    if ((i + 1) < items) {
      out = out + sep;
    }
  }

  return out;
}

// populate the given uint8_t array with values contained in the given String
void split(String value, char sep, uint8_t vals[], int items) {
  int count = 0;
  char *val = strtok((char *)value.c_str(), &sep);
 
  while (val != NULL && count < items) {
    vals[count++] = (uint8_t)atoi(val);
    val = strtok(NULL, &sep);
  }
}


`join()` the array before publishing, and `split()` the incoming data to get the array back out. Small consolation is that "102 121 40 129" is more readable from a dashboard than "fy(?".

- adam

abachman
 
Posts: 230
Joined: Mon Feb 01, 2010 12:48 pm

Please be positive and constructive with your questions and comments.