echo '' ;

ESP8266 NodeMCU Module – Timer

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.

Functions of Timer Module


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

--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

--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

NEXT

NodeMCU Get Start
NodeMCU Build Firmware
NodeMCU Flash Firmware
NodeMCU IDE
ESP8266 NodeMCU Modules
NodeMCU Module–Firmware Info
NodeMCU Module – GPIO
NodeMCU Module – Node
NodeMCU Module – WiFi
NodeMCU Module – Timer
NodeMCU Module – I2C
NodeMCU Module – File
NodeMCU Module – NET
NodeMCU Module – HTTP
NodeMCU Module – MQTT
ESP8266 NodeMCU Interface
NodeMCU Interface LED
NodeMCU Interface Button
NodeMCU Interface 7 Seg
NodeMCU Interface LCD
NodeMCU Interface ADC
NodeMCU Interface DHT11
NodeMCU Interface MCP23008
NodeMCU Interface MCP23017
NodeMCU Interface ADXL345
NodeMCU Interface DS18B20
ESP8266 NodeMCU Tutorials
NodeMCU Tutorials Google Time
NodeMCU Tutorials WebServer
ESP8266 NodeMCU Projects
Imperial March Ringtone Play
WiFi Router Status Indicator
ESP8266 Home Automation
Others
NodeMCU All Post
Sitemap

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading