Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function connectToAwsIot() {
  2. var options = {
  3. host: awsIotConfig.endpoint,
  4. port: 8883,
  5. protocol: 'mqtt',
  6. cert: atob(awsIotConfig.clientCertificate),
  7. key: atob(awsIotConfig.privateKey),
  8. ca: atob(awsIotConfig.caCertificate)
  9. }
  10.  
  11. console.log('Connecting to AWS IoT...');
  12.  
  13. require('tls').connect(options, function (res) {
  14. console.log('TLS handshake successful');
  15. mqtt.connect(res);
  16. });
  17.  
  18. mqtt.on('connect', function () {
  19. console.log('MQTT connection successful');
  20. digitalWrite(pinLedGreen, false);
  21.  
  22. console.log('Subscribing to ' + awsIotConfig.responseTopic);
  23. mqtt.subscribe(awsIotConfig.responseTopic);
  24.  
  25. setPinModes();
  26. setupWatches();
  27. });
  28.  
  29. mqtt.on('publish', function (pub) {
  30. console.log('Received message on ' + pub.topic + ': ' + pub.message);
  31. });
  32.  
  33. mqtt.on('disconnected', function () {
  34. digitalWrite("B13", true);
  35. console.log('Disconnected from AWS IoT');
  36. });
  37.  
  38. mqtt.on('error', function (err) {
  39. console.log('Error during AWS IoT connection: ' + err);
  40. });
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement