Browse Source

fix multi platform support

master
iansun2 2 years ago
parent
commit
fecc79b6e0
  1. 7
      platformio.ini
  2. 16
      src/config.hpp
  3. 119
      src/main.cpp

7
platformio.ini

@ -26,3 +26,10 @@ board_build.filesystem = littlefs
upload_protocol = espota upload_protocol = espota
upload_port = 192.168.4.1 upload_port = 192.168.4.1
monitor_speed = 9600 monitor_speed = 9600
[env:arduino_uno]
platform = atmelavr
board = uno
framework = arduino
monitor_speed = 9600
lib_deps = sstaub/Ticker@^4.4.0

16
src/config.hpp

@ -9,8 +9,8 @@
#endif #endif
// do not edit this // DO NOT EDIT THIS !!
#if PLATFORM == ESP32 || PLATFORM == ESP8266 #if PLATFORM != NO_WIFI
#define WIRELESS_SUPPORT #define WIRELESS_SUPPORT
#endif #endif
@ -20,12 +20,12 @@
/*===== Pin =====*/ /*===== Pin =====*/
#define HEAT_GUN_FAN_PIN D2 #define HEAT_GUN_FAN_PIN 1
#define HEATER1_PIN D0 #define HEATER1_PIN 1
#define HEATER2_PIN D5 #define HEATER2_PIN 1
#define CONNECTION_LED_PIN D5 #define CONNECTION_LED_PIN 1
#define HEATER1_LED_PIN D4 #define HEATER1_LED_PIN 1
#define HEATER2_LED_PIN D5 #define HEATER2_LED_PIN 1
#define HEATER1_THERMISTOR_PIN A0 #define HEATER1_THERMISTOR_PIN A0
#define HEATER2_THERMISTOR_PIN A0 #define HEATER2_THERMISTOR_PIN A0

119
src/main.cpp

