Skip to content

Instantly share code, notes, and snippets.

@aaryadev
Created February 16, 2019 07:54
Show Gist options
  • Save aaryadev/db268b156c09be967a17000d48f48b3d to your computer and use it in GitHub Desktop.
Save aaryadev/db268b156c09be967a17000d48f48b3d to your computer and use it in GitHub Desktop.
nodemcu read EEPROM lua
print("eeprom")
GPIO_PIN = 7 --interrupt pin
id=0
device = 0x50 -- address of EEPROM
memadr=0x00 -- let's read from begining
sda, scl = 4,5
i2c.setup(id, sda, scl, i2c.SLOW)
buffer = adc.read(0)
--string.sub (buffer, 1 , 40)
--string.sub (buffer, 4 , 11)
--print (buffer)
--writing into EEPROM--
function WriteEE(memloc, data)
addrh=bit.rshift(memloc, 8)
addrl=bit.band(memloc,0xff)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, addrh)
i2c.write(id, addrl)
i2c.write(id,string.byte(data))
i2c.stop(id)
print("EEPROM WRITE COMPLETED")
end
--reading from EEPROM--
function ReadEE(memloc, length)
addrh=bit.rshift(memloc, 8)
addrl=bit.band(memloc,0xff)
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
i2c.write(id, addrh)
i2c.write(id, addrl)
i2c.stop(id)
i2c.start(id)
i2c.address(id, device, i2c.RECEIVER)
c = i2c.read(id, length)
i2c.stop(id)
print("EEPROM READ COMPLETED")
return c
end
--interrupt function--
function interrupt(level, stamp)-- callback function while interrupt
tmr.delay(700000)
operation()
gpio.trig(GPIO_PIN,"up", interrupt)--re-enable interrupt on pin while exit
end
--main operation--
function operation()
WriteEE(memadr,adc.read(0))
a = adc.read(0)
b = func(a) --to make to print only the required data as in the window shown--
reg = ReadEE(0,string.format(b)) -- Read 286 bytes of data
print("analog value is: ",reg)
end
--to make it to print particular data--
function func(a)
c=0
while( a ~= 0)
do
a = a/10
c = c + 1
end
return c
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment