OK so here is my solution.
I made a wrapper class called SafeSerial. In SafeSerial.h:
class SafeSerial : public Serial_
size_t write(const uint8_t* buffer, size_t size) override;
* SafeSerial implementation for Arduino Leonardo.
* Overrides the Serial_::write() method and ...
Theoretically, nothing should happen to serial data if nothing is listening.
For a traditional UART the data is just sent regardless. It doesn't need someone listening for that to happen. Like a light doesn't need someone to see it for it to be on.
For USB CDC/ACM the data should be discarded even before it's attempted to be sent.
The latter, though, does ...
If you already have a 5V source then you can use a MOSFET as a switch to turn on/off the power to the tablet like this:
But if you want to turn on/off a USB charger that goes to an AC outlet, then you will need to control a relay to turn on/off the AC supply to the USB charger. You can use a relay module like this: https://www.amazon.com/Tolako-Arduino-...