diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ThermistorController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ThermistorController.java index 00fef1aa..dddeb125 100644 --- a/components/src/main/java/com/opensourcewithslu/components/controllers/ThermistorController.java +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ThermistorController.java @@ -10,13 +10,19 @@ @Controller("/thermistor") public class ThermistorController { private final ThermistorHelper thermistorHelper; - // Finalize (Still gathering my thoughts about this) - public ThermistorController(@Named("thermistor-input") DigitalInput sensorInput, @Named("thermistor-output") DigitalOutput sensorOutput){ + + public ThermistorController(@Named("thermistor-input") DigitalInput sensorInput, @Named("thermistor-output") DigitalOutput sensorOutput) { this.thermistorHelper = new ThermistorHelper(sensorInput, sensorOutput); } - @Get("/value") - public int getThermistorValue(){ - return thermistorHelper.getThermistorValue(); + @Get("/celsius") + public double getTemperatureCelsius() { + return thermistorHelper.getTemperatureCelsius(); + } + + @Get("/fahrenheit") + public double getTemperatureFahrenheit() { + return thermistorHelper.getTemperatureFahrenheit(); } } + diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 9f05380a..42451dde 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -137,6 +137,8 @@ pi4j: name: Thermistor Input address: 18 provider: pigpio-digital-input + pull: PULL_DOWN + debounce: 2000 # end::digitalInput[] # tag::digitalOutput[] @@ -165,6 +167,12 @@ pi4j: shutdown: LOW initial: LOW provider: pigpio-digital-output + thermistor-output: + name: Thermistor Output + address: 18 + provider: pigpio-digital-output + initial: LOW + shutdown: LOW # end::digitalOutput[] # tag::multiInput[] diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ThermistorHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ThermistorHelper.java index 77873556..d46ab6ce 100644 --- a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ThermistorHelper.java +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ThermistorHelper.java @@ -7,14 +7,37 @@ public class ThermistorHelper { private static final Logger log = LoggerFactory.getLogger(ThermistorHelper.class); - private String helperName; - private int globalValue; + private DigitalInput sensorInput; + private DigitalOutput sensorOutput; - public ThermistorHelper(DigitalInput sensorInput, DigitalOutput sensorOutput){ - //Finalize (I'm still gathering my thoughts about how to handle this) + public ThermistorHelper(DigitalInput sensorInput, DigitalOutput sensorOutput) { + this.sensorInput = sensorInput; + this.sensorOutput = sensorOutput; } - public int getThermistorValue() { + public double getTemperatureCelsius() { + int rawValue = getThermistorValue(); + return convertRawToCelsius(rawValue); + } + + public double getTemperatureFahrenheit() { + double celsius = getTemperatureCelsius(); + return celsius * 9.0 / 5.0 + 32; + } + + private double convertRawToCelsius(int rawValue) { + double resistance = calculateResistance(rawValue); + double tempCelsius = 1.0 / (0.001129148 + (0.000234125 * Math.log(resistance)) + (0.0000000876741 * Math.pow(Math.log(resistance), 3))) - 273.15; + return tempCelsius; + } + + private double calculateResistance(int rawValue) { + double Vout = (rawValue / 1023.0) * 3.3; + return (10_000 * Vout) / (3.3 - Vout); + } + + private int getThermistorValue() { return 1; } } +