Energystorage may improve power management in microgrids that include renewable energy sources. The storage devices match energy generation to consumption, facilitating a smooth and robust energy balance within the microgrid. This paper addresses the optimal control of the microgrid's energystorage devices. Stored energy is controlled to balance power generation of renewable sources to optimize overall power consumption at the microgrid point of common coupling. Recent works emphasize constraints imposed by the storage device itself, such as limited capacity and internal losses. However, these works assume flat, highly simplified network models, which overlook the physical connectivity. This work proposes an optimal power flow solution that considers the entire system: the storage device limits, voltages limits, currents limits, and power limits. The power network may be arbitrarily complex, and the proposed solver obtains a globally optimal solution.