diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/DHT11Controller.java b/components/src/main/java/com/opensourcewithslu/components/controllers/DHT11Controller.java new file mode 100644 index 00000000..a2600c2f --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/DHT11Controller.java @@ -0,0 +1,25 @@ +package com.opensourcewithslu.components.controllers; + +import com.opensourcewithslu.inputdevices.DHT11Helper; +import com.pi4j.io.gpio.digital.DigitalInput; +import io.micronaut.http.annotation.Controller; +import io.micronaut.http.annotation.Get; +import jakarta.inject.Named; + +@Controller("/dht11Sensor") +public class DHT11Controller { + + private final DHT11Helper dht11Helper; + + public DHT11Controller(@Named("dht11-sensor-input") DigitalInput dht11In) { + this.dht11Helper = new DHT11Helper(dht11In); + } + + @Get("/getData") + public String getData() { + float temperature = dht11Helper.temperature; + float humidity = dht11Helper.humidity; + + return("Temperature: " + temperature + ", Humidity: " + humidity); + } +} diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index bf632e38..3f1977f4 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -99,6 +99,12 @@ pi4j: pull: PULL_DOWN debounce: 30000 provider: pigpio-digital-input + dht11-sensor: + name: DHT11 Sensor + address: 17 + pull: PULL_DOWN + debounce: 10000 + provider: pigpio-digital-input ### DIGITAL OUTPUTS digital-output: # tag::digitalOutput[] diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/DHT11Helper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/DHT11Helper.java new file mode 100644 index 00000000..f02afda2 --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/DHT11Helper.java @@ -0,0 +1,97 @@ +package com.opensourcewithslu.inputdevices; + +import com.pi4j.io.gpio.digital.DigitalInput; +import com.pi4j.io.gpio.digital.DigitalState; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class DHT11Helper { + + private static final Logger log = LoggerFactory.getLogger(DHT11Helper.class); + + private final DigitalInput dht11Input; + + public float temperature; + + public float humidity; + + private final int[] byteStream = new int[5]; + + public DHT11Helper(DigitalInput sensorInput) + { + this.dht11Input = sensorInput; + + initialize(); + } + + public void initialize() + { + log.info("Initializing DHT11 Sensor"); + + dht11Input.addListener(e -> { + if(e.state() == DigitalState.LOW) + { + return; + } + + int j = 0; + for (int i = 0; i < byteStream.length; i++) + { + int counter = 0; + while (dht11Input.isLow()) + { + counter++; + if (counter == 255) + break; + } + + byteStream[i] = counter; + + counter = 0; + while (dht11Input.isHigh()) + { + counter++; + if (counter == 255) + break; + } + + for (int k = 0; k < 8; k++) + { + counter = 0; + while (dht11Input.isLow()) + { + counter++; + if (counter == 255) + break; + } + + byteStream[j] <<= 1; + if (counter > 16) + byteStream[j] |= 1; + + counter = 0; + while (dht11Input.isHigh()) + { + counter++; + if (counter == 255) + break; + } + + if (counter == 255) + break; + } + j++; + } + + if ((j >= 40) && (byteStream[4] == ((byteStream[0] + byteStream[1] + byteStream[2] + byteStream[3]) & 0xFF))) + { + log.info("Successfully read data from DHT11 sensor"); + + humidity = byteStream[0]; + temperature = byteStream[2]; + } + else + log.info("Failed to read data from DHT11 sensor"); + }); + } +}