nodemcu / nodemcu-firmware

Lua based interactive firmware for ESP8266, ESP8285 and ESP32
https://nodemcu.readthedocs.io
MIT License
7.62k stars 3.12k forks source link

Missing HX711 code in ESP32 - Here it is in lua script #3599

Open shetyespratham opened 1 year ago

shetyespratham commented 1 year ago

Missing feature

HX711 feature is missing in nodemcu of esp32. I have written and tested the code in Lua script which people can use.

Justification

HX711 is used by many and available with ESP8266. Having it in ESP32 will make people's life easier.

Workarounds

Lua Script I tested successfully on 40KG loadcell.

function tare(t) sum=hx711_read() sum=0 for i=1,t,1 do sum=sum+hx711_read() end offsetAod=sum/t isTare=1 return offsetAod end function getAverageWeight(t) nowAodValue=0 for i=1,t,1 do nowAodValue=nowAodValue+hx711_read() end nowAodValue=nowAodValue/t print("newAodValue="..nowAodValue) weight=(nowAodValue-offsetAod) * coefficient return weight / -2.55 -- this is a calibration value (weight / actual weight) -- when i checked known weight with the weight i was recieving here -- you can remove this division and then check the weight of object for which you know the real weight -- and then weight returned here / actual weight = calibration factor, which is -2.55 in my case end function hx711_read() tmr.wdclr() gpio.write(hx711_dat, 1) gpio.config({gpio=hx711_dat, dir=gpio.IN }) count=1 ctr=1 gpio.write(hx711_clk, 0)

while(count == 1) do -- waiting for hx711_dat to go LOW count = gpio.read(hx711_dat) ctr=ctr+1 end tmr.wdclr() count = 0 for i=0, 23 do -- receive 24 bits data gpio.write(hx711_clk, 1) gpio.write(hx711_clk, 0) -- bit.lshift(count, 1) count = count * 2 -- shift left by 1 bit ctr=0 ctr=gpio.read(hx711_dat) if ctr==1 then count=count+1 -- set first bit ON if gpio hx711_dat is high end end if count > 8388607 then -- when count > 8388607 i.e. 24th bit is ON so it is a simple version of XOR with x800000 count = count - 8388608 end gpio.write(hx711_clk, 1) gpio.write(hx711_clk, 0) gpio.write(hx711_clk, 1) gpio.write(hx711_clk, 0) gpio.write(hx711_clk, 1) gpio.write(hx711_clk, 0) return count end hx711_clk=22 hx711_dat=23 count=0 isTare=0 offsetAod=0 weight=0 coefficient=-0.00004906289863605140 gpio.config( {gpio=hx711_clk, dir=gpio.OUT, pull=gpio.FLOATING}, {gpio=hx711_dat, dir=gpio.IN_OUT, pull=gpio.FLOATING }) gpio.write(hx711_clk, 1) print("tare value "..tare(10)) print("weight "..getAverageWeight(10)) -- END of the program --