-
-
Save estebanrfp/f9317ec5dd864f796a9abb743fc74360 to your computer and use it in GitHub Desktop.
Simple MQTT Server. Tessel acts as an MQTT client sending temperature data to a host
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mqtt = require('mqtt') | |
// Make sure to change this to the IP address of your MQTT server | |
, host = '192.168.128.204' // or localhost | |
client = mqtt.createClient(1883, host, {keepalive: 10000}); | |
// Subscribe to the temperature topic | |
client.subscribe('temperature'); | |
// When a temperature is published, it will show up here | |
client.on('message', function (topic, message) { | |
console.log("Got a message!", topic, message); | |
}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Pulled directly from the MQTT example folder | |
// https://github.com/adamvr/MQTT.js/blob/master/examples/server/broadcast.js | |
var mqtt = require('mqtt') | |
, util = require('util'); | |
mqtt.createServer(function(client) { | |
var self = this; | |
if (!self.clients) self.clients = {}; | |
client.on('connect', function(packet) { | |
self.clients[packet.clientId] = client; | |
client.id = packet.clientId; | |
console.log('client connected!', client.id); | |
client.subscriptions = []; | |
client.connack({returnCode: 0}); | |
}); | |
client.on('subscribe', function(packet) { | |
var granted = []; | |
for (var i = 0; i < packet.subscriptions.length; i++) { | |
var qos = packet.subscriptions[i].qos | |
, topic = packet.subscriptions[i].topic | |
, reg = new RegExp(topic.replace('+', '[^\/]+').replace('#', '.+') + '$'); | |
granted.push(qos); | |
client.subscriptions.push(reg); | |
} | |
client.suback({messageId: packet.messageId, granted: granted}); | |
}); | |
client.on('publish', function(packet) { | |
for (var k in self.clients) { | |
var c = self.clients[k]; | |
for (var i = 0; i < c.subscriptions.length; i++) { | |
var s = c.subscriptions[i]; | |
if (s.test(packet.topic)) { | |
c.publish({topic: packet.topic, payload: packet.payload}); | |
break; | |
} | |
} | |
} | |
}); | |
client.on('pingreq', function(packet) { | |
client.pingresp(); | |
}); | |
client.on('disconnect', function(packet) { | |
client.stream.end(); | |
}); | |
client.on('close', function(packet) { | |
delete self.clients[client.id]; | |
}); | |
client.on('error', function(e) { | |
client.stream.end(); | |
console.log(e); | |
}); | |
}).listen(process.argv[2] || 1883); | |
console.log('listening for clients!'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mqtt = require('mqtt') | |
// Make sure to replace this line with the IP Address of your MQTT server | |
, host = '192.168.128.204' | |
, port = 1883 | |
, client = mqtt.createClient(port, host, {keepalive: 10000}) | |
, tessel = require('tessel') | |
, climate = require('climate-si7020').use(tessel.port['A']); | |
climate.on('ready', function ready() { | |
console.log('climate ready'); | |
// We will set an interval of 5 seconds to make sure we don't use up all of Tessel's 4 sockets | |
setInterval(function() { | |
// Read the temperature | |
climate.readTemperature(function(err, temperature) { | |
// If there was no error | |
if (!err) { | |
console.log('publishing temp', temperature); | |
// Publish the string representation of the temperature | |
client.publish('temperature', temperature.toString()); | |
} | |
}); | |
}, 5000); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment