What the button should do ¶
Cut all power ¶
The most definite way to stop a robot is to cut all power to it. Yanking the plug out of the wall works, as does a circuit breaker that's easy to flip by hand, or an https://en.wikipedia.org/wiki/Residual-c... whose test button is easy to press. This will cut all power regardless of any component failure aboard the Duet or robot. Of course it requires a physical action to reset.
Cut the 12/24V supply ¶
If your power supply supports the PS_ON pin of the Duet, then executing the G-code http://reprap.org/wiki/G-code#M112:_Emer... will cancel all executing G-code and shut down the 12/24V circuit. Even if you don't support PS_ON, it still commands all heaters and motors to shut down.
Note that this will make your "always-on" fans turn off even though your hot end may be hot, and it also leaves the head wherever it was when you stopped the machine. The former might produce a hot end jam, and the latter might heat the bed. Also, if your axes are smooth enough, turning off the stepper motors entirely may allow the machine to fall. On the other hand, one of the ways for the MOSFETs controlling the heaters to fail is for them to fail shorted, so that the heaters are on 100% regardless of what the MCU commands; shutting off the 12/24V supply will shut them off in spite of this.
Just stop doing what you're doing ¶
If the machine's misbehaviour is mild, you may want to stop what it's doing but not shut everything down. You can write your own G-code macro for what should happen in this case, for example, turn off all heaters and home the head. At the moment, there is no way to make this interrupt actions in progress (including "wait for heater to reach temperature").
Making a button do what you want ¶
The Duet firmware allows you to associate a state change on any endstop with a "trigger", a user-definable action, with the http://reprap.org/wiki/G-code#M581:_Conf... G-code. Trigger 0 does an emergency stop (like M112), Trigger 1 pauses the print, and any higher number n executes the file triggern.g. So you can arrange for a physical button to execute any command by connecting it to the pins of an unused endstop. Note that in addition to the five endstop connectors on the board (X, Y, Z, E0, E1) there are five more pins on the extension connector that can be used in this way.
Note that triggers only fire on a transition off -> on; you can use http://reprap.org/wiki/G-code#M582:_Chec... to make the trigger fire if the button is already pressed. You can use http://reprap.org/wiki/G-code#M574:_Set_... to select whether the button is normally open or closed.
Image from https://www.flickr.com/photos/dumbledad/...