Class HumiTempComponent

java.lang.Object
com.pi4j.crowpi.components.Component
com.pi4j.crowpi.components.HumiTempComponent

public class HumiTempComponent extends Component
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

    Fields inherited from class com.pi4j.crowpi.components.Component

    logger
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new humidity and temperature sensor component with default path and polling interval
    HumiTempComponent(int pollingDelayMs)
    Creates a new humidity and temperature sensor component with default paths and custom polling interval
    HumiTempComponent(String humiPath, String tempPath, int pollingDelayMs)
    Creates a new humidity and temperature sensor component with custom paths and polling interval
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Gets the last read humidity
    protected ScheduledFuture<?>
    Returns the internal scheduled future for the poller thread or null if currently stopped.
    double
    Gets the last read temperature
    protected 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

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public HumiTempComponent(String humiPath, String tempPath, int pollingDelayMs)
      Creates a new humidity and temperature sensor component with custom paths and polling interval
      Parameters:
      humiPath - Path to the file containing humidity
      tempPath - Path to the file containing temperature
      pollingDelayMs - 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

      protected ScheduledFuture<?> getPoller()
      Returns the internal scheduled future for the poller thread or null if currently stopped.
      Returns:
      Active poller instance or null