air780e-forwarder/esp32c3/script/main copy.lua
2024-03-24 20:57:03 +08:00

99 lines
2.8 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

PROJECT = "sms_forwarder_wifi"
VERSION = "1.0.0"
local sys = require("sys")
local config = require("config")
local constants = require("constants")
local air780 = require("air780_helper")
local led_helper = require("led_helper")
local utils = require("utils")
require("sysplus")
require("notification_helper")
if wdt then
wdt.init(9000)
sys.timerLoopStart(wdt.feed, 3000)
end
log.setLevel(config.log_level)
log.style(1)
log.info("bsp", rtos.bsp())
log.info("mem_sys", rtos.meminfo("sys"))
log.info("mem_lua", rtos.meminfo("lua"))
-- 每秒完整GC一次防止内存不足问题
sys.timerLoopStart(function()
collectgarbage("collect")
end, 1000)
led_helper.blink_status_led(constants.led_blink_duration.initializing)
sys.taskInit(function()
local logging_tag = "main - 初始化网络"
log.info(logging_tag, "正在连接无线网络"..config.wifi.ssid)
wlan.init()
wlan.setMode(wlan.STATION)
wlan.connect(config.wifi["ssid"], config.wifi.password)
sys.waitUntil("IP_READY")
local ip_address = wlan.getIP()
log.info(logging_tag, "无线网络连接成功IP地址"..ip_address)
for index, value in ipairs(config.dns_servers) do
log.info(logging_tag, "配置第"..index.."个DNS服务器为"..value)
socket.setDNS(nil, index, value)
end
log.info(logging_tag, "等待时间同步")
sys.waitUntil("NTP_UPDATE")
log.info(logging_tag, "时间同步完成")
end)
sys.taskInit(function ()
local logging_tag = "main - 初始化Air780"
local at_command_result
if config.disable_netled then
log.info(logging_tag, "正在关闭NET灯闪烁")
air780.send_at_command("AT+CNETLIGHT=0")
end
log.info(logging_tag, "初始化完成,等待新短信...")
-- 测试短信推送,解除注释可开机时自动模拟推送一条,用于模块独立测试
-- sys.publish(
-- constants.air780_message_topic_new_notification_request,
-- '10086',
-- '测试短信内容')
led_helper.light_status_led()
end)
-- 收到新消息派发推送
sys.subscribe(constants.air780_message_topic_new_sms_received,
function(data)
led_helper.blink_working_led(constants.led_blink_duration.working)
sms_content = table.concat(data, "\n")
if sms_content then
log.info("main", "收到短信:"..sms_content)
sys.publish(
constants.air780_message_topic_new_notification_request,
sms_content)
led_helper.shut_working_led()
return
else
log.info("main", "收到来自"..phone_number.."的短信,即将转发...")
sys.publish(
constants.air780_message_topic_new_notification_request,
sms_content)
led_helper.shut_working_led()
return
end
end)
sys.run()