Temperature Humidity driver for DHT11 and DHT22 #838
Replies: 5 comments 7 replies
-
That's great! We've done an I2C version of the AM2320, which is a DHT12 equivalent. It is done in the ECMA-419 style and can be found in I am interested to see your one-wire-serial implementation. |
Beta Was this translation helpful? Give feedback.
-
I have ordered a DHT11 and will give it a try. |
Beta Was this translation helpful? Give feedback.
-
DHT11.ts import Timer from "timer"; export default class DHT {
} |
Beta Was this translation helpful? Give feedback.
-
And the DHT22.ts import Timer from "timer";
import Time from "time";
import Monitor from "monitor"
import Digital from "pins/digital";
import RMT from "pins/rmt";
export default class DHT {
#dhtPin
#dhtVCC
#inputRMT
constructor(public pin: number, public dhtVCC: number) {
}
#initDHT = () => {
this.#dhtVCC = new Digital({ pin: this.dhtVCC, mode: Digital.Output })
this.#dhtVCC.write(1)
this.#dhtPin = new Digital({ pin: this.pin, mode: Digital.Output });
this.#dhtPin.write(0); // Send start signal
this.#dhtPin.close()
Timer.delay(20)
}
readRMT = (n = 10, bufferSize = 256) => {
this.#initDHT()
this.#inputRMT = new RMT({ pin: this.pin, channel: 0, divider: 80, direction: "rx", filter: 0, timeout: 1000, ringbufferSize: bufferSize * 2 });
this.#dhtPin = new Digital({ pin: this.pin, mode: Digital.InputPullUp });
let d = ''
Timer.delay(500)
let result = this.#inputRMT.read((new Uint16Array(bufferSize)).buffer);
this.#dhtPin.close()
this.#inputRMT.close()
let count = result.count;
let phase = result.phase;
let values = result.buffer = new Uint16Array(result.buffer, 0, count)
for (let i = 1; i < count; i += +2) {
d += 0 | values[i - phase] > 60
}
var cks =
parseInt(d.substr(0, 8), 2) +
parseInt(d.substr(8, 8), 2) +
parseInt(d.substr(16, 8), 2) +
parseInt(d.substr(24, 8), 2);
let checkSum = parseInt(d.substr(32, 8), 2)
let ok = cks && ((cks & 0xFF) == checkSum)
if (ok == false) {
if (n > 1) {
Timer.delay(500)
return this.readRMT(--n)
} else {
let data = { raw: d, humidity: -1, temperature: -1, abshumidity: -1, celsius: -1, fahrenheit: -1 }
return data
}
}
let data = {
raw: d,
humidity: parseFloat((parseInt(d.substr(0, 16), 2) * 0.1).toFixed(2)),
temperature: parseFloat((parseInt(d.substr(17, 15), 2) * 0.2 * (0.5 - d[16])).toFixed(2))
}
data['abshumidity'] = parseFloat((data.humidity * 0.42 * Math.exp(data.temperature * 10 * 0.006235398) / 10).toFixed(2));
data['celsius'] = data.temperature
data['fahrenheit'] = 32 + data.temperature * 1.8
return data;
}
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for posting that. The use of arrow functions in the class definitions is unusual (at least in my experience). It isn't wrong, but less efficient. Would you mind explaining why it does this:
Instead of the more conventional:
|
Beta Was this translation helpful? Give feedback.
-
I have developed a driver for each of DHT11/DHT22 temperature/humidity sensors based upon the principles of the Espruino counterparts. I just wonder if there is interest in these drivers?
Beta Was this translation helpful? Give feedback.
All reactions