As the title says... Chat on...

User avatar
By mfkfx1
#40966 Hello again,

I created the following module:

Code: Select alllocal ds3231_m = {}

---------- Local variables ---------------------
local id = 0
local address = 0x68


---------- Helper functions --------------------
function bcdToDec(val)
    local hl=bit.rshift(val, 4)
    local hh=bit.band(val,0xf)
    local hr = string.format("%d%d", hl, hh)
  return string.format("%d%d", hl, hh)
end

function decToBcd(val)
   local d = string.format("%d",tonumber(val / 10))
   local d1 = tonumber(d*10)
   local d2 = val - d1
   return tonumber(d*16+d2)
end

---------- Module functions --------------------
function ds3231_m.Init(sda, scl)
   i2c.setup(id, sda, scl, i2c.SLOW)
    return
end

function ds3231_m.PrintTime()
   i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, address, i2c.RECEIVER)
    c=i2c.read(id, 7)
    i2c.stop(id)
   s = bcdToDec(string.byte(c,1))
   m = bcdToDec(string.byte(c,2))
    h = bcdToDec(string.byte(c,3))
   
   time=string.format(" %s:%s:%s", h, m, s)
   print(time);
    return nil
end

function ds3231_m.PrintDate()
    i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, address, i2c.RECEIVER)
    c=i2c.read(id, 7)
    i2c.stop(id)
    s = bcdToDec(string.byte(c,1))
    m = bcdToDec(string.byte(c,2))
    h = bcdToDec(string.byte(c,3))
    wkd = bcdToDec(string.byte(c,4))
    day = bcdToDec(string.byte(c,5))
    month = bcdToDec(string.byte(c,6))
    year = bcdToDec(string.byte(c,7))
   
    time=string.format(" %s.%s.%s", day, month, year)
    print(time);
    return nil
end

function ds3231_m.SetDateTime(wkday, day, month, year, hour, minute, second)
   i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.write(id, decToBcd(second))
    i2c.write(id, decToBcd(minute))
    i2c.write(id, decToBcd(hour))
    i2c.write(id, decToBcd(wkday))
    i2c.write(id, decToBcd(day))
    i2c.write(id, decToBcd(month))
    i2c.write(id, decToBcd(year))
    i2c.stop(id)
    return
end

return ds3231_m



When I try to use single function calls, it works.

This

Code: Select allm = require "ds3231_m"
m.Init(2,1)   --sda, scl = 2, 1
m.PrintTime()
package.loaded.ds3231_m = nil


works as well as

Code: Select allm = require "ds3231_m"
m.Init(2,1)   --sda, scl = 2, 1
m.PrintDate()
package.loaded.ds3231_m = nil


But this (calling three functions)

Code: Select allm = require "ds3231_m"
m.Init(2,1)   --sda, scl = 2, 1
m.PrintTime()
m.PrintDate()
package.loaded.ds3231_m = nil


produces an error: attempt to call field 'PrintDate' (a nil value)

Can anyone tell me what I did wrong?

Thank you!
User avatar
By mfkfx1
#40969 I took a look into the dht22-Module, which is included in nodemcu per default.

Now I have changed the module to:
Code: Select all--local ds3231_m = {}

local moduleName = ...
local M = {}
_G[moduleName] = M

---------- Local variables ---------------------
local id = 0
local address = 0x68


---------- Helper functions --------------------
function bcdToDec(val)
    local hl=bit.rshift(val, 4)
    local hh=bit.band(val,0xf)
    local hr = string.format("%d%d", hl, hh)
  return string.format("%d%d", hl, hh)
end

function decToBcd(val)
   local d = string.format("%d",tonumber(val / 10))
   local d1 = tonumber(d*10)
   local d2 = val - d1
   return tonumber(d*16+d2)
end

---------- Module functions --------------------
function M.Init(sda, scl)
   i2c.setup(id, sda, scl, i2c.SLOW)
    return
end

function M.PrintTime()
   i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, address, i2c.RECEIVER)
    c=i2c.read(id, 7)
    i2c.stop(id)
   s = bcdToDec(string.byte(c,1))
   m = bcdToDec(string.byte(c,2))
    h = bcdToDec(string.byte(c,3))
   
   time=string.format(" %s:%s:%s", h, m, s)
   print(time);
end

function M.PrintDate()
    i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.stop(id)
    i2c.start(id)
    i2c.address(id, address, i2c.RECEIVER)
    c=i2c.read(id, 7)
    i2c.stop(id)
    s = bcdToDec(string.byte(c,1))
    m = bcdToDec(string.byte(c,2))
    h = bcdToDec(string.byte(c,3))
    wkd = bcdToDec(string.byte(c,4))
    day = bcdToDec(string.byte(c,5))
    month = bcdToDec(string.byte(c,6))
    year = bcdToDec(string.byte(c,7))
   
    time=string.format(" %s.%s.%s", day, month, year)
    print(time);
end

function M.SetDateTime(wkday, day, month, year, hour, minute, second)
   i2c.start(id)
    i2c.address(id, address, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.write(id, decToBcd(second))
    i2c.write(id, decToBcd(minute))
    i2c.write(id, decToBcd(hour))
    i2c.write(id, decToBcd(wkday))
    i2c.write(id, decToBcd(day))
    i2c.write(id, decToBcd(month))
    i2c.write(id, decToBcd(year))
    i2c.stop(id)
    return
end

--return ds3231_m
return M


But the error is still present.

Regards