Enhancing Klipper with Temperature Sensor Redundancy: A Guide

Adding Temperature Sensor Redundancy to Klipper

The Mosquito® Magnum+ hotend boasts the ability to use multiple temperature sensors for redundancy, which can also serve as a safety feature. In Klipper, the method to add in this feature requires a workaround that is simple to implement. 
Do not attempt to modify your firmware without consulting the Klipper documentation. Improperly modified firmware can cause a fire!

What You Need

  1. SD Card or SSH access to a Klipper installation
  2. A laptop or PC with a text editor or an IDE
  3. Known values for sensor type and the pin it is configured to

Procedure

  1. Locate and open the "printer.cfg" file found in your instance of Klipper. This file can be accessed via SSH or through the SD card. Using SSH on a Raspberry Pi, "printer.cfg" can be found in the "/home/pi/" directory. Further, the file can be edited using an IDE or even a text editor program.
  2. In this file, locate a point in the file to insert code into. For the simplest approach, you may navigate to the end of the file and insert code there.
  3. Copy and paste the following code into the "printer.cfg" file at the desired location in the file.
  1. #Adds auxillary Temperature Sensor, "_hotendSensor2" and "TS1" can be changed as needed
  2. [temperature_sensor _hotendSensor2] # "_hotendSensor2" can be renamed as per user's preference
  3. sensor_type: PT1000 #set this to the temperature sensor being used
  4. sensor_pin: P0.23 #set this to the sensor pin that this sensor is being connected to
  5. min_temp: 0
  6. max_temp: 500 #set this to the maximum temperature expected for your hotend
  7. gcode_id: TS1 #this value assigns this temperature_sensor module a unique ID that is used by Klipper

  8. #Renames existing M105 command to M99105, which can be changed by user
  9. #Checks for difference of 15C between the 2 sensors and shuts off the printer
  10. #if this condition is exceeded
  11. [gcode_macro M105]
  12. rename_existing: M99105 #M105's behavior will be remapped to M99105
  13. gcode:
  14. M99105
  15. {% set s1 = printer.extruder.temperature|round(1, 'floor') %}
  16. {% set s2 = printer["temperature_sensor _hotendSensor2"].temperature|float|round(1, 'floor') %}
  17. {% if (s1 - s2)|abs > 15.0 and printer.extruder.target != 0.0 %}
  18. M118 Hotend temperature fluctuation between sensors exceeded 15C. Shutting down.
  19. M118 Sensor 1:{s1} Sensor 2:{s2}
  20. M112
  21. {% endif %}

  22. #Allows for the use of the M118 GCode command
  23. [respond] 
      For reference, this code creates a new temperature_sensor object named "_hotendSensor2," as an example. This name can be changed, as long as every instance of it is renamed to the name of your choosing. The parameters from lines 3 through 6 are the values assigned to the newly created temperature_sensor object. Refer to the datasheets for your chosen sensor to ensure correct configuration. The "gcode_id" is another variable that you can set to your preference, making sure that changes made to this variable name are made throughout the rest of the code snippet above.

      The "gcode_macro" section serves to add functionality to the M105 command. This is done by simply renaming the existing M105 command to M99105 (also set to user preference). This newly created function runs M99105, to keep the unmodified M105 functionality intact, and then runs a check ensuring that the temperature values between the two temperature sensors are within 15 degrees Celcius of one another. You may alter that fluctuation margin of error by changing the "15.0" found in line 18. If any fluctuations outside of the desired range are detected, the printer will halt and shut down.

      Finally, "[respond]" needs to be added in, as shown above. This allows for the use of M118, which is important in sending the status message into the serial monitor/ terminal, as soon as a fluctuation is detected. Without the inclusion of "[respond]," you may still get core functionality from this code, as long as lines 19 and 20, which contain the M118 commands, are removed.

      4. Upon adding this code, save the file and restart the instance of Klipper.
      5. Ensure that the changes made are working as expected by observing that both hot end temperature values are reported upon every instance of M105. Below is an example of this.

Testing

This functionality within the code was tested using the following procedures:
Testing for Failsafe:
  1. Unplugging either of the temperature sensors
  2. Removing either sensor from the hotblock, artificially creating the temperature difference
Testing for Validation:
  1. Heating the hotblock to 500C and letting it stabilize for 10 minutes
  2. Heating hotblock to 400C with one heater cartidge
Failsafe testing resulted in the printer shutting down, leaving an error message in the terminal/serial monitor. Validation testing served to ensure the code would not falsely trigger the failsafe.