6static const char *
const TAG =
"as3935";
10 LOG_PIN(
" IRQ Pin: ", this->
irq_pin_);
13 this->write_indoor(this->
indoor_);
24 ESP_LOGCONFIG(TAG,
" Antenna tuning: ENABLED - lightning detection will not function in this mode");
27 this->calibrate_oscillator();
31void AS3935Component::dump_config() {
32 ESP_LOGCONFIG(TAG,
"AS3935:");
33 LOG_PIN(
" Interrupt Pin: ", this->
irq_pin_);
34#ifdef USE_BINARY_SENSOR
35 LOG_BINARY_SENSOR(
" ",
"Thunder alert", this->thunder_alert_binary_sensor_);
38 LOG_SENSOR(
" ",
"Distance", this->distance_sensor_);
39 LOG_SENSOR(
" ",
"Lightning energy", this->energy_sensor_);
43void AS3935Component::loop() {
49 ESP_LOGI(TAG,
"Noise was detected - try increasing the noise level value!");
51 ESP_LOGI(TAG,
"Disturber was detected - try increasing the spike rejection value!");
53 ESP_LOGI(TAG,
"Lightning has been detected!");
54#ifdef USE_BINARY_SENSOR
55 if (this->thunder_alert_binary_sensor_ !=
nullptr) {
56 this->thunder_alert_binary_sensor_->publish_state(
true);
57 this->
set_timeout(10, [
this]() { this->thunder_alert_binary_sensor_->publish_state(
false); });
62 if (this->distance_sensor_ !=
nullptr)
63 this->distance_sensor_->publish_state(distance);
66 if (this->energy_sensor_ !=
nullptr)
67 this->energy_sensor_->publish_state(energy);
72void AS3935Component::write_indoor(
bool indoor) {
73 ESP_LOGV(TAG,
"Setting indoor to %d", indoor);
83void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
84 ESP_LOGV(TAG,
"Setting watchdog sensitivity to %d", watchdog_threshold);
85 if ((watchdog_threshold < 1) || (watchdog_threshold > 10))
95void AS3935Component::write_noise_level(uint8_t noise_level) {
96 ESP_LOGV(TAG,
"Setting noise level to %d", noise_level);
97 if ((noise_level < 1) || (noise_level > 7))
107void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
108 ESP_LOGV(TAG,
"Setting spike rejection to %d", spike_rejection);
109 if ((spike_rejection < 1) || (spike_rejection > 11))
118void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
119 ESP_LOGV(TAG,
"Setting lightning threshold to %d", lightning_threshold);
120 switch (lightning_threshold) {
139void AS3935Component::write_mask_disturber(
bool enabled) {
140 ESP_LOGV(TAG,
"Setting mask disturber to %d", enabled);
151void AS3935Component::write_div_ratio(uint8_t div_ratio) {
152 ESP_LOGV(TAG,
"Setting div ratio to %d", div_ratio);
175void AS3935Component::write_capacitance(uint8_t capacitance) {
176 ESP_LOGV(TAG,
"Setting tune cap to %d pF", capacitance * 8);
192 ESP_LOGV(TAG,
"Calling read_interrupt_register_");
202 ESP_LOGV(TAG,
"Calling clear_statistics_");
212 ESP_LOGV(TAG,
"Calling get_distance_to_storm_");
217 ESP_LOGV(TAG,
"Calling get_lightning_energy_");
223 pure_light = temp << 16;
226 pure_light |= temp << 8;
239uint8_t AS3935Component::read_div_ratio() {
240 ESP_LOGV(TAG,
"Calling read_div_ratio");
246 }
else if (reg_val == 1) {
248 }
else if (reg_val == 2) {
250 }
else if (reg_val == 3) {
253 ESP_LOGW(TAG,
"Unknown response received for div_ratio");
257uint8_t AS3935Component::read_capacitance() {
258 ESP_LOGV(TAG,
"Calling read_capacitance");
268void AS3935Component::display_oscillator(
bool state, uint8_t osc) {
269 if ((osc < 1) || (osc > 3))
279bool AS3935Component::calibrate_oscillator() {
280 ESP_LOGI(TAG,
"Starting oscillators calibration");
283 this->display_oscillator(
true, 2);
285 this->display_oscillator(
false, 2);
295 if (!reg_val_srco && !reg_val_trco) {
296 ESP_LOGI(TAG,
"Calibration was succesful");
299 ESP_LOGW(TAG,
"Calibration was NOT succesful");
304void AS3935Component::tune_antenna() {
305 uint8_t div_ratio = this->read_div_ratio();
306 uint8_t tune_val = this->read_capacitance();
308 "Starting antenna tuning\n"
309 " Division Ratio is set to: %d\n"
310 " Internal Capacitor is set to: %d\n"
311 " Displaying oscillator on INT pin. Measure its frequency - multiply value by Division Ratio",
312 div_ratio, tune_val);
313 this->display_oscillator(
true, ANTFREQ);