Lua Module at
The at module allows interaction with GSM/GPRS/3G/4G modules using AT commands. It facilitates communication with these modules for sending and receiving data.
To import it, use the following declaration:
Configuration
The at module requires specific configuration to function. This configuration is defined in the agent's configuration file.
Here is an example with all possible options:
iot:
modules:
at:
- name: at1 # module name
port: /dev/ttyUSB0 # serial port
baud: 5 # timeout
read_timeout: 5 # timeout
Configuration Details
- name: The name of the module. It must be unique and should not contain spaces or special characters.
- port: The serial port to use. It must be in the format
/dev/ttyUSB0orCOM3(for Windows). - baud: Transmission speed (baud rate). Defaults to
9600(e.g.,9600,115200). - read_timeout: Timeout for reading data. Defaults to
5seconds.
Lua Functions
at.send(cmd)
Sends an AT command to the configured module.
- Parameters:
-
cmd(string): The AT command to send. -
Return:
"OK"if the command was successfully sent.- Otherwise, returns
niland an error message:write error: <error>.
local at = require("at1")
local status, err = at.send("AT+CSQ")
if not status then
print("Error sending AT command: " .. err)
else
print("AT command sent successfully.")
end
at.query(cmd [, terminator])
Sends an AT command to the configured module and waits for a response.
- Parameters:
cmd(string): The AT command to send.-
terminator(string, optional): The response terminator character. Defaults to\r\n. -
Return:
- The response from the module as a string.
- On error, returns
nilfollowed by an error message.
local at = require("at1")
local response, err = at.query("AT+CSQ")
if err then
print("Error during AT query: " .. err)
else
print("AT response: " .. response)
end
Associated Lua Fields
at.name: Returns the instance name of theatmodule.at.type: Returns the module type (here,at1).