Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WiFi location service with sample #708

Merged
merged 5 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/zephyr/location/wifi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
mniestroj marked this conversation as resolved.
Show resolved Hide resolved
mniestroj marked this conversation as resolved.
Show resolved Hide resolved

cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(location_wifi)

target_sources(app PRIVATE src/main.c)
target_sources_ifdef(CONFIG_GOLIOTH_WIFI_PLAYBACK app PRIVATE src/wifi_playback.c)
25 changes: 25 additions & 0 deletions examples/zephyr/location/wifi/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2021-2024 Golioth, Inc.
# SPDX-License-Identifier: Apache-2.0

source "${ZEPHYR_GOLIOTH_FIRMWARE_SDK_MODULE_DIR}/examples/zephyr/common/Kconfig.defconfig"

config GOLIOTH_WIFI_PLAYBACK
bool
default y
depends on DT_HAS_GOLIOTH_WIFI_PLAYBACK_ENABLED

if GOLIOTH_WIFI_PLAYBACK

config GOLIOTH_WIFI_PLAYBACK_SPEED_FACTOR
int "Speed factor"
default 1000
help
Speed of playback.

1000 means 1x (normal) speed.
3000 means 3x speed.
500 means 0.5x speed.

endif # GOLIOTH_WIFI_PLAYBACK

source "Kconfig.zephyr"
210 changes: 210 additions & 0 deletions examples/zephyr/location/wifi/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
# Golioth WiFi location demo

## Overview

This sample demonstrates how to connect to Golioth and get location
using scanned WiFi networks.

## Requirements

* Golioth credentials
* Network connectivity

## Building and Running

### Authentication specific configuration

#### PSK based auth - Hardcoded

Configure the following Kconfig options based on your Golioth
credentials:

* GOLIOTH_SAMPLE_PSK_ID - PSK ID of registered device
* GOLIOTH_SAMPLE_PSK - PSK of registered device

by adding these lines to configuration file (e.g. `prj.conf`):

```cfg
CONFIG_GOLIOTH_SAMPLE_PSK_ID="my-psk-id"
CONFIG_GOLIOTH_SAMPLE_PSK="my-psk"
```

#### PSK based auth - Runtime

We provide an option for setting Golioth credentials through the Zephyr
shell. This is based on the Zephyr Settings subsystem.

Enable the settings shell by including the following configuration overlay
file:

```sh
$ west build -- -DEXTRA_CONF_FILE=../../common/runtime_settings.conf
```

Alternatively, you can add the following options to ``prj.conf``:

```cfg
CONFIG_GOLIOTH_SAMPLE_HARDCODED_CREDENTIALS=n

CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y

CONFIG_SETTINGS=y
CONFIG_SETTINGS_RUNTIME=y
CONFIG_GOLIOTH_SAMPLE_SETTINGS=y
CONFIG_GOLIOTH_SAMPLE_WIFI_SETTINGS=y
CONFIG_GOLIOTH_SAMPLE_SETTINGS_AUTOLOAD=y
CONFIG_GOLIOTH_SAMPLE_SETTINGS_SHELL=y
```

At runtime, configure PSK-ID and PSK using the device shell based on your
Golioth credentials:

```sh
uart:~$ settings set golioth/psk-id <my-psk-id@my-project>
uart:~$ settings set golioth/psk <my-psk>
uart:-$ kernel reboot cold
```

#### Certificate based auth

Configure the following Kconfig options based on your Golioth
credentials:

* CONFIG_GOLIOTH_AUTH_METHOD_CERT - use certificate-based
authentication
* CONFIG_GOLIOTH_SAMPLE_HARDCODED_CRT_PATH - device certificate
* CONFIG_GOLIOTH_SAMPLE_HARDCODED_KEY_PATH - device private key

by adding these lines to configuration file (e.g. `prj.conf`):

```cfg
CONFIG_GOLIOTH_AUTH_METHOD_CERT=y
CONFIG_GOLIOTH_SAMPLE_HARDCODED_CRT_PATH="keys/device.crt.der"
CONFIG_GOLIOTH_SAMPLE_HARDCODED_KEY_PATH="keys/device.key.der"
```

### Platform specific configuration

#### Native Simulator

This application has been built and tested with Native Simulator
(native_sim).

On your Linux host computer, open a terminal window, locate the source
code of this sample application (i.e., `examples/zephyr/location/wifi`)
and type:

```console
$ west build -b native_sim examples/zephyr/location/wifi
$ west build -t run
```

#### ESP32-DevKitC-WROVER

Configure the following Kconfig options based on your WiFi AP
credentials:

- GOLIOTH_SAMPLE_WIFI_SSID - WiFi SSID
- GOLIOTH_SAMPLE_WIFI_PSK - WiFi PSK

by adding these lines to configuration file (e.g. `prj.conf`):

```cfg
CONFIG_GOLIOTH_SAMPLE_WIFI_SSID="my-wifi"
CONFIG_GOLIOTH_SAMPLE_WIFI_PSK="my-psk"
```

On your host computer open a terminal window, locate the source code of
this sample application (i.e., `examples/zephyr/location/wifi`) and type:

