简体   繁体   English

在微控制器和MQTT上使用的数据格式

[英]Data format to use on a micro controller and MQTT

I'm using MQTT on an Arduino to send sensors data to a Javascript page. 我在Arduino上使用MQTT将传感器数据发送到Javascript页面。

Until now I've sent only simple data format: int or floats. 到目前为止,我仅发送了简单的数据格式:int或float。

What format can I use to send more complex, structured data? 我可以使用哪种格式发送更复杂的结构化数据? for example: 例如:

{ "temperature": 32, "humidity": 67 }

I'd like to send it on a single MQTT message. 我想在一条MQTT消息上发送它。

I could use Json, but I fear it's a bit heavy for an Arduino. 我可以使用Json,但我担心它对于Arduino来说有点沉重。


Update 更新

Ok, seems to be a general consensus on JSON, however I'm a bit concerned about the memory requirement. 好的,这似乎是关于JSON的普遍共识,但是我有点担心内存需求。

I've found two libraries for json encoding/decoding on an Arduino: 我在Arduino上找到了两个用于json编码/解码的库:

ArduinoJson claims to be less memory consuming, however doesn't support: ArduinoJson声称消耗较少的内存,但是不支持:

  • reading from Stream ( latest versions of the Arduino MQTT library supports receiving messages in streams) 从Stream读取( 最新版本的Arduino MQTT库支持在流中接收消息)
  • filtering of incoming json (to selectively parse only required Json fields) 过滤传入的json(以仅选择性地解析必需的Json字段)

Use the topics themselves to structure the data: 使用主题本身来构建数据:

/home/sensors/count 2
/home/sensors/sensor1/name Kitchen
/home/sensors/sensor1/temperature 21
/home/sensors/sensor1/humidity 47
/home/sensors/sensor2/name Upstairs
/home/sensors/sensor2/temperature 23
/home/sensors/sensor2/humidity 48

Publishing many simple messages is easier for a resource constrained Arduino. 对于资源受限的Arduino,发布许多简单消息更加容易。 The alternative is constructing some large JSON string like you are thinking: 替代方法是按照您的想法构造一些​​大型JSON字符串:

/home/sensors = {{"name":"Kitchen", "temperature": 21, "humidity": 47 },
                 {"name":"Upstairs", "temperature": 23, "humidity": 48 }}

Also, I think the topics is the more useful design (API) for the both publisher and client. 另外,我认为主题对于发布者和客户来说都是更有用的设计(API)。 For the Arduino side, consider how the topic approach does not force every location to have both temperature and humidity sensors? 对于Arduino方面,请考虑主题方法如何不强制每个位置都具有温度和湿度传感器? For the client side, the topic approach allows a use case that shows just the humidity. 对于客户端,主题方法允许一个仅显示湿度的用例。

The way I do it is just publish the temp in Celsius (or Faranheit) from the Arduino to the broker for example 我这样做的方法只是将摄氏温度(或Faranheit)从Arduino发布到代理

/raw/sensorid temp

Then on the broker I transform it all into JSON including adding a timestamp and republish to another topic. 然后在代理上,将其全部转换为JSON,包括添加时间戳并重新发布到另一个主题。 I find it easier this way as I don't need to change the arduino code if I move the sensor or have replace it. 我发现这种方式更容易,因为如果我移动传感器或更换传感器,则无需更改arduino代码。 Here is the code I use if you are interested, it's using Onewire sensors. 如果您有兴趣, 是我使用的代码,它使用的是Onewire传感器。

For example; 例如;

Arduino Publishes:
/raw/DE24532156 10


On my broker I Transforms the data to:
/house/temps {"location":"outside","tempC":10,"tempF":50,"average":14,"max":16,"min":8,"timestamp":12234234}

I do all my transformation in python using mqttwarn . 我使用mqttwarn在python中进行所有转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM