In this paper, the optimal control theory is applied to a temperature optimization problem by coupling finite element and finite volume codes. The optimality system is split into the state and adjoint system. The direct problem is solved by the widely adopted finite volume OpenFOAM code and the adjoint-control equation using a variational formulation of the problem with the in-house finite element FEMuS code. The variational formulation of the problem is the natural framework for accurately capturing the control correction while OpenFOAM guarantees the accuracy of the state solution. This coupling is facilitated through the open-source MED and MEDCoupling libraries of the SALOME platform. The code coupling is implemented with the MED libraries and additional routines added in the FEMuS and OpenFOAM codes. We demonstrate the accuracy, robustness, and performance of the proposed approach with examples targeting different objectives using distributed and boundary controls in each case.