Configure the network

Read and change the network configuration over the API. In the WebGUI, see Network settings. The examples use the Python library and sign in with an application key. Replace qb2-xxxxxxxxx with the hostname or IP address of your sensor and application-key-for-qb2-xxxxxxxxx with the secret of your key.

Get the network configuration

The method GetConfig returns the network configuration set on the sensor.

import blickfeld_qb2

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    response = service.get_config()
    print(response)

Output:

Network()

A sensor on which no network configuration was set returns an empty configuration and uses DHCP and link-local addressing. The method GetStatus returns the configuration in use, with the addresses, in active_config.

Set a static network configuration

In a network without a DHCP server, set the IPv4 address by hand. The example sets two addresses, the gateway and two DNS servers.

After the change, the sensor is only reachable under the new address. If you cannot reach it any more, connect over the fallback IP address 192.168.26.26.

import blickfeld_qb2

# Two addresses, the gateway and two DNS servers
manual = blickfeld_qb2.system.config.NetworkStatic(
    ipv4=blickfeld_qb2.system.config.NetworkStaticIpv4(
        ip=["10.0.2.15/24", "10.0.2.43/24"],
        gateway="10.0.2.2",
        dns_server=["8.8.4.4", "8.8.8.8"],
    )
)

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

# Open a secure and authenticated connection to the sensor
with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Set the configuration
    service.set_config(manual=manual)

Set a wireless configuration

This example connects the sensor to a WiFi network. It tries the configuration with validate first and applies it only when the sensor got an address in the WiFi network.

validate applies the configuration for a moment, up to about a minute, and then returns to the current one. Meanwhile, the sensor leaves its current network and is not reachable under its current address.

import blickfeld_qb2
from grpclib import GRPCError

token_factory = blickfeld_qb2.TokenFactory(application_key_secret="application-key-for-qb2-xxxxxxxxx")

with blickfeld_qb2.Channel(fqdn_or_ip="qb2-xxxxxxxxx", token=token_factory) as channel:
    service = blickfeld_qb2.system.services.Network(channel)

    # Create a new configuration
    configuration = blickfeld_qb2.system.config.Network()

    # Activate DHCP
    configuration.dhcp = True

    # Configure the wifi settings
    configuration.wireless.ssid = "your_wifi_ssid"
    configuration.wireless.password = "your_wifi_password"

    # Validate the wireless configuration: the sensor applies it shortly and returns to its current configuration.
    # It raises an error with the reason when the sensor gets no address or no gateway in the WiFi network.
    try:
        response = service.validate(config=configuration)
    except GRPCError as error:
        raise SystemExit(f"Validation failed: {error.message}") from None
    print("Addresses in the WiFi network:", response.status.active_config.manual.ipv4.ip)

    # Apply the configuration
    service.set_config(dhcp=configuration.dhcp, wireless=configuration.wireless)

    # The sensor is now reachable under one of the addresses of the validation