Wireless sensor network (WSN) infrastructure and protocols change according to the application needs. To achieve the best performance of the WSN, its operation should be adapted to the application needs. We propose a middleware for WSNs that provides a layer between applications and the network. The middleware offers a standard mechanism for representing user queries, sensor tasks and data. It also provides an automatic choice of the best network configuration and data dissemination strategy. Users are able to access the WSN without worrying about the underlying infrastructure and software. From the WSN perspective, the system provides the best match between communication protocols and application requirements.