As you learned in the Xedge32 introduction, the Xedge32 development environment simplifies IoT projects by seamlessly connecting Lua to the ESP32's extensive features. By enabling all device-specific code to be written in the user-friendly Lua language, Xedge32 makes the development process simpler and faster. In this first tutorial, we'll walk you through a basic example that blinks an LED. We'll connect the LED to a GPIO pin and demonstrate how to control it using Lua.
For this tutorial, you only need an ESP32 development board (ideally the latest ESP32-S3), a breadboard, and an LED. A beginner-friendly breadboard allows you to create circuits without soldering, making it great for trying out ideas. Starting with an LED is an excellent choice for your first project, as it's easy to use and gives you quick visual feedback. While LEDs often come included in DIY electronics kits, you can also purchase them separately.
ESP32 Development Board:
Soldering Iron: Many ESP32 development boards come without pre-soldered pins.
Once you have all the components, you may set them up as shown in the diagram below. Although a resistor is included in the design below to protect the LED from too much current, we've often set up LEDs without one for simple examples.
The connectors in Figure 1 above show how to wire it all up. In practice, you can use Jumper Wires to connect everything easily. In our test bench, we simply inserted an LED into the breadboard, as shown in the image below:
For wiring the LED on a breadboard according to our bench setup, follow these steps:
Note: You do not need to use GPIO 9. You can use any GPIO pin. Just remember to adjust the example program below accordingly.
You only need a few lines of Lua code to blink an LED. Lua is an exceptionally intuitive language, making it easy even for beginners to pick up. If you need a deeper understanding beyond our brief explanation below, you can delve into our online Lua tutorial for more guidance.
local function blink() local pin = esp32.gpio(9,"OUT") while true do pin:value(false) -- LED On coroutine.yield(true) -- Sleep for one timer tick pin:value(true) -- LED Off coroutine.yield(true) -- Sleep end end timer=ba.timer(blink) -- Create timer timer:set(1000) -- Timer tick = one second
Here's a step-by-step breakdown of the above code:
The script defines a local function named
blink . This function is designated to handle the LED's blinking behavior.
Utilizing Coroutines for Timing:
blink function, an infinite loop (
while true do ) uses the Lua Coroutines concept for its timing mechanism. Specifically,
coroutine.yield(true) is employed to make the function "sleep" for a specified duration. In this context, it pauses the loop between LED state changes for one second.
LED State Control:
The loop inside the
blink function manages the LED's state. It first turns the LED on with
pin:value(true) , sleeps for a second, turns it off with
pin:value(false) , and then sleeps for another second. This on-off cycle continues indefinitely, creating the blink effect.
GPIO Port 4 Initialization:
Before the blinking starts, the GPIO port 4 is set up as an output using
esp32.gpio(4,"OUT") and is referenced by the
pin variable. If your LED is connected to a different GPIO port, you must modify this number.
Creating The Timer:
The code lines 10 and 11 outside the function create the timer and initializes the blinking pattern, setting the timer to trigger the
blink function every 1000 milliseconds (every second).
Disk and click
New Folder on the context menu.
blink as the new folder name and click Enter.
Disk , right-click the
blink directory, and click New App in the context menu.
Running button and click Save.
blink app now visible in the left pane tree view. The
blink app should be green, indicating the app is running.
blink app and click
New File on the context menu.
blinkled.xlua and click Enter.
blinkled.xlua file to open the file In Xedge.
Save & Run button to save and start the blink LED example.
The following video includes additional information on how to create Xedge applications:
Xedge32 is comprised of three components:
To summarize, the Barracuda App Server is the foundational layer, Xedge offers a versatile Lua development environment, and Xedge32 is the specialized version for the ESP32 platform.
In the next tutorial, we will explore how to use button click-generated interrupts to toggle the LED's on/off state as an alternative to using a timer for blinking the LED. This button and LED control mechanism is a component of an IoT tutorial. You may focus on this initial part if are not interested in IoT. Nevertheless, it's worth mentioning that Xedge32 is specifically tailored for developing advanced IoT solutions.
Your time is valuable. Let our expert consulting services streamline your networking, security, and device management tasks. And for those moments of DIY inspiration, our tutorials offer unmatched guidance. Partner with Real Time Logic and witness the seamless blend of expertise and empowerment.
Expedite your IoT and edge computing development with the "Barracuda App Server Network Library", a compact client/server multi-protocol stack and IoT toolkit with an efficient integrated scripting engine. Includes Industrial Protocols, MQTT client, SMQ broker, WebSocket client & server, REST, AJAX, XML, and more. The Barracuda App Server is a programmable, secure, and intelligent IoT toolkit that fits a wide range of hardware options.
SharkSSL is the smallest, fastest, and best performing embedded TLS stack with optimized ciphers made by Real Time Logic. SharkSSL includes many secure IoT protocols.
SMQ lets developers quickly and inexpensively deliver world-class management functionality for their products. SMQ is an enterprise ready IoT protocol that enables easier control and management of products on a massive scale.
SharkMQTT is a super small secure MQTT client with integrated TLS stack. SharkMQTT easily fits in tiny microcontrollers.
An easy to use OPC UA stack that enables bridging of OPC-UA enabled industrial products with cloud services, IT, and HTML5 user interfaces.
Use our user programmable Edge-Controller as a tool to accelerate development of the next generation industrial edge products and to facilitate rapid IoT and IIoT development.
Learn how to use the Barracuda App Server as your On-Premises IoT Foundation.
The compact Web Server C library is included in the Barracuda App Server protocol suite but can also be used standalone.
The tiny Minnow Server enables modern web server user interfaces to be used as the graphical front end for tiny microcontrollers. Make sure to check out the reference design and the Minnow Server design guide.
Why use FTP when you can use your device as a secure network drive.
PikeHTTP is a compact and secure HTTP client C library that greatly simplifies the design of HTTP/REST style apps in C or C++.
The embedded WebSocket C library lets developers design tiny and secure IoT applications based on the WebSocket protocol.
Send alarms and other notifications from any microcontroller powered product.
The RayCrypto engine is an extremely small and fast embedded crypto library designed specifically for embedded resource-constrained devices.
Real Time Logic's SharkTrust™ service is an automatic Public Key Infrastructure (PKI) solution for products containing an Embedded Web Server.
The Modbus client enables bridging of Modbus enabled industrial products with modern IoT devices and HTML5 powered HMIs.