The Internet of Things (IoT) is a universal network to supervise the physical world through sensors installed on different devices. The network can improve many areas, including healthcare because IoT technology has the potential to reduce pressure caused by aging and chronic diseases on healthcare systems. For this reason, researchers attempt to solve the challenges of this technology in healthcare. In this paper, a fuzzy logic-based secure hierarchical routing scheme using the firefly algorithm (FSRF) is presented for IoT-based healthcare systems. FSRF comprises three main frameworks: fuzzy trust framework, firefly algorithm-based clustering framework, and inter-cluster routing framework. A fuzzy logic-based trust framework is responsible for evaluating the trust of IoT devices on the network. This framework identifies and prevents routing attacks like black hole, flooding, wormhole, sinkhole, and selective forwarding. Moreover, FSRF supports a clustering framework based on the firefly algorithm. It presents a fitness function that evaluates the chance of IoT devices to be cluster head nodes. The design of this function is based on trust level, residual energy, hop count, communication radius, and centrality. Also, FSRF involves an on-demand routing framework to decide on reliable and energy-efficient paths that can send the data to the destination faster. Finally, FSRF is compared to the energy-efficient multi-level secure routing protocol (EEMSR) and the enhanced balanced energy-efficient network-integrated super heterogeneous (E-BEENISH) routing method based on network lifetime, energy stored in IoT devices, and packet delivery rate (PDR). These results prove that FSRF improves network longevity by 10.34% and 56.35% and the energy stored in the nodes by 10.79% and 28.51% compared to EEMSR and E-BEENISH, respectively. However, FSRF is weaker than EEMSR in terms of security. Furthermore, PDR in this method has dropped slightly (almost 1.4%) compared to that in EEMSR.