Your First Xedge32 Project

An Introduction to Device Management with Lua

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.

Don't have an ESP32? No worries! You can still explore and run the provided Lua code on your Windows or Linux machine by using an ESP32 simulator. Check out the section titled Running the Lua Examples on Windows and Linux for complete details on how to set this up.

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.

Required Hardware Components:

  1. ESP32 Development Board:

    • Preferred Model: ESP32-S3.

    • Ensure the model number includes either 'N8R8' or 'N16R8'. This specifies the flash and RAM memory size in megabytes.

    • We recommend getting a breadboard-friendly (small) development board such as XIAO ESP32S3.

  2. Basic Starter Kit with Breadboard

    or

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

ESP32 LED Wiring

Figure 1: How to connect all components using a breadboard.

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:

XIAO ESP32S3 GPIO
ESP32 blink LED

Figure 2: Our test bench with the LED's positive pin connected to 3V3 and the LED's negative pin connected to GPIO 9.

For wiring the LED on a breadboard according to our bench setup, follow these steps:

  1. Connect the LED's positive pin (anode), usually identified by a rounded edge, to the 3.3-volt pin.
  2. Connect the LED's negative pin (cathode), typically marked by a flat edge, directly to GPIO pin 9.

Note: You do not need to use GPIO 9. You can use any GPIO pin. Just remember to adjust the example program below accordingly.

The Lua LED blink script:

If you're familiar with Arduino ESP32 tutorials, they usually start by setting up WiFi. However, with Xedge32, you've already accomplished this during the installation process, as detailed in the Xedge32 installation documentation. Therefore, WiFi configuration isn't needed for the examples in this tutorial. Think of Xedge32 more like an operating system that allows you to dynamically create, start, stop, and manage multiple apps concurrently.

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:

  • Function blink:
    The script defines a local function named blink . This function is designated to handle the LED's blinking behavior.

  • Utilizing Coroutines for Timing:
    Within the 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).

Lua LED blink script References:

How to create the Xedge blink LED app:

  1. Install the Xedge32 IDE.
  2. When the Xedge32-powered ESP is running, use a browser, navigate to the ESP32's IP address, and click the Xedge IDE link to start the Xedge IDE.
  3. Create a new Xedge app as follows:
    1. Right-click Disk and click New Folder on the context menu.
    2. Enter blink as the new folder name and click Enter.
    3. Expand Disk , right-click the blink directory, and click New App in the context menu.
    4. Click the Running button and click Save.
  4. Expand the blink app now visible in the left pane tree view. The blink app should be green, indicating the app is running.
  5. Right-click the blink app and click New File on the context menu.
  6. Type blinkled.xlua and click Enter.
  7. Click the new blinkled.xlua file to open the file In Xedge.
  8. Select all and delete the template code.
  9. Copy the above blink LED Lua code and paste the content into the Xedge editor.
  10. Click the Save & Run button to save and start the blink LED example.

The following video includes additional information on how to create Xedge applications:


Understanding the Xedge32 Ecosystem: How It Fits with Barracuda App Server and Xedge

Xedge32 is comprised of three components:

  • The Barracuda App Server serves as a comprehensive foundation for developing embedded systems and IoT applications, providing features like web server capabilities, data encryption, and multiple protocol support. It sets the stage for creating secure and scalable IoT solutions.
  • Xedge builds upon this foundation as a generic, non-ESP32-specific Lua development platform. It accelerates the development process by allowing rapid prototyping without requiring full compile and deploy cycles.
  • Finally, Xedge32 is explicitly tailored for the ESP32 microcontroller. It utilizes the Xedge framework, which in turn is built upon the Barracuda App Server. By leveraging the Lua programming language, Xedge32 enables easy access to ESP32's hardware capabilities, such as its GPIOs, and provides a smooth development experience for both professionals and hobbyists. This makes it particularly useful for ESP32 IoT projects that integrate with systems like Home Assistant, Azure, and Amazon.

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.

Xedge32 Documentation:

Note: The generic Barracuda App Server and the Lua documentation include C APIs, which you do not need to pay attention to unless you plan on expanding on the Xedge32 C code project, which can be found on GitHub. For most use cases, Xedge32 can be used "as is" using stock firmware.

Next: Controlling an LED Using a Button

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 of the tutorial if you are not interested in IoT. Nevertheless, it's worth mentioning that Xedge32 is specifically tailored for developing advanced IoT solutions.


Comments:

We welcome your thoughts and discussions on this article. While our site does not host a comment section, please feel free to comment on the following LinkedIn article:

Low-Code and Easy IoT Programming Using Lua.


Hitting Roadblocks? We're Your Way Forward!

Our extensive tutorials on embedded web servers and IoT are your roadmap to success. But every journey can have its challenges. Our seasoned experts are here to pave the way if you're pressed for time or hit a roadblock. At Real Time Logic, we equip you with knowledge and offer a helping hand when you need it most. Together, let's achieve the extraordinary!



OPC-UA

OPC-UA Client & Server

An easy to use OPC UA stack that enables bridging of OPC-UA enabled industrial products with cloud services, IT, and HTML5 user interfaces.

Edge Controller

Edge Controller

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.

On-Premises IoT

On-Premises IoT Platform

Learn how to use the Barracuda App Server as your On-Premises IoT Foundation.

Embedded Web Server

Barracuda Embedded Web Server

The compact Web Server C library is included in the Barracuda App Server protocol suite but can also be used standalone.

WebSocket Server

Microcontroller Friendly

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.

WebDAV Server

Network File System

Why use FTP when you can use your device as a secure network drive.

HTTP Client

Secure HTTP Client Library

PikeHTTP is a compact and secure HTTP client C library that greatly simplifies the design of HTTP/REST style apps in C or C++.

WebSocket Client

Microcontroller Friendly

The embedded WebSocket C library lets developers design tiny and secure IoT applications based on the WebSocket protocol.

SMTP Client

Secure Embedded SMTP Library

Send alarms and other notifications from any microcontroller powered product.

Crypto Library

RayCrypto C Library

The RayCrypto engine is an extremely small and fast embedded crypto library designed specifically for embedded resource-constrained devices.

Embedded PKI Service

Automatic SSL Certificate Management for Devices

Real Time Logic's SharkTrust™ service is an automatic Public Key Infrastructure (PKI) solution for products containing an Embedded Web Server.

Modbus

Modbus TCP client

The Modbus client enables bridging of Modbus enabled industrial products with modern IoT devices and HTML5 powered HMIs.

Posted in Xedge32