java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.HumiTempComponent
This example demonstrates the temperature and humidity component on the CrowPi.
As Java does not allow for precise enough timings itself, this component does not use Pi4J to retrieve the pulses
of the GPIO pin for DHT11 sensor and instead relies on a linux system driver which reads the pulses and write the
results into a file.ut.
A clean alternative would be using a separate microcontroller which handles the super precise timing-based communication itself and interacts with the Raspberry Pi using I²C, SPI or any other bus. This would offload the work and guarantee even more accurate results. As the CrowPi does not have such a dedicated microcontroller though, using this driver was the best available approach.
-
Field Summary
-
Constructor Summary
ConstructorDescriptionCreates a new humidity and temperature sensor component with default path and polling intervalHumiTempComponent
(int pollingDelayMs) Creates a new humidity and temperature sensor component with default paths and custom polling intervalHumiTempComponent
(String humiPath, String tempPath, int pollingDelayMs) Creates a new humidity and temperature sensor component with custom paths and polling interval -
Method Summary
Modifier and TypeMethodDescriptiondouble
Gets the last read humidityprotected ScheduledFuture<?>
Returns the internal scheduled future for the poller thread or null if currently stopped.double
Gets the last read temperatureprotected void
startPoller
(long pollerPeriodMs) (Re-)starts the poller with the desired time period in milliseconds.Methods inherited from class com.pi4j.crowpi.components.Component
sleep, triggerSimpleEvent
-
Constructor Details
-
HumiTempComponent
public HumiTempComponent()Creates a new humidity and temperature sensor component with default path and polling interval -
HumiTempComponent
public HumiTempComponent(int pollingDelayMs) Creates a new humidity and temperature sensor component with default paths and custom polling interval- Parameters:
pollingDelayMs
- Delay in millis between reading measurement values
-
HumiTempComponent
Creates a new humidity and temperature sensor component with custom paths and polling interval- Parameters:
humiPath
- Path to the file containing humiditytempPath
- Path to the file containing temperaturepollingDelayMs
- Polling cycle of reading the measured values
-
-
Method Details
-
getHumidity
public double getHumidity()Gets the last read humidity- Returns:
- Return the humidity in %
-
getTemperature
public double getTemperature()Gets the last read temperature- Returns:
- Returns the temperature in °C
-
startPoller
protected void startPoller(long pollerPeriodMs) (Re-)starts the poller with the desired time period in milliseconds. If the poller is already running, it will be cancelled and rescheduled with the given time. The first poll happens immediately in a separate thread and does not get delayed.- Parameters:
pollerPeriodMs
- Polling period in milliseconds
-
getPoller
Returns the internal scheduled future for the poller thread or null if currently stopped.- Returns:
- Active poller instance or null
-