echo '' ;

ESP32 Lua-RTOS Modules- Basic Examples

In this “ESP32 Lua-RTOS Modules- Basic Examples” article we will explore the PIO Module (GPIO) and Interface LED as well as Buzzer, Wifi, MQTT, EEPROM I2C Code.

eLua
RTOS eLua

PIO Module(GPIO Module)

This section will interface with LED as well as Buzzer using ESP32 Lua RTOS.

LED Blink Using while function with Delay

pio.pin.setdir(pio.OUTPUT, pio.GPIO23)
pio.pin.setpull(pio.NOPULL, pio.GPIO23)

while true do
	pio.pin.setval(1, pio.GPIO23)
	tmr.delayms(1000)
	pio.pin.setval(0, pio.GPIO23)
	tmr.delayms(1000)
end

LED Blink Using while function and thread function with Delay

thread.start(function()
	pio.pin.setdir(pio.OUTPUT, pio.GPIO23)
	pio.pin.setpull(pio.NOPULL, pio.GPIO23)
	
	while true do
		pio.pin.setval(1, pio.GPIO23)
		tmr.delayms(1000)
		pio.pin.setval(0, pio.GPIO23)
		tmr.delayms(1000)
	end
end)

Toggle the LED every 1 seconds

In Lua RTOS, the GPIO module is used for interacting with General Purpose Input/Output pins on microcontrollers like the ESP32. GPIO stands for General Purpose Input/Output, and it allows you to control and read the state of digital pins on the microcontroller.

This example initializes a GPIO pin as an output and toggles its state every second using a timer. Please replace ledPin with the actual GPIO pin number where your LED is connected.

-- GPIO Example for Lua RTOS on ESP32

-- Pin configuration
local ledPin = 2 -- Replace with the actual GPIO pin number where your LED is connected

-- Initialize GPIO
gpio.mode(ledPin, gpio.OUTPUT)

-- Function to toggle the LED
function toggleLED()
    local currentState = gpio.read(ledPin)
    if currentState == gpio.HIGH then
        gpio.write(ledPin, gpio.LOW)
    else
        gpio.write(ledPin, gpio.HIGH)
    end
end

-- Set up a timer to toggle the LED every 1000 milliseconds (1 second)
tmr.create():alarm(1000, tmr.ALARM_AUTO, toggleLED)

-- Enter the Lua RTOS event loop
os.run()

WiFi in ESP32 Lua RTOS

Connecting to Wi-Fi using ESP32

-- Configure wifi in Station Mode with SSID from Citilab
net.wf.setup(net.wf.mode.STA, "ArunEworld","Arun")

ESP32 Lua RTOS: Start Wi-Fi

-- Start wifi
net.wf.start()

MQTT using Lua RTOS

To use MQTT (Message Queuing Telemetry Transport) in Lua RTOS, you typically need to include an MQTT library and set up the necessary configurations. Here’s a basic example of using the mqtt module in Lua RTOS to publish and subscribe to MQTT messages. Please note that the example assumes you have already set up a broker (e.g., Mosquitto) for testing purposes.

Connect to the MQTT server from ArunEworld.com

-- Connect to MQTT server from ArunEworld.com
client = mqtt.client("100", "mqtt.aruneworld.com", 1883, false)
client:connect("ArunEworld","Arun")

Publish to MQTT Server

-- Publis to queue
client:publish("/100","Welcome to ArunEworld",mqtt.QOS0)

Publish and Subscribe to MQTT

Replace the placeholder values (e.g., "Mqtt.ArunEworld.com", "ArunEworld-WiFi", "your_wifi_password") with your actual MQTT broker and Wi-Fi details.

-- MQTT Example for Lua RTOS on ESP32

-- Load necessary modules
local mqtt = require "mqtt"

-- MQTT broker details
local brokerHost = "Mqtt.ArunEworld.com"
local brokerPort = 1883
local clientId = "esp32_client"
local topic = "esp32_topic"

-- WiFi details
local ssid = "ArunEworld-WiFi"
local password = "your_wifi_password"

-- Connect to WiFi
wifi.setmode(wifi.STATION)
wifi.sta.config({ ssid = ssid, pwd = password })

-- Function to handle incoming messages
function handleMessage(topic, message)
    print("Received message on topic '" .. topic .. "': " .. message)
end

-- Connect to MQTT broker
mqtt.client(brokerHost, brokerPort, clientId)
    :connect(function(client)
        print("Connected to MQTT broker")

        -- Subscribe to a topic
        client:subscribe(topic, function(client)
            print("Subscribed to topic: " .. topic)
        end)

        -- Publish a message every 5 seconds
        tmr.create():alarm(5000, tmr.ALARM_AUTO, function()
            client:publish(topic, "Hello from ESP32")
        end)
    end)
    :on("message", handleMessage)

This example connects to a Wi-Fi network, establishes a connection to an MQTT broker, subscribes to a topic, and publishes a message every 5 seconds. Adjust the code according to your specific requirements and ensure you have the required Lua RTOS modules installed.


EEPROM I2C Code

-- www.ArunEworld.com/embedded/espresif/esp32/lua_rtos
-- ----------------------------------------------------------------
-- WHITECAT ECOSYSTEM
--
-- Lua RTOS examples
-- ----------------------------------------------------------------
-- This sample demonstrates how to communicate with i2c devices.
-- The sample writes values into an EEPROM (4c256d) and then
-- read from the EEPROM and test that readed values are the writed
-- values. 
-- ----------------------------------------------------------------

-- Attach the eeprom to the i2c bus
function eeprom_attach()
	eeprom = i2c.attach(i2c.I2C0, i2c.MASTER, 400000)
end

-- Once the internally-timed write cycle has started and the EEPROM
-- inputs are disabled, acknowledge polling can be initiated.
-- This involves sending a start condition followed by the device address word.
-- If this fails (timeout, or ack not received) we can't write.
function eeprom_poll(devAddress) 
	local done = false
	
	while not done do
		try(
		  function() 
	  		eeprom:start()
			eeprom:address(devAddress, false)
	  		eeprom:stop()
			done = true
		  end
		)
	end
end

-- Write to eeprom (byte)
function eeprom_write(devAddress, address, value)
	eeprom:start()
	eeprom:address(devAddress, false)
	eeprom:write(0x00, i, i)
	eeprom:stop()

	eeprom_poll(devAddress)
end

-- Read from eeprom (byte)
function eeprom_read(devAddress, address)
	local read

	eeprom:start()
	eeprom:address(devAddress, false)
	eeprom:write(0x00, address)
	eeprom:start()
	eeprom:address(devAddress, true)

	read = eeprom:read()
	
	eeprom:stop()

	return read
end

-- Attach eeprom
eeprom_attach()

-- Write test bytes
for i=0,100 do
	eeprom_write(0x51, i, i)
end

-- Check bytes
for i=0,100 do
	if (eeprom_read(0x51, i) ~= i) then
		error("Readed "..read..", expected "..i)
	end
end

NEXT

ESP32 Lua-RTOS IDE Setup
ESP32 Lua-RTOS Modules
ESP32 Lua-RTOP Module PIO
ESP32 Lua-RTOS Basic Ex
Others
ESP32 Lua RTOS Sitemap
ESP32 Lua-RTOS All Post

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