解决方案
Product
Services
开发者
工厂客户
Register
Login
首页
文章
详情
怎么用C语言对接智能配电箱
2022-12-10 发布
浏览:151 次
本文介绍了如何用
C
对接 智能配电箱 的方法: 可以通过接口实现。
智能配电箱
更多...
支持命令
开关
power
先通后断
point
先断后通
reset
产品手册
版本
对应产品手册
HTTP接口控制
以设备控制(向设备下发命令)为例
C语言
// 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 #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.thingboot.com/%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; }
文章:
怎么用C语言对接智能配电箱
相关技术文章:
怎样用C语言对接5位总控插排
查看
如何用C语言对接智能分体控制箱|12路
查看
怎样用C语言对接50A带计量智能电源控制断路器
查看
怎样用C语言对接吸顶式智能人体感应器
查看
怎样用C语言对接2路低压直流控制板
查看