Raspberry Pi consulta el último nivel del tanque a través de la API y sigue recibiendo 401
Traducido del English por IA ·
Soy nuevo en el mundo del IoT, soy desarrollador backend y me han asignado esto.Ya tenemos un sensor de nivel de tanque que envía datos a Hermetry (dispositivo «tank-01», clave level_pct). Alguien quiere una Pi en la oficina que haga sonar un zumbador cuando el nivel supere 90. Mi plan era un pequeño servicio en Python en la Pi que consultara Hermetry cada 5 segundos para obtener el último valor. Copié el token de acceso de la página de tank-01 y hice lo siguiente:
GET https://<host>/api/telemetry/latest?kind=iot_device&entityId=6f1c...&keys=level_pct
Authorization: Bearer <tank-01 token>
-> 401
Lo probé con y sin «Bearer», y el resultado fue el mismo. ¿El token es incorrecto o este es el endpoint equivocado?
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?
Traducido del English por IA ·
Eso es una credencial de dispositivo. Sirve para que un dispositivo inicie sesión, no lee nada por ti. La API de gestión necesita una clave de API. Además, consultar cada 5 segundos es demasiado para un buzzer.
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.
Traducido del English por IA ·
Brian tiene razón, y además te falta el prefijo de la organización. Monté casi lo mismo para una cámara de refrigeración el año pasado.1. API Keys (Developer menu) > New API Key. En Module permissions dale solo telemetría de lectura, nada más. Configura una lista blanca de IP con la dirección de la oficina y anota la caducidad: por defecto son 90 días, un año como máximo. Pon un recordatorio en el calendario, el nuestro caducó un sábado.2. Todas las rutas de gestión cuelgan del tenant:
GET https://<host>/tenant/<tenantId>/api/telemetry/latest?kind=iot_device&entityId=<deviceId>&keys=level_pct
Authorization: Bearer sk_live_...
La respuesta tiene este aspecto: {«latest»: {«level_pct»: {«ts»: …, «value»: 91.2}}}. Compara el ts con el anterior para no hacer sonar el zumbador dos veces con la misma lectura.3. Gestiona el 429 y espera los segundos de Retry-After. Consulta cada 30 s más o menos, el tanque no se llena en 5.La otra vía, si quieres que sea instantánea: convierte la Pi en un dispositivo, haz que una regla en el tanque le envíe un comando cuando level_pct supere 90, y deja que la Pi espere en GET /api/v1/rpc con su propio token de dispositivo. Más piezas móviles, sin polling. Para un zumbador me quedaría con la API key. Las convenciones están en 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.
Traducido del English por IA ·
Eran dos errores: el token y la parte /tenant/<tenantId> que faltaba (al tener la clave salió un 404 una vez, y luego lo vi). Ahora funciona cada 30 s con una clave de solo lectura limitada a nuestra IP. El zumbador sonó esta mañana cuando lo llenaron, así que funciona.
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.
Traducido del English por IA ·
Guarde la clave en un archivo accesible solo para root, no en el script que algún día pegará en un ticket.
Store the key in a root-only file, not in the script you’ll paste into a ticket some day.
- Debes estar registrado para responder a este debate.
Únete a la conversación
Inicia sesión para preguntar y responder, dar me gusta a las publicaciones útiles y marcar la respuesta que resolvió tu problema. Cada aportación suma puntos y niveles.

