Browse Source

fix multi platform support

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

9
platformio.ini

@ -25,4 +25,11 @@ lib_deps = ottowinter/ESPAsyncWebServer-esphome@^3.0.0
board_build.filesystem = littlefs
upload_protocol = espota
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
// do not edit this
#if PLATFORM == ESP32 || PLATFORM == ESP8266
// DO NOT EDIT THIS !!
#if PLATFORM != NO_WIFI
#define WIRELESS_SUPPORT
#endif
@ -20,12 +20,12 @@
/*===== Pin =====*/
#define HEAT_GUN_FAN_PIN D2
#define HEATER1_PIN D0
#define HEATER2_PIN D5
#define CONNECTION_LED_PIN D5
#define HEATER1_LED_PIN D4
#define HEATER2_LED_PIN D5
#define HEAT_GUN_FAN_PIN 1
#define HEATER1_PIN 1
#define HEATER2_PIN 1
#define CONNECTION_LED_PIN 1
#define HEATER1_LED_PIN 1
#define HEATER2_LED_PIN 1
#define HEATER1_THERMISTOR_PIN A0
#define HEATER2_THERMISTOR_PIN A0

119
src/main.cpp

@ -18,13 +18,6 @@
/*===== Object =====*/
Ticker heater;
Ticker serial_reporter;
/*===== Variable =====*/
//-300: off
double temp1_current = 0;
@ -37,66 +30,65 @@ boolean stringComplete = false; // whether the string is complete
/*===== 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;
// 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;
const double C1 = 1/298.15;
// Vtm(Vadc) = Vin * Rtm/(Rs+Rtm)
// Rtm = Vtm * Rs / (Vin - Vtm)
// 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))
// 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 )
// 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;
// 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;
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;
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
}
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
}
}
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);
}
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);
}
}
@ -152,6 +144,18 @@ void heat_handler(){
#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 =====*/
void setup() {
@ -170,10 +174,18 @@ void setup() {
#endif
/*[Ticker Init]*/
heater.attach(0.1, heat_handler);
#ifndef SERIAL_DEBUG
serial_reporter.attach(0.5, serial_report_handler);
#ifdef WIRELESS_SUPPORT
heater.attach(0.1, heat_handler);
#ifndef SERIAL_DEBUG
serial_reporter.attach(0.5, serial_report_handler);
#endif
#else
heater.start();
#ifndef SERIAL_DEBUG
serial_reporter.start();
#endif
#endif
}
@ -183,6 +195,9 @@ void loop() {
//delay(100);
#ifdef WIRELESS_SUPPORT
wireless_handler();
#else
heater.update();
serial_reporter.update();
#endif
#ifndef SERIAL_DEBUG

Loading…
Cancel
Save