Pn2222 Transistor as Switch for my coffe machine ESP8266

A transistor can be used as a switch in certain applications but it's NOT the same as a mechanical switch.

However a regular electro-mechanical RELAY IS and electrically-operated and electrically-isolated switch. A relay is almost always better if you want to replace a switch in an unknown circuit.

You still need a driver circuit because the Arduino can't directly supply enough current for a relay coil. You can probably use the 2N2222 for that. Or you can buy a "relay board" which has a relay and river built-in.