Modeling and optimization of the IEEE 802.15.4 protocol for reliable and timely communications