The advent of ontology systems has brought inference capabilities to database systems. Also, the number of interconnected objects is constantly growing, thus forming a new paradigm for the Internet of the future, where not only will any single device be accessible and usable from anywhere and at anytime, but also, the system will be able to self--organize and adapt to external agents. This paper focuses on the description and the realization of an ontology system for wireless sensors and actuators networks dealing with heterogeneous device integration and composite event detection. The system has been developed with widely accepted tools such as protege and Pellet and has been implemented on the server of a wireless sensor network testbed that features 350 devices and is fully IPv6 compliant. The main features of the proposed system are the complete interoperability thanks to the support of advanced web languages on constrained devices, the capability of classifying any node of the network according to its sensors and its geographic position, and a general method for detecting events and anomalies among the data collected by the network. Finally, the paper provides evidence of the viability of the approach by describing its implementation and showing the results of a first experimental campaign.