More robust measurement
This commit is contained in:
41
src/main.cpp
41
src/main.cpp
@@ -11,15 +11,12 @@ const float minDistance = 0.01;
|
|||||||
const float maxDistance = 4.0;
|
const float maxDistance = 4.0;
|
||||||
const float minimumChange = 0.2;
|
const float minimumChange = 0.2;
|
||||||
|
|
||||||
#define DISTANCE_INTERVAL 5 // s
|
#define MEASURE_INTERVAL 500 // ms
|
||||||
#define LED_INTERVAL 5 // s
|
#define PUBLISH_INTERVAL 5000 // ms
|
||||||
|
|
||||||
NewPing sonar(trigPin,echoPin,maxDistance*100.0);
|
NewPing sonar(trigPin,echoPin,maxDistance*100.0);
|
||||||
HomieNode obstacleNode("obstacle", "Obstacle", "object");
|
HomieNode obstacleNode("obstacle", "Obstacle", "object");
|
||||||
|
|
||||||
#define heartbeatMillis 5000
|
|
||||||
#define messageMillis 5000
|
|
||||||
|
|
||||||
void signal_led(bool ons = true);
|
void signal_led(bool ons = true);
|
||||||
void heartbeat_led(int times = 2);
|
void heartbeat_led(int times = 2);
|
||||||
void longbeat_led();
|
void longbeat_led();
|
||||||
@@ -34,7 +31,7 @@ void setup() {
|
|||||||
pinMode(ledPin, OUTPUT);
|
pinMode(ledPin, OUTPUT);
|
||||||
|
|
||||||
Homie_setBrand("EtxeanIoT");
|
Homie_setBrand("EtxeanIoT");
|
||||||
Homie_setFirmware("etxean-distancesensor", "1.0.0");
|
Homie_setFirmware("etxean-distancesensor", "1.0.3");
|
||||||
Homie.setLoopFunction(loopHandler);
|
Homie.setLoopFunction(loopHandler);
|
||||||
obstacleNode
|
obstacleNode
|
||||||
.advertise("distance").setName("Distance").setDatatype("float").setUnit("m");
|
.advertise("distance").setName("Distance").setDatatype("float").setUnit("m");
|
||||||
@@ -49,9 +46,10 @@ void loop() {
|
|||||||
Homie.loop();
|
Homie.loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
long lastHeartbeat = 0;
|
long lastPublish = 0;
|
||||||
long lastDistanceSent = 0;
|
long lastMeasurement = 0;
|
||||||
float lastDistance = 0.0;
|
float lastDistance = 0.0;
|
||||||
|
float validDistance = -1;
|
||||||
|
|
||||||
bool checkBounds(float value, float min, float max) {
|
bool checkBounds(float value, float min, float max) {
|
||||||
return !isnan(value) && value >= min && value <= max;
|
return !isnan(value) && value >= min && value <= max;
|
||||||
@@ -67,29 +65,36 @@ String toPayload(bool value)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loopHandler() {
|
void loopHandler() {
|
||||||
if (millis() - lastDistanceSent >= DISTANCE_INTERVAL * 1000UL || lastDistanceSent == 0) {
|
if (millis() - lastMeasurement >= MEASURE_INTERVAL) {
|
||||||
heartbeat_led();
|
|
||||||
float distance = readDistance();
|
float distance = readDistance();
|
||||||
//float distance = random(5,400)/100.0;
|
//float distance = random(5,400)/100.0;
|
||||||
Homie.getLogger() << "Distance: " << distance << " m";
|
Homie.getLogger() << "Distance: " << distance << " m";
|
||||||
obstacleNode.setProperty("distance").send(String(distance));
|
|
||||||
|
|
||||||
bool valid = checkBounds(distance,minDistance,maxDistance);
|
bool valid = checkBounds(distance,minDistance,maxDistance);
|
||||||
Serial << ", valid = " << toPayload(valid);
|
Serial << ", valid = " << toPayload(valid) << endl;
|
||||||
|
if (valid)
|
||||||
|
validDistance = distance;
|
||||||
|
lastMeasurement = millis();
|
||||||
|
}
|
||||||
|
if (millis() - lastPublish >= PUBLISH_INTERVAL) {
|
||||||
|
heartbeat_led();
|
||||||
|
|
||||||
|
bool valid = validDistance > 0;
|
||||||
obstacleNode.setProperty("valid").send(toPayload(valid));
|
obstacleNode.setProperty("valid").send(toPayload(valid));
|
||||||
if (valid)
|
if (valid)
|
||||||
{
|
{
|
||||||
bool changed = signalChange(distance, lastDistance);
|
Homie.getLogger() << "Publish distance: " << validDistance << " m";
|
||||||
|
obstacleNode.setProperty("distance").send(String(validDistance));
|
||||||
|
bool changed = signalChange(validDistance, lastDistance);
|
||||||
obstacleNode.setProperty("changed").send(toPayload(changed));
|
obstacleNode.setProperty("changed").send(toPayload(changed));
|
||||||
if (changed)
|
if (changed)
|
||||||
{
|
{
|
||||||
signal_led();
|
signal_led();
|
||||||
Serial << ", changed";
|
Serial << ", changed" << endl;
|
||||||
}
|
}
|
||||||
lastDistance = distance;
|
lastDistance = validDistance;
|
||||||
|
validDistance = -1;
|
||||||
}
|
}
|
||||||
Serial << endl;
|
lastPublish = millis();
|
||||||
lastDistanceSent = millis();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user