In Lua on the ESP8266 NodeMCU module, you can use the tmr
module to work with a timer. Here’s a basic guide on using the ESP8266 NodeMCU Module – Timer. The tmr Module provides 7 (0-6) static timer functions of the timer module. we can’t use more than 7. Also, you can create an object-based timer function with a custom name.
we can use this timer function in the following applications
- To blink an LED for a certain time duration its like a repeated process.
- To monitor the WiFi Status of ESP8266 IP address.
Contents
Functions of Timer Module
Function Name | Description |
---|---|
tmr.alarm() | This is a convenience function combining tmr. |
tmr.create() | Creates a dynamic timer object. |
tmr.delay() | Busyloops the processor for a specified number of microseconds. |
tmr.interval() | Changes a registered timer’s expiry interval. |
tmr.now() | Returns the system counter, which counts in microseconds. |
tmr.register() | Configures a timer and registers the callback function to call on expiry. |
tmr.resume() | Resume an individual timer. |
tmr.resume_all() | Resume all timers. |
tmr.softwd() | Provides a simple software watchdog, which needs to be re |
tmr.start() | Starts or restarts a previously configured timer. |
tmr.state() | Checks the state of a timer. |
tmr.stop() | Stops a running timer, but does not unregister it. |
tmr.suspend() | Suspend an armed timer. |
tmr.suspend_all() | Suspend all currently armed timers. |
tmr.time() | Returns the system uptime, in seconds. |
tmr.unregister() | Stops the timer (if running) and unregisters the associated callback. |
tmr.wdclr() | Feed the system watchdog. |
Print the Hello word Every 2 Sec Once
Pr-Request
The Following Lua Predefined Functions are required for this example
- print()
- NodeMCU Timer Module function: tmr.alarm()
Note: By Default, NodeMCU Firmware should contain tmr modules.
Required
- Any ESP8266 Module – 1Nos
- ESPlorer IDE
Basic Connection Diagram
Note: this below diagram for flash nodeMCU Firmware to ESP8266. We use the same circuit. But GPIO-0 Should be High or float Mode
Code
- Source Code in Github
--www.ArunEworld.com/embedded/esp8266/esp8266-nodemcu/ --Required NodeMCU Firmware Modules require("tmr") --Timer parameters Set TIMER_ID = 0 TIMER_DELAY_DURATION = 2000 --2second TIMER_MODE = 1 --Repeatably --timer alarm function tmr.alarm(TIMER_ID, TIMER_DELAY_DURATION, TIMER_MODE, function() print("Hello World") end)
Code Explanation
- First checking the required NodeMCU Lua modules tmr are present in ESP8266. if not present the required function will give an error, it does not go next line.
- Set the Timer parameters like id, delay duration, and mode in TIMER_ID , TIMER_DELAY_DURATION , TIMER_MODE variables.
- Then execute the timer-0 with 2sec delay duration in repeat mode(No need to call the tmr.start() function). Every one second once the timer call function call the used defined lua
- Timer call back function prints Hello World using print().
WiFi IP address status monitor
Aim
To continuously monitor whether ESP8266 gets IP address from the router in station mode using timer function with 1000ms delay. If ESP8266 got IP Address from the router, then ESP8266 timer stops.
Pr-Request
- Embedded Applications – Timer
- Lua Predefined Function
- required()
- print()
- local function_name() (user defined)
- Lua Loop functions
- if , else
- NodeMCU Timer Module function
- tmr.alarm()
- tmr.stop()
- NodeMCU WiFi Modules functions
- wifi.sta.config()
Note : NodeMCU Firmware should be contains tmr and wifi modules.
Required
- Any ESP8266 Module – 1Nos
- ESPlorer IDE
Connection Diagram
Note: this below diagram for flash nodeMCU Firmware to ESP8266. We use the same circuit. But GPIO-0 Should be High or float Mode
Flow Chart
Code
- Source Code GitHub
--www.ArunEworld.com/embedded/esp8266/esp8266-nodemcu/ --Required NodeMCU Firmware Modules require("tmr") require("wifi") --WiFi Credentials Set SSID = "ArunEwold" PASSWORD = "8300026060" --Timer parameters Set TIMER_ID = 0 TIMER_DELAY_DURATION = 1000 --1second TIMER_MODE = 1 --Repeatably --WiFi Configuration wifi.sta.config(SSID, PASSWORD) --Local WiFi IP Address Monitor User Defined Function with No Arguments local function WiFi_IP_ADDRESS_STATUS_MONITOR() --Check the IP address i if loop using Wifi.sta.getip() function if wifi.sta.getip()==nil then print("connecting to "..SSID.." :- AP...") else print("Connected to "..SSID.." ip : "..wifi.sta.getip()) tmr.stop(0) -- Stop the timer end end --timer alarm function tmr.alarm(TIMER_ID, TIMER_DELAY_DURATION, TIMER_MODE, function() WiFi_IP_ADDRESS_STATUS_MONITOR() end)
Code Explanation
- First checking the required NodeMCU Lua modules tmr , wifi are present in ESP8266. if not present the required function will give error, its does not goes next line.
- Set the WiFi credentials in SSID and PASSWORD variable.
- Set the Timer parameters like id, delay duration, and mode in TIMER_ID , TIMER_DELAY_DURATION , TIMER_MODE variables.
- Configure the SSID and PASSWORD variable strings in NodeMCU WiFi Module function wifi.sta.config()
- Configuration once done, then execute the timer-0 with 1sec delay duration in repeat mode(No need to call the tmr.start() function). Every one second once the timer call function call the used defined lua function WiFi_IP_ADDRESS_STATUS_MONITOR() with no arguments .
- The User defined function WiFi_IP_ADDRESS_STATUS_MONITOR() is a local function with no arguments, its has one user-defined functions print() and two NodeMCU firmware wifi and tmr functions wifi.sta.getip() and tmr.stop() .
FAQ
What is WiFi Credentials
- WIFi User name and password like a parameters called WiFi Credentials