Traducido del English por IA ·
Bien, esa es la misma configuración que usamos para unas cuantas lámparas de señalización. Tres partes:
- Perfil de dispositivo del relé, pestaña Commands: añadir un comando
light.set, nivel Basic, un parámetroonde tipo Yes / no. El nivel importa, porque una clave API solo puede enviar métodos que estén en el catálogo del perfil. - Firmware: suscribirse a
v1/rpc/req/+, conmutar GPIO12 (el relé de la Basic R2), responder con el mismo id e informar del nuevo estado.
void onMsg(char* topic, byte* p, unsigned int n) {
// topic: v1/rpc/req/42
String id = String(topic).substring(11);
StaticJsonDocument<128> d; deserializeJson(d, p, n);
bool on = d["params"]["on"];
digitalWrite(12, on ? HIGH : LOW);
mqtt.publish(("v1/rpc/res/" + id).c_str(), "{"ok":true}");
mqtt.publish("v1/telemetry", on ? "{"relay":1}" : "{"relay":0}");
}
- Tu script: una clave API desde API Keys (menú Developer) con solo lo que necesite. La ruta de envío requiere
iot_rpc.readmás el permiso del nivel, así que marca esos dos y nada más, rellena la lista de IP permitidas con la dirección del PC del laboratorio y mantén la caducidad de 90 días.
import requests
r = requests.post(
f"https://{HOST}/tenant/{TENANT}/api/iot/devices/{DEVICE}/rpc",
headers={"Authorization": f"Bearer {KEY}"},
json={"method": "light.set", "params": {"on": True}, "timeoutMs": 10000},
)
print(r.status_code, r.json()["rpc"]["status"])
Un comando bidireccional con un timeout de 30 s o menos espera y devuelve 200 con SUCCESSFUL. Referencia: Commands APIEl ESP8266 maneja TLS, pero la memoria es escasa; usa BearSSL con buffers más pequeños si la conexión falla.
Good, that’s the same setup we use for a few signal lamps. Three pieces:
- Device profile of the relay, Commands tab: add a command
light.set, tier Basic, one parameteronof type Yes / no. Tier matters, because an API key can only send methods that are in the profile’s catalog. - Firmware: subscribe to
v1/rpc/req/+, switch GPIO12 (the relay on the Basic R2), answer on the same id, and report the new state.
void onMsg(char* topic, byte* p, unsigned int n) {
// topic: v1/rpc/req/42
String id = String(topic).substring(11);
StaticJsonDocument<128> d; deserializeJson(d, p, n);
bool on = d["params"]["on"];
digitalWrite(12, on ? HIGH : LOW);
mqtt.publish(("v1/rpc/res/" + id).c_str(), "{"ok":true}");
mqtt.publish("v1/telemetry", on ? "{"relay":1}" : "{"relay":0}");
}
- Your script: an API key from API Keys (Developer menu) with only what it needs. The send route wants
iot_rpc.readplus the tier’s permission, so tick those two and nothing else, fill the IP allowlist with the lab PC’s address and keep the 90-day expiry.
import requests
r = requests.post(
f"https://{HOST}/tenant/{TENANT}/api/iot/devices/{DEVICE}/rpc",
headers={"Authorization": f"Bearer {KEY}"},
json={"method": "light.set", "params": {"on": True}, "timeoutMs": 10000},
)
print(r.status_code, r.json()["rpc"]["status"])
A two-way command with a timeout of 30 s or less waits and comes back 200 with SUCCESSFUL. Reference: Commands API
The ESP8266 does TLS, but memory is tight; use BearSSL with smaller buffers if the connect fails.

