解决方案
Product
Services
开发者
工厂客户
Register
Login
首页
文章
详情
怎么用Python语言控制智能灯座
2022-09-27 发布
浏览:836 次
本文介绍了如何用
Python
对接 智能灯座 的方法: 可以通过接口实现。
智能灯座
更多...
支持命令
线路
power
先通后断
point
先断后通
reset
产品手册
版本
对应产品手册
HTTP接口控制
以设备控制(向设备下发命令)为例
Python-http.client
Python-Requests
# 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 import hashlib import json import http.client import time from urllib.parse import urlencode def calculate_md5(s): return hashlib.md5(s.encode()).hexdigest() def send_request(): app_id = "开发者ID" # 替换为实际的 AppID app_secret = "开发者密码" # 替换为实际的 AppSecret ts = int(time.time()) # 获取当前时间戳(秒) # 计算签名 md5(md5(开发者密码)拼接上面的ts参数) sign = calculate_md5(calculate_md5(app_secret) + str(ts)) # 构建 URL base_url = f"https://api.thingboot.com/{app_id}/device/control/" params = { "sign": sign, "ts": ts } # 构建请求体数据 device = "1878" # 替换为实际的设备ID;可传多个[用,间隔] order = {"power1": 1} # 替换为实际的命令 request_body = { "device": device, "order": order } json_body = json.dumps(request_body) # 创建请求 conn = http.client.HTTPSConnection("api.thingboot.com") headers = { "Content-Type": "application/json" } # 发送请求 conn.request("POST", f"/{app_id}/device/control/?{urlencode(params)}", json_body, headers) # 获取响应 response = conn.getresponse() response_data = response.read().decode('utf-8') print("Response Status Code:", response.status) print("Response Body:", response_data) conn.close() # 调用函数 send_request()
# 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 import hashlib import json import requests import time from urllib.parse import urlencode def calculate_md5(s): return hashlib.md5(s.encode()).hexdigest() def send_request(): app_id = "开发者ID" # 替换为实际的 AppID app_secret = "开发者密码" # 替换为实际的 AppSecret ts = int(time.time()) # 获取当前时间戳(秒) # 计算签名 md5(md5(开发者密码)拼接上面的ts参数) sign = calculate_md5(calculate_md5(app_secret) + str(ts)) # 构建 URL base_url = f"https://api.thingboot.com/{app_id}/device/control/" params = { "sign": sign, "ts": ts } # 构建请求体数据 device = "1878" # 替换为实际的设备ID;可传多个[用,间隔] order = {"power1": 1} # 替换为实际的命令 request_body = { "device": device, "order": order } # 发送请求 response = requests.post( base_url, params=params, json=request_body, headers={"Content-Type": "application/json"} ) # 输出响应 print("Response Status Code:", response.status_code) print("Response Body:", response.text) # 调用函数 send_request()
文章:
怎么用Python语言控制智能灯座
相关技术文章:
怎样用Python语言对接AC1-10A带电量统计智能开关
查看
怎样用Python语言对接吸顶式人体活动监测器
查看
怎么用Python语言控制智能壁挂Pro远程语音喇叭
查看
怎么用Python语言控制 智能广播喇叭 3
查看
怎样用Python语言控制4400W 线路控制器
查看