Harmony Clean Flat Responsive WordPress Blog Theme

Communication among devices in JSON message through MQTT

오전 7:41 Unknown 0 Comments Category :

This time, I made 3 layer which are device, gateway and server. The device publishes a JSON message to gateway, the server gets a message from gateway and sends a message to gateway, and the gateway gets message from both device and server and sends a message what device sent to server. This is a structure for this code.

pi 1 is a device, pi2 is a gateway and PC is a server as I said.

This is device(pi1) code:
---------------------------------------------------------------------------------------------------
var mqtt = require('mqtt'); 
var pi3_01 = mqtt.connect('mqtt://192.168.0.11'); 
var num = 0;
var count = 0;
var frequency = 5000;
 
function setPub(frequency)
{

setInterval(function() {
setNum();
count++;
console.log('frequency--->'+ frequency);
 
pi3_01.publish('pi3_02', JSON.stringify({From: 'pi3_02', seq: + count, data: +num}));
console.log('-------------------');
console.log(JSON.stringify({From: 'pi3_01', seq: + count, data: +num}));
}, frequency);
}
 
function setNum()
{
num = Math.floor(Math.random() *100) +1;
}
 
pi3_01.on('connect', function() {
console.log('pi3_01 on');
setPub(frequency);
});
---------------------------------------------------------------------------------------------------


This is gateway(pi2) code:
---------------------------------------------------------------------------------------------------
 
pi3_01.on('message', function(topic, message)
{
var jsontext = JSON.parse(message);

if(topic =='pi3_02')
{
PC.publish('pi3_01', JSON.stringify({seq: +jsontext.seq,
From: jsontext.From, data: +jsontext.data}));
console.log(jsontext);
}
if(topic =='PC')
console.log(jsontext)
});
 

function sub()
{
pi3_01.subscribe('pi3_02');
pi3_01.subscribe('PC');
}
 
function unsub()
{
pi3_01.unsubscribe('pi3_02');
pi3_01.unsubscribe('PC');
console.log('+++++++++++++++++++++++++++++++++++++++++');
}
loop();
--------------------------------------------------------------------------------------------------

This gateway opens and closes subscribe frequently( every 5seconds ) so that gateway can save power when it closes subscribe. 


This is server(PC) code:
--------------------------------------------------------------------------------------------------
var mqtt = require('mqtt');
var PC = mqtt.connect('mqtt://192.168.0.15'); //PC Broker ip
var pi3_01 = mqtt.connect('mqtt://192.168.0.11'); //Gateway Broker ip
 
PC.subscribe('pi3_01');

PC.on('message', function(topic, message) {
var jsontext = JSON.parse(message);
console.log(jsontext);
pi3_01.publish('PC', JSON.stringify({From: 'PC', Message: 'I got it'}));
});
--------------------------------------------------------------------------------------------------

The result is followed:

devices


Device send JSON which has a device's name, sequence number and data(random number).

Gateway 



 Gateway sends a message what it get from device and get a message from server. The server send a message that there was no problem.


server


This server shows what it got from gateway and send a message to gate that it 
So, there are 3 layers(device, gateway, and server) that  communicate each other.



RELATED POSTS

0 개의 댓글