1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-07 10:31:49 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Otto Winter
e4bbb56f6b Apply removed patch 2019-05-29 11:36:31 +02:00
Otto Winter
96d30e28d4 Bump version to v1.13.0b5 2019-05-29 11:22:10 +02:00
Jesse Hills
41b73ff892 Add CT Clamp component (#559)
* Add CT Clamp component

* Update lint

* Some more fixes

* Make updates to work as an analog sensor consumer

* Remove unused imports
Update lint suggestions

* Move setup_priority to header

* Remove unused calibration value

* Remove Unique ID

- Will be auto generated

* Update to use loop and not slow down main loop

Co-authored-by: Otto Winter <otto@otto-winter.com>
2019-05-29 11:22:00 +02:00
Otto Winter
afc4e45fb0 Warn if a component does long-running work in loop thread (#565)
* Warn if a component does long-running work in loop thread

* Update application.cpp
2019-05-29 11:21:15 +02:00
gitolicious
8778ddd5c5 Renamed upload button (#563)
* Renamed upload button

* Renamed upload button in code
2019-05-29 11:17:56 +02:00
12 changed files with 199 additions and 36 deletions

View File

View File

@@ -0,0 +1,67 @@
#include "ct_clamp_sensor.h"
#include "esphome/core/log.h"
#include <cmath>
namespace esphome {
namespace ct_clamp {
static const char *TAG = "ct_clamp";
void CTClampSensor::dump_config() {
LOG_SENSOR("", "CT Clamp Sensor", this);
ESP_LOGCONFIG(TAG, " Sample Duration: %.2fs", this->sample_duration_ / 1e3f);
LOG_UPDATE_INTERVAL(this);
}
void CTClampSensor::update() {
// Update only starts the sampling phase, in loop() the actual sampling is happening.
// Request a high loop() execution interval during sampling phase.
this->high_freq_.start();
// Set timeout for ending sampling phase
this->set_timeout("read", this->sample_duration_, [this]() {
this->is_sampling_ = false;
this->high_freq_.stop();
if (this->num_samples_ == 0) {
// Shouldn't happen, but let's not crash if it does.
this->publish_state(NAN);
return;
}
float raw = this->sample_sum_ / this->num_samples_;
float irms = std::sqrt(raw);
ESP_LOGD(TAG, "'%s' - Raw Value: %.2fA", this->name_.c_str(), irms);
this->publish_state(irms);
});
// Set sampling values
this->is_sampling_ = true;
this->num_samples_ = 0;
this->sample_sum_ = 0.0f;
}
void CTClampSensor::loop() {
if (!this->is_sampling_)
return;
// Perform a single sample
float value = this->source_->sample();
// Adjust DC offset via low pass filter (exponential moving average)
const float alpha = 0.001f;
this->offset_ = this->offset_ * (1 - alpha) + value * alpha;
// Filtered value centered around the mid-point (0V)
float filtered = value - this->offset_;
// IRMS is sqrt(∑v_i²)
float sq = filtered * filtered;
this->sample_sum_ += sq;
this->num_samples_++;
}
} // namespace ct_clamp
} // namespace esphome

View File

@@ -0,0 +1,46 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/esphal.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
namespace esphome {
namespace ct_clamp {
class CTClampSensor : public sensor::Sensor, public PollingComponent {
public:
void update() override;
void loop() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_sample_duration(uint32_t sample_duration) { sample_duration_ = sample_duration; }
void set_source(voltage_sampler::VoltageSampler *source) { source_ = source; }
protected:
/// High Frequency loop() requester used during sampling phase.
HighFrequencyLoopRequester high_freq_;
/// Duration in ms of the sampling phase.
uint32_t sample_duration_;
/// The sampling source to read values from.
voltage_sampler::VoltageSampler *source_;
/** The DC offset of the circuit.
*
* Diagram: https://learn.openenergymonitor.org/electricity-monitoring/ct-sensors/interface-with-arduino
*
* This is automatically calculated with an exponential moving average/digital low pass filter.
*
* 0.5 is a good initial approximation to start with for most ESP8266 setups.
*/
float offset_ = 0.5f;
float sample_sum_ = 0.0f;
uint32_t num_samples_ = 0;
bool is_sampling_ = false;
};
} // namespace ct_clamp
} // namespace esphome

View File

@@ -0,0 +1,27 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import CONF_SENSOR, CONF_ID, ICON_FLASH, UNIT_AMPERE
AUTO_LOAD = ['voltage_sampler']
CONF_SAMPLE_DURATION = 'sample_duration'
ct_clamp_ns = cg.esphome_ns.namespace('ct_clamp')
CTClampSensor = ct_clamp_ns.class_('CTClampSensor', sensor.Sensor, cg.PollingComponent)
CONFIG_SCHEMA = sensor.sensor_schema(UNIT_AMPERE, ICON_FLASH, 2).extend({
cv.GenerateID(): cv.declare_id(CTClampSensor),
cv.Required(CONF_SENSOR): cv.use_id(voltage_sampler.VoltageSampler),
cv.Optional(CONF_SAMPLE_DURATION, default='200ms'): cv.positive_time_period_milliseconds,
}).extend(cv.polling_component_schema('60s'))
def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
yield cg.register_component(var, config)
yield sensor.register_sensor(var, config)
sens = yield cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_source(sens))
cg.add(var.set_sample_duration(config[CONF_SAMPLE_DURATION]))

View File

@@ -24,7 +24,7 @@ def validate_calibration_parameter(value):
return cv.Schema({
cv.Required(CONF_TEMPERATURE): cv.float_,
cv.Required(CONF_VALUE): cv.float_,
})
})(value)
value = cv.string(value)
parts = value.split('->')

View File

