# OAK PoE deployment guide

Power-over-Ethernet (PoE) allows ethernet cable to be used to both power a device and give it connectivity with 1 Gbps bandwidth
full-duplex at up to 100 meters (328 feet).

### Powering PoE device

You will need a PoE switch or Injector to power the PoE device (see [Powering PoE
devices](https://docs.luxonis.com/hardware/platform/deploy/powering-poe.md)).

After powering the device, the power LED on the switch/injector should turn on, as on the video below.

RJ45 PoE connector

M12 PoE connector

### Check connector

Make sure to properly tighten the connector - either ethernet gland for RJ45 PoE cameras, or the M12 connector - to make a good
seal. This ensures that the device meets its [IP
rating](https://docs.luxonis.com/hardware/platform/environmental-specifications/ip-rating.md).

> **Cable diameter**
> The ethernet cable diameter for RJ45 PoE cameras should be between 5mm and 7mm to fit the gland. Higher diameter will not fit,
and lower diameter will not seal properly.

### Initial Connection

DepthAI library will look for all available devices inside the same [LAN](https://en.wikipedia.org/wiki/Local_area_network). If
the camera is not on the same LAN, you can manually specify the IP address of the device. If DHCP server is not available, the
camera will fallback to a static IP 169.254.1.222.

The best way to test your device is by running the [OAK Viewer](https://docs.luxonis.com/software-v3/depthai/tools/oak-viewer.md).
It's a GUI application that allows you to easily evaluate the camera by visualizing its output in real-time. If it finds the
device and starts streaming frames, then the connection is working properly.

### Debugging

If you are getting an error like RuntimeError: No available devices, it means that DepthAI couldn't find any PoE device on the
same subnet. Make sure that the camera is powered on and connected to the same network as your computer. See the tabs below for
debugging steps.

We recommend using the latest depthai version before any kind of troubleshooting.

#### Different Subnet

The DepthAI library only searches for available OAK PoE cameras inside the same subnet. You can still ping the camera, but
autodiscovery won't find the device. If the camera is not in the same subnet, you can manually specify the device IP, as shown in
the code below.

#### Python

```py
import depthai as dai
pipeline = dai.Pipeline()
# ... Populate pipeline ...

# Define IP address of the device
device_info = dai.DeviceInfo("169.254.1.222")

with dai.Device(device_info) as device:
    # Define queues, get messages...
```

#### C++

```cpp
#include "depthai/depthai.hpp"

int main(int argc, char** argv) {
    dai::Pipeline pipeline;
    // ... Populate pipeline ...

    // Define IP address of the device
    auto deviceInfo = dai::DeviceInfo("169.254.1.222");

    dai::Device device(deviceInfo);

    // Define queues, get messages...
    return 0;
}
```

#### Multiple interfaces

When using PoE devices with a PC that is connected to both WiFi and Ethernet, network routing needs adjustment to avoid
connectivity issues. A common problem is the system detecting the same PoE device twice, leading to initialization errors
(RuntimeError: Failed to find device after booting, error message: X_LINK_DEVICE_NOT_FOUND).

Quick Fix: Disconnect from WiFi to prevent duplicate device discovery.

You can also manually configure network routing to ensure the DepthAI device communicates through PoE while maintaining internet
access via WiFi:

#### Windows

 1. Open Command Prompt: Press Win + R, type cmd, and press Enter to open Command Prompt.

 2. Identify Network Adapters: Type ipconfig and press Enter. Note the names of your network adapters (e.g., Ethernet, Wi-Fi) and
    their IP addresses.

 3. Add a Route:
    
    * Use the route command to add a persistent route to the DepthAI device. The general format is: route -p ADD [DepthAI-IP] MASK
      [Subnet-Mask] [Adapter-Gateway-IP].
    * Replace [DepthAI-IP] with the IP address of your DepthAI device, [Subnet-Mask] with the appropriate subnet mask, and
      [Adapter-Gateway-IP] with the gateway IP of the adapter you wish to use for DepthAI communication.

 4. Verify the Route: After adding the route, you can verify it by typing route print and pressing Enter.

#### Linux

 1. Open Terminal: You can find the Terminal application in the applications menu or use the shortcut Ctrl + Alt + T.

 2. Identify Network Interfaces: Type ip addr and press Enter. Note the names of your network interfaces (e.g., eth0 for Ethernet,
    wlan0 for Wi-Fi).

 3. Add a Route:
    
    * Use the ip route command to add a route to the DepthAI device: sudo ip route add [DepthAI-IP]/24 via [Adapter-Gateway-IP]
      dev [Network-Interface].
    * Replace [DepthAI-IP] with the IP address of your DepthAI device (make sure the mask - 24 - is correct), [Adapter-Gateway-IP]
      with the gateway IP of the adapter you wish to use for DepthAI communication, and [Network-Interface] with the name of the
      network interface.

 4. Verify the Route: You can check the route by typing ip route show and pressing Enter.

Make sure to replace the values in brackets with the appropriate values for your system. Default settings for DepthAI devices
(when disconnected from a DHCP server) are specified in No DHCP panel above.

More info on routing/making routes persistant can be found here:
[Windows](https://www.howtogeek.com/22/adding-a-tcpip-route-to-the-windows-routing-table/),
[macOS](https://www.backup.ch/post/macos-how-to-add-a-static-route/), [Ubuntu](https://www.cyberciti.biz/faq/linux-route-add/).

#### macOS

 1. Open Terminal: You can find Terminal in Applications under Utilities, or use Spotlight to search for it.

 2. Identify Network Interfaces: Type ifconfig and press Enter. Note the names of your network interfaces (e.g., en0 for Ethernet,
    en1 for Wi-Fi).

 3. Add a Route:
    
    * Under network settings -> set service order, drag the interface used for DepthAI to the bottom of the list.
    * Direct the relevant traffic to the DepthAI adapter: sudo route add -net 169.254 -interface [dongle-interface].
    * Replace [dongle-interface] with the DepthAI network interface.

#### No DHCP

By default, PoE devices will try to get a dynamic IP address from DHCP server. However, if there's no DHCP server on the network,
devices will fall back to their static IP 169.254.1.222. In this case, you can either:

 1. Connect the switch to a router, to get a dynamic IP address from the DHCP server.
 2. Set static IP of your computer (eg. 169.254.1.10, netmask: 255.255.0.0) to be in the same subnet as the PoE device, and then
    connect to your device.
 3. Manually specify device IP address when connect to it, you can use either IP address or MxId:

#### Python

```py
import depthai as dai
pipeline = dai.Pipeline()
# ... Populate pipeline ...
device_info = dai.DeviceInfo("169.254.1.222")
# device_info = depthai.DeviceInfo("14442C108144F1D000") # MXID
# device_info = depthai.DeviceInfo("3.3.3") # USB port name

with dai.Device(device_info) as device:
    # Define queues, get messages...
```

#### C++

```cpp
#include "depthai/depthai.hpp"
int main(int argc, char** argv) {
    dai::Pipeline pipeline;
    // ... Populate pipeline ...
    auto deviceInfo = dai::DeviceInfo("169.254.1.222");
    // auto deviceInfo = dai::DeviceInfo("14442C108144F1D000");
    // auto deviceInfo = dai::DeviceInfo("3.3.3");
    dai::Device device(deviceInfo);
    // Define queues, get messages...
    return 0;
}
```

> **Multiple devices**
> If you are using multiple PoE devices, option 2 and 3 won't work, as all devices will try to use the same IP address. If that is
the case, you will need to assign static IP addresses to your PoE devices, see
> [set static IP](https://docs.luxonis.com/software-v3/depthai/depthai-components/bootloader.md)
> .

#### Firewall

UDP Device discovery is handled on port 11491 and TCP XLink connection is handled on port 11490.

On Ubuntu by default the firewall is disabled, so you shouldn't have any issues. You can check this though by executing the
following command:

```bash
> sudo ufw status
Status: inactive
```

If you have your firewall enabled, you might need to allow these two ports:

```bash
sudo ufw allow 11490/tcp
sudo ufw allow 11491/udp
```

We have noticed that the above rules don't always work as expected, and it's sometimes necessary to run the command below. For
production environment, you would want to set static IP on your OAK PoE camera, otherwise it could change and you would need to
re-set the firewall rules.

```bash
ufw allow from [OAK_POE_IP]
```

#### Network equipment

We have noticed that with certain network equipment, our default 15 seconds timeout for connecting to the POE device is
insufficient, and needs to be increased. You can increase the timeout by using environmental variable (values in milliseconds).

#### Windows Powershell

```bash
$env:DEPTHAI_WATCHDOG_INITIAL_DELAY=60000
$env:DEPTHAI_BOOTUP_TIMEOUT=60000
python3 script.py
```

#### Linux/macOS

```bash
DEPTHAI_WATCHDOG_INITIAL_DELAY=60000 DEPTHAI_BOOTUP_TIMEOUT=60000 python3 script.py
```

#### Windows CMD

```bash
set DEPTHAI_WATCHDOG_INITIAL_DELAY=60000
set DEPTHAI_BOOTUP_TIMEOUT=60000
python3 script.py
```

#### VPN

VPN connectivity could also disrupt the connection with the PoE device (as your computer may be searching only the remote network
for the device, so would be unable to discover it on the local network), so we suggest turning the VPN off when using the PoE
devices or otherwise ensuring that your local routing is setup such that local devices are usable/discoverable while VPN
connectivity is active.

### Runtime

After successfully connecting to the device and uploading the pipeline, the connection should be stable, there should be minimal
latency (below 1sec), and the bandwidth should be 700 Mbps.

### Debugging

#### Connection drop

If the connection drops after a period of time, there are a few potential issues that could be causing it.

#### Network issue

Due to the network and networking equipment, OAK PoE cameras might drop the connection after running as expected for a few hours
or a few days. There are 3 potential solutions/workarounds for this issue:

 1. Decrease the network load, so the connection doesn't drop. This can be achieved by decreasing the FPS of the camera, or by
    decreasing the resolution of the camera.
 2. Auto-reconnect if the camera drops connection. We have a demo here that does that. The downside is that the reconnection can
    take up to about 30 seconds. First, the watchdog has to kick in and reset the camera (~5sec). Then, the bootloader has to
    reboot on the camera so it's accessible again (~5sec). And finally, the host computer has to initialize a new connection and
    transfer the firmware, the pipeline, and all assets (NNs) to the camera, so it can boot up the pipeline (application). We have
    [auto-reconnection example here](https://gist.github.com/Erol444/c77add48747b05c81f1bf30434cfbdfb).
 3. [Standalone mode](https://docs.luxonis.com/software/depthai/standalone.md) (best solution); in case of a dropped connection,
    host computer can just reconnect to the TCP ([example
    here](https://github.com/luxonis/oak-examples/tree/master/gen2-poe-tcp-streaming)) or an HTTP server ([example
    here](https://docs.luxonis.com/software/depthai/examples/script_http_server.md#script-http-server)) running on the camera,
    which takes <100ms. If there's any crash (either user's application, or firmware), the device will reboot automatically in <5
    seconds.

#### Device crash

Device could crash due to a bug in the firmware, or in the user's application.

> **Use the latest bootloader/DepthAI!**
> Make sure you are using the latest DepthAI version, and have the latest
> [Bootloader](https://docs.luxonis.com/software-v3/depthai/depthai-components/bootloader.md)
> flashed on the device, as there may have been some bug fixes in latest versions.

If there was a firmware bug or (user-defined) pipeline issue, a [Crash
report](https://docs.luxonis.com/software-v3/depthai/examples/crash_report.md) will get generated, which you should send over to
us (support@luxonis.com).

If no crash report was found when running the script linked above, it means that either it's a power issue, or connection dropped
(or, less likely, a hardware issue).

Alternatively, with depthai 2.28.0, automatic crash report generation is enabled by default. This will send the crash report to
Luxonis servers and will include:

 * crash dump itself
 * pipeline configuration
 * depthai version
 * host operating system version (MacOS/Linux/Windows)

It does not include any user data, images or NN models. It does however include the
[Script](https://docs.luxonis.com/software-v3/depthai/depthai-components/nodes/script.md) node code. If you wish to disable
automatic crash report generation, you can do so by setting the environment variable DEPTHAI_DISABLE_CRASHDUMP_COLLECTION to 1.

#### Power issue

If your PoE device does not work, or in some rare cases, it works for a period of time and then suddenly stops working, there
might be an issue with your PoE switch/injector. For example, when the power budget per port seems to be sufficient, but the
overall power budget for the switch is being exceeded due to demands from devices on other ports. It is worth checking the
specifications of your PoE switch / injector with respect to its overall power budget.

#### High CPU usage

If the CPU of the PoE device is constantly around 100% (on LeonOS core), there's a good chance that the device will drop the
connection sooner or later and throw an error like ping was missed, closing the device connection. You can read more about device
CPU usage (and how to debug it) [on this documention](https://docs.luxonis.com/software/depthai/debugging.md#cpu-usage).

#### Low speed / High latency

If you noticed low FPS or high message latency you should check the following:

 1. [PoE Link test](https://github.com/luxonis/oak-examples/tree/master/random-scripts#poe-test-script) - You should get speed
    1000 (Mbps). If it's 100, it likely means you are using Slow networking equipment (see below)
 2. [OAK bandwidth test](https://github.com/luxonis/oak-examples/tree/master/random-scripts#oak-bandwidth-test) - Should be around
    800/200 Mbps
 3. [OAK Latency test](https://github.com/luxonis/oak-examples/tree/master/random-scripts#oak-latency-test) - Should be below
    10ms, but it depends on your network

If you get lower bandwidth/latency as expected, you should check debugging steps below. We also have non-PoE-related [Low Latency
documentation](https://docs.luxonis.com/software/depthai/optimizing.md).

#### Slow networking equipment

If your PoE link speed is below 1000 Mbps, there may be a bottleneck in your setup:

 * Ethernet Cable: Use Cat5e or higher. While Cat5e supports 1 Gbps, Cat5 is limited to 100 Mbps. It might also be that your cable
   is damaged, and only 2 pairs are working (up to 100Mbps)
 * PoE Switch: Ensure it supports 1 Gbps speeds. Such switches are usually labeled 10/100/1000 Mbps.
 * Router: If applicable, ensure it can handle 1 Gbps. Some routers might have limited ports with this capability.
 * PoE Injector: Ensure it supports 1 Gbps. Some models are limited to 100 Mbps.
 * Cable Length: Keep your Ethernet cable under 100 meters (328 feet). For longer distances, use a PoE extender.
 * Host Network Card: Ensure it supports 1 Gbps. Older cards might be capped at 100 Mbps. If necessary, upgrade your network card.

Do not use WiFi to connect to the PoE device. WiFi is not designed for high bandwidth applications, and will likely result in a
bottleneck. If you must use it, use WiFi standards supporting at least 1 Gbps such as 802.11ac (WiFi 5), 802.11ax (WiFi 6), or
802.11ay (WiFi 6E). Note that bandwidth diminishes with distance and obstructions between the router and host.

#### NIC settings

Some default Network Interface Card (NIC) settings on Linux might not be ideal for communication with OAK POE cameras, which can
result in slow FPS, high latency, and/or high OAK CPU usage. You can use ethtool to configure these settings.

ethtool settings that provided better performance for us

In one case configuring sudo ethtool -C NAME rx-usecs 1022 (where NAME was enp59s0f1) improved FPS from 12 to 20.

#### Advance network settings

> **For advance users only! Luxonis does not provide support for these settings.**

For more advance users we have exposed some network settings that allows you to fine-tune the system for better performance. You
can configure them with the device config object.

For [RVC2](https://docs.luxonis.com/hardware/platform/rvc/rvc2.md) sysctl configuration, see [available settings
here](https://gist.github.com/Erol444/89a7fb1299d7a390b9f83c9028cfea81) (note that some settings are read-only, and some might
crash the system). Please refer to FreeBSD's documentation (12.0) for more information on sysctl settings.

```py
config = dai.Device.Config()
config.board.network.mtu = 9000 # Jumbo frames. Default 1500
config.board.network.xlinkTcpNoDelay = False # Default True
config.board.sysctl.append("net.inet.tcp.delayed_ack=1") # configure sysctl settings. 0 by default.

with dai.Device(config) as device:
    # Your code here
```

### Advanced (optional)

### PoE Extender

We have tested the power/communication [with this PoE
extender](https://www.amazon.com/Revotech-Extender-Repeater-Standard-POE5003/dp/B0BDF3NB3D/). It's a 1Gbps extender that accepts
PoE+ (30W) and has 2x PoE (15W) port outputs, so you can connect two OAK PoE cameras to it.

We were able to stream a video from both devices simultaneously.

### WiFi Bridge

One option to connect the PoE device to the network is to use a WiFi bridge. This is useful when you don't have a PoE switch or
injector, but you have a WiFi network available. The bridge will connect to the WiFi network and provide an ethernet port
(connectivity) to your OAK PoE device. We have tested the [Vonets WiFi
Bridge](https://www.amazon.com/Vonets-Wireless-Repeater-1200Mbps-Ethernet/dp/B0BNWR4H6Z) and it works as expected.

You first need to connect to the WiFi bridge (eg. via your phone) and configure it to connect to your WiFi network. Then you can
connect the OAK PoE device (via PoE injector) to the bridge.

This WiFi bridge accepts 5V for power, you can use either USB-A connector (we connected that to a powerbank), or a standard barrel
connector.

### Video streaming with OAK

Because an OAK PoE camera is connected to the network, it can stream video feed directly to a computer. Here are some video
streaming demos that use Script node (only supported on OAK PoE cameras) and can be run in [Standalone
mode](https://docs.luxonis.com/software/depthai/standalone.md):

 * [TCP streaming](https://github.com/luxonis/oak-examples/tree/master/gen2-poe-tcp-streaming) using Script node - either TCP
   server or client
 * [MJPEG streaming with HTTP
   server](https://docs.luxonis.com/software/depthai/examples/script_mjpeg_server.md#script-mjpeg-server) using Script node
 * [MQTT publishing](https://github.com/luxonis/oak-examples/tree/master/gen2-poe-mqtt) using Script node (both pub/sub should
   work)

Below are some demos which stream video feed from OAK camera via XLink to the host computer and then stream it from the host
computer to another computer/server, which can also work with any other OAK camera (even USB OAK devices):

 * [WebRTC streaming](https://github.com/luxonis/oak-examples/tree/master/gen2-webrtc-streaming)
 * [MJPEG streaming](https://github.com/luxonis/oak-examples/tree/master/gen2-mjpeg-streaming)
 * [RTSP streaming](https://github.com/luxonis/oak-examples/tree/master/gen2-rtsp-streaming)

### Factory reset

In case you can ping the device but can't connect to it in any way (search for the device, specifying IP), you should do a factory
reset as the device might have been soft-bricked. This usually happens with older bootloader versions. Note that only OAK-D-PoE
and OAK-1-PoE have a USB-C connector, while OAK-D Pro PoE has USB connectivity via M8 connector.

#### Series 1 PoE devices

Steps below apply for [OAK-D PoE](https://docs.luxonis.com/hardware/products/OAK-D%2520PoE.md) and [OAK-1
PoE](https://docs.luxonis.com/hardware/products/OAK-1%2520PoE.md) devices.

 1. Open the enclosure of your OAK PoE camera.
 2. Locate the USB-C connector and boot DIP switch.
 3. Change DIP switch to 0x16 (switches 2,4,5 are up, others are down, as on the image below) for USB communication.
 4. Connect OAK PoE camera via USB-C cable to the computer.
 5. Run the (first) [Factory Reset script](https://docs.luxonis.com/software/depthai/standalone.md#factory-reset).
 6. Change DIP switch back to 0x3 (switches 5,6 up, others down) for PoE communication.
 7. Close back the enclosure, making sure the gasket is in the correct place (for waterproofing).

#### Series 2 PoE devices (M8 connector)

Due to the S2 OAK PoE devices not having any physical buttons on the enclosure (as a result of their IP rating), the programming
board is used to re-flash the firmware on your PoE device. This is particularly useful when the static IP of the device is set to
a different subnet and cannot be accessed. You will need the [OAK Programming
board](https://shop.luxonis.com/products/copy-of-programming-board) to re-flash the firmware and restore the device to its default
settings.

Steps:

 1. Connect the programming board to the M8 connector of your OAK POE device. Make sure the orientation is correct (as shown in
    the image below) to avoid damage to the M8 connector. The programming board connector does not screw or lock on the POE
    connector, and thus care must be taken that the connector does not fall out of the socket while flashing.

 2. Press and hold the Recovery button on the programming board while simultaneously connecting it to a USB port on your host
    device (the device from which you will flash the firmware). The LED on the programming board should light up when connected
    properly. When the programming board is connected with the host computer you can release the Recovery button.

 3. Once connected, you should be able to discover your PoE device on your host computer.

 4. You can now proceed to re-flash the firmware the same way as for other OAK devices with the [Device
    Manager](https://docs.luxonis.com/software-v3/depthai/v3/depthai-components/bootloader.md#Danger%20Zone) tool, which provides
    a GUI for multiple operating systems. The tool includes a "Factory Reset" option that allows you to reset your Series 2 OAK
    PoE device to its default settings.

### Next Steps

After successfully deploying the device, you can use the following resources to learn more about the software ecosystem:

 1. [Software docs](https://docs.luxonis.com/software-v3.md)
 2. [DepthAI code examples](https://docs.luxonis.com/software-v3/depthai/examples.md)
