2019年11月26日 星期二

Node-Red flow範例 << 模擬隨機值實現Script。>>

Analog Random Values ​​Implementation Script. |  Node-Red Flow Example



//===setting=== 
var hpoint = 100;
var lpoint = 0;
var cent = 10; //centralize number
//===============

var mpoint = 0;
var output = context.get('output')||0;
var addpoint = context.get('addpoint')||0;

//count
var count = context.get('count')||0;
count += 1;
context.set('count',count);

//rcount
var rcount = context.get('rcount')||0;
if(count >= rcount){
    for(i=0;i<cent;i++){        //centralize
        mpoint = mpoint + Math.floor(Math.random() * hpoint);
    }
    rcount = parseInt(Math.floor(Math.random() * 100)/5);
    addpoint = ((mpoint/cent)-output)/rcount;
    context.set('addpoint',addpoint);
    context.set('rcount',rcount);
    context.set('count',0);
}else{
    output = parseFloat((output + addpoint).toFixed(1));
    context.set('output',output);
}
msg.payload = output;
return msg;



Flow JSON Sourcecode


[{"id":"59e7f383.6f34ec","type":"comment","z":"35516ee7.622d92","name":"Random","info":"","x":220,"y":100,"wires":[]},{"id":"a38db82b.383e78","type":"inject","z":"35516ee7.622d92","name":"","topic":"","payload":"","payloadType":"date","repeat":"2","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":160,"wires":[["f49a2ffd.11127"]]},{"id":"a99544a8.554ed8","type":"debug","z":"35516ee7.622d92","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":630,"y":220,"wires":[]},{"id":"f49a2ffd.11127","type":"function","z":"35516ee7.622d92","name":"random","func":"//===setting=== \nvar hpoint = 100;\nvar lpoint = 0;\nvar cent = 10; //centralize number\n//===============\n\nvar mpoint = 0;\nvar output = context.get('output')||0;\nvar addpoint = context.get('addpoint')||0;\n\n//count\nvar count = context.get('count')||0;\ncount += 1;\ncontext.set('count',count);\n\n//rcount\nvar rcount = context.get('rcount')||0;\nif(count >= rcount){\n    for(i=0;i<cent;i++){        //centralize\n        mpoint = mpoint + Math.floor(Math.random() * hpoint);\n    }\n    rcount = parseInt(Math.floor(Math.random() * 100)/5);\n    addpoint = ((mpoint/cent)-output)/rcount;\n    context.set('addpoint',addpoint);\n    context.set('rcount',rcount);\n    context.set('count',0);\n}else{\n    output = parseFloat((output + addpoint).toFixed(1));\n    context.set('output',output);\n}\nmsg.payload = output;\nreturn msg;","outputs":1,"noerr":0,"x":420,"y":160,"wires":[["a99544a8.554ed8","7f105c44.bf22e4"]]},{"id":"7f105c44.bf22e4","type":"ui_chart","z":"35516ee7.622d92","name":"","group":"7784c75b.8caac8","order":0,"width":"6","height":"6","label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"200","removeOlder":1,"removeOlderPoints":"50","removeOlderUnit":"3600","cutout":0,"useOneColor":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":610,"y":160,"wires":[[],[]]},{"id":"7784c75b.8caac8","type":"ui_group","z":"","name":"Default","tab":"ef07f6d1.ceaea8","disp":true,"width":"6","collapse":false},{"id":"ef07f6d1.ceaea8","type":"ui_tab","z":"","name":"Home","icon":"dashboard"}]

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...