@@ -61,6 +61,7 @@ RESERVED_IDS = [
'App', 'pinMode', 'delay', 'delayMicroseconds', 'digitalRead', 'digitalWrite', 'INPUT',
'OUTPUT',
'uint8_t', 'uint16_t', 'uint32_t', 'uint64_t', 'int8_t', 'int16_t', 'int32_t', 'int64_t',
'close', 'pause', 'sleep', 'open',
]

View File

@@ -3,7 +3,7 @@
MAJOR_VERSION = 1
MINOR_VERSION = 13
PATCH_VERSION = '0b4'
PATCH_VERSION = '0b5'
__short_version__ = '{}.{}'.format(MAJOR_VERSION, MINOR_VERSION)
__version__ = '{}.{}'.format(__short_version__, PATCH_VERSION)

View File

@@ -66,6 +66,42 @@ void Application::dump_config() {
component->dump_config();
}
}
void Application::loop() {
uint32_t new_app_state = 0;
const uint32_t start = millis();
for (Component *component : this->components_) {
if (!component->is_failed()) {
component->call_loop();
}
new_app_state |= component->get_component_state();
this->app_state_ |= new_app_state;
this->feed_wdt();
}
this->app_state_ = new_app_state;
const uint32_t end = millis();
if (end - start > 200) {
ESP_LOGV(TAG, "A component took a long time in a loop() cycle (%.1f s).", (end - start) / 1e3f);
ESP_LOGV(TAG, "Components should block for at most 20-30ms in loop().");
ESP_LOGV(TAG, "This will become a warning soon.");
}
const uint32_t now = millis();
if (HighFrequencyLoopRequester::is_high_frequency()) {
yield();
} else {
uint32_t delay_time = this->loop_interval_;
if (now - this->last_loop_ < this->loop_interval_)
delay_time = this->loop_interval_ - (now - this->last_loop_);
delay(delay_time);
}
this->last_loop_ = now;
if (this->dump_config_scheduled_) {
this->dump_config();
this->dump_config_scheduled_ = false;
}
}
void ICACHE_RAM_ATTR HOT Application::feed_wdt() {
static uint32_t LAST_FEED = 0;

View File

@@ -87,34 +87,7 @@ class Application {
void setup();
/// Make a loop iteration. Call this in your loop() function.
void loop() {
uint32_t new_app_state = 0;
for (Component *component : this->components_) {
if (!component->is_failed()) {
component->call_loop();
}
new_app_state |= component->get_component_state();
this->app_state_ |= new_app_state;
this->feed_wdt();
}
this->app_state_ = new_app_state;
const uint32_t now = millis();
if (HighFrequencyLoopRequester::is_high_frequency()) {
yield();
} else {
uint32_t delay_time = this->loop_interval_;
if (now - this->last_loop_ < this->loop_interval_)
delay_time = this->loop_interval_ - (now - this->last_loop_);
delay(delay_time);
}
this->last_loop_ = now;
if (this->dump_config_scheduled_) {
this->dump_config();
this->dump_config_scheduled_ = false;
}
}
void loop();
/// Get the name of this Application set by set_name().
const std::string &get_name() const { return this->name_; }

View File

@@ -607,9 +607,9 @@ const startAceWebsocket = () => {
editor.session.setAnnotations(arr);
if(arr.length) {
saveUploadButton.classList.add('disabled');
editorUploadButton.classList.add('disabled');
} else {
saveUploadButton.classList.remove('disabled');
editorUploadButton.classList.remove('disabled');
}
aceValidationRunning = false;
@@ -646,7 +646,7 @@ editor.session.setOption('tabSize', 2);
editor.session.setOption('useWorker', false);
const saveButton = editModalElem.querySelector(".save-button");
const saveUploadButton = editModalElem.querySelector(".save-upload-button");
const editorUploadButton = editModalElem.querySelector(".editor-upload-button");
const saveEditor = () => {
fetch(`./edit?configuration=${activeEditorConfig}`, {
credentials: "same-origin",
@@ -698,14 +698,14 @@ setInterval(() => {
}, 100);
saveButton.addEventListener('click', saveEditor);
saveUploadButton.addEventListener('click', saveEditor);
editorUploadButton.addEventListener('click', saveEditor);
document.querySelectorAll(".action-edit").forEach((btn) => {
btn.addEventListener('click', (e) => {
activeEditorConfig = e.target.getAttribute('data-node');
const modalInstance = M.Modal.getInstance(editModalElem);
const filenameField = editModalElem.querySelector('.filename');
editModalElem.querySelector(".save-upload-button").setAttribute('data-node', activeEditorConfig);
editorUploadButton.setAttribute('data-node', activeEditorConfig);
filenameField.innerHTML = activeEditorConfig;
fetch(`./edit?configuration=${activeEditorConfig}`, {credentials: "same-origin"})

View File

@@ -440,7 +440,7 @@
</div>
<div class="modal-footer">
<a class="waves-effect waves-green btn-flat save-button">Save</a>
<a class="modal-close waves-effect waves-green btn-flat action-upload save-upload-button">Save &amp; Upload</a>
<a class="modal-close waves-effect waves-green btn-flat action-upload editor-upload-button">Upload</a>
<a class="modal-close waves-effect waves-green btn-flat">Close</a>
</div>
</div>

View File

@@ -129,6 +129,19 @@ sensor:
b_constant: 3950
reference_resistance: 10k
reference_temperature: 25°C
- platform: ntc
sensor: resist
name: NTC Sensor2
calibration:
- 10.0kOhm -> 25°C
- 27.219kOhm -> 0°C
- 14.674kOhm -> 15°C
- platform: ct_clamp
sensor: my_sensor
name: CT Clamp
sample_duration: 500ms
update_interval: 5s
- platform: tcs34725
red_channel:
name: Red Channel