Calibrate distance calculation with temperature
This commit is contained in:
@@ -61,6 +61,7 @@ void PingNode::send()
|
|||||||
setProperty(cStatusTopic).send(valid ? "ok" : "error");
|
setProperty(cStatusTopic).send(valid ? "ok" : "error");
|
||||||
if (valid) {
|
if (valid) {
|
||||||
setProperty(cDistanceTopic).send(String(_distance));
|
setProperty(cDistanceTopic).send(String(_distance));
|
||||||
|
setProperty(cPingTopic).send(String(_ping_us));
|
||||||
setProperty(cChangedTopic).send(changed ? "true": "false");
|
setProperty(cChangedTopic).send(changed ? "true": "false");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ void PingNode::loop()
|
|||||||
{
|
{
|
||||||
float ping_us = sonar->ping_median();
|
float ping_us = sonar->ping_median();
|
||||||
// Calculating the distance @ 10 °C from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
|
// Calculating the distance @ 10 °C from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
|
||||||
float newDistance = ping_us*0.0001685;
|
float newDistance = ping_us*_microseconds2meter;
|
||||||
fixRange(&newDistance, cMinDistance, cMaxDistance);
|
fixRange(&newDistance, cMinDistance, cMaxDistance);
|
||||||
if (newDistance > 0) {
|
if (newDistance > 0) {
|
||||||
_ping_us = ping_us;
|
_ping_us = ping_us;
|
||||||
@@ -118,7 +119,11 @@ void PingNode::setup()
|
|||||||
void PingNode::setMicrosecondsToMetersFactor(float temperatureCelcius)
|
void PingNode::setMicrosecondsToMetersFactor(float temperatureCelcius)
|
||||||
{
|
{
|
||||||
//float soundSpeed = 337.0; // @ 10°C
|
//float soundSpeed = 337.0; // @ 10°C
|
||||||
float soundSpeed = 331.4 * 0.6*temperatureCelcius;
|
float soundSpeed = 331.4 + 0.6*temperatureCelcius;
|
||||||
|
printCaption();
|
||||||
|
Homie.getLogger() << cIndent
|
||||||
|
<< "SpeedOfSound: " << soundSpeed << " " << cUnitMetersPerSecond
|
||||||
|
<< " at " << temperatureCelcius << " " << cUnitDegrees << endl;
|
||||||
// Calculating the distance from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
|
// Calculating the distance from d = t_ping /2 * c => t_ping /2 * 337 [m/s] => t_ping_us / 1e-6 * 1/2 * 337
|
||||||
_microseconds2meter = 0.5e-6 * soundSpeed;
|
_microseconds2meter = 0.5e-6 * soundSpeed;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ private:
|
|||||||
|
|
||||||
NewPing* sonar;
|
NewPing* sonar;
|
||||||
float _distance = NAN;
|
float _distance = NAN;
|
||||||
int _ping_us = NAN;
|
int _ping_us = 0;
|
||||||
float _lastDistance = 0;
|
float _lastDistance = 0;
|
||||||
ChangeHandler _changeHandler = [](){};
|
ChangeHandler _changeHandler = [](){};
|
||||||
|
|
||||||
|
|||||||
@@ -36,8 +36,14 @@ HomieInternals::Uptime relayUptime;
|
|||||||
|
|
||||||
bool RelayNode::handleOnOff(const String &value)
|
bool RelayNode::handleOnOff(const String &value)
|
||||||
{
|
{
|
||||||
if (value == "true" || value == "false")
|
if (value == "true" || value == "false" || value == "toggle")
|
||||||
{
|
{
|
||||||
|
if (value == "toggle")
|
||||||
|
{
|
||||||
|
bool current = getRelayState();
|
||||||
|
setRelay(!current);
|
||||||
|
}
|
||||||
|
else
|
||||||
setRelay(value == "true");
|
setRelay(value == "true");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#define cUnitPercent "%"
|
#define cUnitPercent "%"
|
||||||
#define cUnitVolt "V"
|
#define cUnitVolt "V"
|
||||||
#define cUnitMeter "m"
|
#define cUnitMeter "m"
|
||||||
|
#define cUnitMetersPerSecond "m/s"
|
||||||
#define cUnitMicrosecond "μs"
|
#define cUnitMicrosecond "μs"
|
||||||
|
|
||||||
// Topics
|
// Topics
|
||||||
|
|||||||
15
src/main.cpp
15
src/main.cpp
@@ -1,7 +1,7 @@
|
|||||||
#include <Homie.h>
|
#include <Homie.h>
|
||||||
|
|
||||||
#include "PingNode.hpp"
|
#include "PingNode.hpp"
|
||||||
#include "RelayNode.hpp"
|
//#include "RelayNode.hpp"
|
||||||
#include "DHT22Node.hpp"
|
#include "DHT22Node.hpp"
|
||||||
|
|
||||||
#define TURN_ON LOW
|
#define TURN_ON LOW
|
||||||
@@ -9,16 +9,19 @@
|
|||||||
|
|
||||||
const int trigPin = D1;
|
const int trigPin = D1;
|
||||||
const int echoPin = D2;
|
const int echoPin = D2;
|
||||||
const int relayPin = 14; // D5
|
const int relayPin = D5;
|
||||||
const int dhtPin = D7; ;
|
const int dhtPin = D7; ;
|
||||||
const int ledPin = LED_BUILTIN;
|
const int ledPin = LED_BUILTIN;
|
||||||
|
|
||||||
unsigned long HEARTBEAT_INTERVAL = 5;
|
unsigned long HEARTBEAT_INTERVAL = 5;
|
||||||
unsigned long lastHeartbeat = 0;
|
unsigned long lastHeartbeat = 0;
|
||||||
|
unsigned long TEMPERATURE_INTERVAL = 120;
|
||||||
|
unsigned long lastTemperatureUpdate = 0;
|
||||||
|
|
||||||
|
|
||||||
PingNode obstacleNode("obstacle",trigPin,echoPin);
|
PingNode obstacleNode("obstacle",trigPin,echoPin);
|
||||||
DHT22Node airNode("air",dhtPin,20);
|
DHT22Node airNode("air",dhtPin,TEMPERATURE_INTERVAL);
|
||||||
RelayNode relayNode("relay",relayPin,ledPin);
|
//RelayNode relayNode("relay",relayPin,ledPin);
|
||||||
|
|
||||||
void signal_led(bool on = true);
|
void signal_led(bool on = true);
|
||||||
void heartbeat_led(int times = 2);
|
void heartbeat_led(int times = 2);
|
||||||
@@ -32,6 +35,10 @@ void loopHandler() {
|
|||||||
heartbeat_led();
|
heartbeat_led();
|
||||||
lastHeartbeat = millis();
|
lastHeartbeat = millis();
|
||||||
}
|
}
|
||||||
|
if (millis() - lastTemperatureUpdate > TEMPERATURE_INTERVAL * 1000UL || lastTemperatureUpdate == 0) {
|
||||||
|
obstacleNode.setTemperature(airNode.getTemperature());
|
||||||
|
lastTemperatureUpdate = millis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|||||||
Reference in New Issue
Block a user