Average 5 Pings

This flow uses the node-red-node-ping and node-red-contrib-particle nodes to ping a host once per second, calculate the average of those 5 pings, then send the output to a spark/particle core.

You could ignore the particle nodes and send the output directly to whatever you like.

You could also switch the node-red-node-ping for the node-red-contrib-advanced-ping node which will allow you to trigger it at will rather than at set intervals

NOTE: This flow requires you to configure the particle cloud with your own API key.

[{"id":"3fef00b5.b05768","type":"ParticleFunc out","baseurl":"","devid":"mutant_station","fname":"averagePing","param":"","repeat":0,"x":961,"y":239,"z":"c7101f5e.02f7","wires":[[]]},{"id":"b10a8ccf.532d58","type":"ping","name":"Ping google once/second","host":"www.google.com","timer":"1","x":340,"y":299,"z":"c7101f5e.02f7","wires":[["95e35e8f.c9a95"]]},{"id":"868947f6.89026","type":"debug","name":"","active":false,"console":"false","complete":"false","x":885,"y":303,"z":"c7101f5e.02f7","wires":[]},{"id":"95e35e8f.c9a95","type":"function","name":"Average 5 Values","func":"var values = 5; //define the number of values to average\n//----------------------------\n\ncontext.valueArray = context.valueArray || []; //setup a persistant array and initalise it\ncontext.count = context.count || 0; //init the counter and set it to 0 if its not already   \n\n//take whatever came in and add it to the position \"context.count\" in context.valueArray\n\nif (msg.payload==\"false\") {\n    context.valueArray[context.count] = 0\n} else {\n    context.valueArray[context.count] = msg.payload;\n}\n\ncontext.count += 1; //add 1 to context count\nif (context.count == values) { //if this function has been run x times, then we have all x values\n    var temp = 0.00; //make a temp variavle and set it to 0\n    \n    for (j=0; j<values; j++) {\n        //add the value of the item at position j in the array to our temp variable\n        temp = temp + context.valueArray[j];\n    }\n    var average = parseInt(temp/values);\n    var result = {\"payload\": String(average)}; //dump the caluulated average and out temp var out\n    // add all the variables to a second message for debug\n    var parsedvalueArray = {\"payload\": \"Array = \" + context.valueArray + \"\\npreAverage = \" + temp + \"\\nAverage = \" + average};\n    \n    context.count=0; //set the counter back to 0 for the next go\n    return [result, parsedvalueArray]; //then shove it out to the next node\n}","outputs":"2","noerr":0,"x":608,"y":298,"z":"c7101f5e.02f7","wires":[["868947f6.89026","3fef00b5.b05768","ebeffc35.fe72f8"],["349d8273.a36d36"]]},{"id":"349d8273.a36d36","type":"debug","name":"","active":true,"console":"false","complete":"false","x":887,"y":363,"z":"c7101f5e.02f7","wires":[]},{"id":"ebeffc35.fe72f8","type":"ParticleFunc out","baseurl":"","devid":"cobbled_particle","fname":"averagePing","param":"","repeat":0,"x":961,"y":194,"z":"c7101f5e.02f7","wires":[[]]}]
JamiePhonic

Flow Info

created 1 year, 11 months ago

Node Types

Core
  • debug (x2)
  • function (x1)
Other

Tags

  • ping
  • particle
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option