ESP8266 WiFi – RTC Tijdklok DS3231 (NodeMCU)
Op deze pagina vind je een voorbeeld om via de ESP-01 module een RTC tijdklok in te stellen en uit te lezen via de GPIO pinnen met de firmware NodeMCU.
Hardware
Een RTC tijdklok met batterij, de klok maakt gebruik van de DS1302 chip.
Informatie (ENG)
The DS1307 Serial Real-Time Clock is a low-power, full binary-coded decimal (BCD) clock/calendar plus 56 bytes of NV SRAM. Address and data are transferred serially via a 2-wire, bi-directional bus. The clock/calendar provides seconds, minutes, hours, day, date, month, and year information. The end of the month date is automatically adjusted for months with fewer than 31 days, including corrections for leap year. The clock operates in either the 24-hour or 12-hour format with AM/PM indicator. The DS1307 has a built-in power sense circuit that detects power failures and automatically switches to the battery supply.
Features
- Real-time clock (RTC) counts seconds,minutes, hours, date of the month, month, day of the week, and year with leap-year compensation valid up to 2100
- 56-byte, battery-backed, nonvolatile (NV) RAM for data storage
- Two-wire serial interface
- Programmable squarewave output signal
- Automatic power-fail detect and switch circuitry
- Consumes less than 500nA in battery backup mode with oscillator running
- Optional industrial temperature range:-40°C to +85°C
- Available in 8-pin DIP or SOIC Underwriters Laboratory (UL) recognized
Pinout
Pin (aansluitkant, batterij boven): | Functie: |
---|---|
01 | SQ (optionele temp sensor) |
02 | DS |
03 | SCL (serial clock) |
04 | SDA (serial data) |
05 | VCC (+5v) |
06 | GND |
07 | BATT (batterij) |
Wat heb je nodig?
1) ESPlorer IDE
Sluit de RTC aan volgens onderstaand schema:
Getest op firmware: nodemcu_float_0.9.6-dev_20150704.bin
Upload deze “library” code als ds3231.lua
Tip: Download de code sla het op als bestand, en gebruik de “Upload…” knop in ESPlorer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
-------------------------------------------------------------------------------- -- DS3231 I2C module for NODEMCU -- NODEMCU TEAM -- LICENCE: http://opensource.org/licenses/MIT -- Tobie Booth <tbooth@hindbra.in> -------------------------------------------------------------------------------- local moduleName = ... local M = {} _G[moduleName] = M -- Default value for i2c communication local id = 0 --device address local dev_addr = 0x68 local function decToBcd(val) return((((val/10) - ((val/10)%1)) *16) + (val%10)) end local function bcdToDec(val) return((((val/16) - ((val/16)%1)) *10) + (val%16)) end -- initialize i2c --parameters: --d: sda --l: scl function M.init(d, l) if (d ~= nil) and (l ~= nil) and (d >= 0) and (d <= 11) and (l >= 0) and ( l <= 11) and (d ~= l) then sda = d scl = l else print("iic config failed!") return nil end print("init done") i2c.setup(id, sda, scl, i2c.SLOW) end --get time from DS3231 function M.getTime() i2c.start(id) i2c.address(id, dev_addr, i2c.TRANSMITTER) i2c.write(id, 0x00) i2c.stop(id) i2c.start(id) i2c.address(id, dev_addr, 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 --set time for DS3231 function M.setTime(second, minute, hour, day, date, month, year) i2c.start(id) i2c.address(id, dev_addr, 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(day)) i2c.write(id, decToBcd(date)) i2c.write(id, decToBcd(month)) i2c.write(id, decToBcd(year)) i2c.stop(id) end return M |
Je kan nu bovenstaande bibliotheek aanroepen om de RTC in te stellen en uit te lezen:
1 2 |
ds3231 = require("ds3231") ds3231.init(3, 4) |
Datum en tijd instellen: ds3231_instellen.lua:
1 2 3 4 5 6 7 8 9 |
ds3231 = require("ds3231") ds3231.init(3, 4) -- Zet datum en tijd op 20 Maart 2016 21:30 ds3231.setTime(0, 21, 30, 00, 20, 3, 16); -- Maak resources vbrij na gebruik van de module. ds3231 = nil package.loaded["ds3231"] = nil |
Uitlezen van de RTC ds3231_read.lua:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
require("ds3231") -- ESP-01 GPIO Mapping gpio0, gpio2 = 3, 4 ds3231.init(gpio0, gpio2) second, minute, hour, day, date, month, year = ds3231.getTime(); -- Get current time print(string.format("Time & Date: %s:%s:%s %s/%s/%s", hour, minute, second, date, month, year)) -- Don't forget to release it after use ds3231 = nil package.loaded["ds3231"]=nil |
Console output:
Time & Date: 21:5:27 18/3/16
Uitlezen van de RTC ds3231_readcsv.lua:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
require("ds3231") -- ESP-01 GPIO Mapping gpio0, gpio2 = 3, 4 ds3231.init(gpio0, gpio2) second, minute, hour, day, date, month, year = ds3231.getTime(); -- Get current time print(string.format("Tijd en datum: %s;%s;%s;%s;%s;%s", hour, minute, second, date, month, year)) -- Don't forget to release it after use ds3231 = nil package.loaded["ds3231"]=nil |
Console output:
Tijd en datum: 21;8;42;18;3;16
Ps: is de datum of tijd niet te lezen vanuit de RTC (storing), dan krijg je dit terug:
Time & Date: 165:165:165 165/165/165
Bron:
nodemcu @ github.com