Segmented organisms have serially repeated structures  that become specialized in some segments . We show here that the Drosophila corpora allata, prothoracic glands, and trachea have a homologous origin and can convert into each other. The tracheal epithelial tubes develop from ten trunk placodes [3, 4], and homologous ectodermal cells in the maxilla and labium form the corpora allata and the prothoracic glands. The early endocrine and trachea gene networks are similar, with STAT and Hox genes inducing their activation. The initial invagination of the trachea and the endocrine primordia is identical, but activation of Snail in the glands induces an epithelial-mesenchymal transition (EMT), after which the corpora allata and prothoracic gland primordia coalesce and migrate dorsally, joining the corpora cardiaca to form the ring gland. We propose that the arthropod ectodermal endocrine glands and respiratory organs arose through an extreme process of divergent evolution from a metameric repeated structure.