3D Guardian

Youtube demo: https://www.youtube.com/watch?v=tD0p_E7eSwg
=Introduction=
3D Guardian is my Arduino based DIY gadget which can be used to monitor the environment and perform some actions (e.g. shut down your 3D printer) if the environment moves beyond a critical range. It is mainly designed to serve as a security device for 3D printers (will shut it down in case of a trouble - elevated smoke, CO levels, high temperature etc.), but also serves as a thermostat and fan control if your printer is inside a cabinet with an exhaust pipe with a fan.
The gadget has the following features:
* It is very cheap to build (probably less than 30$ if sourcing the parts from ebay).
* It is open source (anyone can, and in fact should, modify the original Arduino software).
* It is convenient to use (LCD shield 1602, 2 lines x 16 characters, with 5 buttons).
* It uses three LEDs of different colors to tell you instantly the current state (Training, Guarding, Programming, Warning or Alarm).
* It uses a loud piezo buzzer (from a broken CO detector), for audible warning and alarm signals.
* In case of an alarm, it sends a signal to a separate (power) module, which has an AC plug, two AC outlets (e.g. if your printer uses two separate PSUs), and a Solid State Relay (SSR; 380V/10A), which shuts down the printer. The plug goes to your house AC grid (or to your UPS), the printer's PSUs are connected to the AC outlets, and the 3D Guardian is connected to the power module via a low voltage / current cable. (I am using an old curled telephone receiver cable.)
* It stores all the important data (input parameters and sensors data) in the non-volatile (EEPROM) memory. It has an auto-save option (can be used to restore the state from the last run), and two custom memory registers for all the training data.
* It is very flexible:
** It can use a different number of sensors (up to seven with Arduino Nano), easily configurable in config.h. The default version is configured for 7 sensors (four thermistors, CO sensor, smoke sensor, and a bed resistance sensor). I placed one thermistor at the top of the printer frame (with the smoke detector), second one at the top of the PC mini-tower containing all the electronics for the printer, the third one glued on top of the 16V/25A PSU I am using to heat the bed, the fourth one glued on top of the AC SSR which turns the printer on/off. The CO detector is placed inside the printer enclosure, at the bottom.
** It has a flexible menu system - any number of levels, and one can easily insert new menu items or delete old ones anywhere.
* It has a Training mode (initial mode when using for the first time): just run your printer as usual for at least one long (> 1 hour) printing job, and let the gadget record the ranges of the data from all the sensors. Then the gadget can be switched to Guarding mode, where it will use the training data to figure out when to issue a warning or trigger the alarm (shutting down the printer). Meaning - there is no need for a guesswork about "how much is too much?" with any sensor readings.
* If your enclosure has an exhaust fan, the device can be used to control the fan to maintain a target (configurable from the menu) enclosure temperature. Also, there is an easy access menu command, "Case clearing", which will run the fan at full speed at the end of your printing job (for a configurable number of seconds), to remove all the fumes before you open the cabinet. For PLA printing, you can choose the Manual fan option, set to a less then full fan power, for continuous fumes clearing during the print.
* 3D Guardian can control a stepper motor, to open/close the exhaust outlet in the printer's enclosure. The outlet is all made of metal (fireproof), and goes to the default "closed lid" state if power disappears. It can also operate a second stepper motor, for the air inlet lid.
* When in Guarding mode, the gadget will issue a warning (visible - via LCD and LED, and audible - short chirps from its piezo) when you start approaching the alarm territory. If this is because the environment changed since the time you trained the device (e.g., it is summer now, and you trained it in the winter, in a cold room), there is a simple menu command to instantly "retrain" the gadget based on the current sensor data (no need to switch to Training mode). This can also be a sign of a failing sensor (check the detailed sensor stats to see what's going on), or perhaps this is a pre-fire situation, so take immediate corrective actions.
* I added WiFi microcontroller ESP8266 (devkit v0.9 with micro-USB connector; 3-4$ on ebay) to the gadget. It communicates with the Arduino Nano via hardware serial connection. This allowed me to add WiFi connectivity to 3D Guardian, and make it a part of my home automation system (using MQTT and OpenHAB servers). Now I can send a shutdown or "clearing case" command to the printer remotely from my cellphone, using Internet. I can also monitor remotely output from all sensors, and get Alarm and Warning signals instantly.
* The device now has the ability to measure resistance of the heated bed "on the fly" (when the bed is in use), by means of a current sensor (a Hall effect based module) and a voltage sensor (Arduino ADC + P-channel MOSFET + a 2-resistor voltage divider). See #Resistance sensor below. This approach should detect early signs of a problem with the bed wiring (arcing, bad contacts, broken wire), and will shut down the printer before fire starts.
* I added Panic button (instantly shuts down the printer) to the controller.
Couple of snapshots of the user interface:
 
= Hardware =
The printable parts for this project can be found on thingiverse:
https://www.thingiverse.com/thing:2432366
== Controller and power units ==
*Main components (controller unit):
** Arduino Nano (2.70$; http://www.ebay.ca/itm/152329968912)
** ESP8266 devkit v0.9 (3.30$; http://www.ebay.ca/itm/272526162060)
** 1602 LCD Board Keypad Shield (2.60$; http://www.ebay.ca/itm/1602-LCD-with-Keypad-Shield-Board-Blue-Backlight-for-Arduino-Duemilanove-Robot-/201556596016)
** DC-DC buck converter (0.75$; http://www.ebay.ca/itm/291934579477)
** Piezo buzzer: I used one from an old smoke or CO alarm. It has three connectors, and requires a transistor + 3 resistors circuit to drive it (at the resonance frequency).
* Minor parts (controller unit)
** Two transistors - almost any silicone NPN transistor will do (they handle currents <100 mA, and voltages <25V). I used the ones I happened to have.
** Panic button: a button style micro switch.
** Dual switch "Prog": used to disable the serial connection between Arduino and ESP8266, when either of the two is being re-programmed. Normally should be in closed position (because nothing works in the Off mode). As a reminder, the yellow LED flashes and the LCD screen display "Programming" when the switch is in the off position.
** Diode and LEDs - basic ones.
* Power unit:
** AC solid state relay, 380V/10A (3.50$; http://www.ebay.ca/itm/141916727762)
** 50k thermistor
Power unit:
== Printer unit ==
 
This unit resides right next to the printer (enclosure). It is connected to the controller unit via 6m RS-232 (25-wire) serial cable, and also connected to two stepper motors (one opens the exhaust lid, the other one opens the air inlet), and exhaust fan (12V; 2-3 wire types). A few wires go inside the printer's enclosure: +5V/GND (from the controller unit), wires to CO, smoke sensors and the enclosure thermistor ("Th1"; 50k). Two 12 awg wires go to the heated bed. Many more wires (not shown) go from the motherboard (placed inside the printer unit) inside the printer enclosure (hot end, limiting switches, 5 stepper motors, two thermistors - hot end and heated bed, two fans). The printer's display/buttons module is also placed in the printer unit. The unit has two PSUs - Mean Well LRS-350-15 set to 16V (for heated bed only), and ATX PSU 12V/25A. The 12V from the ATX PSU is sent inside the printer enclosure to power the two 5W/12V COB LED strips.
Components:
* Stepper motor driver A4988 (1.05$; http://www.ebay.ca/itm/182216985456)
* Current sensor ACS712-20A (1.80$; http://www.ebay.ca/itm/181847181426). It doesn't seem to be well designed for high amperage circuits; I will use a more expensive (5$) unit with larger screw terminals as a replacement (http://www.ebay.ca/itm/112084032519).
* MOSFET 25A board (technically not part of 3D Guardian, but a must for 3D printers): 5.40$, http://www.ebay.ca/itm/182402244152
* MOSFET N-channel transistor IRFZ44N (used to provide PWM control to a 2/3 wire fan; doesn't need a heat sink for up to 5A fan): 0.16$, http://www.ebay.ca/itm/291548962786
* Low power MOSFET P-channel transistor BS250 (used as a switch, to enable Arduino measuring the bed voltage).
* Minor components: 5.1V Zener diode, 100uF x 25V capacitor, two 50k thermistors, and a few resistors.
External components:
* Exhaust fan: 12V, 1...5A. For now I'm using an old CPU fan DELL DB9733-12HBTL (1.35A), which is pretty good in terms of air flow (28 CFM), but not very good in terms of static pressure (~300 Pa). One can find a cheap (~10$) 12V car vacuum (http://www.ebay.ca/itm/131785623948), which is rated at 5A and much higher static pressure of ~2000 Pa - I will try it as a replacement. (I suspect static pressure is much more important in my design than air flow.)
* Two identical (or almost identical) stepper motors connected in series: any 4-wire bipolar motor with the coil resistance <15 Ohm rated for at least 0.7A per coil will do. The two motors should have very similar coil resistance and the same angle per step (say, 1.8 degrees).
* Smoke detector: MQ-2 board from ebay
* CO detector: MQ-7 board from ebay
= The printer =
The printer itself (Anet A8 in my case) only contains the following electronic parts: 
* X/Y/Z/E stepper motors;
* X/Y/Z limiting switches; 
* Heating elements (heated bed and hot end);
* Two thermistors (for the bed and hot end);
* Two fans (extruder and filament cooling);
* 3D Guardian sensors: CO, smoke, and ambient temperature thermistor.
All the rest (motherboard, display, PSUs, MOSFET boards etc.) were moved off printer.
= Fireproof enclosure =
 
Built of the stuff which doesn't easily burn - drywall, angle metal, and borosilicate glass. It is air tight, and has a built-in active exhaust system - exhaust outlet with a lid operated by a stepper motor; air inlet with a lid operated by a stepper motor; 2 m long flexible aluminum exhaust pipe; 12V / 1.35A exhaust fan at the far end of the exhaust pipe. It also has 2x5W LED stripes.
= Putting it together =
If you use UPS and a camera for your 3D printer, the best way to connect everything together is as shown on this graph:
Here red lines correspond to AC voltage wiring. With the above setup,
* Everything (printer, 3D Guardian, and the camera) are protected by the UPS
* Initially everything is off. A single button press (on the UPS) will power up everything - 3D Guardian, camera, and the printer. A single button press will power everything down
* In case of an alarm, the printer will be shut down, but 3D Guardian and the camera will continue to operate, and be protected by the UPS.
= The code =
Here is the source code - it is currently a beta so likely has some bugs. Use it at your own risk!
* Beta release v0.2
It consists of two parts:
* The code for the microcontroller ESP8266 (devkit v0.9 with micro-USB connector), in ThreeD_Guardian_ESP folder. You compile it with Arduino IDE, where you should add support for the ESP board as follows:
** Add the following 3rd party board manager under "File -> Preferences -> Additional Boards Manager URLs": http://arduino.esp8266.com/stable/package_esp8266com_index.json
** Open the "Tools -> Board -> Board Manager" and click install for the ESP8266"
** Select "NodeMCU 0.9" in "Tools -> Board"
* The code for Arduino Nano - in ThreeD_Guardian folder. Choose "Arduino Nano" in "Tools -> Board" o Arduino IDE.
= User guide =
3D Guardian User Guide
= Resistance sensor =
Resistance sensor