-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtt_handler.js
64 lines (45 loc) · 1 KB
/
mqtt_handler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
var mqtt = require('mqtt');
var serverUrl= "mqtt://10.0.0.9";
var airTopic = "nodePlant/air";// air node (sub only)
var soilTopic="nodePlant/soil";// soil node (sub)
//var mCtrlTopic="nodePlant/motor";//topic for controlling motor
var client=mqtt.connect(serverUrl, {clientID:"myPi"});
var airMsg="";
var soilMsg="";
client.on("connect",function(){
client.subscribe(airTopic,function(err){
if (err) throw err;
});
client.subscribe(soilTopic,function(err){
if(err) throw err;
});
});
client.on("message",function(airTopic,message){
//console.log(message);
airMsg = message;
});
client.on("message",function(soilTopic,message){
soilMsg=message;
});
//export variables
module.exports={
/*
sendCommad: function (var command)
{
client.publish(mCtrlTopic,command);
client.end();
}
getSoilData: function()
{
return soilMsg;
}
getAirData: function()
{
return airMsg;
}
*/
};
client.on("error",function(err){
throw err;
process.exit(1);
});