Skip to content

Instantly share code, notes, and snippets.

@tomasinouk
Last active August 3, 2016 17:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomasinouk/5a0c0c56b94456b0fe56 to your computer and use it in GitHub Desktop.
Save tomasinouk/5a0c0c56b94456b0fe56 to your computer and use it in GitHub Desktop.
Reading battery capacity nodeMCU - lua
id=0
-- represent actually PIN on nodeMCU such as
-- D7 and D5 in this case
-- GND needs to be connected
sda=7 -- GPIO13
scl=5 -- GPIO14
-- initialize i2c, set pin1 as sda, set pin2 as scl
i2c.setup(id,sda,scl,i2c.SLOW)
-- user defined function: read from reg_addr content of dev_addr
function read_reg(dev_addr, reg_addr)
i2c.start(id)
a = i2c.address(id, dev_addr ,i2c.TRANSMITTER)
print(a)
i2c.write(id,reg_addr)
i2c.stop(id)
i2c.start(id)
i2c.address(id, dev_addr,i2c.RECEIVER)
c=i2c.read(id,1)
i2c.stop(id)
return c
end
-- get content of register 0x0d (13) of device 0x0B (11)
reg = read_reg(0x0B, 0x0d)
print("Battery capacity: " , string.byte(reg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment