Created
February 16, 2019 07:54
-
-
Save aaryadev/db268b156c09be967a17000d48f48b3d to your computer and use it in GitHub Desktop.
nodemcu read EEPROM lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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