Adding a virtual Frame temperature sensor

Hi @all,

I´m working on a Python script that reads out four DS18B20 sensors, which are mounted in the four vertikal aluminium extrusions on the corners to get a arithmetic mean for the frame temperature.

I´ve added a additional MCU temperature sensor to klipper like this:

[temperature_sensor frame]
sensor_type: temperature_host
sensor_path: /home/pi/frame

If i change the value in the “frame” file, klipper shows me the right temperature. But if I run the script, klipper don´t refresh the value.

Here is my script:

#!/usr/bin/env python
#coding=utf-8
import os, time, sys

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

file_path = 'frame'
sys.stdout = open(file_path, "w")

sensorcount = 4
sensors = ['28-0621b2312f10', '28-0120426e687f', '28-0621b245c0cf', '28-0120428c8a85'];
temp_sum = 0;
sensorpath = '/sys/bus/w1/devices/'
sensorfile = '/w1_slave'

def callsensor(sensor):
  f = open(sensorpath + sensor + sensorfile, 'r')
  lines = f.readlines()
  f.close()
  if lines[0].strip() [-3:] != 'YES':
    sys.exit('Fehler bei Sensor ' + sensor)

  temp_line = lines[1].find('t=')				
  if temp_line != -1:						
    temp_output = lines[1].strip() [temp_line+2:]
    t= in temp_output schreiben
    return temp_output

for i in range(0, sensorcount):
  s = sensors[i]    						
  temp_sum = temp_sum + int(callsensor(s))

temp_arr = int(temp_sum/sensorcount)
print(temp_arr)
sys.exit()

It´s working well, but klipper doesn´t update the temp value.
I think it´s caused by the truncation before the write of the file from python.

I know that i can implement the four sensors in klipper directly, but not the arithmetic mean. I just want one value for the frame, so i can wait for the frame to get on temperature before the print starts.

Can someone help me with this problem?

I´ve found a working solution. The script now only updates the file if the value changes by 0.1 degrees.

#!/usr/bin/env python3
#coding=utf-8
import os, time, sys						#Bibliotheken laden

os.system('modprobe w1-gpio')					#Kernelmodule laden
os.system('modprobe w1-therm')

file_path = 'frame'						#Ausgabedatei festlegen
#sys.stdout = open(file_path, "w")				#Standardausgabe in Datei umleiten

sensorcount = 4							#Festlegen, wieviele Sensoren vorhanden sind
sensors = ['28-0621b2312f10', '28-0120426e687f', '28-0621b245c0cf', '28-0120428c8a85'];	#Array mit den Sensor-IDs
temp_sum = 0;
temp_arr_old = 0;
sensorpath = '/sys/bus/w1/devices/'				#Pfad zum Sensorverzeichnis
sensorfile = '/w1_slave'					#Geraetedatei

def callsensor(sensor):
  f = open(sensorpath + sensor + sensorfile, 'r')		#Pfad, Sensor-ID und Geraetedatei zusammensetzen, Datei im Lesemodus oeffnen
  lines = f.readlines()						#Inhalt der Datei in lines schreiben
  f.close()							#Datei schliessen
  if lines[0].strip() [-3:] != 'YES':				#Index 0 (Zeile 1) strippen und pruefen, ob die letzten 3 Buchstaben YES sind
    sys.exit('Fehler bei Sensor ' + sensor)			#Wenn nein: Abbruch mit Sensornummer als Argument
  temp_line = lines[1].find('t=')				#Den Index (Position) von t= in temp_line schreiben
  if temp_line != -1:						#Wenn nicht -1 (=String nicht gefunden)
    temp_output = lines[1].strip() [temp_line+2:]		#Index 1 (Zeile 2) strippen und die Zeichen nach t= in temp_output schreiben
    return temp_output						#Funktion beenden und temp_celsius ausgeben

while True:
  temp_sum = 0
  for i in range(0, sensorcount):					#for-Schleife von 0 bis 3
    s = sensors[i]    						#Sensor-ID 0-3 auf s schreiben
    temp_sum = temp_sum + int(callsensor(s))

  temp_arr = int(temp_sum/sensorcount)
  if int(temp_arr/100) != int(temp_arr_old/100):
   with open(file_path, "w+") as external_file:
      temp_arr_old = temp_arr
      print(str(temp_arr), file=external_file, end='')
      external_file.close()
sys.exit()							#Programm beenden

Next step is to set the script as a service, so it starts on bootup

Out of curiosity how big are the temperature differences between the four extrusions?

It’s up to 10 degree. Depending on the airflow of the chamber heater.

Now the script is working very well. I´ve added as a service, so it starts automaticly when the pi boots up.

But after a while, klipper doesn´t refresh the temperature. After a reboot of klipper it´s working again.

@koconnor Is there a failsafe mode in klipper?

I have added a temperature_combined sensor class you can use

1 Like

@emjay276 Thank you very much for your work. I´ll try it immediately

1 Like

I noticed the ‘temperature_combined’ feature popup this weekend and tried it out on my Voron 0
This turns the processor fan in the back of the machine when either of the processors get hot.
Works well thanks.

#####################################################################
#      Temperature Sensors
#####################################################################
[temperature_sensor SKR_Pico]
sensor_type: temperature_mcu

[temperature_sensor Raspberry_Pi]
sensor_type: temperature_host

#####################################################################
# Processor Fan Control
#####################################################################
[temperature_fan processors_maximum]
pin: gpio20                                                       # the fan control pin (must be a mosfet or a pwm fan)
control: watermark                                                # the type of hysteresis we are using
max_delta: 3.0                                                    # fan turns on/off at 3° over/under target_temp
sensor_type: temperature_combined                                 # new temperature type
sensor_list: temperature_sensor SKR_Pico, temperature_sensor Raspberry_Pi
combination_method: max                                           # use the maximum reading of the sensors
maximum_deviation: 999.9                                          # we don't care about the difference in temperature
max_temp: 85                                                      # just the valid temperature ranges
min_temp: 0
target_temp: 57.0                                                 # the initial target temperature for the fan
max_speed: 1.0
min_speed: 0.3
2 Likes