Skip to content

Instantly share code, notes, and snippets.

@industrialinternet
Last active January 27, 2016 18:46
Show Gist options
  • Save industrialinternet/041e686af631a42cc8e1 to your computer and use it in GitHub Desktop.
Save industrialinternet/041e686af631a42cc8e1 to your computer and use it in GitHub Desktop.
// a very simple mDot LoRaWAN connection manager
var tx_wdog = {intv: 60000, counter:0, flag: false, sendingFlag: null, ts:null, tid:null};
var con = {state:0, statePrevious: 0, cmd: null, msg: null, status: null, rx_msg: null, sendInt: 60000};
var tx_string = '';
var rx_string = '';
var log = true;
function cLog(logMsg){
var d = new Date();
print(d.toUTCString()+ ' '+ logMsg);
}
function serialSend(tx_msg){
d = new Date();
tx_wdog.sendingFlag = true;
Serial1.print(tx_msg+"\r\n");
}
// tx-downlink
Serial1.on('data', function (data) {
var hexVal = String(data).charCodeAt(0);
var _rx = "";
for (as=0; as<data.length; as++){
hexVal = String(data).charCodeAt(as);
if(hexVal == 10 || hexVal == 13){ // \r Carriage Return 0x0D, 13 -- \n Line Feed 0x0A, 10
//_rx += <cr-lf>;
} else {
_rx += String.fromCharCode(hexVal);
}
}
rx_string = rx_string + _rx;
// con.cmd responce terminator OK or ERROR
if(rx_string.indexOf("OK") >=0){
tx_wdog.sendingFlag = false;
tx_wdog.counter = 0;
conLEDs(0);
rx_string = rx_string.replace("OK","");
if(con.state <4){
cLog("rx: Ok - "+rx_string);
stateMan(rx_string,'OK');
} else {
cLog("rx: Ok - "+rx_string);
cmdHandler(rx_string);
}
rx_string = "";
} else if (rx_string.indexOf("ERROR") >=0){
tx_wdog.sendingFlag = false;
tx_wdog.counter = 0;
rx_string = rx_string.replace("ERROR","");
cLog("rx: Error - "+rx_string);
stateMan(rx_string,'ERROR');
rx_string = "";
}
});
function cmdHandler(rx){
// handle downlink commmands
}
// LoRaWAN connection manager
function stateMan(response,rType){
// *************************
// State transition manager
// *************************
//cLog('res: '+response+' type: '+ rType+' cmd: '+con.cmd+' state: '+con.state);
if(con.state === 4 && rType === 'OK'){
return;
}
// t5 & t6 handle errors on joined network
if(con.state === 4 && rType === 'ERROR'){
con.cmd = null;
//cLog('network not joined: '+response.indexOf("network not joined"));
if(response.indexOf("network not joined")>0){ // transtion to s2 re-join network
con.state = 2;
}
if(response.indexOf("Operation Timed Out")>0){
//con.state = 2;
}
}
//s0 Test for mDot
if(con.state === 0){
if(con.cmd === null ){
cLog('re/starting state mananger');
con.cmd = "AT";
serialSend(con.cmd);
} else if (rType === 'OK'){ // t1
con.cmd = null;
con.statePrevious = 0;
con.state = 1;
} else if (con.cmd === "AT" && rType === 'ERROR'){
con.cmd = null;
setTimeout(function() {
stateMan('','');
},30000);
}
}
//s1 echo off
if(con.state === 1){
if(con.cmd === null){
con.cmd = "ATE0";
serialSend(con.cmd);
} else if (con.cmd === "ATE0" && rType === 'OK'){ // t2
con.cmd = null;
con.statePrevious = 1;
con.state = 2;
}
}
// s2 join network
if(con.state === 2){
if(con.cmd === null){
con.cmd = "AT+JOIN";
serialSend(con.cmd);
} else if (con.cmd === "AT+JOIN" && rType === 'OK'){ // t3 joined ok
con.cmd = null;
con.statePrevious = 1;
con.state = 3;
} else if (rType === 'ERROR'){ // t4 join failed
con.cmd = null;
setTimeout(function(){stateMan('','');},60000);
}
}
// s3 Joined network so recive msg from Conduit in hex
if(con.state === 3){
if(con.cmd === null){
con.cmd = "AT+RXO=1";
serialSend(con.cmd);
con.cmd = null;
con.statePrevious = 3;
con.state = 4;
}
}
}
// watch dog
function tx_watchDog(){
//cLog('tx watch dog count: '+tx_wdog_counter);
if(tx_wdog.flag){
con.cmd = null; // Re-Start state manager
con.state = 0;
tx_wdog.flag = false;
stateMan('','');
}
if(tx_wdog.sendingFlag){
tx_wdog.counter++;
if(tx_wdog.counter > 2){
cLog('tx watch dog expired re-set mDot');
var d = new Date();
tx_wdog.ts = d.toUTCString();
tx_wdog.sendingFlag = false;
tx_wdog.counter = 0;
tx_wdog.flag = true;
digitalWrite(B1,0); // hard re-set mDot
}
}
}
setInterval(tx_watchDog,tx_wdog.intv); // tick every x sec
// called once on boot
function onInit(){
cLog('on init');
stateMan('','');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment