Advertisement
Guest User

Untitled

a guest
Aug 27th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. var fs = require('fs');
  2.  
  3. // function to create file from base64 encoded string
  4. function base64_decode(base64str, file) {
  5. // create buffer object from base64 encoded string, it is important to tell the constructor that the string is base64 encoded
  6. var bitmap = new Buffer(base64str, 'base64');
  7. // write buffer to file
  8. fs.writeFileSync(file, bitmap);
  9. }
  10.  
  11. var mqtt = require('mqtt');
  12.  
  13. var options = {
  14. username: 'JackRoboticS',
  15. password:'tlXrjyglDSg69lQxaENkqTX5I6SsBC0ZFSSbBFS8',
  16. port: 1883,
  17. host: 'service.anto.io',
  18. clientId: 'JackRoboticS_API'+Math.random()
  19. };
  20. var client = mqtt.connect(options);
  21. client.on('connect', function () {
  22. client.subscribe('channel/JackRoboticS/image/image'); // < เลือก subscribe ข้อมูลจาก mqtt
  23. });
  24. client.on('message', function (topic, message) {
  25. var MSG = message.toString();
  26. base64_decode(MSG, 'savefilename.png'); //< บันทึกไฟล์ลง disk
  27. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement