|
|
@@ -8,20 +8,40 @@ import uasyncio
|
|
|
from machine import Pin,PWM,ADC
|
|
|
from web_server import WebSrv
|
|
|
from MicroWebSrv.upload_dat import upload
|
|
|
+from my_timer import MyTimer as Timer
|
|
|
+
|
|
|
+def average(series, new_value):
|
|
|
+ series.append(new_value)
|
|
|
+ if len(series) > 8:
|
|
|
+ series.pop(0)
|
|
|
+ total = sum(series)
|
|
|
+ # print(series)
|
|
|
+ return total/len(series)
|
|
|
+
|
|
|
|
|
|
class Ntc():
|
|
|
def __init__(self, pin) -> None:
|
|
|
- self.ntc = ADC(Pin(pin), atten=ADC.ATTN_11DB)
|
|
|
+ self.adc = ADC(Pin(pin), atten=ADC.ATTN_11DB)
|
|
|
self.VCC = 3344
|
|
|
- def get_temperature(self):
|
|
|
- val_uv = self.ntc.read_uv()/1000
|
|
|
+ self.temp = 0
|
|
|
+ self.temp_list = []
|
|
|
+ self.tim = Timer(-1)
|
|
|
+ self.tim.init(period=200, mode=Timer.PERIODIC,
|
|
|
+ callback=self.cal_temperature)
|
|
|
+
|
|
|
+ def cal_temperature(self, tim):
|
|
|
+ val_uv = self.adc.read_uv()/1000
|
|
|
Rt = val_uv / ((self.VCC - val_uv)/10000)
|
|
|
- T1 = 1/(log(Rt/10000)/3950 + 1/(273.15+25)) - 273.15 + 0.5
|
|
|
+ T1 = 1/(log(Rt/10000)/3950 + 1/(273.15+25)) - 273.15
|
|
|
+ # 0.5 是误差。T1是复数,T1.real是取它的实部
|
|
|
+ temp = T1.real + 0.5
|
|
|
+ self.temp = average(self.temp_list, temp)
|
|
|
# print("Rt:", Rt)
|
|
|
# print("voltage:", val_uv)
|
|
|
# print("T1:", T1.real)
|
|
|
- return T1.real
|
|
|
|
|
|
+ def get_temperature(self):
|
|
|
+ return self.temp
|
|
|
|
|
|
|
|
|
|
|
|
@@ -39,9 +59,8 @@ class HeatPower():
|
|
|
# self.sw_pin.duty(180)
|
|
|
self.duty = 0.0
|
|
|
self.duty_percent(0.7)
|
|
|
- self.center_temp = 0
|
|
|
- self.wire_temp = 0
|
|
|
self.status = 0
|
|
|
+ self.dat = {'status':0, 'pwr_percent':0}
|
|
|
|
|
|
def duty_percent(self, percent):
|
|
|
if percent > 1:
|
|
|
@@ -49,60 +68,76 @@ class HeatPower():
|
|
|
elif percent < 0:
|
|
|
percent = 0
|
|
|
self.duty = percent
|
|
|
- set_value = round(self.duty * 1024)
|
|
|
- print(self.duty)
|
|
|
- print(set_value)
|
|
|
+ set_value = round(self.duty * 1023)
|
|
|
+ # print(self.duty)
|
|
|
+ # print(set_value)
|
|
|
self.sw_pin.duty(set_value)
|
|
|
+ self.dat['pwr_percent'] = self.duty
|
|
|
|
|
|
|
|
|
def get_sw_status(self):
|
|
|
- if self.duty > 0:
|
|
|
- return True
|
|
|
- else:
|
|
|
- return False
|
|
|
-
|
|
|
- def get_sw_percent(self):
|
|
|
- return self.duty
|
|
|
+ return self.dat
|
|
|
|
|
|
def off(self):
|
|
|
- self.heat_status = 0
|
|
|
+ self.dat['status'] = 0
|
|
|
self.duty_percent(0)
|
|
|
- print("heat off")
|
|
|
+ # print("heat off")
|
|
|
|
|
|
- def on(self):
|
|
|
- # TODO
|
|
|
- ""
|
|
|
- self.duty_percent(0.7)
|
|
|
- self.heat_status = 1
|
|
|
- print("heat on")
|
|
|
+ def on(self, power_percent=0.8):
|
|
|
+ self.dat['status'] = 1
|
|
|
+ self.duty_percent(power_percent)
|
|
|
+ # print("heat on")
|
|
|
+
|
|
|
+ def ajust_duty(self, step=0):
|
|
|
+ self.duty += step
|
|
|
+ if self.duty > 1:
|
|
|
+ self.duty = 1
|
|
|
+ elif self.duty < 0:
|
|
|
+ self.duty = 0
|
|
|
+ self.duty_percent(self.duty)
|
|
|
|
|
|
-def sensor(heat):
|
|
|
+ def ajust_power(self, wt, ct):
|
|
|
+ if not self.dat['status']:
|
|
|
+ return
|
|
|
+ if wt > 88 or ct > 45:
|
|
|
+ step = (88-wt)/100
|
|
|
+ self.ajust_duty(step)
|
|
|
+ print('wt:', wt, "step:", step)
|
|
|
+ else:
|
|
|
+ step = (40-ct)/100
|
|
|
+ print('ct:', ct, "step:", step)
|
|
|
+ self.ajust_duty(step)
|
|
|
+
|
|
|
+
|
|
|
+def sensor():
|
|
|
wire_ntc = Ntc(34)
|
|
|
center_ntc = Ntc(36)
|
|
|
- while True:
|
|
|
- heat.wire_temp = wire_ntc.get_temperature()
|
|
|
- heat.center_temp = center_ntc.get_temperature()
|
|
|
-
|
|
|
- print("wire_temp:", heat.wire_temp)
|
|
|
- print( "center_temp:", heat.center_temp)
|
|
|
- if heat.wire_temp > 98:
|
|
|
- heat.off()
|
|
|
- elif heat.wire_temp < 85:
|
|
|
- if heat.center_temp > 44:
|
|
|
- heat.off()
|
|
|
- else:
|
|
|
- if heat.center_temp < 40 and heat.status == 0:
|
|
|
- heat.on()
|
|
|
- upload.set_data('heat_status', heat.status)
|
|
|
- upload.set_data('wire_temp', heat.wire_temp)
|
|
|
- upload.set_data('center_temp', heat.center_temp)
|
|
|
- # print("mem_info():", micropython.mem_info())
|
|
|
+ heat = HeatPower()
|
|
|
+ def heat_status_print():
|
|
|
+ # nonlocal wire_ntc,center_ntc,heat
|
|
|
+ wire_temp = wire_ntc.get_temperature()
|
|
|
+ # center_temp = 0
|
|
|
+ center_temp = center_ntc.get_temperature()
|
|
|
+ print("wire_temp:", wire_temp, "\tcenter_temp:", center_temp, '\tduty:', heat.duty)
|
|
|
+ heat.ajust_power(wire_temp, center_temp)
|
|
|
+ # heat.ajust_duty(1)
|
|
|
+ upload.set_data('heat', heat.dat)
|
|
|
+ upload.set_data('ntc', {"wire_temp": wire_temp, 'center_temp':center_temp})
|
|
|
# 主动垃圾回收是个好习惯,如果没有经常开辟内存,可不用
|
|
|
gc.collect()
|
|
|
- # print("collect mem_info():", micropython.mem_info())
|
|
|
- time.sleep_ms(800)
|
|
|
+
|
|
|
+ tim = Timer()
|
|
|
+ tim.init(1, 1200, heat_status_print)
|
|
|
+
|
|
|
+
|
|
|
+def test():
|
|
|
+ wire_ntc = Ntc(34,2)
|
|
|
+ center_ntc = Ntc(36,3)
|
|
|
+ print("class Ntc:", wire_ntc, center_ntc)
|
|
|
+ print("class adc:", wire_ntc.adc, center_ntc.adc)
|
|
|
+ print("tim:", wire_ntc.tim, center_ntc.tim)
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- heat_power = HeatPower()
|
|
|
- srv = WebSrv()
|
|
|
- sensor(heat_power)
|
|
|
+ sensor()
|
|
|
+ # test()
|