Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dht11 implementation Issue #183 #186

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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);
}
}
6 changes: 6 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
});
}
}
Loading