diff --git a/c210m/config.lua b/c210m/config.lua new file mode 100644 index 0000000..9b96999 --- /dev/null +++ b/c210m/config.lua @@ -0,0 +1,252 @@ +local _C = { + TAG = "【配置】" +} + +-- 日志等级 +-- log.setLevel("INFO") +-- OTA密钥 +PRODUCT_KEY = "0VrZxxSwrI7qE3GU8ddLFww5FbP3DqQu" +-- 默认配置 +local default_config = { + -- 联网最长等待时间(秒) + -- ip_ready_timeout_s = 30, + -- 配置接口地址 + config_url = "http://miot.xaxi.cn/app/device/conf/", + -- http请求最长等待时间(毫秒) + http_timeout_ms = 1000 * 3, + -- 数据上传频率(秒) + d_u = 600, + -- gps上传模式 0不上传,1开机上传,2跟随数据上传 + g_u = 2, + -- 是否需要0TA更新 1更新 0不更新 + ota = 1, + -- 屏幕数据刷新频率(秒) 0不刷新并息屏 + s_u = 5, + -- 接口请求timeout时间(秒) + t_o = 5 +} + +function _C.init() + if not fskv.init() then return 1 end + local used, total, kv_count = fskv.status() + if not (used and total and kv_count) then return 2 end + if kv_count == 0 and not fskv.set("config", default_config) then return 3 end + return 0 +end + +-- local function get_ip_ready_timeout_s() +-- local timeout = fskv.get("config", "ip_ready_timeout_s") +-- return (type(timeout) == "number") and timeout or default_config.ip_ready_timeout_s +-- end + +-- local function set_ip_ready_timeout_s(v) +-- if type(v) ~= "number" then +-- log.warn(COSO, _C.TAG, "缺少 ip_ready_timeout_s 字段") +-- return +-- end +-- local _ip_ready_timeout_s = get_ip_ready_timeout_s() +-- if v == _ip_ready_timeout_s then return end +-- if v < 10 or v > 30 then +-- log.error(COSO, _C.TAG, "ip_ready_timeout_s 校验失败", v) +-- return +-- end +-- if not fskv.sett("config", "ip_ready_timeout_s", v) then +-- log.error(COSO, _C.TAG, "更新失败 ip_ready_timeout_s", v) +-- end +-- log.info(COSO, _C.TAG, "更新成功 ip_ready_timeout_s", v) +-- end + +local function get_config_url() + local url = fskv.get("config", "config_url") + url = (type(url) == "string" and #url > 0) and url or default_config.config_url + return url .. mobile.imei() +end + +local function set_config_url(v) + if type(v) ~= "string" then + log.warn(COSO, _C.TAG, "缺少 config_url 字段") + return + end + local _config_url = get_config_url() + if v == _config_url then return end + if not fskv.sett("config", "config_url", v) then + log.error(COSO, _C.TAG, "更新失败 config_url", v) + end + log.info(COSO, _C.TAG, "更新成功 config_url", v) +end + +local function get_http_timeout_ms() + local timeout = fskv.get("config", "http_timeout_ms") + return (type(timeout) == "number") and timeout or default_config.http_timeout_ms +end + +local function set_http_timeout_ms(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 http_timeout_ms 字段") + return + end + local _http_timeout_ms = get_http_timeout_ms() + if v == _http_timeout_ms then return end + if v < 1000 or v > 5000 then + log.error(COSO, _C.TAG, "http_timeout_ms 校验失败", v) + return + end + if not fskv.sett("config", "http_timeout_ms", v) then + log.error(COSO, _C.TAG, "更新失败 http_timeout_ms", v) + end + log.info(COSO, _C.TAG, "更新成功 http_timeout_ms", v) +end + +local function get_d_u() + local d_u = fskv.get("config", "d_u") + return (type(d_u) == "number") and d_u or default_config.d_u +end + +local function set_d_u(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 d_u 字段") + return + end + local _d_u = get_d_u() + if v == _d_u then return end + if v < 10 or v > 3600 then + log.error(COSO, _C.TAG, "d_u 校验失败", v) + return + end + if not fskv.sett("config", "d_u", v) then + log.error(COSO, _C.TAG, "更新失败 d_u", v) + end + log.info(COSO, _C.TAG, "更新成功 d_u", v) +end + +local function get_g_u() + local g_u = fskv.get("config", "g_u") + return (type(g_u) == "number") and g_u or default_config.g_u +end + +local function set_g_u(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 g_u 字段") + return + end + local _g_u = get_g_u() + if v == _g_u then return end + if v < 0 or v > 2 then + log.error(COSO, _C.TAG, "g_u 校验失败", v) + return + end + if not fskv.sett("config", "g_u", v) then + log.error(COSO, _C.TAG, "更新失败 g_u", v) + end + log.info(COSO, _C.TAG, "更新成功 g_u", v) +end + +local function get_ota() + local ota = fskv.get("config", "ota") + return (type(ota) == "number") and ota or default_config.ota +end + +local function set_ota(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 ota 字段") + return + end + local _ota = get_ota() + if v == _ota then return end + if v < 0 or v > 1 then + log.error(COSO, _C.TAG, "ota 校验失败", v) + return + end + if not fskv.sett("config", "ota", v) then + log.error(COSO, _C.TAG, "更新失败 ota", v) + end + log.info(COSO, _C.TAG, "更新成功 ota", v) +end + +local get_s_u = function() + local s_u = fskv.get("config", "s_u") + return (type(s_u) == "number") and s_u or default_config.s_u +end + +local function set_s_u(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 s_u 字段") + return + end + local _s_u = get_s_u() + if v == _s_u then return end + if v < 0 or v > 3600 then + log.error(COSO, _C.TAG, "s_u 校验失败", v) + return + end + if not fskv.sett("config", "s_u", v) then + log.error(COSO, _C.TAG, "更新失败 s_u", v) + end + log.info(COSO, _C.TAG, "更新成功 s_u", v) +end + +local function get_t_o() + local t_o = fskv.get("config", "t_o") + return (type(t_o) == "number") and t_o or default_config.t_o +end + +local function set_t_o(v) + if type(v) ~= "number" then + log.warn(COSO, _C.TAG, "缺少 t_o 字段") + return + end + local _t_o = get_t_o() + if v == _t_o then return end + if v < 1 or v > 5 then + log.error(COSO, _C.TAG, "t_o 校验失败", v) + return + end + if not fskv.sett("config", "t_o", v) then + log.error(COSO, _C.TAG, "更新失败 t_o", v) + end + log.info(COSO, _C.TAG, "更新成功 t_o", v) +end + +-- function _C.get_net_timeout() +-- return get_ip_ready_timeout_s() +-- end + +function _C.get_d_u() + return get_d_u() +end + +function _C.get_g_u() + return get_g_u() +end + +function _C.get_ota() + return get_ota() +end + +function _C.get_s_u() + return get_s_u() +end + +function _C.get_t_o() + return get_t_o() +end + +function _C.check() + local _config_url = get_config_url() + local _http_timeout_ms = get_http_timeout_ms() + local code, _, body = http.request("GET", _config_url, nil, nil, { timeout = _http_timeout_ms }).wait() + log.debug(COSO, _C.TAG, _config_url,_http_timeout_ms,code) + if code ~= 200 then return 101 end + body = json.decode(body) + log.debug(COSO, _C.TAG, body) + -- set_ip_ready_timeout_s(body.ip_ready_timeout_s) + set_config_url(body.config_url) + set_http_timeout_ms(body.http_timeout_ms) + set_d_u(body.d_u) + set_g_u(body.g_u) + set_ota(body.ota) + set_s_u(body.s_u) + set_t_o(body.t_o) +end + +return _C diff --git a/c210m/coso.lua b/c210m/coso.lua new file mode 100644 index 0000000..7eaa862 --- /dev/null +++ b/c210m/coso.lua @@ -0,0 +1,52 @@ +require("globals") + +local function start() + local wakeup_sta = { + [0] = "上电开机", + [1] = "定时器唤醒", + } + local slp_sta = { + [0] = "普通开机", + [4] = "休眠唤醒", + } + local wakeup, sleep = pm.lastReson() + log.info(COSO, "【设备开机】",wakeup_sta[wakeup],slp_sta[sleep], rtos.buildDate(), rtos.firmware()) + if sleep > 0 then + mobile.flymode(0,false) + end + local code, connet_time = net.init(30) + if code ~= 0 then return sta.handling(code) end + log.info(COSO, "【网络】", "连接成功", connet_time) + sys.publish("config_check") +end + +local function configTask() + sys.waitUntil("config_check") + log.info(COSO, "【检查配置") + local code = cfg.init() + if code ~= 0 then return sta.handling(code) end + log.info(COSO, "【数据库】", "初始化完成") + cfg.check() + log.debug(COSO, "【DEV】", cfg.get_d_u()) + log.debug(COSO, "【DEV】", cfg.get_g_u()) + log.debug(COSO, "【DEV】", cfg.get_ota()) + log.debug(COSO, "【DEV】", cfg.get_s_u()) + log.debug(COSO, "【DEV】", cfg.get_t_o()) + sys.publish("get_data") +end + +local function dataTask() + sys.waitUntil("get_data") + sys.publish("report_data") +end + +local function reportTask() + sys.waitUntil("report_data") + sleep.goPSM(10) +end + + +sys.taskInit(start) +sys.taskInit(configTask) +sys.taskInit(dataTask) +sys.taskInit(reportTask) diff --git a/c210m/globals.lua b/c210m/globals.lua new file mode 100644 index 0000000..8d7b37a --- /dev/null +++ b/c210m/globals.lua @@ -0,0 +1,9 @@ +COSO = "COSO" +sys = require("sys") +sysplus = require("sysplus") +sta = require("status") +cfg = require("config") + +net = require("net") +ota = require("ota") +sleep = require("sleep") diff --git a/c210m/main.lua b/c210m/main.lua new file mode 100644 index 0000000..b421194 --- /dev/null +++ b/c210m/main.lua @@ -0,0 +1,4 @@ +PROJECT = "COSO_C210M_DEV" +VERSION = "1.0.0" +require("coso") +sys.run() diff --git a/c210m/net.lua b/c210m/net.lua new file mode 100644 index 0000000..8208928 --- /dev/null +++ b/c210m/net.lua @@ -0,0 +1,20 @@ +local _C = { + TAG = "【4G网络】" +} + +function _C.init(v) + local start_time = os.clock() + local result + while not (result or ((os.clock() - start_time) * 100 > v)) do + result = sys.waitUntil("IP_READY", 990) + if not result then + log.warn(COSO, _C.TAG, "联网中:", (os.clock() - start_time) * 100, "超时:", v) + sys.wait(10) + end + end + return result and 0 or 3, (os.clock() - start_time) * 100 +end + +return _C + + diff --git a/c210m/ota.lua b/c210m/ota.lua new file mode 100644 index 0000000..8aba45e --- /dev/null +++ b/c210m/ota.lua @@ -0,0 +1,40 @@ +local _C = { + TAG = "【OTA】", + MSG = { + _default = "未知状态", + [0] = "升级包下载成功,重启模块", + [1] = "连接失败,请检查url拼写或服务器配置(是否为内网)", + [2] = "url错误,检查url拼写", + [3] = "服务器断开,检查服务器白名单配置", + [4] = "接收报文错误,检查模块固件或升级包内文件是否正常", + [5] = "版本号书写错误,iot平台版本号需要使用xxx.yyy.zzz形式", + [99] = "OTA失败,继续执行原有逻辑", + } +} + +local libfota2 = require("libfota2") + +local function libfota_cb(result) + log.info(COSO, _C.TAG, _C.MSG[result] or _C.MSG._default) + if result == 0 then + sys.publish("ota_success") + else + log.info(COSO, _C.TAG, _C.MSG[99]) + sys.publish("get_data") + end +end + +local function ota_update() + sys.waitUntil("ota_update") + libfota2.request(libfota_cb) +end + +local function ota_success() + sys.waitUntil("ota_success") + rtos.reboot() +end + +sys.taskInit(ota_update) +sys.taskInit(ota_success) + +return _C diff --git a/c210m/report.lua b/c210m/report.lua new file mode 100644 index 0000000..551b32a --- /dev/null +++ b/c210m/report.lua @@ -0,0 +1,5 @@ +local _C = { + TAG = "【上报数据】" +} + +return _C diff --git a/c210m/sleep.lua b/c210m/sleep.lua new file mode 100644 index 0000000..7a54f14 --- /dev/null +++ b/c210m/sleep.lua @@ -0,0 +1,18 @@ +local _C = { + TAG = "【休眠】" +} + +function _C.goPSM(v) + log.info(COSO, _C.TAG,"即将休眠") + mobile.flymode(0, true) + gpio.close(23) + gpio.close(gpio.WAKEUP1) + gpio.close(gpio.PWR_KEY) + pm.dtimerStart(3, v * 1000) + pm.power(pm.WORK_MODE, 3) + sys.wait(3000) + log.warn(COSO, _C.TAG,"休眠失败,尝试重启") + rtos.reboot() +end + +return _C \ No newline at end of file diff --git a/c210m/status.lua b/c210m/status.lua new file mode 100644 index 0000000..575f058 --- /dev/null +++ b/c210m/status.lua @@ -0,0 +1,20 @@ +local _C = { + MSG = { + _default = "未知状态", + [0] = "成功", + [1] = "【数据库】初始化失败", + [2] = "【数据库】状态异常", + [3] = "【数据库】数据初始化失败", + [4] = "【4G网络】联网超时", + [5] = "【OTA】升级中", + [101] = "【OTA】无效的update字段", + [102] = "【OTA】升级请求异常", + } +} + +function _C.handling(code) + log.warn(COSO, _C.MSG[code] or _C.MSG._default) + if code == 5 then sys.publish("ota_update") end +end + +return _C diff --git a/pins_Air780EPM.json b/pins_Air780EPM.json new file mode 100644 index 0000000..54dd7ea --- /dev/null +++ b/pins_Air780EPM.json @@ -0,0 +1,52 @@ +{ + "model": "Air780EPM", + "pins": [ + [7, "PWR_KEY", ""], + [16, "GPIO27", ""], + [17, "UART1_RXD", ""], + [18, "UART1_TXD", ""], + [19, "GPIO22", ""], + [20, "PWM1", ""], + [22, "PWM0", ""], + [23, "GPIO2", ""], + [25, "CAN_TXD", ""], + [26, "PWM4", ""], + [28, "UART2_RXD", ""], + [29, "UART2_TXD", ""], + [30, "GPIO29", ""], + [31, "GPIO30", ""], + [32, "GPIO31", ""], + [33, "GPIO32", ""], + [38, "DBG_RXD", ""], + [39, "DBG_TXD", ""], + [49, "LCD_RST", ""], + [50, "LCD_SDA", ""], + [51, "LCD_RS", ""], + [52, "LCD_CS", ""], + [53, "LCD_CLK", ""], + [54, "CAM_MCLK", ""], + [55, "CAM_RX0", ""], + [56, "CAM_RX1", ""], + [57, "UART3_TXD", ""], + [58, "UART3_RXD", ""], + [61, "VBUS", ""], + [66, "I2C1_SDA", ""], + [67, "I2C1_SCL", ""], + [78, "ONEWIRE", ""], + [79, "USIM_DET", ""], + [80, "CAM_BCLK", ""], + [81, "CAM_CS", ""], + [82, "USB_BOOT", ""], + [83, "SPI0_CS", ""], + [84, "SPI0_MISO", ""], + [85, "SPI0_MOSI", ""], + [86, "SPI0_CLK", ""], + [97, "GPIO16", ""], + [99, "GPIO23", ""], + [100, "GPIO17", ""], + [101, "WAKEUP0", ""], + [102, "GPIO20", ""], + [106, "CAN_RXD", ""], + [107, "GPIO21", ""] + ] +}