0
   d = random,randint(1,30)
   data = [d, strftime("%Y%m%d %H%M%S", gmtime())] #random num , system time
   client.publish("gas", str(data)]

This is a part of my python code which is ver2. I'm trying to send a list using MQTT. However, If I write bytearray instead of str which is third line It says "ValueError: string must be of size 1". So I wrote str then make it sting type Can I send a just list which is NOT string type.

ahnstar
  • 55
  • 1
  • 3
  • 7

1 Answers1

1

MQTT message payloads are just byte arrays, there is no inherent format to them. Strings tend to works as long as both ends of the transaction are using the same character encoding.

If you want to send structured data (such as the ost) then you need to decide on a way to encode that structure so the code receiving the message will know how to reconstruct it.

The current usual solution to this problem is to encode structures are JSON, but XML or something like protobuffers are also good candidates.

The following question has some examples of converting Python lists to JSON objects

Serializing list to JSON

Community
  • 1
  • 1
hardillb
  • 54,545
  • 11
  • 67
  • 105