Solar Membrane Distillation (SMD) is an under-investigation desalination process suitable for developing self-sufficient small scale applications. The use of solar energy considerably reduces the operating costs, however, its intermittent nature requires a non-stationary optimal operation that can be achieved by means of advanced control strategies. In this paper, a hierarchical control system composed by two layers is used for optimizing the operation of a SMD pilot plant, in terms of thermal efficiency, distillate production and cost savings. The upper layer is formed by a Nonlinear Model Predictive Control (NMPC) scheme, that allows us to obtain the optimal operation by optimizing the solar energy use. The lower layer includes a direct control system, in charge of attaining the variable references provided by the upper layer. Simulation and experimental tests are included and commented in order to demonstrate the benefits of the developed control system.