Von English mit KI übersetzt ·
Gut, das ist dasselbe Setup, das wir für einige Signallampen verwenden. Drei Teile:
- Gerätprofil des Relais, Reiter Commands: den Befehl
light.sethinzufügen, Stufe Basic, ein Parameteronvom Typ Yes / no. Die Stufe ist wichtig, da ein API-Schlüssel nur Methoden senden kann, die im Katalog des Profils enthalten sind. - Firmware:
v1/rpc/req/+abonnieren, GPIO12 schalten (das Relais auf dem Basic R2), mit derselben ID antworten und den neuen Zustand melden.
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}");
}
- Dein Skript: ein API-Schlüssel unter API Keys (Menü Developer) mit nur den nötigen Rechten. Die Sende-Route benötigt
iot_rpc.readplus die Berechtigung der Stufe, also genau diese beiden ankreuzen und nichts weiter, die IP-Allowlist mit der Adresse des Labor-PCs füllen und das 90-Tage-Ablaufdatum beibehalten.
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"])
Ein zweiseitiger Befehl mit einem Timeout von 30 s oder weniger wartet und kommt mit 200 und SUCCESSFUL zurück. Referenz: Commands APIDer ESP8266 unterstützt TLS, aber der Speicher ist knapp; verwende BearSSL mit kleineren Puffern, falls der Verbindungsaufbau fehlschlägt.
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.

