开发者
解决方案
产品
服务
开发者
工厂客户
注册
登录
首页
开发者
开放平台
开放平台接口调用代码示例
设备接口调用代码示例
以设备控制(向设备下发命令)为例
OCaml
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命令 *) (* 确保安装了必要的库 opam install cohttp digestif *) open Lwt.Infix open Cohttp open Cohttp_lwt_unix let () = (* 配置参数 *) let app_id = "实际AppID" in (* 替换为你的AppID *) let app_secret = "实际AppSecret" in (* 替换为你的AppSecret *) let device_id = "1878" in (* 替换为你的设备ID *) let command = "{\"power1\":1}" in (* 替换为你的控制命令 *) (* 计算时间戳 *) let ts = int_of_float (Unix.time ()) in (* 计算签名 [严格按 md5(md5(密码)+时间戳) 逻辑] *) let first_md5 = Digestif.MD5.digest_string app_secret |> Digestif.MD5.to_hex in let sign_input = first_md5 ^ string_of_int ts in let sign = Digestif.MD5.digest_string sign_input |> Digestif.MD5.to_hex in (* 构造请求 URL *) let url = Printf.sprintf "https://api.yoyoiot.cn/%s/device/control/?sign=%s&ts=%d" app_id sign ts in (* 构造请求体 *) let request_body = let open Yojson.Basic in let order = from_string command in `Assoc [ ("device", `String device_id); ("order", order) ] in (* 创建 HTTP 请求 *) let headers = Header.add (Header.init ()) "Content-Type" "application/json" in let request = Request.make headers `POST (Uri.of_string url) in (* 发送请求并处理响应 *) let body_str = Client.call request (Body.of_string (Yojson.Basic.to_string request_body)) >>= fun (response, body) -> match response.status with | #Http_status.Code.successful_status -> Cohttp_lwt.Body.to_string body | code -> Lwt.return (Printf.sprintf "请求失败: %d" (Http_status.Code.code_of code)) in (* 输出结果 *) body_str >>= fun body -> print_endline body; Lwt.return () |> Lwt_main.run (* 将上述代码保存为一个 .ml 文件(如 request.ml),然后使用 OCaml 编译器和运行时环境运行 *) (* ocamlfind opt -linkpkg -package cohttp,digestif,yojson request.ml -o request && ./request *)