```console
$ west build -b esp32_devkitc_wrover/esp32/procpu examples/zephyr/location/wifi
$ west flash
```

#### nRF52840 DK + ESP32-WROOM-32

This subsection documents using nRF52840 DK running Zephyr with
offloaded ESP-AT WiFi driver and ESP32-WROOM-32 module based board (such
as ESP32 DevkitC rev. 4) running WiFi stack. See [AT Binary
Lists](https://docs.espressif.com/projects/esp-at/en/latest/AT_Binary_Lists/index.html)
for links to ESP-AT binaries and details on how to flash ESP-AT image on
ESP chip. Flash ESP chip with following command:

```console
esptool.py write_flash --verify 0x0 PATH_TO_ESP_AT/factory/factory_WROOM-32.bin
```

Connect nRF52840 DK and ESP32-DevKitC V4 (or other ESP32-WROOM-32 based
board) using wires:

| nRF52840 DK | ESP32-WROOM-32 | ESP32-WROVER-32 |
| ----------- | --------------- | ----------------|
| P1.01 (RX) | IO17 (TX) | IO22 (TX) |
| P1.02 (TX) | IO16 (RX) | IO19 (RX) |
| P1.03 (CTS) | IO14 (RTS) | IO14 (RTS) |
| P1.04 (RTS) | IO15 (CTS) | IO15 (CTS) |
| P1.05 | EN | EN |
| GND | GND | GND |

Configure the following Kconfig options based on your WiFi AP
credentials:

* GOLIOTH_SAMPLE_WIFI_SSID - WiFi SSID
* GOLIOTH_SAMPLE_WIFI_PSK - WiFi PSK

by adding these lines to configuration file (e.g. `prj.conf` or
`board/nrf52840dk_nrf52840.conf`):

```cfg
CONFIG_GOLIOTH_SAMPLE_WIFI_SSID="my-wifi"
CONFIG_GOLIOTH_SAMPLE_WIFI_PSK="my-psk"
```

On your host computer open a terminal window, locate the source code of
this sample application (i.e., `examples/zephyr/location/wifi`) and type:

```console
$ west build -b nrf52840dk/nrf52840 examples/zephyr/location/wifi
$ west flash
```

### Sample output

This is the output from the serial console:

```console
*** Booting Zephyr OS build v4.0.0-1-gb39d67f51986 ***
[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> location_wifi_main: main: Start WiFi location sample
[00:00:00.000,000] <inf> golioth_mbox: Mbox created, bufsize: 1848, num_items: 10, item_size: 168
[00:00:00.400,003] <inf> golioth_coap_client_zephyr: Golioth CoAP client connected
[00:00:00.400,003] <inf> location_wifi_main: Golioth client connected
[00:00:00.400,003] <inf> golioth_coap_client_zephyr: Entering CoAP I/O loop
[00:00:00.580,002] <inf> location_wifi_main: 50.663974800 17.942322850 (32)
[00:00:00.760,001] <inf> location_wifi_main: 50.664181170 17.942337360 (23)
[00:00:01.170,001] <inf> location_wifi_main: 50.664464180 17.942332180 (22)
[00:00:01.700,001] <inf> location_wifi_main: 50.665216090 17.942386110 (26)
[00:00:02.190,001] <inf> location_wifi_main: 50.665924320 17.942342850 (17)
[00:00:02.690,001] <inf> location_wifi_main: 50.666588620 17.942297690 (21)
[00:00:03.180,001] <inf> location_wifi_main: 50.667436140 17.942253480 (25)
[00:00:03.680,001] <inf> location_wifi_main: 50.667930110 17.942142910 (27)
[00:00:04.170,001] <inf> location_wifi_main: 50.668083230 17.942170380 (33)
[00:00:04.670,001] <inf> location_wifi_main: 50.669921000 17.948761000 (569)
[00:00:05.170,001] <inf> location_wifi_main: 50.667979840 17.942204310 (36)
[00:00:05.660,001] <inf> location_wifi_main: 50.665045000 17.947862000 (604)
[00:00:06.170,001] <inf> location_wifi_main: 50.668312550 17.942551440 (31)
[00:00:06.670,001] <inf> location_wifi_main: 50.668242700 17.942328220 (52)
[00:00:07.170,001] <inf> location_wifi_main: 50.668386960 17.942680660 (46)
[00:00:07.670,001] <inf> location_wifi_main: 50.668344030 17.943467020 (37)
[00:00:08.180,001] <inf> location_wifi_main: 50.668361530 17.943982820 (50)
[00:00:08.680,001] <inf> location_wifi_main: 50.668426910 17.945390790 (58)
[00:00:09.170,001] <inf> location_wifi_main: 50.668490680 17.947672650 (43)
[00:00:09.780,001] <inf> location_wifi_main: 50.668505260 17.947435020 (34)
[00:00:10.160,001] <inf> location_wifi_main: 50.668612950 17.948811810 (71)
[00:00:10.670,001] <inf> location_wifi_main: 50.668666040 17.948747050 (107)
[00:00:11.270,001] <inf> location_wifi_main: 50.664927000 17.947822000 (593)
```
10 changes: 10 additions & 0 deletions examples/zephyr/location/wifi/boards/nrf52840dk_nrf52840.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CONFIG_WIFI=y
CONFIG_WIFI_ESP_AT=y
CONFIG_WIFI_ESP_AT_MDM_RX_BUF_COUNT=40
CONFIG_WIFI_ESP_AT_CIPDINFO_USE=y
CONFIG_WIFI_ESP_AT_DNS_USE=y
CONFIG_WIFI_ESP_AT_SCAN_MAC_ADDRESS=y
CONFIG_WIFI_ESP_AT_SCAN_RESULT_RSSI_ORDERED=y

CONFIG_NET_L2_WIFI_SHELL=y
CONFIG_GOLIOTH_SAMPLE_WIFI=y
31 changes: 31 additions & 0 deletions examples/zephyr/location/wifi/boards/nrf52840dk_nrf52840.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2023 Golioth, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/

&uart1_default {
cts-rts {
psels = <NRF_PSEL(UART_CTS, 1, 3)>,
<NRF_PSEL(UART_RTS, 1, 4)>;
};
};

&uart1_sleep {
cts-rts {
psels = <NRF_PSEL(UART_CTS, 1, 3)>,
<NRF_PSEL(UART_RTS, 1, 4)>;
low-power-enable;
};
};

&uart1 {
status = "okay";
hw-flow-control;

esp_wifi: esp-wifi {
compatible = "espressif,esp-at";
power-gpios = <&gpio1 5 (GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN)>;
status = "okay";
};
};
15 changes: 15 additions & 0 deletions examples/zephyr/location/wifi/dts/bindings/vendor-prefixes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Device tree binding vendor prefix registry. Keep this list in
# alphabetical order.
#
# This isn't an exhaustive list, but you should add new prefixes to it
# before using them to avoid name-space collisions.
#
# The contents of this file are parsed during documentation generation.
# Anything that starts with a '#' is treated as a comment and ignored.
# Non-empty lines should be in this format:
#
# <vendor-prefix><TAB><Full name of vendor>

# zephyr-keep-sorted-start
golioth Golioth, Inc.
# zephyr-keep-sorted-stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) 2024 Golioth, Inc.
# SPDX-License-Identifier: Apache-2.0

description: WiFi driver with playback of scanned WiFi networks

compatible: "golioth,wifi-playback"

include: base.yaml
10 changes: 10 additions & 0 deletions examples/zephyr/location/wifi/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Application
CONFIG_GOLIOTH_SAMPLE_COMMON=y
CONFIG_GOLIOTH_SAMPLE_HARDCODED_CREDENTIALS=y

# Golioth Firmware SDK with all dependencies
CONFIG_GOLIOTH_FIRMWARE_SDK=y

CONFIG_GOLIOTH_LOCATION=y
CONFIG_GOLIOTH_LOCATION_WIFI=y
CONFIG_WIFI=y
16 changes: 16 additions & 0 deletions examples/zephyr/location/wifi/sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sample:
description: Location WiFi
name: location wifi
common:
build_only: true
tags:
- golioth
- location
- socket
tests:
sample.golioth.location_wifi:
platform_allow:
- esp32_devkitc_wrover/esp32/procpu
- native_sim
- native_sim/native/64
- nrf52840dk/nrf52840
13 changes: 13 additions & 0 deletions examples/zephyr/location/wifi/socs/esp32_procpu.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
CONFIG_WIFI=y
CONFIG_HEAP_MEM_POOL_SIZE=49152

CONFIG_NET_L2_ETHERNET=y

CONFIG_NEWLIB_LIBC=y

CONFIG_NET_DHCPV4=y

CONFIG_ESP32_WIFI_STA_AUTO_DHCPV4=y

CONFIG_NET_L2_WIFI_SHELL=y
CONFIG_GOLIOTH_SAMPLE_WIFI=y
3 changes: 3 additions & 0 deletions examples/zephyr/location/wifi/socs/esp32_procpu.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
&wifi {
status = "okay";
};
24 changes: 24 additions & 0 deletions examples/zephyr/location/wifi/socs/native.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use offloaded sockets using host BSD sockets
CONFIG_ETH_DRIVER=n
CONFIG_ETH_NATIVE_POSIX=n
CONFIG_NET_DRIVERS=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y
CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y
CONFIG_HEAP_MEM_POOL_SIZE=1024

# Use embedded libc to use Zephyr's eventfd instead of host eventfd
CONFIG_PICOLIBC=y

# Static networking
CONFIG_NET_CONFIG_LOG_LEVEL_DBG=y
CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"

# WiFi playback
CONFIG_WIFI_OFFLOAD=y
CONFIG_NET_L2_WIFI_MGMT=y
CONFIG_NET_IF_MAX_IPV4_COUNT=2
6 changes: 6 additions & 0 deletions examples/zephyr/location/wifi/socs/native.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/ {
wifi-playback {
compatible = "golioth,wifi-playback";
status = "okay";
};
};
Loading
Loading