Hi, I installed a wallbox that I connect to the SmartLife app to manage my car’s charging current.
If possible, I’d like to manage the charging current with a Raspberry Pi script, sending commands via the Tuya cloud. I’ve already connected the wallbox to the app and have all the necessary login credentials.
I tried creating a text script, but it always gives me an error.
import requests
import time
import hashlib
import hmac
CLIENT_ID = “ygx8…”
CLIENT_SECRET = “0b7…”
DEVICE_ID = “bf…”
BASE_URL = “https://openapi.tuyaeu.com”
def sign_request(client_id, client_secret, access_token, t, method, path, body=“”):
content_md5 = “” # body vuoto, md5 vuoto
content_type = “”
# Costruisco la stringa da firmare con \n reali
string_to_sign = f"{method}\n{content_md5}\n{content_type}\n{t}\n{path}"
message = client_id + access_token + t + string_to_sign
signature = hmac.new(client_secret.encode(‘utf-8’),
msg=message.encode(‘utf-8’),
digestmod=hashlib.sha256).hexdigest().upper()
return signature, string_to_sign
def get_token():
t = str(int(time.time() * 1000))
path = “/v1.0/token?grant_type=1”
method = “GET”
access_token = “”
sign, string_to_sign = sign_request(CLIENT_ID, CLIENT_SECRET, access_token, t, method, path)
headers = {
"client_id": CLIENT_ID,
"sign": sign,
"t": t,
"sign_method": "HMAC-SHA256"
}
print("=== TOKEN REQUEST ===")
print("Stringa da firmare:")
print(string_to_sign)
print("Firma calcolata:", sign)
url = BASE_URL + path
response = requests.get(url, headers=headers)
print("Risposta server:", response.text)
data = response.json()
if data.get("success"):
return data["result"]["access_token"]
else:
raise Exception(f"Errore token: {data}")
def get_device_status(token):
t = str(int(time.time() * 1000))
path = f"/v1.0/devices/{DEVICE_ID}/status"
method = “GET”
body = “”
sign, string_to_sign = sign_request(CLIENT_ID, CLIENT_SECRET, token, t, method, path, body)
headers = {
"client_id": CLIENT_ID,
"access_token": token,
"sign": sign,
"t": t,
"sign_method": "HMAC-SHA256"
}
print("\n=== DEVICE STATUS REQUEST ===")
print("Stringa da firmare:")
print(string_to_sign)
print("Firma calcolata:", sign)
url = BASE_URL + path
response = requests.get(url, headers=headers)
print("Risposta server:", response.text)
if name == “main”:
token = get_token()
get_device_status(token)
here’s the error:
PS C:\Users\remod\Desktop\walbox> python .\walbox_status.py
=== TOKEN REQUEST ===
Stringa da firmare:
GET
1754754603731
/v1.0/token?grant_type=1
Firma calcolata: 010057068742C216387FFF7472E2BD745E69F18C18187BF15E91044359026470
Risposta server: {“code”:1004,“msg”:“sign invalid”,“success”:false,“t”:1754754634144,“tid”:“95dd23bc753811f0884d66005ac75605”}
Traceback (most recent call last):
File “C:\Users\remod\Desktop\walbox\walbox_status.py”, line 77, in
token = get_token()
File “C:\Users\remod\Desktop\walbox\walbox_status.py”, line 50, in get_token
raise Exception(f"Errore token: {data}")
Exception: Errore token: {‘code’: 1004, ‘msg’: ‘sign invalid’, ‘success’: False, ‘t’: 1754754634144, ‘tid’: ‘95dd23bc753811f0884d66005ac75605’}