解决方案
Product
Services
开发者
工厂客户
Register
Login
首页
文章
详情
怎样用Javascript语言对接灯光
2025-08-24 发布
浏览:681 次
本文介绍了如何用
Javascript
对接 智能灯座 的方法: 可以通过接口实现。
智能灯座
更多...
支持命令
线路
power
先通后断
point
先断后通
reset
产品手册
版本
对应产品手册
HTTP接口控制
以设备控制(向设备下发命令)为例
Javascript-Fetch
Javascript-Axios
Javascript-jQuery
Javascript-XHR
Javascript-Native
Javascript-Request
Javascript-Unirest
NodeJS脚本
微信小程序
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const crypto = require('crypto'); const sign = crypto.createHash('md5').update(crypto.createHash('md5').update(AppSecret).digest('hex') + ts.toString()).digest('hex'); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 构建请求体 const formData = new FormData(); formData.append('device', '1878');///替换成您需要发送的设备ID;可传多个[用,间隔] formData.append('order', JSON.stringify({ power1: 1 })); //替换成您需要发送的指令 // 发送请求 fetch(url, { method: 'POST', body: formData }) .then(response => response.json()) .then(data => console.log(JSON.stringify(data, null, 2))) .catch(error => console.error('Error:', error));
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command const axios = require('axios'); const crypto = require('crypto'); // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const sign = crypto.createHash('md5').update(crypto.createHash('md5').update(AppSecret).digest('hex') + ts.toString()).digest('hex'); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 构建请求体 const formData = new FormData(); formData.append('device', '1878');//替换成您需要发送的设备ID;可传多个[用,间隔] formData.append('order', JSON.stringify({ power1: 1 })); //替换成您需要发送的指令 // 发送请求 axios.post(url, formData, { headers: { 'Accept': '*/*', 'Host': 'api.thingboot.com', 'Connection': 'keep-alive', 'Content-Type': 'multipart/form-data' // 必须设置 Content-Type } }) .then(response => { console.log(JSON.stringify(response.data, null, 2)); }) .catch(error => { console.error('Error:', error); });
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const sign = CryptoJS.MD5(CryptoJS.MD5(AppSecret).toString() + ts.toString()).toString(); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 请求体数据 const device = '1878'; // 替换为实际的设备 ID;可传多个[用,间隔] const order = '{"power1":1,"power2":1}'; // 替换为实际的命令 // 构建请求体 const postData = { device: device, // 替换成您需要发送的设备ID;可传多个[用,间隔] order: order // 替换成您需要发送的指令 }; // 创建 XHR 对象 const xhr = new XMLHttpRequest(); // 配置请求 xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.setRequestHeader('Accept', '*/*'); xhr.setRequestHeader('Host', 'api.thingboot.com'); xhr.setRequestHeader('Connection', 'keep-alive'); // 设置请求完成时的回调 xhr.onload = function () { if (xhr.status >= 200 && xhr.status < 300) { console.log(JSON.stringify(JSON.parse(xhr.responseText), null, 2)); } else { console.error('Error:', xhr.statusText); } }; // 设置错误处理 xhr.onerror = function () { console.error('Request failed'); }; // 发送请求 xhr.send(postData);
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command const https = require('https'); const crypto = require('crypto'); // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const sign = crypto.createHash('md5').update(crypto.createHash('md5').update(AppSecret).digest('hex') + ts.toString()).digest('hex'); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 请求体数据 const device = '1878'; // 替换为实际的设备 ID;可传多个[用,间隔] const order = '{"power1":1,"power2":1}'; // 替换为实际的命令 // 构建请求体 const postData = { device: device, // 替换成您需要发送的设备ID;可传多个[用,间隔] order: order // 替换成您需要发送的指令 }; const options = { hostname: 'api.thingboot.com', path: `/${AppID}/device/control/?sign=${sign}&ts=${ts}`, method: 'POST', headers: { 'Content-Type': 'application/json', } }; const req = https.request(options, (res) => { let chunks = []; res.on('data', (chunk) => { chunks.push(chunk); }); res.on('end', () => { const body = Buffer.concat(chunks).toString(); console.log(body); }); }); req.on('error', (error) => { console.error(error); }); req.write(postData); req.end();
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command //首先,确保你已经安装了 request 库。如果没有安装,可以通过以下命令安装: npm install request const request = require('request'); const crypto = require('crypto'); // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const sign = crypto.createHash('md5').update(crypto.createHash('md5').update(AppSecret).digest('hex') + ts.toString()).digest('hex'); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 请求体数据 const device = '1878'; // 替换为实际的设备 ID;可传多个[用,间隔] const order = '{"power1":1,"power2":1}'; // 替换为实际的命令 // 构建请求体 const postData = { device: device, // 替换成您需要发送的设备ID;可传多个[用,间隔] order: order // 替换成您需要发送的指令 }; const options = { method: 'POST', url: url, headers: { 'Content-Type': 'application/json' }, body: postData }; request(options, function (error, response, body) { if (error) throw new Error(error); console.log(body); });
// First you need to prepare the following values // 1. AppID (in your console development settings) // 2. AppSecret (in your console development settings) // 3. ts (timestamp of the current time, seconds) // 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) // In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string // Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // Request needs to pass two parameters device and order: // device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface // order[json string]: the command to be issued, for example: // {"power":1}, usually to connect the circuit of the switch // {"power3":0}, usually to close the third line of the switch or controller // {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content // The same type of product, the command is the same, different product types of commands, please view the product manual page of each product // Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command const unirest = require('unirest'); const crypto = require('crypto'); // 定义变量 const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) const sign = crypto.createHash('md5').update(crypto.createHash('md5').update(AppSecret).digest('hex') + ts.toString()).digest('hex'); // 构建 URL const url = `https://api.thingboot.com/${AppID}/device/control/?sign=${sign}&ts=${ts}`; // 请求体数据 const device = '1878'; // 替换为实际的设备 ID;可传多个[用,间隔] const order = '{"power1":1,"power2":1}'; // 替换为实际的命令 // 构建请求体 const postData = { device: device, // 替换成您需要发送的设备ID;可传多个[用,间隔] order: order // 替换成您需要发送的指令 }; unirest.post(url) .headers({ 'Content-Type': 'application/json' }) .send(postData) .end(function (response) { if (response.error) { console.error(response.error); } else { console.log(response.body); } });
/* 核心请求地址:https://api.thingboot.com/拼接AppID的值/device/control/?sign=拼接签名&拼接时间戳也就是ts ts时间戳获取当前时间戳即可,签名为md5(md5(开发者密码)拼接上面的ts时间戳的值) multipart/form-data 请求中传递参数device_id和order即可 Tip:一定要替换成正式的AppID和AppSecret,再根据实时的时间戳计算签名,请求一定需要device_id设备ID和order命令 */ var http = require("http"); var requestInfo={ "method": "POST", "hostname": "api.thingboot.com", "path": "/{AppID}/device/control/", "headers": { "X-APISpace-Token":"", "Content-Type":"" } }; var req = http.request(requestInfo, function (res) { var chunks = []; res.on("data", function (chunk) { chunks.push(chunk); }); res.on("end", function () { var body = Buffer.concat(chunks); console.log(body.toString()); }); }); req.write("device=设备ID&order=命令"); /* First you need to prepare the following values 1. AppID (in your console development settings) 2. AppSecret (in your console development settings) 3. ts (timestamp of the current time, seconds) 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; Request needs to pass two parameters device and order: device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface order[json string]: the command to be issued, for example: {"power":1}, usually to connect the circuit of the switch {"power3":0}, usually to close the third line of the switch or controller {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content The same type of product, the command is the same, different product types of commands, please view the product manual page of each product Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command */ req.end();
/* First you need to prepare the following values 1. AppID (in your console development settings) 2. AppSecret (in your console development settings) 3. ts (timestamp of the current time, seconds) 4. YourSign = md5(md5(AppSecret)ts);(md5 is an encryption method, AppSecret is the AppSecret prepared above, ts is the timestamp prepared above, concatenate the string after the AppSecret after md5 encryption) In simple terms, the signature is md5(md5(your developer password)concatenate the above ts timestamp value); ts is the timestamp of the current timestamp; that is, to perform one MD5 on the developer password (AppSecret), then concatenate the timestamp, and then perform one MD5 on the entire concatenated string Core request address: api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; Request needs to pass two parameters device and order: device[string]: unique device ID, can pass multiple [separated by ,], can be viewed in the console, or pulled through the interface order[json string]: the command to be issued, for example: {"power":1}, usually to connect the circuit of the switch {"power3":0}, usually to close the third line of the switch or controller {"play:gbk:16":"hello, welcome to visit"}, let the voice speaker report the specified content The same type of product, the command is the same, different product types of commands, please view the product manual page of each product Note: you must replace the formal AppID and AppSecret with real-time timestamp calculation of the signature, the request must have device device ID and order command */ // 在页面的 JS 文件中(如 pages/index/index.js) Page({ data: {}, // 控制设备的方法 controlDevice() { const AppID = '开发者ID'; // 替换为实际的 AppID const AppSecret = '开发者密码'; // 替换为实际的 AppSecret const ts = Math.floor(Date.now() / 1000); // 秒级时间戳(取整) // 生成签名:md5(md5(AppSecret) + ts) const sign = this.generateSign(AppSecret, ts); // 构建 URL(注意:小程序要求域名必须在小程序后台配置合法) const url = `https://api.thingboot.com/${encodeURIComponent(AppID)}/device/control/?sign=${sign}&ts=${ts}`; // 请求体数据 const device = '1878'; // 目标设备 ID const order = '{"power1":1}'; // 控制命令 // 验证 JSON 合法性 let orderDecoded; try { orderDecoded = JSON.parse(order); } catch (e) { console.error('Invalid order JSON:', e); return; } // 发起请求 wx.request({ url: url, method: 'POST', data: { device: device, order: orderDecoded }, header: { 'Content-Type': 'application/json' // 设置请求头为 JSON }, success: (res) => { console.log('请求成功:', res.data); wx.showToast({ title: '操作成功', icon: 'success' }); }, fail: (err) => { console.error('请求失败:', err); wx.showToast({ title: '请求失败', icon: 'error' }); } }); }, // 生成签名的方法(小程序环境没有原生 MD5,需引入第三方库或使用后端生成) generateSign(AppSecret, ts) { // 注意:小程序环境无法直接使用 PHP 的 md5(),需通过以下方式解决: // 1. 使用后端接口生成签名(推荐) // 2. 引入第三方 JS MD5 库(如 js-md5) // 示例:假设已引入 js-md5 库(需在 app.js 或页面中引入) const md5 = require('js-md5'); // 或通过 npm 安装 const sign = md5(md5(AppSecret) + ts); return sign; }, // 用户点击按钮时触发 onTapControl() { this.controlDevice(); } });
文章:
怎样用Javascript语言对接灯光
相关技术文章:
如何用Javascript语言控制30A远程控制断路器
查看
怎样用Javascript语言对接智能壁挂Pro远程语音通知喇叭
查看
如何用Javascript语言对接远程火灾预警烟雾报警器
查看
如何用Javascript语言控制远程温湿度采集器
查看
如何用Javascript语言控制20W 云语音播报音柱
查看