Skip to main content

Simulating devices with Native Simulator

caution

Native Simulator runs on Linux only. On Windows, follow this guide inside a Linux VM or WSL 2. Use the Linux tab in the setup steps below.

Install Dependencies and West​

Install Dependencies​

  1. Install dependencies with apt:

    sudo apt update
    sudo apt install --no-install-recommends git cmake ninja-build gperf \
    ccache dfu-util device-tree-compiler wget python3-dev python3-pip \
    python3-setuptools python3-tk python3-venv python3-wheel xz-utils file \
    make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1
  2. Zephyr supports Ubuntu 22.04 LTS and later and needs at least CMake 3.20.5, Python 3.10 and devicetree compiler 1.4.6. Check the installed versions:

    cmake --version
    python3 --version
    dtc --version

    If any of them is older, see Zephyr's dependency instructions.

Install West​

We recommend creating a python3 virtual environment to avoid running into tooling incompatibilities.

  1. Create your workspace directory:

    mkdir ~/golioth-zephyr-workspace
  2. Create a new virtual environment:

    Even though we haven't pulled down Zephyr yet, we can create the virtual environment in the place where we will pull down Zephyr.

    python3 -m venv ~/golioth-zephyr-workspace/.venv
  3. Activate the virtual environment:

    source ~/golioth-zephyr-workspace/.venv/bin/activate

    Whenever the virtual environment is active, your shell's prompt will be prefixed with (.venv).

    Deactivate the virtual environment when you're done by running deactivate.

    note

    Anytime you need to use west or Zephyr, remember to re-activate the virtual environment.

  4. Now, use pip to install west (beginning with the wheel dependency):

    Because we're in a python3 virtualenv, we don't need to specify pip3 and can just use pip (because virtual env knows the best version to use)

    pip install wheel
    pip install west

Install Golioth Firmware SDK​

  1. With west installed, download Zephyr and the Golioth Firmware SDK:

    cd ~
    west init -m https://github.com/golioth/golioth-firmware-sdk.git --mr v0.23.0 --mf west-zephyr.yml ~/golioth-zephyr-workspace
    cd golioth-zephyr-workspace/modules/lib/golioth-firmware-sdk
    git submodule update --init --recursive
    west update
    info

    Depending on your internet and I/O speed, west update can take upwards of 5 or 10 minutes.

  2. Tell west to automatically configure CMake:

    west zephyr-export
  3. Install the remaining dependencies:

    west packages pip --install

Toolchain check​

Native Simulator builds Zephyr as a Linux executable with your host's gcc. See the POSIX architecture host requirements.

If you have been following other tutorials, make sure no toolchain override is left in your environment:

export -p | grep "ZEPHYR"

If ZEPHYR_TOOLCHAIN_VARIANT is set to anything other than zephyr, set it to zephyr before building:

export ZEPHYR_TOOLCHAIN_VARIANT=zephyr

Zephyr then builds Native Simulator targets with the host toolchain.

Build for Native Simulator​

At this point, you can build a Golioth Zephyr sample:

cd ~/golioth-zephyr-workspace/modules/lib/golioth-firmware-sdk
west build -p -b native_sim/native/64 examples/zephyr/hello

Native Simulator samples load Golioth credentials from host environment variables. Set GOLIOTH_SAMPLE_PSK_ID and GOLIOTH_SAMPLE_PSK when running the program:

How to find credentials

Golioth Console device credentials

  • Golioth credentials are available in the Credentials tab for your device
    • Open the Golioth Console
    • Select Devices on the left sidebar and choose your device from the resulting list
    • Click on the Credentials tab and copy your PSK-ID and PSK
GOLIOTH_SAMPLE_PSK_ID="<YOUR-PSK-ID>" GOLIOTH_SAMPLE_PSK="<YOUR-PSK>" ./build/zephyr/zephyr.exe

The program will create a pseudo-terminal and print its name (e.g.):

uart connected to pseudotty: /dev/pts/1

You can use screen or a similar program of your choice to connect to the interactive shell:

screen /dev/pts/1

For more on networking, see the native_sim page on the Zephyr Docs

Example Output​

After starting the program, you can view program output either in the pseudo-terminal or on stdout. It should look similar to the following:

WARNING: Using a test - not safe - entropy source
uart connected to pseudotty: /dev/pts/1
*** Booting Zephyr OS build 7823374e8721 ***
*** Golioth Firmware SDK v0.17.0-65-g3634d7e1d392 ***
[00:00:00.000,000] <inf> net_config: Initializing network
[00:00:00.000,000] <inf> net_config: IPv4 address: 192.0.2.1
[00:00:00.000,000] <dbg> hello_zephyr: main: start hello sample
[00:00:00.000,000] <inf> golioth_mbox: Mbox created, bufsize: 1848, num_items: 10, item_size: 168
[00:00:00.320,003] <inf> golioth_coap_client_zephyr: Golioth CoAP client connected
[00:00:00.320,003] <inf> hello_zephyr: Sending hello! 0
[00:00:00.320,003] <inf> hello_zephyr: Golioth client connected
[00:00:00.320,003] <inf> golioth_coap_client_zephyr: Entering CoAP I/O loop
[00:00:05.330,000] <inf> hello_zephyr: Sending hello! 1
[00:00:10.340,000] <inf> hello_zephyr: Sending hello! 2

You can confirm this connection by viewing the Status section of the summary page for your device in the Golioth web console. You will also see the hello messages listed in the Log tab:

Golioth web console log messages

note

Logs reach the Log tab through a CBOR logs Pipeline. New projects include it by default; older projects need to add it.

Additional Golioth Example Code​

Congratulations on running the Hello app! The same process may be used to run other Golioth example applications. Be sure to reference the README file for each for detailed configuration and usage information.

  • certificate_provisioning: Use certificate authentication
  • certificate_rotation: Rotate device certificates using Golioth PKI
  • fw_update: Use Golioth over-the-air (OTA) firmware update
  • hello: Connect and send hello logging messages
  • lightdb: Set, get, observe, and delete stateful data between device and cloud
  • location: Use network information to determine device location
  • logging: Demonstrate logging messages of each different log level
  • rpc: Issue a remote procedure call (rpc) and received data back from device
  • settings: Demonstrate fleet-wide device settings service
  • stream: Send time-series data from device to cloud