RTC Clock NodeMCU
RTC Clock NodeMCU
This page is to Setup and Config RTC Clock NodeMCU
Hardware and Software
Hardware
RTC Clock NodeMCU
Software
ESplorer
Sanki Note
I2C -- SDA : D3 (GPIO 0 ) / SCL D4 (GPIO 2)
Examples
This example is dsl1307_Lib.lua
Examples
This example is AT24C Library (Lua) -- AT24CXXX_Test.lua
local Nom_du_module = ...
local M = {}
_G[Nom_du_module] = M
local function decToBcd(valeur)
return((valeur/10*16) + (valeur%10))
end
local function bcdToDec(valeur)
return((valeur/16*10) + (valeur%16))
end
function M.init(d, l, a)
sda = d
scl = l
device = a
if (i2c.setup(id, sda, scl, i2c.SLOW)) ~= 0 then print("Connect I2C RTC")
elseif (i2c.setup(id, sda, scl, i2c.SLOW)) == 0 then print("Erreur dans la configuration du port I2C!") return nil
end
end
function M.obtenir_heure()
-- Configuration ------------------------------------------------
i2c.start(id)
i2c.address(id, device, i2c.TRANSMITTER)
require("ds1307_Lib")
i2c.write(id, 0x00)
i2c.stop(id)
ds1307_Lib.init(3, 4, 0x68)
i2c.start(id)
seconde = 00
i2c.address(id, device, i2c.RECEIVER)
minute = 27
local c = i2c.read(id, 7)
heure = 15
i2c.stop(id)
jour = 5
date = 06
return bcdToDec(tonumber(string.byte(c, 1))),
mois = 07
annee =16
bcdToDec(tonumber(string.byte(c, 2))),
bcdToDec(tonumber(string.byte(c, 3))),
ds1307_Lib.configurer_heure(seconde, minute, heure, jour, date, mois, annee)
bcdToDec(tonumber(string.byte(c, 4))),
bcdToDec(tonumber(string.byte(c, 5))),
ds1307_Lib = nil
bcdToDec(tonumber(string.byte(c, 6))),
package.loaded["ds1307_Lib"]=nil
bcdToDec(tonumber(string.byte(c, 7)))
-- Example ------------------------------------------------------------
end
require("ds1307_Lib")
function M.configurer_heure(seconde, minute, heure, jour, date, mois, annee)
ds1307_Lib.init(3, 4, 0x68)
i2c.start(id)
second, minute, hour, day, date, month, year = ds1307_Lib.obtenir_heure();
i2c.address(id, device, i2c.TRANSMITTER)
print(string.format("Date and Time : %s:%2s:%2s %2s/%2s/%s", hour, minute, second, date, month, year))
i2c.write(id, 0x00)
i2c.write(id, decToBcd(seconde))
i2c.write(id, decToBcd(minute))
ds1307_Lib = nil
i2c.write(id, decToBcd(heure))
package.loaded["ds1307_Lib"]=nil
i2c.write(id, decToBcd(jour))
i2c.write(id, decToBcd(date))
i2c.write(id, decToBcd(mois))
i2c.write(id, decToBcd(annee))
i2c.stop(id)
end
return M