EFR32MG多协议SoC芯片是 Silicon Labs 专门针对 IOT 推出的 ZigBee 方案,高达19.5dbm的发射功率,-102.7dbm的接收灵敏度,在业界内拥有最佳的RF射频性能,具有最全的Zigbee网络协议,被广泛应用在智能家居,工业控制等行业。
在开发 Zigbee Gateway 时,有时候需要自定义消息类型发送给 Mqtt Server 端,因此我们要 publish 自定义的数据。
下面的参考代码是用户自定义的 deviceannounce topic 的数据。
static void publishMqttDeviceAnnounce(EmberEUI64 eui64,EmberNodeId nodeId,uint8_t capInfo);//add by sven static void publishMqttDeviceAnnounce(EmberEUI64 eui64,EmberNodeId nodeId,uint8_t capInfo) { //This function call breaks simulation test, mask it out //at this point as Mqtt messages are actually not included //in simluation test. It works normally with the real devices. #ifndef EMBER_TEST cJSON* deviceAnnounceJson; char euiString[EUI64_STRING_LENGTH] = { 0 }; char nodeIdString[NODEID_STRING_LENGTH] = { 0 }; eui64ToString(eui64, euiString); nodeIdToString(nodeId, nodeIdString); deviceAnnounceJson = cJSON_CreateObject(); cJSON_AddStringToObject(deviceAnnounceJson, "nodeId", nodeIdString); cJSON_AddStringToObject(deviceAnnounceJson, "eui64", euiString); cJSON_AddIntegerToObject(deviceAnnounceJson, "capabilityInfo", capInfo); publishMqttTopic("deviceannounce", deviceAnnounceJson); #endif //EMBER_TEST }
在这里参考代码中包含了 cJSON 数据结构的创建,赋值及发送的整个流程。不同类型需要调用不同的函数,例如:
#define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
#define cJSON_AddDoubleToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateDouble(n))
#define cJSON_AddIntegerToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateInteger(n))
#define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))