@ -18,13 +18,6 @@
/*===== Object =====*/
Ticker heater;
Ticker serial_reporter;
/*===== Variable =====*/ /*===== Variable =====*/
//-300: off //-300: off
double temp1_current = 0; double temp1_current = 0;
@ -37,66 +30,65 @@ boolean stringComplete = false; // whether the string is complete
/*===== Function =====*/ /*===== Function =====*/
double getTemperature(uint8_t thermistor_pin){ double getTemperature(uint8_t thermistor_pin){
// vin ---- Rs --(adc)-- Rtm ---- gnd // vin ---- Rs --(adc)-- Rtm ---- gnd
const double Vin = 3.3; //3.3 for ESP/STM32 series const double Vin = 3.3; //3.3 for ESP/STM32 series
const double Rs = 10000; //strip resistor resistance const double Rs = 10000; //strip resistor resistance
const double Rtm_std = 100000; //thermistor resistance at 25C const double Rtm_std = 100000; //thermistor resistance at 25C
const uint32_t adc_resolution = 1023; //10bit ADC const uint32_t adc_resolution = 1023; //10bit ADC
const uint32_t B = 3950; const uint32_t B = 3950;
const double C1 = 1/298.15; const double C1 = 1/298.15;
// Vtm(Vadc) = Vin * Rtm/(Rs+Rtm) // Vtm(Vadc) = Vin * Rtm/(Rs+Rtm)
// Rtm = Vtm * Rs / (Vin - Vtm) // Rtm = Vtm * Rs / (Vin - Vtm)
// Rtm = Rtm_std * exp(B*(1/Tk - 1/298.15)) // Rtm = Rtm_std * exp(B*(1/Tk - 1/298.15))
// Rtm = Rtm_std * exp(B*(1/Tk - C1)) // Rtm = Rtm_std * exp(B*(1/Tk - C1))
// exp(B*(1/Tk - C1)) = Rtm / Rtm_std // exp(B*(1/Tk - C1)) = Rtm / Rtm_std
// B*(1/Tk - C1) = log[Rtm / Rtm_std] // B*(1/Tk - C1) = log[Rtm / Rtm_std]
// 1/Tk = log[Rtm / Rtm_std] / B + C1 // 1/Tk = log[Rtm / Rtm_std] / B + C1
// Tk = 1/( log[Rtm / Rtm_std] / B + C1 ) // Tk = 1/( log[Rtm / Rtm_std] / B + C1 )
// T = Tk -273.15; // T = Tk -273.15;
double Vtm = (analogRead(thermistor_pin) * Vin) / adc_resolution; double Vtm = (analogRead(thermistor_pin) * Vin) / adc_resolution;
double Rtm = Vtm * Rs / (Vin - Vtm); double Rtm = Vtm * Rs / (Vin - Vtm);
double T = 1 / ( log(Rtm / Rtm_std) / B + C1) - 273.15; double T = 1 / ( log(Rtm / Rtm_std) / B + C1) - 273.15;
return T; return T;
} }
void heat_drive(uint8_t heater, bool heat_sw){ void heat_drive(uint8_t heater, bool heat_sw){
if(heater == 1){ if(heater == 1){
digitalWrite(HEATER1_PIN, heat_sw); digitalWrite(HEATER1_PIN, heat_sw);
digitalWrite(HEATER1_LED_PIN, !heat_sw); //low: turn on digitalWrite(HEATER1_LED_PIN, !heat_sw); //low: turn on
}else if(heater == 2){ }else if(heater == 2){
digitalWrite(HEATER2_PIN, heat_sw); digitalWrite(HEATER2_PIN, heat_sw);
digitalWrite(HEATER2_LED_PIN, !heat_sw); //low: turn on digitalWrite(HEATER2_LED_PIN, !heat_sw); //low: turn on
} }
} }
void heat_handler(){ void heat_handler(){
temp1_current = getTemperature(HEATER1_THERMISTOR_PIN); temp1_current = getTemperature(HEATER1_THERMISTOR_PIN);
temp2_current = getTemperature(HEATER2_THERMISTOR_PIN); temp2_current = getTemperature(HEATER2_THERMISTOR_PIN);
if(temp1_target > MAX_TEMP || temp1_target < 0 || temp1_current > temp1_target){ if(temp1_target > MAX_TEMP || temp1_target < 0 || temp1_current > temp1_target){
heat_drive(1,0); heat_drive(1,0);
}else{ }else{
heat_drive(1,1); heat_drive(1,1);
} }
if(temp2_target > MAX_TEMP || temp2_target < 0 || temp2_current > temp2_target){ if(temp2_target > MAX_TEMP || temp2_target < 0 || temp2_current > temp2_target){
heat_drive(2,0); heat_drive(2,0);
}else{ }else{
heat_drive(2,1); heat_drive(2,1);
} }
} }
@ -152,6 +144,18 @@ void heat_handler(){
#endif #endif
/*===== Object =====*/
#ifdef WIRELESS_SUPPORT
Ticker heater;
Ticker serial_reporter;
#else
Ticker heater(heat_handler, 100);
Ticker serial_reporter(serial_report_handler, 500);
#endif
/*===== Main =====*/ /*===== Main =====*/
void setup() { void setup() {
@ -170,10 +174,18 @@ void setup() {
#endif #endif
/*[Ticker Init]*/ /*[Ticker Init]*/
heater.attach(0.1, heat_handler); #ifdef WIRELESS_SUPPORT
#ifndef SERIAL_DEBUG heater.attach(0.1, heat_handler);
serial_reporter.attach(0.5, serial_report_handler); #ifndef SERIAL_DEBUG
serial_reporter.attach(0.5, serial_report_handler);
#endif
#else
heater.start();
#ifndef SERIAL_DEBUG
serial_reporter.start();
#endif
#endif #endif
} }
@ -183,6 +195,9 @@ void loop() {
//delay(100); //delay(100);
#ifdef WIRELESS_SUPPORT #ifdef WIRELESS_SUPPORT
wireless_handler(); wireless_handler();
#else
heater.update();
serial_reporter.update();
#endif #endif
#ifndef SERIAL_DEBUG #ifndef SERIAL_DEBUG

Loading…
Cancel
Save