For the easy part...
There are two functions in the sketch for distance:
float microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74.0 / 2.0f;
}
float microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return (float)microseconds / 29.0f / 2.0f;
}
and the current code is set to call call the metric one
float readDistance(byte samples)
{
if (samples == 0) samples = 1;
if (samples > 10) samples = 10;
digitalWrite(SENSOR_EN, HIGH);
//need about 60-75ms after power up before HC-SR04 will be usable, so just sleep in the meantime
LowPower.powerDown(SLEEP_60MS, ADC_OFF, BOD_OFF);
LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
PING();
LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
unsigned long duration = 0;
for (byte i=0; i<samples; i++)
{
duration += PING();
if (samples > 1) LowPower.powerDown(SLEEP_15MS, ADC_OFF, BOD_OFF);
}
digitalWrite(SENSOR_EN, LOW);
return microsecondsToCentimeters(duration / samples);
}
and in the gateway metrics.js file you have the metric defined:
//SonarMote
sonar : { name:'CM', regexp:/\b([\d\.]+)cm?\b/i, value:'', unit:'cm', pin:1, graph:1, graphOptions: { legendLbl:'Level', lines: { lineWidth:1 }, colors:['#09c']} },
OK, I am playing with the placement of the sensor to see if that fixes the problem. I've commented out this section:
//SonarMote
sonar : { name:'CM', regexp:/\b([\d\.]+)in?\b/i, value:'', unit:'in', pin:1, graph:1, graphOptions: { legendLbl:'Level', l
ines: { lineWidth:1 }, colors:['#09c']} },
Also changed the metrics.js to "in" everywhere CM was, and it still shows CM.
//SonarMote
sonar : { name:'IN', regexp:/\b([\d\.]+)in?\b/i, value:'', unit:'in', pin:1, graph:1, graphOptions: { legendLbl:'Level', l
ines: { lineWidth:1 }, colors:['#09c']} },
I'm sure its a simple case of "you don't know , what you dont' know". And clearly I don't know! :)
I don't know if this is the correct way to do it but it work's (kinda);
Here is the change in the metrics.js
sonar : { name:'IN', regexp:/CM\:([\d\.]+)/i, value:'', unit:'in', pin:1, graph:1, graphOptions: { legendLbl:'Level', lines: { lineWidth:1 }, colors:['#09c']}, duplicateInterval:3600, valuation:function(value) {return value*.39;}, graphOptions: { legendLbl:'Level', lines: { lineWidth:1 }, colors:['#09c']} },
The problem was every once in awhile it would display the number which went out about 7 decimal places. I'm sure that could be resolved using ".toFixed(2)" (without the quotes) which Felix used for temperature just the other day. I tried but couldn't get the syntax correct.
Edit: Here is the thread he used .toFixed(2)
https://lowpowerlab.com/forum/pi-gateway/issue-with-gateway-when-converting-fahrenheit-to-celsius/