7static const char *
const TAG =
"as3935";
11 LOG_PIN(
" IRQ Pin: ", this->
irq_pin_);
14 this->write_indoor(this->
indoor_);
25 ESP_LOGCONFIG(TAG,
" Antenna tuning: ENABLED - lightning detection will not function in this mode");
28 this->calibrate_oscillator();
32void AS3935Component::dump_config() {
33 ESP_LOGCONFIG(TAG,
"AS3935:");
34 LOG_PIN(
" Interrupt Pin: ", this->
irq_pin_);
35#ifdef USE_BINARY_SENSOR
36 LOG_BINARY_SENSOR(
" ",
"Thunder alert", this->thunder_alert_binary_sensor_);
39 LOG_SENSOR(
" ",
"Distance", this->distance_sensor_);
40 LOG_SENSOR(
" ",
"Lightning energy", this->energy_sensor_);
44void AS3935Component::loop() {
50 ESP_LOGI(TAG,
"Noise was detected - try increasing the noise level value!");
52 ESP_LOGI(TAG,
"Disturber was detected - try increasing the spike rejection value!");
54 ESP_LOGI(TAG,
"Lightning has been detected!");
55#ifdef USE_BINARY_SENSOR
56 if (this->thunder_alert_binary_sensor_ !=
nullptr) {
57 this->thunder_alert_binary_sensor_->publish_state(
true);
58 this->
set_timeout(10, [
this]() { this->thunder_alert_binary_sensor_->publish_state(
false); });
63 if (this->distance_sensor_ !=
nullptr)
64 this->distance_sensor_->publish_state(distance);
67 if (this->energy_sensor_ !=
nullptr)
68 this->energy_sensor_->publish_state(energy);
73void AS3935Component::write_indoor(
bool indoor) {
74 ESP_LOGV(TAG,
"Setting indoor to %d", indoor);
84void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
85 ESP_LOGV(TAG,
"Setting watchdog sensitivity to %d", watchdog_threshold);
86 if ((watchdog_threshold < 1) || (watchdog_threshold > 10))
96void AS3935Component::write_noise_level(uint8_t noise_level) {
97 ESP_LOGV(TAG,
"Setting noise level to %d", noise_level);
98 if ((noise_level < 1) || (noise_level > 7))
108void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
109 ESP_LOGV(TAG,
"Setting spike rejection to %d", spike_rejection);
110 if ((spike_rejection < 1) || (spike_rejection > 11))
119void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
120 ESP_LOGV(TAG,
"Setting lightning threshold to %d", lightning_threshold);
121 switch (lightning_threshold) {
140void AS3935Component::write_mask_disturber(
bool enabled) {
141 ESP_LOGV(TAG,
"Setting mask disturber to %d", enabled);
152void AS3935Component::write_div_ratio(uint8_t div_ratio) {
153 ESP_LOGV(TAG,
"Setting div ratio to %d", div_ratio);
176void AS3935Component::write_capacitance(uint8_t capacitance) {
177 ESP_LOGV(TAG,
"Setting tune cap to %d pF", capacitance * 8);
193 ESP_LOGV(TAG,
"Calling read_interrupt_register_");
203 ESP_LOGV(TAG,
"Calling clear_statistics_");
213 ESP_LOGV(TAG,
"Calling get_distance_to_storm_");
218 ESP_LOGV(TAG,
"Calling get_lightning_energy_");
219 uint32_t pure_light = 0;
224 pure_light = temp << 16;
227 pure_light |= temp << 8;
240uint8_t AS3935Component::read_div_ratio() {
241 ESP_LOGV(TAG,
"Calling read_div_ratio");
247 }
else if (reg_val == 1) {
249 }
else if (reg_val == 2) {
251 }
else if (reg_val == 3) {
254 ESP_LOGW(TAG,
"Unknown response received for div_ratio");
258uint8_t AS3935Component::read_capacitance() {
259 ESP_LOGV(TAG,
"Calling read_capacitance");
269void AS3935Component::display_oscillator(
bool state, uint8_t osc) {
270 if ((osc < 1) || (osc > 3))
280bool AS3935Component::calibrate_oscillator() {
281 ESP_LOGI(TAG,
"Starting oscillators calibration");
284 this->display_oscillator(
true, 2);
286 this->display_oscillator(
false, 2);
296 if (!reg_val_srco && !reg_val_trco) {
297 ESP_LOGI(TAG,
"Calibration was succesful");
300 ESP_LOGW(TAG,
"Calibration was NOT succesful");
305void AS3935Component::tune_antenna() {
306 uint8_t div_ratio = this->read_div_ratio();
307 uint8_t tune_val = this->read_capacitance();
309 "Starting antenna tuning\n"
310 "Division Ratio is set to: %d\n"
311 "Internal Capacitor is set to: %d\n"
312 "Displaying oscillator on INT pin. Measure its frequency - multiply value by Division Ratio",
313 div_ratio, tune_val);
314 this->display_oscillator(
true, ANTFREQ);