开发者
解决方案
产品
服务
开发者
工厂客户
注册
登录
首页
开发者
开放平台
开放平台接口调用代码示例
设备接口调用代码示例
以设备控制(向设备下发命令)为例
C语言
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命令 #include
#include
#include
#include
#include
#include
// 计算 MD5 哈希值 md5(md5(开发者密码)拼接上面的ts参数) void calculate_md5(const char *input, unsigned char *output) { MD5_CTX md5Context; MD5_Init(&md5Context); MD5_Update(&md5Context, input, strlen(input)); MD5_Final(output, &md5Context); } // 将 MD5 哈希值转换为十六进制字符串 void md5_to_hex(const unsigned char *md5_hash, char *hex_string) { for (int i = 0; i < MD5_DIGEST_LENGTH; i++) { sprintf(hex_string + (i * 2), "x", md5_hash[i]); } hex_string[MD5_DIGEST_LENGTH * 2] = '\0'; } int main() { CURL *curl; CURLcode res; // 配置参数 const char *app_id = "开发者ID"; // 替换为实际的 AppID const char *app_secret = "开发者密码"; // 替换为实际的 AppSecret const char *device_id = "1878"; // 替换为实际的设备ID;可传多个[用,间隔] const char *command = "{\"power1\":1}"; // 替换为实际的命令 // 计算时间戳 time_t ts = time(NULL); // 计算 MD5 哈希值 unsigned char md5_hash[MD5_DIGEST_LENGTH]; char md5_hex[MD5_DIGEST_LENGTH * 2 + 1]; // 计算 app_secret 的 MD5 calculate_md5(app_secret, md5_hash); md5_to_hex(md5_hash, md5_hex); // 计算 sign char sign_input[strlen(md5_hex) + 20]; // 足够大的缓冲区 sprintf(sign_input, "%s%ld", md5_hex, ts); calculate_md5(sign_input, md5_hash); md5_to_hex(md5_hash, md5_hex); // 构造请求 URL char url[1024]; snprintf(url, sizeof(url), "https://api.yoyoiot.cn/%s/device/control/?sign=%s&ts=%ld", app_id, md5_hex, ts); // 构造请求体 char post_data[1024]; snprintf(post_data, sizeof(post_data), "{\"device\":\"%s\",\"order\":%s}", device_id, command); // 初始化 libcurl curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if (curl) { // 设置请求 URL curl_easy_setopt(curl, CURLOPT_URL, url); // 设置请求头 struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 设置请求体 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); // 执行请求 res = curl_easy_perform(curl); // 检查请求结果 if (res != CURLE_OK) { fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res)); } // 清理 curl_slist_free_all(headers); curl_easy_cleanup(curl); } curl_global_cleanup(); return 0; }