Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- dht11 code for esp8266
- -- better approach counting back 40 bits from the last meaningful bit
- -- untested yet
- -- Kevin D
- pin = 4
- function ReadData()
- gpio_read=gpio.read
- gpio_write=gpio.write
- h=0
- hl=0
- t=0
- tl=0
- cs=0
- bl=0
- cscalc=0
- offset=0
- data={}
- for j = 1, 50, 1 do
- data[j]=0
- end
- gpio.mode(pin, gpio.OUTPUT)
- gpio.write(pin, gpio.LOW)
- tmr.delay(20000)
- gpio.mode(pin, gpio.INPUT)
- for j=1,50,1 do
- bl=0
- while (gpio_read(pin)==0) do end
- while (gpio_read(pin)==1 and bl<10 ) do
- bl=bl+1
- end
- if(bl>=10) then
- offset=j-1-40
- break
- else
- data[j]=bl
- end
- end
- for i=1,8,1 do
- if (data[i+0+offset]>2) then
- h = h+2^(8-i)
- end
- end
- for i=1,8,1 do
- if (data[i+8+offset]>2) then
- hl = hl+2^(8-i)
- end
- end
- for i=1,8,1 do
- if (data[i+16+offset]>2) then
- t = t+2^(8-i)
- end
- end
- for i=1,8,1 do
- if (data[i+24+offset]>2) then
- tl = tl+2^(8-i)
- end
- end
- for i=1,8,1 do
- if (data[i+32+offset]>2) then
- cs = cs+2^(8-i)
- end
- end
- cscalc=(h+hl+t+tl)%256
- if(cs==cscalc) then
- print ("t: "..t.."."..tl)
- print ("h: "..h.."."..hl)
- else
- print ("erreur checksum")
- end
- end
- ReadData()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement