4G通信
概述
YFAir8000 通过 Air8000W 模组内置的 LTE Cat.1 芯片实现 4G 通信。本例程演示通过 NTP 校时 验证 4G 网络可达性,并将 NTP 模块封装为可复用组件。
程序流程
Main()
│
├─ 1. wdt.init(9000) + feed(3000) → 看门狗
│
├─ 2. ntp:init() → 等待 4G 网络注册 + IP 就绪
│
├─ 3. ntp:sync(10000) → 尝试 NTP 同步,10 秒超时
│ └─ 从9个服务器中逐个尝试,一个失败自动换下一个
│
├─ 4. 成功 → 打印 本地时间/UTC时间/时间戳
│ └─ ntp:start_auto(3600) → 每小时自动同步
│
└─ 5. 失败 → log.error
项目结构
4G网络/
├── main.lua # 入口,等待4G → NTP 校时验证
└── ntp.lua # NTP 校时模块(可复用,9个国内服务器)
核心代码详解
1. 等待 4G 网络就绪
function Ntp:init()
sys.wait(3000)
while not socket.adapter(socket.dft()) do
sys.waitUntil("IP_READY", 1000)
end
log.info("NTP", "4G网络已就绪")
end
2. NTP 同步(多服务器自动切换)
Ntp.DEFAULT_SERVERS = {
"ntp.aliyun.com", -- 阿里云
"ntp.tencent.com", -- 腾讯云
"cn.ntp.org.cn", -- 中国 NTP 快速公开
"cn.pool.ntp.org", -- 中国 NTP 池
"ntp.sjtu.edu.cn", -- 上海交大
"ntp.tuna.tsinghua.edu.cn",-- 清华 TUNA
}
function Ntp:sync(timeout_ms, servers)
timeout_ms = timeout_ms or 10000
servers = servers or Ntp.DEFAULT_SERVERS
socket.sntp(servers) -- 传入服务器列表,自动尝试
local ok = sys.waitUntil("NTP_UPDATE", timeout_ms)
if ok then
self.synced = true
end
return ok
end
3. 时间获取
-- 本地时间(北京时间)
ntp:datetime() -- "2026-05-19 14:30:00"
-- UTC 时间
ntp:utc_datetime() -- "2026-05-19 06:30:00"
-- Unix 时间戳
ntp:timestamp() -- 1747636200
4. 自动定时同步
-- 每小时自动同步一次
ntp:start_auto(3600)
-- 内部通过独立协程定时执行
sys.taskInit(function()
while true do
sys.wait(interval * 1000)
self:sync(10000)
end
end)
5. main.lua 完整流程
sys.taskInit(function()
ntp:init() -- 等待4G就绪
local ok = ntp:sync(10000) -- NTP同步
if ok then
log.info("本地时间:", ntp:datetime())
log.info("UTC 时间:", ntp:utc_datetime())
log.info("时间戳:", ntp:timestamp())
ntp:start_auto(3600) -- 每小时自动同步
else
log.error("NTP校时失败")
end
end)
全部代码
main.lua
main.lua
PROJECT = "ntp_test"
VERSION = "1.0.0"
local ntp = require("ntp")
if wdt then
wdt.init(9000)
sys.timerLoopStart(wdt.feed, 3000)
end
sys.taskInit(function()
-- 1. 等待网络
ntp:init()
-- 2. NTP 同步
local ok = ntp:sync(10000)
if ok then
log.info("MAIN", "================================")
log.info("MAIN", "NTP 校时成功 ✓ 4G网络可达")
log.info("MAIN", "本地时间: " .. ntp:datetime())
log.info("MAIN", "UTC 时间: " .. ntp:utc_datetime())
log.info("MAIN", "时间戳: " .. tostring(ntp:timestamp()))
log.info("MAIN", "================================")
-- 3. 启动自动同步(每小时)
ntp:start_auto(3600)
else
log.error("MAIN", "NTP 校时失败 ✗")
end
end)
sys.run()
ntp.lua
ntp.lua
Ntp = {}
Ntp.DEFAULT_SERVERS = {
"ntp.aliyun.com", "ntp1.aliyun.com",
"ntp2.aliyun.com", "ntp.tencent.com",
"time1.cloud.tencent.com", "cn.ntp.org.cn",
"cn.pool.ntp.org", "ntp.sjtu.edu.cn",
"ntp.tuna.tsinghua.edu.cn",
}
function Ntp:new()
local obj = { synced = false, auto_interval = nil }
setmetatable(obj, self)
self.__index = self
return obj
end
function Ntp:init()
log.info("NTP", "等待4G网络注册...")
sys.wait(3000)
while not socket.adapter(socket.dft()) do
log.warn("NTP", "IP未就绪, 继续等待...")
sys.waitUntil("IP_READY", 1000)
end
log.info("NTP", "4G网络已就绪")
end
function Ntp:sync(timeout_ms, servers)
timeout_ms = timeout_ms or 10000
servers = servers or Ntp.DEFAULT_SERVERS
log.info("NTP", "开始 NTP 同步...")
socket.sntp(servers)
local ok = sys.waitUntil("NTP_UPDATE", timeout_ms)
if ok then
self.synced = true
log.info("NTP", "同步成功: " .. self:datetime())
else
log.error("NTP", "同步失败,超时 " .. tostring(timeout_ms) .. "ms")
end
return ok
end
function Ntp:datetime(fmt)
return os.date(fmt or "%Y-%m-%d %H:%M:%S")
end
function Ntp:timestamp()
return os.time()
end
function Ntp:utc_datetime(fmt)
return os.date("!" .. (fmt or "%Y-%m-%d %H:%M:%S"))
end
function Ntp:start_auto(interval)
interval = interval or 3600
self.auto_interval = interval
local s = self
sys.taskInit(function()
while true do
sys.wait(interval * 1000)
log.info("NTP", "自动同步...")
s:sync(10000)
end
end)
end
function Ntp:stop_auto()
self.auto_interval = nil
end
return Ntp