DEV Community

zhuyue
zhuyue

Posted on

4G module update for the power failure alarm device with call telephone function

Because of the current used 4G module AIR720UH unit price continues to rise to nearly 40RMB, have no choice but to change the module used by the power failure alarm from AIR720UH to AIR780EP, making the price down to about 18RMB.

I use the LUA language to do secondary development on the module to realize the power failure detection, report events to the server via TCP, and call the user telephone by server, remote upgrade, local alarm and other functions.

The development of the device itself is very simple, the difficulty lies in the server-side features open, including the development of TCP server-side, especially the handling of large concurrency, phone alarms via voice SMS, WX alarm function, WX-based automatic deduction function and so on;

In order to avoid the return to the factory for maintenance caused by software bugs and to realize the continuous upgrading and iteration of new functions, remote FOTA is a very important function on the device side;

The LUA upgrade interface provided by the module is very simple, and there are a few considerations when realizing the FOTA function.
1) AIR780EP does not support full upgrades, only differential upgrades, probably due to the limitations of the module's memory or flash space.
Therefore, when making OTA upgrade package through luatools, make sure to choose the old version of firmware; otherwise, it will report “FULL_OTA_SAVE_ADDR not found” or “Size of upgrade package exceeded the limitation”, and can not generate the upgrade package normally; 1) AIR780EP does not support full upgrade, but only differential upgrade, probably due to the limitation of module memory or flash space. Otherwise, it will report “FULL_OTA_SAVE_ADDR not found” or “Size of upgrade package exceeds the limit” error, and the upgrade package cannot be generated normally;

2) Need to do a good job of version management, call libfota.request incoming url parameters need to bring the current version number, such as web links, the cloud server code needs to be parsed from the http get request version, and according to the current version and the target version of the corresponding OTA upgrade package to read, return to the device.The server-side PHP code is relatively simple, just a few lines of code if you don't do version determination and restrictions:<?php$binsize = filesize('v902_v903.sota');
$bindata = fread(fopen('v902_v903.sota', “r”), $binsize);
echo $bindata; ?
?>

Of course the device code implemented in lua needs to strictly monitor the normal operation of the device through the watchdog, there are two main layers of monitoring, one is the heartbeat monitoring of the TCP long link, and the other is the monitoring of the taskInit initiated work task.Two variables are used, one variable is used for heartbeat timing and the other is used for work task invocation timing. The heartbeat timing variable is accumulated in the work task and cleared in the event of a normal TCP heartbeat packet being sent out, the work task timing variable is accumulated in the clear watchdog's timing program, and only when the value of the variable used for heartbeat monitoring is less than a certain value is the variable cleared in the work task's program. In the watchdog clearing timer program, the watchdog is cleared only when the value of the work task timing variable is less than a certain value.

With the two simple codes and the watchdog, the dual monitoring of the TCP connection and the task is foolproof.

Image description

Image description

Image description

Image description

Image description

Top comments (0)