/
RAK5010.js
165 lines (149 loc) · 5.36 KB
/
RAK5010.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
var PINS = {
LTE_RXD : D8,
LTE_CTS : D11,
LTE_TXD : D6,
LTE_RTS : D7,
LTE_STATUS : D18,
LTE_WDISABLE : D29,
LTE_DTR : D26,
LTE_AP_READY : D30,
LTE_PSM : D3,
GPS_EN : D39,
NRF_SCL : D13,
NRF_SDA : D14,
GPRS_RESET : D28,
GPRS_PWRKEY : D2,
OPT_INT : D15, // OPT3001 interrupt
LPS22_INT : D17, // LPS22HB interrupt
BAT_SET : D41,
CHG_DET : D40,
ADC_VBAT : D4,
};
var i2c = new I2C();
i2c.setup({sda:PINS.NRF_SDA, scl:PINS.NRF_SCL, bitrate:400000});
// SHTC3 humidity + temp - needs module
// LPS22HB pressure - we have a module for this
/// Returns a LIS3DH instance. callback when initialised. Then use 'read' to get data
exports.setAccelOn = function(isOn, callback) {
if (this.LIS3DH) this.LIS3DH.off();
delete this.LIS3DH;
if (isOn) {
if (callback) setTimeout(callback, 100, this.LIS3DH); // wait for first reading
// {int:pin} isn't used yet, but at some point the module might include support
return this.LIS3DH = require("LIS3DH").connectI2C(i2c, { int : PINS.LIS3DH_INT1});
}
};//tested ok
/// Returns a OPT3001 instance. callback when initialised. Then use 'read' to get data
exports.setOptoOn = function(isOn, callback) {
if (this.OPT3001) this.OPT3001.off();
delete this.OPT3001;
if (isOn) {
if (callback) setTimeout(callback, 1000, this.OPT3001); // wait for first reading
// {int:pin} isn't used yet, but at some point the module might include support
return this.OPT3001 = require("OPT3001").connectI2C(i2c, { int : PINS.OPT_INT });
}
};//tested ok
/// Returns a LPS22HB instance. callback when initialised. Then use 'get' to get data, or the `on('data'` event
exports.setPressureOn = function(isOn, callback) {
if (this.LPS22HB) this.LPS22HB.stop();
delete this.LPS22HB;
if (isOn) {
if (callback) setTimeout(callback, 1000, this.LPS22HB); // wait for first reading
return this.LPS22HB = require("LPS22HB").connectI2C(i2c, { int : PINS.LPS22_INT });
}
};
/// Returns a SHT3C instance. callback when initialised. Then use 'read(callback)' to get data
exports.setEnvOn = function(isOn, callback) {
delete this.SHT3C;
if (isOn) {
if (callback) setTimeout(callback, 100, this.SHT3C); // wait for first startup
return this.SHT3C = require("SHT3C").connectI2C(i2c);
}
};
// Turn cell connectivity on - will take around 8 seconds. Calls the `callback(usart)` when done. You then need to connect either SMS or QuectelM35 to the serial device `usart`
exports.setCellOn = function(isOn, callback) {
if (isOn) {
if (this.cellOn) {
setTimeout(callback,10,Serial1);
return;
}
var that=this;
return new Promise(function(resolve) {
Serial1.removeAllListeners();
Serial1.on('data', function(x) {}); // suck up any data that gets transmitted from the modem as it boots (RDY, etc)
Serial1.setup(115200,{tx:PINS.LTE_TXD, rx:PINS.LTE_RXD, cts:PINS.LTE_RTS});
PINS.GPRS_PWRKEY.reset();
setTimeout(resolve,200);
}).then(function() {
PINS.GPRS_PWRKEY.set();
return new Promise(function(resolve){setTimeout(resolve,5000);});
}).then(function() {
this.cellOn = true;
Serial1.removeAllListeners();
if (callback) setTimeout(callback,10,Serial1);
});
} else {
this.cellOn = false;
PINS.GPRS_PWRKEY.reset(); // turn power off.
setTimeout(function() {
PINS.GPRS_PWRKEY.set();
if (callback) setTimeout(callback,1000);
}, 800);
}
};
/// Set whether the TP4054 should charge the battery (default is yes)
exports.setCharging = function(isCharging) {
PINS.BAT_SET.write(!isCharging);
};
/// Get whether the TP4054 is charging the battery
exports.isCharging = function(isCharging) {
return !PINS.CHG_DET.read();
};//tested ok
/// Return undefined. callback(error, gpsInstance) is called when GPS is initialised
exports.setGPSOn = function(isOn, callback) {
PINS.GPS_EN.write(isOn);
if (!isOn) this.setCellOn(false,callback);
else this.setCellOn(isOn, function(usart) {
var at = require("AT").connect(usart);
var gps = { at:at,on:function(callback) {
callback=callback||function(){};
at.cmd("AT+QGPS=1\r\n",1000,function cb(d) { // speed-optimal
if (d&&d.startsWith("AT+")) return cb; // echo
callback(d=="OK"?null:d);
});
},off:function(callback) {
callback=callback||function(){};
at.cmd("AT+QGPSEND\r\n",1000,function cb(d) {
if (d&&d.startsWith("AT+")) return cb; // echo
callback(d=="OK"?null:d);
});
},get:function(callback) {
// ERROR: 516 means 'no fix'
callback=callback||function(){};
at.cmd("AT+QGPSLOC=2\r\n",1000,function cb(d) {
if (!d) { callback({error:"Timeout"}); return; }
if (d.startsWith("AT+")) return cb; // echo
if (d.startsWith("+CME ERROR:")) callback({error:d.substr(5)});
else if (d.startsWith("+QGPSLOC:")) {
//+QGPSLOC: <UTC>,<latitude>,<longitude>,<hdop>,<altitude>,<fix>,<cog>,<spkm>,<spkn>,<date>,<nsat>
d = d.substr(9).trim();
var a = d.split(",");
callback({
raw : d,
UTC:a[0],lat:+a[1],lon:+a[2],alt:+a[4]
});
} else callback({error:d});
});
}};
gps.on(function(err) {
callback(err, err?undefined:gps);
});
});
};
exports.i2c = i2c;
exports.PINS = PINS;
exports.AIN = D5;
exports.NRF_IO1 = D19;
exports.NRF_IO2 = D20;
exports.NRF_IO3 = D34;
exports.NRF_IO4 = D33;