Self-Optimized Clustering and Routing in the Internet of Things