Control your Raspberry Pi with Lua
Lua is a language that is sometimes misunderstood. Unlike Python and some other programming languages, this language is a general extension language and is widely used in game engines, frameworks, etc. Overall, I find Lua to be a valuable tool for developers, allowing them to enhance and extend their projects in some powerful ways.
You can follow Seth Kenlon's article "Is Lua worth learning? 》Introducing downloading and running commonly used Lua, the article also includes simple Lua code examples. However, to get the most out of Lua, it's best to use it with a framework that adopts the language. In this tutorial, I demonstrate how to use a framework called Mako Server, which is designed to make it easy for Lua programmers to code IoT and web applications. I also showed you how to extend this framework using the API to use the Raspberry Pi's GPIO pins.
Requirements
Before following this tutorial, you will need a running Raspberry Pi that you can log into. Although I will be compiling C code in this tutorial, you don't need any experience with C coding. However, you will need some experience using POSIX terminals.
Installation
First, open a terminal window on the Raspberry Pi and install the following tools to download code and compile C code using Git:
$ sudo apt install git unzip gcc make
Next, pass Run the following commands to compile the open source Mako Server code and the lua-periphery library (the Raspberry Pi's GPIO library):
$ wget -O Mako-Server-Build.sh \https://raw.githubusercontent.com/RealTimeLogic/BAS/main/RaspberryPiBuild.sh
Check out the script to see what it does, and run it when you feel it's OK:
$ sh ./Mako-Server-Build.sh
The compilation process may take some time, especially on older Raspberry Pis. After compilation is complete, the script will ask you to install the Mako Server and lua-periphery modules to /usr/local/bin/
. I recommend installing it to simplify using the software. Don't worry, you can uninstall it if you no longer need it:
$ cd /usr/local/bin/$ sudo rm mako mako.zip periphery.so
To test the installation, enter mako
in the terminal. This will start the Mako server and see some output in your terminal. You can stop the server by pressing CTRL C
.
IoT and Lua
Now that the Mako server is set up on your Raspberry Pi, you can start programming IoT and web applications and use Lua to operate the Raspberry Pi's GPIO headers. foot. The Mako Server framework provides Lua developers with a powerful yet simple API to create IoT applications, while the lua-periphery module allows Lua developers to interact with the Raspberry Pi’s GPIO pins and other peripherals.
First create an application directory and a .preload
script, in which the Lua code for testing GPIO is inserted. .preload
The script is a Mako server extension that loads and runs as a Lua script when the application starts.
$ mkdir gpiotst$ nano gpiotst/.preload
Copy the following content into Nano Editor and save the file:
-- Load periphery.so and access the LED interfacelocal LED = require('periphery').LEDlocal function doled()local led = LED("led0") -- Open LED led0trace"Turn LED on"led:write(true) -- Turn on LED (set max brightness)ba.sleep(3000)-- 3 secondstrace"Turn LED off"led:write(false)-- Turn off LED (set zero brightness)led:close()endba.thread.run(doled) -- Defer execution -- to after Mako has started
上面的 Lua 代码使用你编译并包含在 Mako 服务器中的 Lua-periphery 库控制树莓派 LED。该脚本定义了一个名为 doled
的函数来控制 LED。该脚本首先使用 Lua require
函数加载 periphery
库(共享库 periphery.so)。返回的数据是一个包含所有 GPIO API 函数的 Lua 表。但是,你只需要 LED API,你可以通过在调用 require
后附加 .LED
来直接访问它。接下来,代码定义了一个名为 doled
的函数,它执行以下操作:
- 通过调用
periphery
库中的LED
函数,并将字符串led0
传给它,打开树莓派主 LED,识别为led0
。 - 将消息
Turn LED on
打印到跟踪(控制台)。 - 通过调用 LED 对象上的
write
方法并将布尔值true
传递给它来激活 LED,该值设置 LED 的最大亮度。 - 通过调用
ba.sleep(3000)
等待 3 秒。 - 将消息
Turn LED off
打印到跟踪。 - 通过调用 LED 对象上的
write
方法并将布尔值false
传递给它来停用 LED,这会将 LED 的亮度设置为零。 - 通过调用 LED 对象上的
close
函数关闭LED
。
在 .preload
脚本的末尾,doled
函数作为参数传递给 ba.thread.run
函数。这允许将 doled
函数的执行推迟到 Mako 服务器启动之后。
要启动 gpiotst
应用,请按如下方式运行 Mako 服务器:
$ mako -l::gpiotst
控制台中打印以下文本:
Opening LED:opening 'brightness': Permission denied.
访问 GPIO 需要 root 访问权限,因此按 CTRL+C
停止服务器并重新启动 Mako 服务器,如下所示:
$ sudo mako -l::gpiotst
现在树莓派 LED 亮起 3 秒。成功!
Lua 解锁 IoT
在本入门教程中,你学习了如何编译 Mako 服务器,包括 GPIO Lua 模块,以及如何编写用于打开和关闭树莓派 LED 的基本 Lua 脚本。在以后的文章中,我将在本文的基础上进一步介绍 IoT 功能。
Meanwhile, you can delve deeper into the Lua-periphery GPIO library by reading its Documentation to learn more about the functionality and how to use it with different peripherals. To get the most out of this tutorial, consider following the Interactive Mako Server Lua Tutorial to better understand Lua, the Web, and IoT. Happy coding!
The above is the detailed content of Control your Raspberry Pi with Lua. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Nginx installation and configuration Lua support By default, Nginx does not support Lua modules. You need to install the LuaJIT interpreter and recompile Nginx, or you can use the modules required by openrestry developed by Chinese people: LuaJIT, Ngx_devel and lua-nginx-module1. Environment preparation [root @nginx_lua~]#yuminstall-ygccgcc-c++makepcre-develzlib-developenssl-devel2. Download the latest luajit and ngx_devel_kit and lua-nginx-module decompression [r

The implementation idea is to directly configure the blacklist in nginx and implement it by writing logical blocks; write the filter in the server (Java) and unify the interceptors in the filter; write the interceptor in the server (Java) and unify the interceptors Interception; here are 3 implementation ideas. As for implementation solutions, there may be more, but if we think about it, writing logical blocks in nginx does not seem to be what many people are good at; it is not impossible to do it at the code level, but this is First, during peak business periods involving high concurrency, this will inevitably put greater pressure on back-end services. So are there any other better ways to deal with it? This is what lua is about, that is, nginx acts as a gateway and still acts as a proxy server, because nginx can integrate lua

Recently, I heard someone on a conference call say that the open source community is a combination of curiosity and a culture of problem-solving. Curiosity is the basis of our problem solving. When solving problems of all sizes, we use a wide range of open source tools, including Linux running on the incredibly convenient Raspberry Pi. We all have different life experiences, so I asked our community of authors for the weirdest uses for a Raspberry Pi they've come across. I have a feeling these amazing builds will inspire others. Experimenting with a Raspberry Pi For me, the Raspberry Pi is a great tool for adding extra development resources to my home network. If I want to create a new website or try a new software tool, I don't have to let my desktop Lin

The integration of Vue.js and Lua language to write lightweight embedded applications. In modern development, the front-end framework Vue.js and the scripting language Lua each have a wide range of applications. Vue.js is a progressive framework for building user interfaces, while Lua is a lightweight scripting language often used for embedded application and game development. This article will introduce how to integrate Vue.js with Lua language to write lightweight embedded applications, and provide code examples. First, we need to install Vue.j

If you want to know how your Raspberry Pi is performing, then you probably need a Raspberry Pi dashboard. In this article, I'll demonstrate how to quickly build an on-demand monitoring dashboard to view your Raspberry Pi's CPU performance, memory, and disk usage in real time, and add more views and actions at any time as needed. If you already use Appsmith, you can also import the sample application directly and get started. Appsmith Appsmith is an open source low-code application building tool that helps developers easily and quickly build internal applications such as dashboards and admin panels. It is a great option for dashboards and reduces the time and complexity required by traditional coding methods. In this example dashboard, I display the following statistics

[Implementation process] 1. Problem analysis If the set command is set, but when setting the expiration time, the setting is not successful due to network jitter and other reasons, then a dead counter (similar to a deadlock) will appear; 2. Solution Redis+Lua It is a good solution. Use a script to make the set command and the expire command work together so that Redis is executed without being interfered with. This guarantees atomic operations to a large extent. Why is it said that it guarantees atomic operations to a large extent rather than completely? ensure? Because problems may occur when Redis is executed internally, but the probability is very small; even for small-probability events, there are corresponding solutions, such as solving deadlocks. An idea worth referring to: To prevent deadlocks, the value of the lock will be stored in

The integration of Vue.js and Lua language, best practices and experience sharing for building a front-end engine for game development Introduction: With the continuous development of game development, the choice of game front-end engine has become an important decision. Among these choices, the Vue.js framework and Lua language have become the focus of many developers. As a popular front-end framework, Vue.js has a rich ecosystem and convenient development methods, while the Lua language is widely used in game development because of its lightweight and efficient performance. This article will explore how to

1. Install LUA Installing LUA on Mac is very simple. Just use brew related commands directly; brewinstalllua uses the lua-v command to see that lua has been installed. 1) Simple use to create a test.lua file, the content is: Execute command: luatest.lua output is: 2. Introduction to lua syntax Lua provides interactive programming and scripted programming: Interactive programming: directly enter the syntax on the command line , you can execute it immediately and see the execution effect. Scripting is programming: write a script file and then execute it. 1. Comment Lua provides two comment methods: single-line comments and multi-line comments 1) Single-line comments use two minus signs;--2) Multi-line comments--[[Multi-line comments multi-line
