Created
August 28, 2016 01:46
-
-
Save jackrobotics/846c897ddd6a94839cf8d84060c585da to your computer and use it in GitHub Desktop.
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 fs = require('fs'); | |
// function to create file from base64 encoded string | |
function base64_decode(base64str, file) { | |
// create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded | |
var bitmap = new Buffer(base64str, 'base64'); | |
// write buffer to file | |
fs.writeFileSync(file, bitmap); | |
} | |
var mqtt = require('mqtt'); | |
var options = { | |
username: 'JackRoboticS', | |
password:'tlXrjyglDSg69lQxaENkqTX5I6SsBC0ZFSSbBFS8', | |
port: 1883, | |
host: 'service.anto.io', | |
clientId: 'JackRoboticS_API'+Math.random() | |
}; | |
var client = mqtt.connect(options); | |
client.on('connect', function () { | |
client.subscribe('channel/JackRoboticS/image/image'); // < เลือก subscribe ข้อมูลจาก mqtt | |
}); | |
client.on('message', function (topic, message) { | |
var MSG = message.toString(); | |
base64_decode(MSG, 'savefilename.png'); //< บันทึกไฟล์ลง disk | |
}); |
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 fs = require('fs'); | |
// function to encode file data to base64 encoded string | |
function base64_encode(file) { | |
// read binary data | |
var bitmap = fs.readFileSync(file); | |
// convert binary data to base64 encoded string | |
return new Buffer(bitmap).toString('base64'); | |
} | |
var mqtt = require('mqtt'); | |
var options = { | |
username: 'JackRoboticS', | |
password:'tlXrjyglDSg69lQxaENkqTX5I6SsBC0ZFSSbBFS8', | |
port: 1883, | |
host: 'service.anto.io', | |
clientId: 'JackRoboticS_API'+Math.random() | |
}; | |
var client = mqtt.connect(options); | |
client.on('connect', function () { | |
var base64str = base64_encode('jack.png'); // < แปลงจากภาพเป็น base64 | |
client.publish('channel/JackRoboticS/image/image', base64str); // < ส่งข้อมูลผ่าน mqtt | |
}); | |
client.on('message', function (topic, message) { | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment