From c6ba77f7af0cc08e39fff552584cd614776f75bf Mon Sep 17 00:00:00 2001 From: p8p671 Date: Fri, 7 Apr 2023 08:04:05 -0700 Subject: [PATCH] base function --- src/main.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index 5c24324..0c0a963 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,6 +8,16 @@ /*[const]*/ #define HEAT_OFF -300 +/*[pin]*/ +#define HEAT_GUN_FAN_PIN D2 +#define HEATER1_PIN D4 +#define HEATER2_PIN D4 +#define CONNECTION_LED_PIN D0 +#define HEATER1_LED_PIN D0 +#define HEATER2_LED_PIN D0 +#define HEATER1_THERMISTOR_PIN A0 +#define HEATER2_THERMISTOR_PIN A0 + /*[wifi]*/ #define AP_SSID "test" #define AP_PASSWD "sonofussr" @@ -24,6 +34,7 @@ AsyncWebServer server(80); AsyncWebSocket ws("/ws"); Ticker temp_reporter; +Ticker heater; /*===== Variable =====*/ @@ -35,6 +46,50 @@ double temp2_current = 0; double temp2_target = HEAT_OFF; +/*===== Function =====*/ + +double getTemperature(uint8_t thermistor_pin){ + // vin ---- Rs --(adc)-- Rtm ---- gnd + const double Vin = 3.3; //3.3 for ESP/STM32 series + const double Rs = 10000; //strip resistor resistance + const double Rtm_std = 100000; //thermistor resistance at 25C + const uint32_t adc_resolution = 1023; //10bit ADC + const uint32_t B = 3950; + + const double C1 = 1/298.15; + + // Vtm(Vadc) = Vin * Rtm/(Rs+Rtm) + // Rtm = Vtm * Rs / (Vin - Vtm) + + // Rtm = Rtm_std * exp(B*(1/Tk - 1/298.15)) + // Rtm = Rtm_std * exp(B*(1/Tk - C1)) + + // exp(B*(1/Tk - C1)) = Rtm / Rtm_std + // B*(1/Tk - C1) = log[Rtm / Rtm_std] + // 1/Tk = log[Rtm / Rtm_std] / B + C1 + // Tk = 1/( log[Rtm / Rtm_std] / B + C1 ) + + // T = Tk -273.15; + + double Vtm = (analogRead(thermistor_pin) * Vin) / adc_resolution; + double Rtm = Vtm * Rs / (Vin - Vtm); + double T = 1 / ( log(Rtm / Rtm_std) / B + C1) - 273.15; + + return T; +} + + +void heat_drive(uint8_t heater, bool heat_sw){ + if(heater == 1){ + digitalWrite(HEATER1_PIN, heat_sw); + digitalWrite(HEATER1_LED_PIN, !heat_sw); //low: turn on + }else if(heater == 2){ + digitalWrite(HEATER2_PIN, heat_sw); + digitalWrite(HEATER2_LED_PIN, !heat_sw); //low: turn on + } +} + + /*===== Handler =====*/ void handleWebSocketMessage(void *arg, uint8_t *data, size_t len) { @@ -95,11 +150,38 @@ void temp_report_handler(){ } +void heat_handler(){ + temp1_current = getTemperature(HEATER1_THERMISTOR_PIN); + temp2_current = getTemperature(HEATER2_THERMISTOR_PIN); + + if(temp1_target > MAX_TEMP || temp1_target < 0 || temp1_current > temp1_target){ + heat_drive(1,0); + }else{ + heat_drive(1,1); + } + + if(temp2_target > MAX_TEMP || temp2_target < 0 || temp2_current > temp2_target){ + heat_drive(2,0); + }else{ + heat_drive(2,1); + } +} + + /*===== Main =====*/ void setup() { Serial.begin(115200); + //pin init + pinMode(HEAT_GUN_FAN_PIN, OUTPUT); + pinMode(HEATER1_PIN, OUTPUT); + pinMode(HEATER2_PIN, OUTPUT); + pinMode(CONNECTION_LED_PIN, OUTPUT); + pinMode(HEATER1_LED_PIN, OUTPUT); + pinMode(HEATER2_LED_PIN, OUTPUT); + + //init LittleFS if(!LittleFS.begin()){ Serial.println("fail init fs"); @@ -129,7 +211,9 @@ void setup() { server.begin(); + //ticker temp_reporter.attach(0.5, temp_report_handler); + heater.attach(0.1, heat_handler); } void loop() {