Raspberry Pi fragt den aktuellen Tankfüllstand über die API ab und bekommt immer 401
Von English mit KI übersetzt ·
Ich bin neu im IoT-Bereich, bin Backend-Entwickler und habe das hier übernommen.Wir haben bereits einen Füllstandssensor für einen Tank, der an Hermetry meldet (Gerät „tank-01“, Schlüssel level_pct). Jemand möchte einen Pi im Büro, der einen Summer ertönen lässt, wenn der Füllstand über 90 steigt. Mein Plan war ein kleiner Python-Dienst auf dem Pi, der Hermetry alle 5 Sekunden nach dem neuesten Wert abfragt. Ich habe das Zugriffstoken von der Seite von tank-01 kopiert und Folgendes gemacht:
GET https://<host>/api/telemetry/latest?kind=iot_device&entityId=6f1c...&keys=level_pct
Authorization: Bearer <tank-01 token>
-> 401
Ich habe es mit und ohne „Bearer“ versucht, das Ergebnis ist dasselbe. Ist das Token falsch oder ist das der falsche Endpunkt?
New to the IoT side of things, I’m a backend dev and got handed this.
We have a tank level sensor already reporting to Hermetry (device „tank-01“, key level_pct). Someone wants a Pi in the office that sounds a buzzer when the level goes over 90. My plan was a tiny Python service on the Pi that polls Hermetry every 5 seconds for the latest value. I copied the access token from tank-01’s page and did:
GET https://<host>/api/telemetry/latest?kind=iot_device&entityId=6f1c...&keys=level_pct
Authorization: Bearer <tank-01 token>
-> 401
Tried with and without „Bearer“, same. Is the token wrong or is this the wrong endpoint?
Von English mit KI übersetzt ·
Das ist eine Gerätezugangsdaten. Damit meldet sich ein Gerät an, es liest aber nichts für Sie aus. Die Management-API benötigt einen API-Schlüssel. Außerdem ist alle 5 Sekunden für einen Buzzer sehr oft.
That’s a device credential. It logs a device in, it doesn’t read anything for you. The management API wants an API key. Also every 5 seconds is a lot for a buzzer.
Von English mit KI übersetzt ·
Brian hat recht, und außerdem fehlt dir das Organisationspräfix. Ich habe letztes Jahr fast dasselbe für einen Kühlraum eingerichtet.1. API Keys (Developer menu) > New API Key. Unter Module permissions nur Lesezugriff auf Telemetrie vergeben, sonst nichts. Eine IP-Allowlist auf die Adresse des Büros setzen und das Ablaufdatum notieren, Standard sind 90 Tage, maximal ein Jahr. Eine Erinnerung in den Kalender eintragen, unserer ist an einem Samstag abgelaufen.2. Alle Verwaltungspfade liegen unter dem Tenant:
GET https://<host>/tenant/<tenantId>/api/telemetry/latest?kind=iot_device&entityId=<deviceId>&keys=level_pct
Authorization: Bearer sk_live_...
Die Antwort sieht etwa so aus: {„latest“: {„level_pct“: {„ts“: …, „value“: 91.2}}}. Vergleiche den ts mit dem vorherigen Wert, damit du bei demselben Messwert nicht zweimal piepst.3. Auf 429 reagieren und die Sekunden aus Retry-After abwarten. Etwa alle 30 s abfragen, der Tank läuft nicht in 5 s voll.Der andere Weg, wenn du es sofort willst: Mach den Pi selbst zu einem Gerät, lass eine Regel am Tank ihm einen Befehl senden, wenn level_pct über 90 steigt, und der Pi wartet auf GET /api/v1/rpc mit seinem eigenen Gerätetoken. Mehr bewegliche Teile, kein Polling. Für einen Summer würde ich den API Key nehmen. Die Konventionen stehen unter REST API basics.
Brian’s right, and you’re also missing the organisation prefix. I set up nearly the same thing for a chiller room last year.
1. API Keys (Developer menu) > New API Key. Under Module permissions give it read-only telemetry, nothing else. Set an IP allowlist to the office’s address, and note the expiry, default is 90 days, a year at most. Put a reminder in the calendar, ours expired on a Saturday.
2. Every management path sits under the tenant:
GET https://<host>/tenant/<tenantId>/api/telemetry/latest?kind=iot_device&entityId=<deviceId>&keys=level_pct
Authorization: Bearer sk_live_...
Answer looks like {„latest“: {„level_pct“: {„ts“: …, „value“: 91.2}}}. Compare the ts with the previous one so you don’t buzz twice on the same reading.
3. Handle 429 and wait the Retry-After seconds. Poll every 30 s or so, the tank doesn’t fill in 5.
The other route, if you want it instant: make the Pi a device itself, have a rule on the tank send it a command when level_pct rises above 90, and let the Pi wait on GET /api/v1/rpc with its own device token. More moving parts, no polling. For a buzzer I’d take the API key. Conventions are on REST API basics.
Von English mit KI übersetzt ·
Das waren zwei Fehler: der Token und der fehlende Teil /tenant/<tenantId> (nachdem ich den Schlüssel hatte, kam einmal 404, dann habe ich es bemerkt). Läuft jetzt mit 30 s und einem schreibgeschützten Schlüssel, der auf unsere IP beschränkt ist. Der Summer ging heute Morgen los, als sie ihn gefüllt haben, also funktioniert es.
That was two bugs, the token and the missing /tenant/<tenantId> bit (got a 404 once I had the key, then saw it). Running at 30 s now with a read-only key locked to our IP. Buzzer went off this morning when they filled it, so it works.
Von English mit KI übersetzt ·
Speichern Sie den Schlüssel in einer nur für root zugänglichen Datei, nicht in dem Skript, das Sie irgendwann in ein Ticket einfügen.
Store the key in a root-only file, not in the script you’ll paste into a ticket some day.
- Du musst angemeldet sein, um auf dieses Thema antworten zu können.
Mitdiskutieren
Melde dich an, um Fragen zu stellen und zu beantworten, hilfreiche Beiträge mit „Gefällt mir“ zu markieren und die Antwort zu kennzeichnen, die dein Problem gelöst hat. Jeder Beitrag bringt Punkte und Stufen.

