Can somebody help me in porting this library to Lua ? https://github.com/adafruit/DHT-sensor- ... er/DHT.cpp if not all just the read() method.
/donnib
Explore... Chat... Share...
sancho wrote:My DHT22 and DHT11 will arive probably next week, I'll port it than (seems really easy as the library is not using interrupts).
alonewolfx2 wrote:sancho wrote:My DHT22 and DHT11 will arive probably next week, I'll port it than (seems really easy as the library is not using interrupts).
it seems already ported in nodelua. maybe you can take a quick look.
--retrieveData()
-- Retrieve data from DHT22 sensor
-- Set pin
pin = 4;
-- Initiliase bit data array
bitData = {}
-- Step 1 - Host send start signal and wait 1ms
-- Set pin to output data
gpio.mode(pin, gpio.OUTPUT)
-- Send out start signal to DHT22, pull low
gpio.write(pin, gpio.LOW)
-- Wait specified time '1ms+'
tmr.delay(20000)
-- Host pull up output pin voltage and wait for sensors response
gpio.write(pin, gpio.HIGH)
-- Set pin to recieve data
gpio.mode(pin, gpio.INPUT)
-- Loop and wait for ACK pulse
retryCount = 0;
-- Loop
while gpio.read(pin) == 1 do
-- Check retry count
if retryCount > 40 then
print "DHT22 not responding"
end
-- Increment count
retryCount = retryCount + 1
-- Wait 1us
tmr.delay(1)
end
-- Once confirmed wait a specified time '80us+'
tmr.delay(80)
-- Step 2 - Read data stream
-- Data recieved as 40 bits of data, 16 bits for humidity, 16 bits for temperature and 6 bits checksum
-- Need to convert from binary to decimal and divide by 10
-- When highest bit of temperature is '1' it means that temperature is below '0'
for i = 0, 5, 1 do
for j = 0, 8, 1 do
-- Reset count
retryCount = 0;
-- Wait for DHT22 pin to pull low
while gpio.read(pin) == 0 do
-- Check retry count
if retryCount > 75 then
print "DHT22 timeout waiting for data"
end
-- Increment count
retryCount = retryCount + 1
-- Wait 1us
tmr.delay(1)
end
-- Once confirmed wait a specified time '40us+'
tmr.delay(40)
-- Check data bit value
if (gpio.read(pin) == 1) then
-- DHT22 pin is high, bit value is a 1
bitData[i*8+j] = 1
else
-- DHT22 pin is low, bit value is a 0
bitData[i*8+j] = 0
end
-- Wait for 1us and pin is high
count = 0
-- Loop
while gpio.read(pin) == 1 and count < 100 do
tmr.delay(1)
-- Increment
count = count + 1
end
end
end
-- Re-initialise DHT22 pin
gpio.mode(pin, gpio.OUTPUT)
gpio.write(pin, gpio.HIGH)
-- Using bit data calculate values for humidity, temperature and checksum
-- Initialise variables
currentHumidity = 0
currentTemperature = 0
checksum = 0
-- First 16 bits is humidity
for i = 0, 16, 1 do
-- Check value
if (bitData[i+1] > 0) then
currentHumidity = bit.bor(currentHumidity, (bit.lshift(1, (15 - i))))
end
end
-- Second 16 bits is temperature
for i = 0, 16, 1 do
-- Check value
if (bitData[i+17] > 0) then
currentTemperature = bit.bor(currentTemperature, (bit.lshift(1, (15 - i))))
end
end
-- Last 8 bits are checksum
for i = 0, 8, 1 do
-- Check value
if (bitData[i+33] > 0) then
checksum = bit.bor(checksum, (bit.lshift(1, (7 - i))))
end
end
-- Convert values for humidity and temperature
lastHumidity = currentHumidity / 10
-- Check temperature is negative or not
if (bit.band(currentTemperature, 0x8000) == 0x8000) then
lastTemperature = (bit.band(currentTemperature, 0x7FFF) / 10) * -1
else
lastTemperature = currentTemperature / 10
end
-- Calculate checksum
-- Initialise variables
checksum1 = bit.rshift(currentHumidity, 8)
checksum2 = bit.band(currentHumidity, 0xFF)
checksum3 = bit.rshift(currentTemperature, 8)
checksum4 = bit.band(currentTemperature, 0xFF)
-- Check checksum
if (checksum == bit.band((checksum1 + checksum2 + checksum3 + checksum4), 0xFF)) then
-- Checksum successful
print "Checksum OK"
else
-- Checksum failed
print "DHT22 checksum error"
end
-- Function end
function transmitData()
-- Transmit sensor data
end
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]