Harmony Clean Flat Responsive WordPress Blog Theme

sending JSON file through MQTT protocol in node.js

오전 8:49 Unknown 0 Comments Category :

This time, I made some codes for end devices and gateway. For end devices, I made a code to transport a JSON message to gateway. JSON message has a random number, sequence number, and device name so that the gateway can know where this data come from. I put the sequence number because the gateway need to know each of data that gateway just got is right data or late one. This is the code what I made.


Function json_data() makes a json message which has a device name, data, and sequence number. function setNum() is for random number by using Math package. On the mqtt, we can transport only buffer or string message not JSON. So we need to change JSON to string by using 'stringify'.  In result, this code send a JSON message per 3 seconds.

In gateway, there is a switch to turn on and off the subscribe. The 'count' is a switch to turn on and off subscribe per 3 seconds. When gateway get a JSON message, it checks a sequence number with count so that gateway know this data is right one or not. And then publish a JSON message after changing JSON to string. The code is followed:

This below picture shows that the network structure between end devices and gateway.

End devices sends a JSON message to gateway and gateway check weather this data is right or wrong one. Gateway gathers all JSON message and send to server(Will make it soon). It was hard to syncronize time between end device and gateway and this code is not perfect yet. I will make it up percectly as possible as I can. Thanks..












RELATED POSTS

0 개의 댓글