# Auto Reconnect

Demonstrates setting a reconnection callback and limited retry attempts on the device. The example opens a simple camera preview;
unplug the device briefly to see reconnection attempts reported via the callback.

This example requires the DepthAI v3 API, see [installation instructions](https://docs.luxonis.com/software-v3/depthai.md).

## Pipeline

## Source code

#### Python

```python
#!/usr/bin/env python3

import cv2
import depthai as dai

def callback(x: dai.Device.ReconnectionStatus):
    print(f"Reconnecting state {x}")

# Create pipeline
device = dai.Device()
device.setMaxReconnectionAttempts(3, callback)

# You can try unplugging the camera to see the reconnection in action
with dai.Pipeline(device) as pipeline:
    # Define source and output
    cam = pipeline.create(dai.node.Camera).build()
    videoQueue = cam.requestOutput((640,400)).createOutputQueue()

    # Connect to device and start pipeline
    pipeline.start()
    while pipeline.isRunning():
        videoIn = videoQueue.get()
        assert isinstance(videoIn, dai.ImgFrame)
        cv2.imshow("video", videoIn.getCvFrame())

        if cv2.waitKey(1) == ord("q"):
            break
```

#### C++

```cpp
#include <atomic>
#include <csignal>
#include <iostream>
#include <memory>
#include <opencv2/opencv.hpp>

#include "depthai/depthai.hpp"

// Global flag for graceful shutdown
std::atomic<bool> quitEvent(false);

// Signal handler
void signalHandler(int signum) {
    quitEvent = true;
}

// Reconnection callback function
void reconnectionCallback(dai::Device::ReconnectionStatus status) {
    std::cout << "Reconnecting state " << static_cast<int>(status) << std::endl;
}

int main() {
    // Set up signal handlers
    signal(SIGTERM, signalHandler);
    signal(SIGINT, signalHandler);

    try {
        // Create device with reconnection settings
        auto device = std::make_shared<dai::Device>();
        device->setMaxReconnectionAttempts(3, reconnectionCallback);

        // Create pipeline
        dai::Pipeline pipeline(device);

        // Define source and output
        auto cam = pipeline.create<dai::node::Camera>()->build();
        auto output = cam->requestOutput(std::make_pair(640, 400));
        auto videoQueue = output->createOutputQueue();

        // Start pipeline
        pipeline.start();
        std::cout << "Pipeline started. Try unplugging the camera to see reconnection in action." << std::endl;
        std::cout << "Press 'q' to quit." << std::endl;

        while(pipeline.isRunning() && !quitEvent) {
            auto videoIn = videoQueue->get<dai::ImgFrame>();
            if(videoIn == nullptr) continue;

            cv::imshow("video", videoIn->getCvFrame());

            int key = cv::waitKey(1);
            if(key == 'q') {
                break;
            }
        }

        // Cleanup
        pipeline.stop();
        pipeline.wait();

    } catch(const std::exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}
```

### Need assistance?

Head over to [Discussion Forum](https://discuss.luxonis.com/) for technical support or any other questions you might have.
