You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wrote some code, hoping to get data from DHT 11, after repeated tests, unfortunately I can't get enough 40 bits of data, I think it may be because the high or low level state of 40-100μs can not be accurately obtained, I tried to use a while loop to determine the current pin state, and run the AdafruitDHT.py script, when python runs and reads out the data, Java DigitalInput.state() loops do not have a full output of the correct high and low level states.The time of a loop is about 6-10 microseconds, which is enough to get the current state of the pin.
Can you give me some possible solutions?
Thanks.
Here's my code. Call the detect method.
/** * The length of the data. */privatestaticfinalIntegerDATA_LENGTH = 40;
/** * The timeout duration in nanoseconds for waiting for a signal. * This variable represents the duration in nanoseconds that will be used * when waiting for a signal. If the signal is not received within this * duration, the waiting process will time out. * It is recommended to set a reasonable and appropriate value for this * timeout to prevent waiting indefinitely. */privatefinalLongwaitSignalTimeOutNanos;
/** * This variable represents the maximum time in nanoseconds to wait for reading data before timing out. */privatefinalLongreadDataTimeOutNanos;
/** * The time duration, in milliseconds, after which the data should be sent. * This variable is used to specify how long to wait sending data. */privatestaticfinalLongSEND_DATA_TIME_MILLIS = 20L;
@GetterprivateDoubletemperature;
@GetterprivateDoublehumidity;
/** * A FutureTask for reading data from a source. */FutureTask<long[]> readDataTask = newFutureTask<>(this::readData);
/** * The interval, in seconds, between two consecutive detections. */privatefinalIntegerdetectionInterval;
/** * The time of the last detection. */privateLocalDateTimelastDetectionTime;
/** * Indicates whether to keep the signal high. */privatevolatilebooleankeepHighSignal = true;
protectedAbstractDhtDevice(DeviceManagerdeviceManager, Stringid, Stringname, IBCMEnumsaddress, IntegerdetectionInterval,LongwaitSignalTimeOutMicros, LongreadDataTimeOutMicros) {
super(deviceManager, id, name, address, DigitalState.HIGH, DigitalState.HIGH, PullResistance.OFF);
waitSignalTimeOutNanos = waitSignalTimeOutMicros * 1000;
readDataTimeOutNanos = readDataTimeOutMicros * 1000;
this.detectionInterval = detectionInterval;
}
/** * Performs a detection of temperature and humidity using a sensor. * * @return The updated temperature and humidity information. */@SneakyThrowspublicHumitureInfodetect() {
try {
lock.lock();
if (Objects.nonNull(lastDetectionTime) && lastDetectionTime.plusSeconds(detectionInterval).isAfter(LocalDateTime.now())) {
returnnewHumitureInfo(temperature, humidity);
}
if (Objects.isNull(lastDetectionTime)) {
TimeUnit.SECONDS.sleep(2);
}
// Send a start signaldigitalOutput.on();
newThread(readDataTask).start();
TimeUnit.MILLISECONDS.sleep(SEND_DATA_TIME_MILLIS);
while (keepHighSignal) {
Thread.onSpinWait();
}
digitalOutput.off();
// Read the datalong[] data = readDataTask.get();
// Processing of dataHumitureInfohumitureInfo = processData(data);
temperature = humitureInfo.getTemperature();
humidity = humitureInfo.getHumidity();
lastDetectionTime = LocalDateTime.now();
returnhumitureInfo;
} finally {
lock.unlock();
digitalOutput.off();
}
}
/** * Reads data from a device and returns an array of long values. * * @return an array of long values representing the read data * @throws DeviceException if there is a timeout while reading the data */privatelong[] readData() {
try {
long[] data = newlong[40];
intdataIndex = 0;
longeachReadEndTime = 0;
longnanoTimer;
// Wait for the host level to pull upkeepHighSignal = false;
awaitSignalOff(SEND_DATA_TIME_MILLIS * 2000000, DigitalState.LOW);
// Wait for the DHT to pull the level lowawaitSignalOff(waitSignalTimeOutNanos , DigitalState.HIGH);
// Wait for the DHT to pull the level highawaitSignalOff(waitSignalTimeOutNanos, DigitalState.LOW);
// Wait for the DHT to pull the level lowawaitSignalOff(waitSignalTimeOutNanos, DigitalState.HIGH);
// Start reading the datawhile (dataIndex < DATA_LENGTH) {
// 50us low level start signalawaitSignalOff(waitSignalTimeOutNanos, DigitalState.LOW);
// The high level starts to read the datananoTimer = System.nanoTime();
longvalidTime = readDataTimeOutNanos + nanoTimer;
while (digitalInput.state() == DigitalState.HIGH) {
if ((eachReadEndTime = System.nanoTime()) > validTime) {
thrownewDeviceException("Read data time out: " + (eachReadEndTime - nanoTimer) + " ns, data size: " + dataIndex);
}
}
// Save high timedata[dataIndex] = (eachReadEndTime - nanoTimer);
dataIndex ++;
}
returndata;
} finally {
keepHighSignal = true;
}
}
/** * Processes the given data and returns a HumitureInfo object. * * @param data An array of long values representing the data to be processed. * @return A HumitureInfo object containing the processed data. */protectedabstractHumitureInfoprocessData(long[] data);
/** * Waits for the specified amount of time for the given digital state to be off. * * @param timeOutNanos the timeout period in nanoseconds * @param digitalState the desired digital state to wait for * @throws DeviceException if the digital state is not off within the timeout period */privatevoidawaitSignalOff(longtimeOutNanos, DigitalStatedigitalState) {
longendTime = System.nanoTime() + timeOutNanos;
while (digitalInput.state() == digitalState) {
if (System.nanoTime() > endTime) {
thrownewDeviceException("Keep " + digitalState.name() + " signal time out: " + (System.nanoTime() - endTime));
}
}
}
/** * Converts a given number to a binary system based on the provided index. * * @param num The number to be converted. * @param index The index indicating the position in the binary system. * @return The converted number in the binary system. */protectedintsysConvert(longnum, intindex) {
return (int) (num * Math.pow(2, 7d - index));
}
@Data@AllArgsConstructorpublicstaticclassHumitureInfoimplementsSerializable {
privateDoubletemperature;
privateDoublehumidity;
}
The text was updated successfully, but these errors were encountered:
I believe dht 11 and 22 operate the same. My implementation may miss a few bits so i use the checksum to ensure the high order between bits were zero so unimportant. As I can’t view all your code maybe seeing all of my code will assist you. .
equipment: raspi4
os: ubuntu 23.10
pi4j version: 2.4.0
I wrote some code, hoping to get data from DHT 11, after repeated tests, unfortunately I can't get enough 40 bits of data, I think it may be because the high or low level state of 40-100μs can not be accurately obtained, I tried to use a while loop to determine the current pin state, and run the AdafruitDHT.py script, when python runs and reads out the data, Java
DigitalInput.state()
loops do not have a full output of the correct high and low level states.The time of a loop is about 6-10 microseconds, which is enough to get the current state of the pin.Can you give me some possible solutions?
Thanks.
Here's my code. Call the
detect
method.The text was updated successfully, but these errors were encountered: