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

User avatar
By mfkfx1
#40845 Hello,

following this tutorial:
http://www.esp8266-projects.com/2015/04 ... -time.html
I created a file ds3231.lua with the following content:

Code: Select allfunction 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
 
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

address = 0x51 -- A2, A1, A0 = 0
id=0

init = function (self, sda, scl)
      self.id = 0
     i2c.setup(self.id, sda, scl, i2c.SLOW)
end

readTime = function (self)
   wkd = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }
   i2c.start(self.id)
   i2c.address(self.id, self.address, i2c.TRANSMITTER)
   i2c.write(self.id, 0x00)
   i2c.stop(self.id)
   i2c.start(self.id)
   i2c.address(self.id, self.address, i2c.RECEIVER)
   c=i2c.read(self.id, 7)
   i2c.stop(self.id)
   return  bcdToDec(string.byte(c,1)),
         bcdToDec(string.byte(c,2)),
         bcdToDec(string.byte(c,3)),
         wkd[tonumber(bcdToDec(string.byte(c,4)))],
         bcdToDec(string.byte(c,5)),
         bcdToDec(string.byte(c,6)),
         bcdToDec(string.byte(c,7))
end

setTime = function (self, second, minute, hour, day, date, month, year)
   i2c.start(self.id)
   i2c.address(self.id, self.address, i2c.TRANSMITTER)
   i2c.write(self.id, 0x00)
   i2c.write(self.id, decToBcd(second))
   i2c.write(self.id, decToBcd(minute))
   i2c.write(self.id, decToBcd(hour))
   i2c.write(self.id, decToBcd(day))
   i2c.write(self.id, decToBcd(date))
   i2c.write(self.id, decToBcd(month))
   i2c.write(self.id, decToBcd(year))
   i2c.stop(self.id)
end


When I try to use this new "module" from another file (i2c_ds3231.lua) with the example code:

Code: Select allrequire('ds3231')
sda, scl = 2, 1
ds3231:init(sda, scl)

s, m, h, d, dt, mn, y = ds3231:readTime()


I get the following error:
i2c_ds3231.lua:3: attempt to index global 'ds3231' (a nil value)

Can you tell me what I did wrong?

Thank you very much!
User avatar
By mfkfx1
#40854 Unfortunately the same error:

Code: Select all> print(ds3231:readTime())
stdin:1: attempt to index global 'ds3231' (a nil value)


Do I need something like

Code: Select alllocal moduleName = ...
local M = {}
_G[moduleName] = M
...
return M


to make this a real "module"?
User avatar
By xtal
#40857 I have not used any modules....so cannot help

I would try a very simple program to use the DS3231[without using require] to get it working ,
then try putting into module or what ever.

There is something called FlashMod that uses modules in flash to save ram ...myself I'll use require

I looked at the project and it confuses me....
For testing, pack it together and save the code on ESP as 'ds3231.lua', restart ESP and run:
Did you do the above step....

Using REQUIRE
I write simple function [ with no internal functions]
I write the function to FLASH -- I'm using ESPlorer v 0.2

Hope this helps...

Code: Select alllocal function LBuf(scnt,c)
   LBx = require("LB"..scnt)               --Load flash finction  LB0,LB1,LB2
   local cbuf = LBx.B0()                         -- execute function

   vt[scnt] = #cbuf     
     if #cbuf>=1455 then
       c:send(scnt.."---C Buffer > 1455 Error---\r\n")
     else
       c:send(cbuf)
     end 
   cbuf = nil                                                       -- release buffer

   package.loaded["LB"..scnt]=nil         -- release memory
   collectgarbage()
end 

-- this is written into flash as LB0.lua
local LB0 = {}     
function LB0.B0()     
    local cbuf = '<!DOCTYPE HTML>\n<html lang="en-US">\n<head><meta http-equiv="refresh" content="'
      ..rfh..'" ></head>'
      ..'<script type = "text/javascript">\nvar timeInSecs;\nvar ticker;'
      ..'function startTimer(secs){ timeInSecs = parseInt(secs)-1; ticker = setInterval("tick()",1000); }'
      ..'function tick() { var secs = timeInSecs; if (secs>0) {timeInSecs--;} else {clearInterval(ticker); startTimer('..rfh..');}'               
      ..'document.getElementById("countdown").innerHTML = secs;} startTimer('..rfh..'); </script>'     
      ..'<body><table bgcolor="Lawngreen" border="1" cellpadding="5">'       
      ..'<tr><th><h3>ESP8266</h3><h3>'..vt.device..'</h3></th><th><h3>-SDK-</h3><h3>NodeMCU</h3></th>'
      ..'<th><h3>'..yver..'</h3><h3>'..vt.sware..'</h3></th><th><h3>AAA</h3><h3>BBB</h3></th>'                 
      ..'<td><IMG SRC="'..vt.s_img..'" LEFT="1" WIDTH="120" HEIGHT="60" BORDER="1"></td>'       
      ..'<th><h3 id="countdown">'..rfh..'</h3><h3>'..vt.pass..'</h3></th></tr>'     
      ..'<th bgcolor="Cyan">'..vt.mac..'</th><th bgcolor="Yellow">'..vt.ipa..'</th>'               
      ..'<th>'..vt.pass..'</th><th>'..vt.pass..'</th><th>BAUD = '..vt.baud..'</th>'
      ..'<th>Refresh = '..vt.rfh..'</th>' --</tr>'
      ..'</table><br>'
     return cbuf
end     
 return LB0