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_);
46void AS3935Component::loop() {
52 ESP_LOGI(TAG,
"Noise was detected - try increasing the noise level value!");
54 ESP_LOGI(TAG,
"Disturber was detected - try increasing the spike rejection value!");
56 ESP_LOGI(TAG,
"Lightning has been detected!");
57#ifdef USE_BINARY_SENSOR
58 if (this->thunder_alert_binary_sensor_ !=
nullptr) {
59 this->thunder_alert_binary_sensor_->publish_state(
true);
60 this->
set_timeout(10, [
this]() { this->thunder_alert_binary_sensor_->publish_state(
false); });
65 if (this->distance_sensor_ !=
nullptr)
66 this->distance_sensor_->publish_state(distance);
69 if (this->energy_sensor_ !=
nullptr)
70 this->energy_sensor_->publish_state(energy);
75void AS3935Component::write_indoor(
bool indoor) {
76 ESP_LOGV(TAG,
"Setting indoor to %d", indoor);
86void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
87 ESP_LOGV(TAG,
"Setting watchdog sensitivity to %d", watchdog_threshold);
88 if ((watchdog_threshold < 1) || (watchdog_threshold > 10))
98void AS3935Component::write_noise_level(uint8_t noise_level) {
99 ESP_LOGV(TAG,
"Setting noise level to %d", noise_level);
100 if ((noise_level < 1) || (noise_level > 7))
110void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
111 ESP_LOGV(TAG,
"Setting spike rejection to %d", spike_rejection);
112 if ((spike_rejection < 1) || (spike_rejection > 11))
121void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
122 ESP_LOGV(TAG,
"Setting lightning threshold to %d", lightning_threshold);
123 switch (lightning_threshold) {
142void AS3935Component::write_mask_disturber(
bool enabled) {
143 ESP_LOGV(TAG,
"Setting mask disturber to %d", enabled);
154void AS3935Component::write_div_ratio(uint8_t div_ratio) {
155 ESP_LOGV(TAG,
"Setting div ratio to %d", div_ratio);
178void AS3935Component::write_capacitance(uint8_t capacitance) {
179 ESP_LOGV(TAG,
"Setting tune cap to %d pF", capacitance * 8);
195 ESP_LOGV(TAG,
"Calling read_interrupt_register_");
205 ESP_LOGV(TAG,
"Calling clear_statistics_");
215 ESP_LOGV(TAG,
"Calling get_distance_to_storm_");
220 ESP_LOGV(TAG,
"Calling get_lightning_energy_");
221 uint32_t pure_light = 0;
226 pure_light = temp << 16;
229 pure_light |= temp << 8;
242uint8_t AS3935Component::read_div_ratio() {
243 ESP_LOGV(TAG,
"Calling read_div_ratio");
249 }
else if (reg_val == 1) {
251 }
else if (reg_val == 2) {
253 }
else if (reg_val == 3) {
256 ESP_LOGW(TAG,
"Unknown response received for div_ratio");
260uint8_t AS3935Component::read_capacitance() {
261 ESP_LOGV(TAG,
"Calling read_capacitance");
271void AS3935Component::display_oscillator(
bool state, uint8_t osc) {
272 if ((osc < 1) || (osc > 3))
282bool AS3935Component::calibrate_oscillator() {
283 ESP_LOGI(TAG,
"Starting oscillators calibration");
286 this->display_oscillator(
true, 2);
288 this->display_oscillator(
false, 2);
298 if (!reg_val_srco && !reg_val_trco) {
299 ESP_LOGI(TAG,
"Calibration was succesful");
302 ESP_LOGW(TAG,
"Calibration was NOT succesful");
307void AS3935Component::tune_antenna() {
308 ESP_LOGI(TAG,
"Starting antenna tuning");
309 uint8_t div_ratio = this->read_div_ratio();
310 uint8_t tune_val = this->read_capacitance();
311 ESP_LOGI(TAG,
"Division Ratio is set to: %d", div_ratio);
312 ESP_LOGI(TAG,
"Internal Capacitor is set to: %d", tune_val);
313 ESP_LOGI(TAG,
"Displaying oscillator on INT pin. Measure its frequency - multiply value by Division Ratio");
314 this->display_oscillator(
true, ANTFREQ);