PROJECT = "sms_to_wifi" VERSION = "1.0.0" local sys = require("sys") local config = require("config") local led_helper = require("led_helper") local utils = require("utils") local util_notify = require("util_notify") local air780_helper = require("air780_helper") require("sysplus") 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")) -- 协议头 local http_header = {["Content-Type"] = "application/json"} -- 每秒完整GC一次,防止内存不足问题 sys.timerLoopStart(function() collectgarbage("collect") end, 1000) led_helper.blink_status_led(config.LED_BLINK_DURATION.initializing) -- 初始化fskv非易失存储 -- utils.clear_fskv() -- 初始化网络 sys.taskInit(function() local logging_tag = "main - 初始化网络" fskv.init() if fskv.kv_get("hostname") then wlan.hostname(fskv.kv_get("hostname")) else local mac = wlan.getMac() wlan.hostname("weiguo_" .. mac) end wlan.init() if fskv.kv_get("wlan_ssid") then wlan.connect(fskv.kv_get("wlan_ssid"), fskv.kv_get("wlan_passwd")) return end log.info(logging_tag, "未配置wifi信息,开始配网!") wlan.smartconfig(wlan.AIRKISS) log.info(logging_tag, "开始微信配网") local ret, ssid, passwd = sys.waitUntil("SC_RESULT", 120*1000) -- 等2分钟 if ret == true then fskv.kv_set("wlan_ssid", ssid) fskv.kv_set("wlan_passwd", passwd) log.info(logging_tag, "获取到wifi信息正在重启",ssid, passwd) sys.wait(1000) -- 官方推荐重启 -- rtos.reboot() else -- smartconfig配置失败,开始AP模式网页配网。 log.info(logging_tag, "网页配网","请连接ssid:weiguo_开头的wifi,打开:http://192.168.4.1进行配网") wlan.smartconfig(wlan.STOP) -- 设置为AP模式, 广播ssid, 接收wifi客户端的链接 sys.wait(1000) wlan.setMode(wlan.AP) wlan.createAP("weiguo_" .. mac, "", "192.168.4.1", "255.255.255.0", 6) -- 监听80端口 httpsrv.start(80, function(client, method, uri, headers, body) log.info("httpsrv", method, uri, json.encode(headers), body) if uri == "/" then return 200, http_header, '{"code":"1","msg":"等待配网"}' end if uri == "/config_wifi" then local wan_config = json.decode(body) if wan_config.ssid and wan_config.password then fskv.kv_set("wlan_ssid", wan_config.ssid) fskv.kv_set("wlan_passwd", wan_config.password) log.info(logging_tag, "配置成功,正在重启") wlan.connect(wan_config.ssid, wan_config.password) return 200, http_header, '{"code":"0","msg":"配置成功"}' -- 官方推荐重启 -- rtos.reboot() else return 200, http_header, '{"code":"1","msg":"配置失败"}' end end return 404, {}, "Not Found" .. uri end) end end) -- 初始化设备CS针脚 local function init_cs() for index, value in ipairs(config.CS_GPIO) do log.info("设备选择GPIO初始化", "配置第"..index.."个设备,GPIO:"..value,air780_helper.cs_index) gpio.setup(value, 0, gpio.PULLDOWN) end end -- 轮巡主函数 local function loop_cs() if air780_helper.reading then return end if air780_helper.cs_index > #config.CS_GPIO then air780_helper.cs_index = 1 end -- 统一拉低 for index, value in ipairs(config.CS_GPIO) do gpio.setup(value, 0, gpio.PULLDOWN) end log.info("轮巡设备", air780_helper.cs_index," GPIO",config.CS_GPIO[air780_helper.cs_index]) gpio.set(config.CS_GPIO[air780_helper.cs_index], gpio.HIGH) air780_helper.cs_index = air780_helper.cs_index + 1 end sys.taskInit( function() -- 联网完成 初始化 sys.subscribe("IP_READY", function(ip) log.info("联网完成", "ip ready", ip) for index, value in ipairs(config.DNS_SERVERS) do log.info("联网完成", "配置第"..index.."个DNS服务器为"..value) socket.setDNS(nil, index, value) end led_helper.shut_status_led() led_helper.blink_working_led(config.LED_BLINK_DURATION.initializing) -- 设置设备CS针脚 init_cs() -- 开启定时轮询 sys.timerLoopStart(loop_cs, config.POLL_INTERVAL) end) log.info("bsp", rtos.bsp()) log.info("mac", wlan.getMac()) log.info("hostname", "weiguo_" .. wlan.getMac()) -- 电源键短按发送测试通知 sys.subscribe( "POWERKEY_SHORT_PRESS", function() log.info("main", "POWERKEY_SHORT_PRESS") util_notify.add( { from = "15264925507", sms = "本地测试短信通知并没有实际收到短信", from_time = "2024-03-14 00:00:00", sms_type = "正常", type = "sms" } ) end ) end ) -- 配置 sys.taskInit( function() -- 监听80端口 httpsrv.start(80, function(client, method, uri, headers, body) log.info("httpsrv", method, uri, json.encode(headers),"b", body) if uri == "/" then local numbers = fskv.kv_get("numbers") local ssid = fskv.kv_get("wlan_ssid") local passwd = fskv.kv_get("wlan_passwd") local config = { code = 0, msg = "完成", numbers = fskv.kv_get("numbers"), ssid = fskv.kv_get("wlan_ssid"), passwd = fskv.kv_get("wlan_passwd"), hostname = fskv.kv_get("hostname"), disable_boot = fskv.kv_get("disable_boot"), notify_type = config.NOTIFY_TYPE, notify_channel = { dingtalk = fskv.kv_get("dingtalk"), wecom = fskv.kv_get("wecom") } } return 200, http_header, json.encode(config) end if uri == "/config" then local web_config = json.decode(body) log.info("httpsrv", "配置数据", json.encode(web_config)) if web_config.numbers ~= nil then fskv.kv_set("numbers", web_config.numbers) end if web_config.hostname ~= nil then fskv.kv_set("hostname", web_config.hostname) end if web_config.ssid ~= nil then fskv.kv_set("wlan_ssid", web_config.ssid) end if web_config.passwd ~= nil then fskv.kv_set("wlan_passwd", web_config.passwd) end if web_config.disable_boot == 1 or web_config.disable_boot == 0 then fskv.kv_set("disable_boot", web_config.disable_boot) end if web_config.ssid ~= nil or web_config.passwd ~= nil then log.info(logging_tag, "配置成功,正在联网") wlan.connect(wan_config.ssid, wan_config.password) -- 官方推荐重启 -- rtos.reboot() end local notify_channel = { dingtalk = fskv.kv_get("dingtalk"), wecom = fskv.kv_get("wecom") } if web_config.notify_channel.dingtalk ~= nil then fskv.kv_set("dingtalk", web_config.notify_channel.dingtalk) end if web_config.notify_channel.wecom ~= nil then fskv.kv_set("wecom", web_config.notify_channel.wecom) end web_config.code = 0 web_config.msg = "配置成功" if web_config.ssid ~= nil or web_config.passwd ~= nil then web_config.msg = "配置成功,设备即将重启!" return 200, http_header, json.encode(web_config) else return 200, http_header, json.encode(web_config) end end return 404, {}, "Not Found" .. uri end) end ) -- 启动 sys.run()