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
  1. 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()

    i2c.start(id)
    i2c.address(id, device, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.stop(id)

    i2c.start(id)
    i2c.address(id, device, i2c.RECEIVER)
    local c = i2c.read(id, 7)
    i2c.stop(id)

    return bcdToDec(tonumber(string.byte(c, 1))),

    bcdToDec(tonumber(string.byte(c, 2))),
    bcdToDec(tonumber(string.byte(c, 3))),
    bcdToDec(tonumber(string.byte(c, 4))),
    bcdToDec(tonumber(string.byte(c, 5))),
    bcdToDec(tonumber(string.byte(c, 6))),
    bcdToDec(tonumber(string.byte(c, 7)))

    end

    function M.configurer_heure(seconde, minute, heure, jour, date, mois, annee)

    i2c.start(id)
    i2c.address(id, device, i2c.TRANSMITTER)
    i2c.write(id, 0x00)
    i2c.write(id, decToBcd(seconde))
    i2c.write(id, decToBcd(minute))
    i2c.write(id, decToBcd(heure))
    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


Examples

          This example is AT24C Library (Lua) -- AT24CXXX_Test.lua
  1. -- Configuration ------------------------------------------------

    require("ds1307_Lib")

    ds1307_Lib.init(3, 4, 0x68)

    seconde = 00
    minute = 27
    heure = 15
    jour = 5
    date = 06
    mois = 07
    annee =16

    ds1307_Lib.configurer_heure(seconde, minute, heure, jour, date, mois, annee)

    ds1307_Lib = nil
    package.loaded["ds1307_Lib"]=nil

    -- Example ------------------------------------------------------------
    require("ds1307_Lib")

    ds1307_Lib.init(3, 4, 0x68)

    second, minute, hour, day, date, month, year = ds1307_Lib.obtenir_heure();
    print(string.format("Date and Time : %s:%2s:%2s %2s/%2s/%s", hour, minute, second, date, month, year))

    ds1307_Lib = nil
    package.loaded["ds1307_Lib"]=nil