> ## Documentation Index
> Fetch the complete documentation index at: https://humain.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Raspberry Pi

> Build, install, and run the Humain Kiosk C library on a Raspberry Pi (arm64, Raspberry Pi OS).

This guide covers deploying the Humain Kiosk C library on a Raspberry Pi running Raspberry Pi OS
(64-bit, Bookworm or later). By the end you will have a persistent voice-enabled kiosk service
running under systemd, with auto-reconnect on network failure.

For audio hardware recommendations — USB microphones, I²S HATs, and speaker amplifiers — see the
[Audio hardware guide](https://humain.mintlify.site/embedded/audio-hardware.md).

***

**1. Install dependencies**

Update the package list and install the core build tools and libcurl:

```bash theme={null}
sudo apt update && sudo apt install -y \
    build-essential \
    cmake \
    libcurl4-openssl-dev \
    git
```

For voice sessions, also install the WebRTC and audio dependencies:

```bash theme={null}
sudo apt install -y \
    libdatachannel-dev \
    libopus-dev \
    libsrtp2-dev \
    libasound2-dev
```

**2. Clone and build the library**

```bash theme={null}
git clone https://github.com/humain/kiosk-c.git
cd kiosk-c
```

Build with text sessions only (minimal, no voice dependencies required):

```bash theme={null}
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --parallel
sudo cmake --install build
```

Or build with voice and the built-in ALSA helper:

```bash theme={null}
cmake -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DKIOSK_WITH_VOICE=ON \
    -DKIOSK_WITH_ALSA=ON
cmake --build build --parallel
sudo cmake --install build
```

Verify the install:

```bash theme={null}
pkg-config --modversion kiosk
# 1.0.0
```

**3. Store your credential securely**

Write the credential to a root-readable environment file:

```bash theme={null}
sudo bash -c 'echo "HUMAIN_CREDENTIAL=hk_live_your_credential_here" > /etc/humain.env'
sudo chmod 600 /etc/humain.env
```

> [!WARNING]
>
> The credential is shown only once in the admin panel. Store it immediately. If lost,
> revoke it and issue a new one — do not share credentials across devices.

**4. Create your kiosk application**

Write a minimal kiosk app that reads voice input from ALSA and streams responses:

```c theme={null}
/* /usr/local/bin/humain-kiosk.c */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "kiosk.h"

static volatile int running = 1;
static kiosk_session_t *g_session = NULL;

static void handle_signal(int sig) {
    (void)sig;
    running = 0;
}

static int print_chunk(const char *chunk, size_t len, void *userdata) {
    (void)userdata;
    fwrite(chunk, 1, len, stdout);
    fflush(stdout);
    return running ? 0 : 1; /* cancel stream if signal received */
}

int main(void) {
    signal(SIGINT, handle_signal);
    signal(SIGTERM, handle_signal);

    kiosk_config_t cfg = { .credential = getenv("HUMAIN_CREDENTIAL") };
    kiosk_client_t *client = kiosk_client_create(&cfg);
    if (!client) { fputs("Failed to create client\n", stderr); return 1; }

    kiosk_error_t err = kiosk_session_open(client, KIOSK_SESSION_TEXT, &g_session);
    if (err != KIOSK_OK) {
        fprintf(stderr, "Session open: %s\n", kiosk_error_string(err));
        kiosk_client_destroy(client);
        return 1;
    }

    char input[512];
    while (running) {
        printf("\nYou: ");
        if (!fgets(input, sizeof(input), stdin)) break;
        input[strcspn(input, "\n")] = '\0';
        if (!*input) continue;

        printf("AI: ");
        err = kiosk_session_stream(g_session, input, print_chunk, NULL);
        printf("\n");

        if (err == KIOSK_ERR_SESSION_ENDED) {
            /* Server ended session — reopen */
            kiosk_session_close(g_session);
            kiosk_session_open(client, KIOSK_SESSION_TEXT, &g_session);
        }
    }

    kiosk_session_close(g_session);
    kiosk_client_destroy(client);
    return 0;
}
```

Compile and install the binary:

```bash theme={null}
gcc humain-kiosk.c -lkiosk -lcurl -o humain-kiosk
sudo mv humain-kiosk /usr/local/bin/
```

**5. Create a systemd service**

Create `/etc/systemd/system/humain-kiosk.service`:

```ini theme={null}
[Unit]
Description=Humain Kiosk voice service
After=network-online.target sound.target
Wants=network-online.target

[Service]
Type=simple
EnvironmentFile=/etc/humain.env
ExecStart=/usr/local/bin/humain-kiosk
Restart=on-failure
RestartSec=5
# Run as a dedicated user (create with: adduser --system humain)
User=humain
Group=audio

# Security hardening
NoNewPrivileges=true
PrivateTmp=true
ProtectSystem=strict
ReadWritePaths=/var/log/humain

[Install]
WantedBy=multi-user.target
```

Enable and start:

```bash theme={null}
sudo systemctl daemon-reload
sudo systemctl enable humain-kiosk
sudo systemctl start humain-kiosk
sudo systemctl status humain-kiosk
```

View live logs:

```bash theme={null}
sudo journalctl -u humain-kiosk -f
```

***

> [accordion] Auto-reconnect on network loss
>
> The library retries transient network errors automatically (up to `max_retries` times with
> exponential back-off). For persistent failure — e.g., the Pi loses Wi-Fi for more than
> 90 seconds — the systemd `Restart=on-failure` directive relaunches the process.
>
> For more resilient applications, add an explicit reconnect loop:
>
> ```c theme={null}
> static kiosk_error_t open_with_retry(kiosk_client_t *client, kiosk_session_t **out) {
>     int delay = 2;
>     for (int attempt = 0; attempt < 10; attempt++) {
>         kiosk_error_t err = kiosk_session_open(client, KIOSK_SESSION_TEXT, out);
>         if (err == KIOSK_OK) return KIOSK_OK;
>         if (err == KIOSK_ERR_AUTH || err == KIOSK_ERR_INACTIVE) return err; /* unrecoverable */
>         fprintf(stderr, "Reconnect attempt %d: %s — waiting %ds\n",
>                 attempt + 1, kiosk_error_string(err), delay);
>         sleep((unsigned)delay);
>         delay = delay < 60 ? delay * 2 : 60; /* cap at 60s */
>     }
>     return KIOSK_ERR_NETWORK;
> }
> ```
>
> Call `open_with_retry()` both on startup and whenever `kiosk_session_message()` or
> `kiosk_session_stream()` returns `KIOSK_ERR_NETWORK`, `KIOSK_ERR_TIMEOUT`, or
> `KIOSK_ERR_SESSION_ENDED`.

> [accordion] Voice session quickstart
>
> Requires the library built with `-DKIOSK_WITH_VOICE=ON -DKIOSK_WITH_ALSA=ON` and the voice
> dependencies installed (see step 1).
>
> ```c theme={null}
> /* pi-voice.c — minimal WebRTC voice session on Raspberry Pi */
> #include <stdio.h>
> #include <stdlib.h>
> #include <signal.h>
> #include "kiosk.h"
>
> static volatile int running = 1;
>
> static void handle_signal(int sig) {
>     (void)sig;
>     running = 0;
> }
>
> static void on_event(kiosk_voice_event_t event, void *userdata) {
>     (void)userdata;
>     const char *labels[] = {
>         "connecting", "listening", "processing", "speaking", "ended"
>     };
>     printf("[%s]\n", labels[event]);
>     fflush(stdout);
> }
>
> int main(void) {
>     const char *credential = getenv("HUMAIN_CREDENTIAL");
>     if (!credential) {
>         fputs("HUMAIN_CREDENTIAL not set\n", stderr);
>         return 1;
>     }
>
>     signal(SIGINT,  handle_signal);
>     signal(SIGTERM, handle_signal);
>
>     kiosk_config_t cfg = { .credential = credential };
>     kiosk_client_t *client = kiosk_client_create(&cfg);
>     if (!client) { fputs("Failed to create client\n", stderr); return 1; }
>
>     /* Set up ALSA capture and playback using the built-in helper */
>     kiosk_audio_capture_fn  capture_fn  = NULL;
>     kiosk_audio_playback_fn playback_fn = NULL;
>     void *alsa_userdata = NULL;
>
>     kiosk_alsa_config_t alsa_cfg = {
>         .capture_device  = "default",
>         .playback_device = "default",
>         .sample_rate     = 16000,
>     };
>     kiosk_error_t err = kiosk_alsa_create(&alsa_cfg,
>                                            &capture_fn,
>                                            &playback_fn,
>                                            &alsa_userdata);
>     if (err != KIOSK_OK) {
>         fprintf(stderr, "ALSA init: %s\n", kiosk_error_string(err));
>         kiosk_client_destroy(client);
>         return 1;
>     }
>
>     kiosk_voice_config_t vcfg = {
>         .capture_fn  = capture_fn,
>         .playback_fn = playback_fn,
>         .event_fn    = on_event,
>         .userdata    = alsa_userdata,
>     };
>
>     kiosk_voice_session_t *vsession = NULL;
>     err = kiosk_voice_session_open(client, &vcfg, &vsession);
>     if (err != KIOSK_OK) {
>         fprintf(stderr, "Voice session open: %s\n", kiosk_error_string(err));
>         kiosk_alsa_destroy(alsa_userdata);
>         kiosk_client_destroy(client);
>         return 1;
>     }
>
>     err = kiosk_voice_session_start(vsession);
>     if (err != KIOSK_OK) {
>         fprintf(stderr, "Voice session start: %s\n", kiosk_error_string(err));
>     } else {
>         printf("Voice session active — speak into the microphone. Press Ctrl+C to stop.\n");
>         while (running) pause(); /* wait for signal */
>     }
>
>     kiosk_voice_session_stop(vsession); /* flushes audio and frees vsession */
>     kiosk_alsa_destroy(alsa_userdata);
>     kiosk_client_destroy(client);
>     return (err == KIOSK_OK) ? 0 : 1;
> }
> ```
>
> Compile and run:
>
> ```bash theme={null}
> export HUMAIN_CREDENTIAL=hk_live_your_credential_here
> gcc pi-voice.c -lkiosk -lcurl -o pi-voice
> ./pi-voice
> ```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.