Update README.md

master
呓喵酱 5 years ago committed by GitHub
parent 83f8d0e26e
commit f079c3b93b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 166
      docs/README.md

@ -170,6 +170,172 @@ qos/sync | subscribe | receive the qos package | 148 (random key)
qos/waterSys | publish | send back the qos package | 148 (return the recived key, or return -1)
### Client Code Examples
**Fake Station Code with NodeJS**
```js
const mqtt = require('mqtt');
const client = mqtt.connect('mqtts://mqtt.yimian.xyz:30032');
var mcu = {
name: 'station',
syncId: 1,
status: 0,
data:{
batteryLevel: 100,
light: 23,
temperature: 23,
humidity: 45.8,
rainfall: 22,
CO: 11,
NH3: 89,
airPressure: 22,
},
};
client.on('connect', function () {
client.subscribe('ctl/' + mcu.name + '/#');
client.subscribe('qos/sync');
console.log('Connected!');
})
client.on('message', function (topic, message) {
console.log(topic.toString() + ': ' + message.toString());
if(topic.toString() == 'qos/sync'){
// sync respond, for calculating delay purpose
client.publish('qos/' + mcu.name, message.toString());
// match syncId to send sensor message
if(parseInt(Number(message) / 100) <= mcu.syncId){
snsrSimulate();
}
}
if(topic.toString() == 'ctl/' + mcu.name + '/status'){
mcu.status = message;
}
if(topic.toString() == 'ctl/' + mcu.name + '/waterSwitch'){
mcu.data.waterSwitch = message;
}
})
// collect and send sensor data
var sendSnsrData = function(){
client.publish('res/' + mcu.name + '/status', String(mcu.status));
for(i in mcu.data){
client.publish('res/' + mcu.name + '/' + i, String(mcu.data[i]));
};
}
// simulation
var snsrSimulate = function() {
var status = code2status(mcu.status);
if(status.status == 'on'){
mcu.data.temperature = 100 * Math.random();
mcu.data.humidity = 100 * Math.random();
mcu.data.light = 100 * Math.random();
mcu.data.CO = 100 * Math.random();;
mcu.data.NH3 = 100 * Math.random();
mcu.data.airPressure = 100 * Math.random();
if(status.battery == 'consuming' && mcu.data.batteryLevel > 0 && Math.random() < .1){
mcu.data.batteryLevel --;
}
if(status.battery == 'charging' && mcu.data.batteryLevel < 100 && Math.random() < .6){
mcu.data.batteryLevel ++;
}
}
sendSnsrData();
};
//setInterval(snsrSimulate, 50000);
var code2status = function(statusCode){
var o = {
status: 'on',
mode: 'normal',
battery: 'consuming',
pump0: 'ok',
waterSwitch: 'ok',
air: 'ok',
pump1: 'ok',
temperature: 'ok',
rainfall: 'ok',
humidity: 'ok',
light: 'ok',
CO: 'ok',
NH3: 'ok',
};
if(statusCode % 2) o.status = 'off';
if(statusCode % 4 >= 2) o.mode = 'sleep';
if(statusCode % 8 >= 4){
o.battery = 'charging';
o.pump0 = 'error';
}
if(statusCode % 16 >= 8){
o.waterSwitch = 'error';
o.air = 'error';
o.pump1 = 'error';
}
if(statusCode % 32 >= 16){
o.temperature = 'error';
o.rainfall = 'error';
}
if(statusCode % 64 >= 32){
o.humidity = 'error';
o.light = 'error';
}
if(statusCode % 128 >= 64){
o.CO = 'error';
}
if(statusCode % 256 >= 128){
o.NH3 = 'error';
}
return o;
}
var status2code = function(status){
var o = {
status: 'on',
mode: 'normal',
battery: 'consuming',
pump0: 'ok',
waterSwitch: 'ok',
air: 'ok',
pump1: 'ok',
temperature: 'ok',
rainfall: 'ok',
humidity: 'ok',
light: 'ok',
CO: 'ok',
NH3: 'ok',
};
Object.assign(o, status);
return (o.status != 'on') * 1 + (o.mode != 'normal') * 2 + (o.battery != 'consuming') * 4
+ (o.pump0 != 'ok') * 4 + (o.waterSwitch != 'ok') * 8 + (o.air != 'ok') * 8
+ (o.pump1 != 'ok') * 8 + (o.temperature != 'ok') * 16 + (o.rainfall != 'ok') * 16
+ (o.humidity != 'ok') * 32 + (o.light != 'ok') * 32 + (o.CO != 'ok') * 64
+ (o.NH3 != 'ok') *128;
}
```
## API - HTTP

Loading…
Cancel
Save