echo '' ;

ESP32 NodeMCU Module – Basic UART, Timer

ESP8266 and ESP32 NodeMCU Lua Module functions are different. So we can’t use ESP8266 fuctions directly in ESP32 Check the Official Documentation of NodeMCU Lua for ESP32

Hello World :

Connecting to ESP8266 and ESPlorer IDE

Set Baud rate: 115200.

=print("ArunEworld")
ArunEworld
>

UART Write

uart.write(0, "Hello, world\n")

 


Timer Module

Feature

  • Create timer alarm based objects,
  • Register and unregistered any timer.
  • Check the state (Status) of the timer while running,
  • Can change the timer interval while running.

Not have the following Features

  • Timer Interval functions,
  • Watch dog Timer functions.

Functions

  • tmr.create()                 – Creates a dynamic timer object.
  • tmr.obj:alarm()            – This is a convenience function combining tmr.
  • tmr.obj:interval()      – Changes a registered timer’s expiry interval.
  • tmr.obj:register()      – Configures a timer and registers the callback function to call on expiry.
  • tmr.obj:start()           – Starts or restarts a previously configured timer.
  • tmr.obj:state()           – Checks the state of a timer.
  • tmr.obj:stop()             – Stops a running timer, but does not unregister it.
  • tmr.obj:unregister()  – Stops the timer (if running) and unregisters the associated callback.

Ex : Create a timer alarm for every 2 second

Create a alarm for every 2 seconds once print some strings like “www.ArunEworld.com” and automatically repeat.

  • Create a timer alarm object  name “ArunEworld_timer” using tmr.create() function.
  • Configures a timer and registers the callback function to call on expiry. using tmr.obj:register()  function.
  • Starts or restarts a previously configured timer using tmr.obj:start() function.

Code

ArunEworld_timer = tmr.create()
ArunEworld_timer:register(2000, tmr.ALARM_AUTO, function() print("www.ArunEworld.com") end)
ArunEworld_timer:start()

Result

> www.ArunEworld.com --Every 2 Sec
www.ArunEworld.com  --Every 2 Sec
www.ArunEworld.com --Every 2 Sec
www.ArunEworld.com --Every 2 Sec

Ex : Change the timer interval time

  • You can change the timer interval for running timer alarm using the function tmr.obj:interval()
  • The above example code sets 2 seconds now change to 3 Seconds Ex : ArunEworld_timer.interval(3000)
  • This functions is very use full for running time we can change the timer interval value directly.

Ex : Check the Timer Current state

  • Check the current state of the timer..(timer may running or stop.)
  • Checks the state of a timer using function  tmr.obj:state()

Code

ArunEworld_timer = tmr.create()
print(ArunEworld_timer:state()) -- nil
ArunEworld_timer:register(1000, tmr.tmr.ALARM_AUTO, function() print("www.facebook.com/ArunEworld") end)
running, tmr_mode = ArunEworld_timer:state()
print(running)
print(tmr_mode)

Result

>running, tmr_mode = ArunEworld_timer:state() print(running) print(tmr_mode)
true
1
> www.facebook.com/ArunEworld

 


Node Module

  • node.bootreason()     – Returns the boot reason and extended reset info.
  • node.chipid()            – Returns the ESP chip ID.
  • node.compile()          – Compiles a Lua text file into Lua bytecode, and saves it as .
  • node.dsleep()            – Enters deep sleep mode, wakes up when timed out.
  • node.flashid()          – Returns the flash chip ID.
  • node.heap()               – Returns the current available heap size in bytes.
  • node.info()               – Returns NodeMCU version, chipid, flashid, flash size, flash mode, flash speed.
  • node.input()             – Submits a string to the Lua interpreter.
  • node.key()                –deprecated Defines action to take on button press (on the old devkit 0.
  • node.led()                 –deprecated Sets the on/off time for the LED (on the old devkit 0.
  • node.output()            – Redirects the Lua interpreter output to a callback function.
  • node.readvdd33()      –deprecated Moved to adc.
  • node.restart()          – Restarts the chip.
  • node.restore()          – Restores system configuration to defaults using the SDK function system_restore(), which doesn’t document precisely what it erases/restores.
  • node.setcpufreq()    – Change the working CPU Frequency.
  • node.stripdebug()    – Controls the amount of debug information kept during node.
  • node.osprint()         – Controls whether the debugging output from the Espressif SDK is printed.
  • node.egc.setmode() – Sets the Emergency Garbage Collector mode.
  • node.task.post()     – Enable a Lua callback or task to post another task request.

Ex : node.heap()

> =node.heap()
241320
>

Ex : node.chipid()

> =node.chipid()
0x5630aea40255
>


GPIO Module

Functions

  • gpio.config()  Configure GPIO mode for one or more pins.
  • gpio.read()  Read digital GPIO pin value.
  • gpio.trig()  Establish or clear a callback function to run on interrupt for a GPIO.
  • gpio.wakeup()  Configuring wake-from-sleep-on-GPIO-level.
  • gpio.write()  Set digital GPIO pin value.

Ex : Set pin as Input, Output and Read

  • GPIO-23 as LED output and GPIO-0 as Button input
  • But this example used while loop in Lua.
  • Set the GPIO-23 pin as output and GPIO-0 pins input (GPIO-0 usually connected to flash button in dev board) using function gpio.config()
  • check the GPIO-0 pin stat using function gpio.read()  and set and clear the pin GPIO-23 using function gpio.write()

Code

gpio.config( { gpio=23, dir=gpio.OUT }, { gpio=0, dir=gpio.IN})
while(true)
do
   if (gpio.read(0)==0) then gpio.write(23, 1)
   else gpio.write(23, 0)
   end
end

result

 

Ex : GPIO pin interrupt based print message serial

  • Set GPIO-0 pins as interrupt using function gpio.trig()
  • if interrupt rise call the another function and print ArunEworld  in serial

Code

gpio.trig(0, gpio.INTR_UP_DOWN,pin_interrupt_1)
function pin_interrupt_1(level) print("ArunEworld") end

 


 


Next :

Previous :


 

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