├── .gitignore ├── README.md ├── asset ├── iot.png ├── struct1.png └── struct2.png └── src ├── app └── index.js ├── broker ├── index.js └── package.json ├── server └── index.js └── temperature ├── index.js ├── iot_index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | node_modules/ 3 | 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## demo说明 2 | 3 | demo是一个使用JavaScript和MQTT实现的一个简单的物联网应用,通过client端上报温度数据,server处理温度数据(业务处理),app端展示结果(根据温度的一些穿衣提示) 4 | 5 | ![](/asset/struct1.png) 6 | 7 | ## demo结构说明 8 | 9 | client端: 10 | 11 | - `src/temperature/index.js` 基于 mqtt.js实现,可以移植到主流开发板对接传感器实现真实功能 12 | - `src/temperature/iot_index.js` 基于阿里云iothub产品,对index.js进行转译,具体可以阅读文件注释 13 | 14 | broker端: 15 | 16 | - `src/broker/index.js` 基于 mosac实现的一个mqtt的broker服务 17 | 18 | server: 19 | 20 | - `src/server/index.js` 基于 mqtt.js订阅数据,处理业务 21 | 22 | app: 23 | 24 | - 使用koa和mqtt.js,通过web展示数据提示 25 | 26 | ![](/asset/struct2.png) 27 | 28 | ## 快速启动 29 | 30 | - 1:进入每个目录,执行 `npm install ` 安装依赖包 31 | - 2:先启动broker, 进入borker目录,执行 `node index` 32 | - 3:启动client, 进入temperature目录,执行 `node index` 33 | - 4:启动app, 进入app目录,执行 `node index` ,在浏览器中输入 `localhost:3000` 查看结果 34 | 35 | ## iot前端工作机会 36 | 37 | pc时代是物与物的交流,互联网时代是人与人的交流,物联网时代是人与物的相互联系,物与物的相互联系,万物互联的时代就要到来,iot的时代就要到来,欢迎登船 38 | 39 | 阿里云iot事业部招前端,有意向者可以发送简历到 40 | 41 | xuanyan.lyw@alibaba-inc.com 或 coolnameismy@gmail.com 42 | 43 | ![](/asset/iot.png) 44 | 45 | 46 | -------------------------------------------------------------------------------- /asset/iot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coolnameismy/javascript-mqtt-demo-wearingTip/f1d9cdb14e82905683c22b0dd9311ace1d815930/asset/iot.png -------------------------------------------------------------------------------- /asset/struct1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coolnameismy/javascript-mqtt-demo-wearingTip/f1d9cdb14e82905683c22b0dd9311ace1d815930/asset/struct1.png -------------------------------------------------------------------------------- /asset/struct2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coolnameismy/javascript-mqtt-demo-wearingTip/f1d9cdb14e82905683c22b0dd9311ace1d815930/asset/struct2.png -------------------------------------------------------------------------------- /src/app/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const Koa = require('koa'); 4 | const mqtt = require('mqtt'); 5 | const app = new Koa(); 6 | 7 | 8 | var msg = {temperature:"-",tips:""}; 9 | // response 10 | app.use(ctx => { 11 | ctx.body = "当前温度:" + msg.temperature + "度" + "\n" + '穿衣提示:'+msg.tips + "\n" ; 12 | }); 13 | 14 | app.listen(3000); 15 | 16 | //mqtt 17 | var client = mqtt.connect('mqtt://localhost:1883'); 18 | 19 | client.on('connect', function () { 20 | console.log('>>> connected'); 21 | client.subscribe('/tips'); 22 | }) 23 | 24 | client.on('message', function (topic, message) { 25 | var data = JSON.parse(message.toString()); 26 | console.log(message.toString()); 27 | console.log(data.tips); 28 | msg = data; 29 | 30 | // if (temperature+1) {} 31 | // message is Buffer 32 | // let str = message.toString(); 33 | // let data = JSON.parse(message); 34 | // console.log(data.tips); 35 | // msg = message.toString(); 36 | }) -------------------------------------------------------------------------------- /src/broker/index.js: -------------------------------------------------------------------------------- 1 | var mosca = require('mosca'); 2 | 3 | var ascoltatore = { 4 | //using ascoltatore 5 | // type: 'mongo', 6 | // url: 'mongodb://localhost:27017/mqtt', 7 | // pubsubCollection: 'ascoltatori', 8 | // mongo: {} 9 | }; 10 | 11 | var settings = { 12 | port: 1883, 13 | backend: ascoltatore 14 | }; 15 | 16 | var server = new mosca.Server(settings); 17 | 18 | server.on('clientConnected', function(client) { 19 | console.log('client connected', client.id); 20 | }); 21 | 22 | // fired when a message is received 23 | server.on('published', function(packet, client) { 24 | console.log('Published', packet.payload); //{"clientId":"mqttjs_02fea7b4","topic":"/tips"} 25 | // console.log('>>>packet', packet); //{"clientId":"mqttjs_02fea7b4","topic":"/tips"} 26 | }); 27 | 28 | server.on('ready', setup); 29 | 30 | // fired when the mqtt server is ready 31 | function setup() { 32 | console.log('Mosca server is up and running'); 33 | } -------------------------------------------------------------------------------- /src/broker/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "broker", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "mosca": "^2.4.0" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/server/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const mqtt = require('mqtt'); 4 | var client = mqtt.connect('mqtt://localhost:1883'); 5 | 6 | client.on('connect', function () { 7 | console.log('>>> connected'); 8 | client.subscribe('/temperature'); 9 | }) 10 | 11 | client.on('message', function (topic, message) { 12 | var temperature = parseInt(message.toString()); 13 | var data = {temperature}; 14 | 15 | if (temperature >= 60) { 16 | data.tips = "热... 500服务器故障"; 17 | } 18 | else if (temperature >= 50) { 19 | data.tips = "今天天气非常热,建议不要穿衣服了"; 20 | } 21 | else if (temperature >= 40) { 22 | data.tips = "今天天气十分的热,建议穿短袖T恤+短裤"; 23 | } 24 | else if (temperature >= 30) { 25 | data.tips = "今天天气有点的热,建议穿短袖T恤"; 26 | } 27 | else if (temperature >= 0) { 28 | data.tips = "今天天气正好,可以穿上一件薄衣服"; 29 | } 30 | else if (temperature >= -10) { 31 | data.tips = "今天天气十分寒冷,棉袄可以穿上一件"; 32 | } 33 | else { 34 | data.tips = "今天天气十分十分寒冷,棉袄可以穿上二件"; 35 | } 36 | client.publish('/tips', JSON.stringify(data)); 37 | // if (temperature+1) {} 38 | // message is Buffer 39 | console.log(JSON.stringify(data)); 40 | }) -------------------------------------------------------------------------------- /src/temperature/index.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | var mqtt = require('mqtt'); 4 | var client = mqtt.connect('mqtt://localhost:1883'); 5 | 6 | 7 | client.on('connect', function () { 8 | console.log('>>> connected') 9 | // client.subscribe('/tips') 10 | setInterval( 11 | ()=>{client.publish('/temperature', '30');}, 12 | 3000 13 | ); 14 | 15 | }) 16 | 17 | client.on('message', function (topic, message) { 18 | // message is Buffer 19 | console.log(message.toString()) 20 | }) 21 | 22 | // client.end(); -------------------------------------------------------------------------------- /src/temperature/iot_index.js: -------------------------------------------------------------------------------- 1 | /** 2 | 说明:本文件作为iot套件的使用示例, 使用时请更改 productKey,productSecret ,deviceName,deviceSecret为你自己的值 3 | 产品地址:https://www.aliyun.com/product/iot 4 | 内测申请请联系:xuanyan.lyw@alibaba-inc.com 5 | 6 | **/ 7 | 8 | 'use strict' 9 | 10 | const mqtt = require('mqtt'); 11 | const crypto = require('crypto'); 12 | 13 | //温度传感器 14 | let productKey = ""; 15 | let productSecret = "" 16 | let deviceType = "1"; 17 | let sdkVersion = "1.0.0"; 18 | //device info 19 | let deviceName = ""; 20 | let deviceSecret = ""; 21 | //topic ,在平台注册 22 | let topic = '///temperature'; 23 | 24 | 25 | //生成client_id 26 | var make_client_id = (productKey,deviceName,deviceType,sdkVersion)=> { 27 | return `${productKey}:${deviceName}:${deviceType}:${sdkVersion}`; 28 | } 29 | 30 | //生成UserName 31 | var make_username = (productKey,productSecret,deviceName,deviceSecret) => { 32 | var key = `${productKey}${productSecret}${deviceName}${deviceSecret}`; 33 | const hash = crypto.createHash('md5'); 34 | hash.update(key); 35 | // console.log(hash.digest('hex')); 36 | return hash.digest('hex').toLocaleUpperCase(); 37 | } 38 | 39 | // make_username(productKey,productSecret,deviceName,deviceSecret); 40 | 41 | 42 | var settings = { 43 | keepalive: 100, 44 | protocolId: 'MQIsdp', 45 | protocolVersion: 3, 46 | clientId: make_client_id(productKey,deviceName,deviceType,sdkVersion), 47 | clean: false, 48 | username:make_username(productKey,productSecret,deviceName,deviceSecret), 49 | port: 8000, 50 | host:"iot-as.aliyuncs.com" 51 | } 52 | 53 | var client = mqtt.connect(settings); 54 | 55 | client.on('connect', function () { 56 | console.log('>>> connected') 57 | setInterval( 58 | ()=>{ 59 | client.publish(topic,"30"); 60 | }, 61 | 3000 62 | ); 63 | 64 | }) 65 | 66 | client.on('message', function (topic, message) { 67 | // message is Buffer 68 | console.log(message.toString()) 69 | 70 | }) 71 | -------------------------------------------------------------------------------- /src/temperature/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "temperature", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "devDependencies": { 12 | "mqtt": "^2.7.1" 13 | } 14 | } 15 | --------------------------------------------------------------------------------