合宙的724模块已经停产,而且不再提供任何售后服务,由于用户众多,对于已经批量使用724模块的用户来说,依赖合宙官方的远程升级已经不再可靠。况且现在update语句返回的结果,成功越来越少,绝大多数都是连接服务器失败的结果,所以,搭建自己的远程升级(OTA)平台迫在眉睫。同时也强烈谴责无良商家在产品生命周期结束前不再提供售后的行为。
下面我用2个简单的php脚本来给广大用户提供一个样板,这是最简单的结构,用来展示一个可用的的轻量化的OTA服务。
php脚本的名字分别为index.php和download.php
其中index.php是需要在lua脚本中引用的,之后下载过程将引导到download.php中。
首先你的脚本的update语句需要改写。由原来的update.request()改为:
local function updateresult(result)
log.info("尝试升级返回",result)
if result then
log.info("升级包下载成功")
sys.restart("UPDATE_DOWNLOAD_SUCCESS")
else
log.info("升级包下载失败")
end
end
update.request(updateresult,"https://你的升级网址/index.php")--
index.php的文件内容如下:
<?php
/**
* Air724 LuatOS OTA 服务器端 - 版本检查入口
*
* 该脚本处理来自 Air724 模块的 GET 请求,返回 JSON 格式的版本信息。
*/
include_once ("download.php");
// 配置区域
$config = [
'firmware_dir' => __DIR__ . '/', // 固件存放目录
'log_file' => __DIR__ . '/ota_log.txt', // 日志文件
];
// 获取请求参数
$imei = isset($_GET['imei']) ? $_GET['imei'] : '';
$version = isset($_GET['version']) ? $_GET['version'] : '0.0.0';
$productKey = isset($_GET['project_key']) ? $_GET['project_key'] : '';
// 记录请求日志
$logMsg = sprintf("[%s] IMEI:%s | Ver:%s | Key:%s | IP:%s\n",
date('Y-m-d H:i:s'), $imei, $version, $productKey, $_SERVER['REMOTE_ADDR']);
file_put_contents($config['log_file'], $logMsg, FILE_APPEND);
// 模拟数据库或配置文件中的最新版本信息
// 在实际生产中,这里应查询数据库获取对应 product_key 的最新版本
if($productKey=="你的项目product_key"){
$latestVersion = "1.1.3";
$needsUpdate = version_compare($latestVersion, $version, '>');
$response = [];
if ($needsUpdate) {
// 需要升级
if($version=="1.1.1"){
$firmwareFileName = "你的升级差分包1.1.2.bin";
}
else if($version=="1.1.2"){
$firmwareFileName = "你的升级差分包1.1.3.bin";
}
file_put_contents($config['log_file'],$firmwareUrl, FILE_APPEND);
DoDownload($firmwareFileName);
} else {
// 已是最新版本
$response = [
"code" => 0,
"data" => [
"version" => $version, // 返回当前版本表示无需更新
"url" => ""
]
];
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response);
}
}
download.php文件内容
<?php
/**
* Air724 LuatOS OTA 服务器端 - 固件下载服务
*
* 该脚本负责提供二进制固件文件的下载。
* 为了安全,建议在此处增加签名验证或 token 校验,防止未授权下载。
*/
function DoDownload($file){
$filePath = __DIR__ . '/' . basename($file);
if (!file_exists($filePath)) {
http_response_code(404);
die('File not found.');
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($filePath) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filePath));
// 清除输出缓冲区并读取文件
ob_clean();
flush();
readfile($filePath);
exit;
}
升级差分包的制作:
合宙工具:https://iot.openluat.com/tool/dfota
选择基础版本,指的是上一个版本的bin,选择目标版本是指本次最新的bin文件,都在“4G远程升级文件”这个文件夹下面,每次编译项目下载脚本的时候会自动生成一个最新的。
上面的工作完成后,启动模块,可以看到update会连接到自家的服务器并下载查分包了。
463

被折叠的 条评论
为什么被折叠?



