The Python code to control the switch and the switch design

The switch design

The switch design is very simple. We take 3.3v from the Pi and connect it to one pole of a simple make/break switch. The other pole is connected to a 1.2Kohm resistor, which is connected to the base of 2N2222 transistor. We tie the transistors emitter to ground, and the collector is tied to our GPIO input pin as well as a 1.2K Ohm resistor. The other end of this resistor is connected to 3.3V from the Pi GPIO pins. When there is no current in the base of the transistor, the collector rises to 3.3V as no current is flowing, so it goes HIGH. When the switch is closed, current flows into the base, the transistor goes into saturation and current flows through the collector, resulting in a LOW voltage state due to the voltage drop across the resistor, making the GPIO input pin read LOW.

There are other, maybe simpler, ways to tie a switch to the Pi and measure the position. You could configure the GPIO pin with an internal pull up resistors for example. Any design that does not damage the GPIO pins is fine, you just need to be able to have the switch make the input read LOW in one position, and HIGH in the other for this design.

The Python code polling the switch

This Python script is run at startup in the background on the slave Pi. It is always looking for a change in the switch position, and if it see a change, it sends data to the Master Pi web server, that emulates a button being selected in a browser window. This way we can use the same basic code path for all remote triggers - anything not triggering directly on the master Pi, comes to the Master Pi as a web page call.

This Python script, is run at start up in the background on the Master Pi to watch the switch on that machine. Since it is on the master, it fires the relays locally and then makes the socket connection to the slave Pi.