[ラズパイ] MQTTをローカルネットワーク間で通信してみる 〜 対話型コンソールの導入
※ 当ページには【広告/PR】を含む場合があります。
2020/11/02
prompts.js
$ npm i prompts -s
#OR
$ yarn add prompts -S
prompt.js
const prompts = require('prompts');
(async () => {
const response = await prompts({
type: 'number',
name: 'value',
message: 'How old are you?',
validate: value => value < 18 ? `Nightclub is 18+ only` : true
});
console.log(response); // => { value: 24 }
})();
validate
$ node prompt.js
? How old are you? › 13
› Nightclub is 18+ only
✔ How old are you? … 45
{ value: 45 }
mqttクライアントで利用する
select
prompt.js
const prompts = require('prompts');
function sleep(_ms) {
return new Promise((resolve) => {
setTimeout(resolve, _ms)
});
}
async function planA() {
console.log('Plan-A has begun.');
await sleep(1000);
console.log('Plan-A finished.');
}
async function planB() {
console.log('Plan-B has begun.');
await sleep(2000);
console.log('Plan-B finished.');
}
async function main() {
console.log('Start');
while (true) {
const question = [
{
type: "select",
name: "plan",
message: "Order?",
choices: [
{ title: "Plan A", value: "a" },
{ title: "Plan B", value: "b" },
{ title: "Quit", value: "q" }
]
}
];
const response = await prompts(question);
console.log(response);
if (!Object.keys(response).length || response.plan == "q") {
console.log('Done!');
break;
} else if (response.plan == "a") {
await planA();
} else if (response.plan == "b") {
await planB();
}
}
}
main();
Plan A
a
planA
Plan B
Quit
response.plan
q
Cntl + c
$ node prompt.js
start
✔ Order? › Plan A
{ plan: 'a' }
Plan-A has begun.
Plan-A finished.
✔ Order? › Plan B
{ plan: 'b' }
Plan-B has begun.
Plan-B finished.
✔ Order? › Quit
{ plan: 'q' }
Done!
mqttクライアントの動作を組み込む
prompt.js
index_sync.js
192.168.0.200
const prompts = require('prompts');
const mqtt = require('mqtt');
let mqttClient;
async function mqttConnect() {
if (!mqttClient) {
mqttClient = mqtt.connect('mqtt://192.168.0.200:1883');
mqttClient.on('connect', () => {
console.log('Connected.');
});
mqttClient.on('message', (topic_, message) => {
console.log('subscriber received topic:', topic_, 'message:', message.toString());
});
} else {
console.log('Already Has Connected.');
}
}
async function mqttSubscrive() {
if (mqttClient) {
const topic = 'hoge/piyo/fuga';
mqttClient.subscribe(topic, (err, granted) => {
console.log('Subscribed.');
});
} else {
console.log('MQTT Client is an empty.');
}
}
async function mqttUnsubscrive() {
if (mqttClient) {
const topic = 'hoge/piyo/fuga';
mqttClient.unsubscribe(topic, (err, granted) => {
console.log('Unsubscribed.');
});
} else {
console.log('MQTT Client is an empty.');
}
}
async function main() {
console.log('Start');
while (true) {
const question = [
{
type: "select",
name: "plan",
message: "Order?",
choices: [
{ title: "Connect", value: "c" },
{ title: "Subscrive", value: "s" },
{ title: "Unsubscrive", value: "u" },
{ title: "Quit", value: "q" }
]
}
];
const response = await prompts(question);
console.log(response);
if (!Object.keys(response).length || response.plan == "q") {
console.log('Done!');
if (mqttClient) {
mqttClient.end();
}
break;
} else if (response.plan == "c") {
await mqttConnect();
} else if (response.plan == "s") {
await mqttSubscrive();
} else if (response.plan == "u") {
await mqttUnsubscrive();
}
}
}
main();
まとめ
参考サイト
記事を書いた人
ナンデモ系エンジニア
電子工作を身近に知っていただけるように、材料調達からDIYのハウツーまで気になったところをできるだけ細かく記事にしてブログ配信してます。
カテゴリー