This paper presents a mixed integer non-linear programming (MINLP) model to optimize, in a centralized fashion, the operation of multiple buildings in a microgrid. The proposed model aims to minimize the total cost of the energy imported from the main grid at the interconnection point, managing the power demand and generation of buildings, while operational constraints of the electrical grid are guaranteed. This approach considers the management of heating, ventilation and air conditioning (HVAC) units, lighting appliances, photovoltaic generation (PV) and energy storage system (ESS) of each building. Comfortable indoor conditions for the occupants are kept by a set of mathematical constraints. Additionally, a strategy that simplifies the original model is presented, based on a set of linearization techniques and equivalent representations, obtained through a pre-processing stage executed in EnergyPlus software. This strategy allows approximating the proposed model into a mixed integer linear programming (MILP) formulation that can be solved using commercial solvers. The proposed model was tested in a 13-bus microgrid for different deterministic cases of study with non-manageable loads and smart buildings. A large-size test case is also considered. Finally, a rolling horizon (RH) strategy is proposed with the aim of addressing the uncertainty of the data, as well as reducing the amount of forecasting data required.