• OAK4
  • Setup simple flask server on the camera

I am trying to run a simple python script inside the OAK4D Pro camera.

from flask import Flask, request, jsonify

app = Flask(__name__)

# Global variable to store the value
shared_variable = None

@app.route('/update', methods=['POST'])
def update_variable():
    global shared_variable
    data = request.json
    if 'value' in data:
        shared_variable = data['value']
        return jsonify({"message": "Variable updated successfully", "new_value": shared_variable}), 200
    else:
        return jsonify({"error": "Missing 'value' in request"}), 400

@app.route('/get', methods=['GET'])
def get_variable():
    return jsonify({"shared_variable": shared_variable}), 200

def get_shared_variable():
    return shared_variable

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080)

I set the static IP and opened the port using:

iptables -A INPUT -p tcp --dport 8080 -j ACCEPT

When I adb shell into the camera and run the python program, the connection times out when I try to connect from an external machine. Is there something I am missing?

Thanks,

Jimit

    jimit
    Let me check that tomorrow, perhaps the port is already being used.

    jimit Works fine on my side. I did not set any iptables rules.

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello():
        return "Hello from OAK–4!"
    
    if __name__ == '__main__':
        # Listen on port 8080
        app.run(host='0.0.0.0', port=8080)

    Server logs:

    (.env) /data # python test_flask.py
     * Serving Flask app 'test_flask'
     * Debug mode: off
    WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
     * Running on all addresses (0.0.0.0)
     * Running on http://127.0.0.1:8080
     * Running on http://10.12.118.96:8080
    Press CTRL+C to quit
    ## after connecting:
    10.12.118.151 - - [07/Apr/2025 10:11:26] "GET / HTTP/1.1" 200 -

    My 4D is connected to my laptop using ADB (for shell use). It's also connected to the same LAN as my laptop via ETH (10.12.118.0/24) -> so the connection is established via ETH, not ADB. If you only use ADB, this won't work as we currently don't support ECM/RNDIS.

    Thanks,
    Jaka

      jakaskerl

      Hi Jaka,

      I was successfully able to run the flask server. I had to restart and try a few different IP addresses. For some reason, the company network was not able to assign an IP to the camera, so I put in an intermediate router and manually set the IP

      Thanks for your help!
      Jimit