本文共 6795 字,大约阅读时间需要 22 分钟。
此步操作设备:电脑
此步使用设备:电脑
cd ruff-linkdevelop-rgblight
/src/index.js
,将 productKey、deviceName、deviceSecret 替换成前一步申请的设备激活凭证:'use strict';var aliyunIot = require('aliyun-iot-device-sdk');// 设备连接到云端var device = aliyunIot.device({// 替换为生成的激活凭证三元组 productKey: '', deviceName: ' ', deviceSecret: ' '});// 连接成功device.on('connect', function() { console.log('connect successfully!'); // 点亮 $('#KY-016').setRGB(0xffffff); // 上报属性 device.postProps({ LightSwitch: 1, RGBColor: { Red: 255, Green: 255, Blue: 255 } });});// ruff 初始化完毕$.ready(function(error) { if (error) { console.log(error); return; } // 监听云端下行消息 device.serve('property/set', function(params) { console.log('receive params:', params); var LightSwitch = params.LightSwitch; // 开灯 if (LightSwitch === 1) { $('#KY-016').setRGB(0xffffff); } // 关灯 if (LightSwitch === 0) { $('#KY-016').setRGB(0x000000); } // 调色 var RGBColor = params.RGBColor; if (RGBColor) { var hexArr = [RGBColor.Red, RGBColor.Green, RGBColor.Blue].map(function( x ) { x = parseInt(x).toString(16); return x.length === 1 ? '0' + x : x; }); var color = Number('0x' + hexArr.join('')); $('#KY-016').setRGB(color); } // 上报属性 device.postProps(params); });});$.end(function() { console.log('disconnect'); // 断开连接 device.end();});
保存退出,项目修改完成!
此步使用设备:开发板+电脑
使用电脑打开刚刚的阿里云控制台页面,点击设备列表里的设备“调试”链接,在页面下方的调试功能里选择“RGB调色(RGBColor)”,方法选为“设置”,下方指令区输入
{"RGBColor":{"Red":255,"Blue":255,"Green":0}}
小灯变紫——数据下发成功(动图)
全程使用设备:电脑
新建目录,进行项目初始化:
init
bnpm i --save @bone/iot-gateway react-color
import React, { Component } from 'react';import { Button, Switch, Form, Grid, Input, Dialog } from '@bone/bone-web-ui';import IotGateway from '@bone/iot-gateway';import { HuePicker } from 'react-color';const Row = Grid.Row;const Col = Grid.Col;const FormItem = Form.Item;const formItemLayout = { labelCol: { fixedSpan: 12 }, wrapperCol: { span: 12 }};const insetLayout = { labelCol: { fixedSpan: 4 }};export default class App extends React.Component { constructor(props) { super(props); this.state = { switch: false, color: '', // 刷新页面不用重复输入 productKey: localStorage.getItem('productKey') || '', deviceName: localStorage.getItem('deviceName') || '' }; // 获取初始数据 this.getProps(props => { this.setState({ switch: props.LightSwitch === 1, color: rgbToHex( props.RGBColor.Red, props.RGBColor.Green, props.RGBColor.Blue ) }); }); } getProps(cb) { IotGateway.post({ url: 'https://api.link.aliyun.com/thing/device/status/query', apiVer: '1.0.1', params: { ProductKey: this.state.productKey, DeviceName: this.state.deviceName } }).then(res => { if (res.code !== 200) { throw new Error(res.localizedMsg || res.message); } let props = {}; res.data.forEach(item => { props[item.attribute] = item.value; }); if (cb) { cb(props); } console.log('get props successfully:', props); }); } setProps(props) { IotGateway.post({ url: 'https://api.link.aliyun.com/thing/device/properties/set', apiVer: '1.0.1', params: { ThingId: { productKey: this.state.productKey, deviceName: this.state.deviceName }, Items: props } }).then(res => { if (res.code !== 200) { throw new Error(res.localizedMsg || res.message); } console.log(res); }); } showValidationText() { Dialog.alert({ title: '提示', content: '请输入设备的 productKey 和 deviceName 才能控制设备哦' }); } onChange = checked => { if (!this.state.productKey || !this.state.deviceName) { this.showValidationText(); return; } this.setState({ switch: checked }); this.setProps({ LightSwitch: checked ? 1 : 0 }); }; onInput = (field, value) => { this.state[field] = value; localStorage.setItem(field, value); this.setState({ [field]: value }); }; onColorChange = color => { if (!this.state.productKey || !this.state.deviceName) { this.showValidationText(); return; } this.setState({ color: color.hex }); this.setProps({ RGBColor: hexToRgb(color.hex) }); }; render() { return (); }}function rgbToHex(r, g, b) { return '#' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);}function hexToRgb(hex) { var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? { Red: parseInt(result[1], 16), Green: parseInt(result[2], 16), Blue: parseInt(result[3], 16) } : null;}
bone start
http://localhost:8000/
进入Web 控制页面
至此 RGB 全彩智能灯的 Web 应用已开发完毕。给自己一点掌声~
转载地址:http://mcrfa.baihongyu.com/