Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. const mqtt = require('mqtt')
  2.  
  3. const mqttUri ='mqtt://f34cde9b-b664-4a94-baa8-e702148c60af%A1298c35-3c84-47d5-a63b-c053d60af10:oJd8HCvrNPSALZajVC5JG6I@40.81.26.31:1883';
  4.  
  5. // Use mqttUri or connectOpts
  6. const client = mqtt.connect(mqttUri);
  7.  
  8. client.on('connect', (connack) => {
  9. setInterval(() => {
  10. publishMockTemp();
  11. }, 3000);
  12. });
  13.  
  14. // Publish mock random temperature periodically
  15. function publishMockTemp() {
  16.  
  17. const temp = Math.floor((Math.random() * 7) + 22);
  18. client.publish('/hello', temp.toString(), { qos: 2 }, (err, packet) => {
  19. //if (!err) console.log('Data sent to /hello' + temp);
  20. if(err)
  21. {
  22. console.log(err)
  23. }else{
  24. console.log('Data send to /hello ' + temp);
  25. }
  26. });
  27.  
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement