开发者
解决方案
产品
服务
开发者
工厂客户
注册
登录
首页
开发者
开放平台
开放平台接口调用代码示例
设备接口调用代码示例
以设备控制(向设备下发命令)为例
Java-Unirest
Shell-cURL
Shell-cURL-Windows
Shell-Httpie
Shell-wget
Shell-PowerShell
Javascript-Fetch
Javascript-Axios
Javascript-jQuery
Javascript-XHR
Javascript-Native
Javascript-Request
Javascript-Unirest
NodeJS脚本
微信小程序
Java-Unirest
Java-OkHttp
Swift
Go语言
PHP-cURL
PHP-HTTP_Request2
PHP-pecl_http
PHP-Guzzle
Python-http.client
Python-Requests
HTTP协议
C语言
C#语言
Objective-C
Ruby语言
OCaml
Dart
R语言
编程方式:
说明:{AppID}为您的应用ID(由平台生成),免费注册登录后,可在控制台查看;sign和ts的取值,请参考
文档
。
// 首先您需要准备好一下几个值 // 1. AppID (在您的控制台的开发设置中获取) // 2. AppSecret (在您的控制台的开发设置中获取) // 3. ts (当前时间的时间戳,秒) // 4. YourSign = md5(md5(AppSecret)ts);(其中的md5是一种加密方式,AppSecret就是上面准备的AppSecret,ts就是上面准备的时间戳,拼接在将AppSecret经过md5加密后的字符串后面) // 简单来说签名为md5(md5(你的开发者密码)拼接上面的ts时间戳的值);ts为时间戳获取当前时间戳即可;也就是将开发者密码(AppSecret)进行一次MD5,然后将这个结果拼接上时间戳,再对整个拼接后的字符串做一次MD5 // 核心请求地址:api.thingboot.com/AppID/device/control/?sign=YourSign&&ts=ts; // 请求需要传递两个参数device和order: // device[字符串]:设备唯一ID,可传多个[用,间隔],可在控制台可查看,也可通过接口拉取 // order[json字符串]:下发的命令,例如: // {"power":1},一般为将通断器的线路接通 // {"power3":0},一般为将开关或控制器的第3条线路关闭 // {"play:gbk:16":"你好,欢迎光临"},让语音喇叭播报指定内容 // 相同产品类型的设备,命令相同,不同产品类型的命令,请到每个产品的产品手册页面查看 // 注意:一定要替换成正式的AppID和AppSecret,再根据实时的时间戳计算签名,请求一定需要device设备ID和order命令 /*首先,确保在你的项目中添加了 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.yoyoiot.cn/%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(); } } }