解决方案
Product
Services
开发者
工厂客户
Register
Login
首页
文章
详情
如何用Java语言控制灯光
2022-08-23 发布
浏览:281 次
本文介绍了如何用
Java
对接 智能灯座 的方法: 可以通过接口实现。
智能灯座
更多...
支持命令
线路
power
先通后断
point
先断后通
reset
产品手册
版本
对应产品手册
HTTP接口控制
以设备控制(向设备下发命令)为例
Java-Unirest
Java-OkHttp
// 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 /*首先,确保在你的项目中添加了 unirest 的 Maven 依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:
com.konghq
unirest-java
3.13.10
*/ import com.konghq.unirest.http.HttpResponse; import com.konghq.unirest.http.Unirest; import com.konghq.unirest.http.exceptions.UnirestException; import org.apache.commons.codec.digest.DigestUtils; public class Main { public static void main(String[] args) { // 定义变量 String AppID = "开发者ID"; // 替换为实际的 AppID String AppSecret = "开发者密码"; // 替换为实际的 AppSecret long ts = System.currentTimeMillis() / 1000; // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) String sign = DigestUtils.md5Hex(DigestUtils.md5Hex(AppSecret) + ts); // 构建 URL String url = String.format("https://api.thingboot.com/%s/device/control/?sign=%s&ts=%d", AppID, sign, ts); // 请求体数据 String device = "1878"; // 替换为实际的设备ID;可传多个[用,间隔] String order = "{\"power1\":1}"; // 替换为实际的命令 try { // 构建请求体 String postData = String.format("{\"device\":\"%s\",\"order\":%s}", device, order); // 发送 POST 请求 HttpResponse
response = Unirest.post(url) .header("Content-Type", "application/json") .body(postData) .asString(); // 输出响应 System.out.println(response.getBody()); } catch (UnirestException e) { e.printStackTrace(); } } }
// 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 /* 首先,确保在你的项目中添加了 OkHttp 的 Maven 依赖。如果你使用的是 Maven,可以在 pom.xml 中添加以下依赖:
com.squareup.okhttp3
okhttp
4.9.3
*/ import okhttp3.*; import org.apache.commons.codec.digest.DigestUtils; import java.io.IOException; public class Main { public static void main(String[] args) { // 定义变量 String AppID = "开发者ID"; // 替换为实际的 AppID String AppSecret = "开发者密码"; // 替换为实际的 AppSecret long ts = System.currentTimeMillis() / 1000; // 获取当前时间戳(秒) // 计算签名 md5(md5(开发者密码)拼接上面的ts参数) String sign = DigestUtils.md5Hex(DigestUtils.md5Hex(AppSecret) + ts); // 构建 URL String url = String.format("https://api.thingboot.com/%s/device/control/?sign=%s&ts=%d", AppID, sign, ts); // 请求体数据 String device = "1878"; // 替换为实际的设备ID;可传多个[用,间隔] String order = "{\"power1\":1}"; // 替换为实际的命令 // 构建请求体 String postData = String.format("{\"device\":\"%s\",\"order\":%s}", device, order); // 创建 OkHttpClient 实例 OkHttpClient client = new OkHttpClient(); // 创建请求体 MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); RequestBody body = RequestBody.create(postData, mediaType); // 创建请求 Request request = new Request.Builder() .url(url) .post(body) .addHeader("Content-Type", "application/json") .build(); // 发送请求并处理响应 try (Response response = client.newCall(request).execute()) { if (!response.isSuccessful()) throw new IOException("Unexpected code " + response); // 输出响应 System.out.println(response.body().string()); } catch (IOException e) { e.printStackTrace(); } } }
文章:
如何用Java语言控制灯光
相关技术文章:
怎么用Java语言对接空调远程指令发送器2
查看
如何用Java语言对接24 路智能分体远程多通道控制器
查看
如何用Java语言控制20W 物联网语音广播音箱
查看
怎样用Java语言控制智能Mini语音喇叭
查看
怎样用Java语言对接10W壁挂语音通知音箱
查看