· 8 years ago · Jan 04, 2018, 07:52 PM
1var options = {
2 clientId : 1,
3 endpoint: 'xxxxxxxxxx.iot.xxxxxxxxxx.amazonaws.com',
4 accessKey: 'xxxxxxxxxx',
5 secretKey: 'xxxxxxxxxxxxxxxxxxxx',
6 regionName: 'xxxxxxxxxx',
7 debug: true
8};
9
10
11var client = _mqtt.connect('ws://xxxxxxxxxx.iot.xxxxxxxxxx.com', options);
12//var client = _mqtt.connect('mqtt://test.mosquitto.org');
13
14client.on('connect', function () {
15 console.log("MQTT connected");
16 client.subscribe('testing')
17 client.publish('testing', 'Hello mqtt2')
18})
19
20client.on('message', function (topic, message) {
21 // message is Buffer
22 console.log(message.toString())
23 client.end()
24})
25
26var device = awsIot.device({
27 keyPath: './certs/xxxxxxxxxx-private.pem.key',
28 certPath: './certs/xxxxxxxxxx-certificate.pem.crt',
29 caPath: './certs/root-CA.crt',
30 clientId: 1,
31 debug: true,
32 host: 'xxxxxxxxxx.iot.xxxxxxxxxx.amazonaws.com'
33});
34
35//
36// Device is an instance returned by mqtt.Client(), see mqtt.js for full
37// documentation.
38//
39device
40 .on('connect', function() {
41 console.log('connect');
42 device.subscribe('topic_1');
43 device.publish('topic_2', JSON.stringify({ test_data: 1}));
44 });
45
46device
47 .on('message', function(topic, payload) {
48 console.log('message', topic, payload.toString());
49 });
50
51{ keyPath: './certs/xxxxxxxxxx-private.pem.key',
52 certPath: './certs/xxxxxxxxxx-certificate.pem.crt',
53 caPath: './certs/root-CA.crt',
54 clientId: 1,
55 debug: true,
56 host: 'xxxxxxxxxx.iot.xxxxxxxxxx.amazonaws.com',
57 keepalive: 300,
58 username: '?SDK=JavaScript&Version=2.2.0',
59 reconnectPeriod: 1000,
60 fastDisconnectDetection: true,
61 resubscribe: false,
62 protocol: 'mqtts',
63 port: 8883,
64 key: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 52 53 41 20 50 52 49 56 41 54 45 20 4b 45 59 2d 2d 2d 2d 2d 0a 4d 49 49 45 70 41 49 42 41 41 4b 43 41 51 45 41 32 42 ... >,
65 cert: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0a 4d 49 49 44 57 54 43 43 41 6b 47 67 41 77 49 42 41 67 49 55 53 33 ... >,
66 ca: <Buffer 2d 2d 2d 2d 2d 42 45 47 49 4e 20 43 45 52 54 49 46 49 43 41 54 45 2d 2d 2d 2d 2d 0d 0a 4d 49 49 45 30 7a 43 43 41 37 75 67 41 77 49 42 41 67 49 51 47 ... >,
67 requestCert: true,
68 rejectUnauthorized: true }
69attempting new mqtt connection...
70net.js:617
71 throw new TypeError('invalid data');
72 ^
73
74TypeError: invalid data