Switching device connections to MCU based on power source
add another microcontroller, interface with spi since u need usart for RF module...u can also use "decay counter 4017" which has 10 input and only 2 pin for output