Skip to content

Instantly share code, notes, and snippets.

@mamuesp
Created November 5, 2018 14:15
Show Gist options
  • Save mamuesp/7b2db3848a31ee126617c5be87260f16 to your computer and use it in GitHub Desktop.
Save mamuesp/7b2db3848a31ee126617c5be87260f16 to your computer and use it in GitHub Desktop.
MJS code of a start procedure in Mongoose-OS with a pattern which will wait until WiFi, MQTT and SNTP connections are succefully set up.
load('api_http.js');
load('api_config.js');
let connected = 0;
let step = 3;
let started = false;
let watchDog = 0;
let timeOut = 30; // might be defined in the configuration ...
let events = {
_netEventHandler: function(ev, evdata, ctx) {
connected |= 0x01;
Timer.del(watchDog);
watchDog = Timer.set(1000 * timeOut, 0, start, {hasConnections: false});
Log.info('Connected to WiFi ... state: ' + JSON.stringify(connected));
if (connected === 0x07) {
Timer.del(watchDog);
start({hasConnections: true});
}
},
_mqttEventHandler: function(conn, ev, data, ctx) {
if (ev === MQTT.EV_CONNACK) {
connected |= 0x02;
Timer.del(watchDog);
watchDog = Timer.set(1000 * timeOut, 0, start, {hasConnections: false});
Log.info('Connected to MQTT ... state: ' + JSON.stringify(connected));
if (connected === 0x07) {
Timer.del(watchDog);
start({hasConnections: true});
}
}
},
_sntpEventHandler: function(ev, evdata, ctx) {
if (ev === Event.SYS + 3) {
connected |= 0x04;
Timer.del(watchDog);
watchDog = Timer.set(1000 * timeOut, 0, start, {hasConnections: false});
Log.info('Time set via SNTP ... state: ' + JSON.stringify(connected) + " - connected: " + JSON.stringify(connected));
if (connected === 0x07) {
Timer.del(watchDog);
start({hasConnections: true});
}
}
}
};
function prepareConnections(ctx) {
watchDog = Timer.set(1000 * timeOut, 0, start, {hasConnections: false});
// Monitor network connectivity.
Event.addHandler(Net.STATUS_GOT_IP, events._netEventHandler, ctx),
// Monitor MQTT connectivity.
MQTT.setEventHandler(events._mqttEventHandler, ctx);
// Monitor SNTP changes
Event.addGroupHandler(Event.SYS, events._sntpEventHandler, ctx);
}
function start(sysInfo) {
// start only once
if (started) {
return;
}
// if the watch dog timer started the system, we will fallback in the "old habit"
connected = (sysInfo !== null && sysInfo.hasConnections) ? connected : 0;
if (connected === 0) {
Log.info('Started without connection by watch dog timer ...');
}
started = true;
Log.info('Start module ...');
// ***********************************
// Put your own starting code here ...
// ***********************************
}
@mamuesp
Copy link
Author

mamuesp commented Nov 5, 2018

This tool will establish configured WiFi, MQTT and SNTP connections, guarded by a watchdog. When all connections are active, the function "start" will be called, from there the system start may continue. If a timeout occurs, the start will be continued withtout the established connections.

To use, just add this MJS file in your Mongoose-OS "fs" directory, and in "fs/init.js" add the call "load('system_start.js');" on top of the file. Then, also in "fs/init.js" ad the call "prepareConnections(this);" - and in the routine "function start(sysInfo) " add your own starting code for your app. That's it ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment