Home >Operation and Maintenance >Nginx >Control your Raspberry Pi with Lua

Control your Raspberry Pi with Lua

王林
王林forward
2023-06-10 09:07:491088browse

用 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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete