Code runs different on USB vs Battery
The USB provides the one thing that is missing from your diagram there: the common ground.

The GND pins of the two boards must be connected together. By using USB the USB becomes that connection. As soon as you use batteries there is no connection, so no communication happens.