-
Notifications
You must be signed in to change notification settings - Fork 1
/
sent_mqtt_in_nodejs.js
40 lines (35 loc) · 1.15 KB
/
sent_mqtt_in_nodejs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var http = require('http');
exports.handler = (event, context, callback) => {
sendMqttMessage();
callback(null, "Hello world!");
};
function sendMqttMessage() {
// MQTT消息的内容
var post_data = '{"hello":"BaiduIot"}'
var post_options = {
// 华北区域请用:api.mqtt.iot.bj.baidubce.com
host: 'api.mqtt.iot.gz.baidubce.com',
port: '80',
// 可以改参数: qos, topic, retain
path: '/v1/proxy?qos=0&topic=topic_to_send&retain=false',
method: 'POST',
headers: {
'Content-Type': 'application/octet-stream',
'Content-Length': Buffer.byteLength(post_data),
// 替换成你的MQTT用户名,格式如: myendpoint/mydevice
'auth.username': '<your mqtt username>',
// 替换成你的MQTT密码,格式如: N....kdiekn9K4LXHpwRA=
'auth.password': '<your mqtt password>'
}
};
// Set up the request
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
// post the data
post_req.write(post_data);
post_req.end();
}