7 static const char *
const TAG =
"as3935";
10 ESP_LOGCONFIG(TAG,
"Setting up AS3935...");
13 LOG_PIN(
" IRQ Pin: ", this->
irq_pin_);
16 this->write_indoor(this->
indoor_);
27 ESP_LOGCONFIG(TAG,
" Antenna tuning: ENABLED - lightning detection will not function in this mode");
30 this->calibrate_oscillator();
34 void AS3935Component::dump_config() {
35 ESP_LOGCONFIG(TAG,
"AS3935:");
36 LOG_PIN(
" Interrupt Pin: ", this->
irq_pin_);
37 #ifdef USE_BINARY_SENSOR 38 LOG_BINARY_SENSOR(
" ",
"Thunder alert", this->thunder_alert_binary_sensor_);
41 LOG_SENSOR(
" ",
"Distance", this->distance_sensor_);
42 LOG_SENSOR(
" ",
"Lightning energy", this->energy_sensor_);
48 void AS3935Component::loop() {
54 ESP_LOGI(TAG,
"Noise was detected - try increasing the noise level value!");
56 ESP_LOGI(TAG,
"Disturber was detected - try increasing the spike rejection value!");
58 ESP_LOGI(TAG,
"Lightning has been detected!");
59 #ifdef USE_BINARY_SENSOR 60 if (this->thunder_alert_binary_sensor_ !=
nullptr) {
61 this->thunder_alert_binary_sensor_->publish_state(
true);
62 this->
set_timeout(10, [
this]() { this->thunder_alert_binary_sensor_->publish_state(
false); });
67 if (this->distance_sensor_ !=
nullptr)
68 this->distance_sensor_->publish_state(distance);
71 if (this->energy_sensor_ !=
nullptr)
72 this->energy_sensor_->publish_state(energy);
77 void AS3935Component::write_indoor(
bool indoor) {
78 ESP_LOGV(TAG,
"Setting indoor to %d", indoor);
88 void AS3935Component::write_watchdog_threshold(uint8_t watchdog_threshold) {
89 ESP_LOGV(TAG,
"Setting watchdog sensitivity to %d", watchdog_threshold);
90 if ((watchdog_threshold < 1) || (watchdog_threshold > 10))
100 void AS3935Component::write_noise_level(uint8_t noise_level) {
101 ESP_LOGV(TAG,
"Setting noise level to %d", noise_level);
102 if ((noise_level < 1) || (noise_level > 7))
112 void AS3935Component::write_spike_rejection(uint8_t spike_rejection) {
113 ESP_LOGV(TAG,
"Setting spike rejection to %d", spike_rejection);
114 if ((spike_rejection < 1) || (spike_rejection > 11))
123 void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
124 ESP_LOGV(TAG,
"Setting lightning threshold to %d", lightning_threshold);
125 switch (lightning_threshold) {
144 void AS3935Component::write_mask_disturber(
bool enabled) {
145 ESP_LOGV(TAG,
"Setting mask disturber to %d", enabled);
156 void AS3935Component::write_div_ratio(uint8_t div_ratio) {
157 ESP_LOGV(TAG,
"Setting div ratio to %d", div_ratio);
180 void AS3935Component::write_capacitance(uint8_t capacitance) {
181 ESP_LOGV(TAG,
"Setting tune cap to %d pF", capacitance * 8);
197 ESP_LOGV(TAG,
"Calling read_interrupt_register_");
207 ESP_LOGV(TAG,
"Calling clear_statistics_");
217 ESP_LOGV(TAG,
"Calling get_distance_to_storm_");
222 ESP_LOGV(TAG,
"Calling get_lightning_energy_");
223 uint32_t pure_light = 0;
228 pure_light = temp << 16;
231 pure_light |= temp << 8;
244 uint8_t AS3935Component::read_div_ratio() {
245 ESP_LOGV(TAG,
"Calling read_div_ratio");
251 }
else if (reg_val == 1) {
253 }
else if (reg_val == 2) {
255 }
else if (reg_val == 3) {
258 ESP_LOGW(TAG,
"Unknown response received for div_ratio");
262 uint8_t AS3935Component::read_capacitance() {
263 ESP_LOGV(TAG,
"Calling read_capacitance");
273 void AS3935Component::display_oscillator(
bool state, uint8_t osc) {
274 if ((osc < 1) || (osc > 3))
284 bool AS3935Component::calibrate_oscillator() {
285 ESP_LOGI(TAG,
"Starting oscillators calibration...");
288 this->display_oscillator(
true, 2);
290 this->display_oscillator(
false, 2);
300 if (!reg_val_srco && !reg_val_trco) {
301 ESP_LOGI(TAG,
"Calibration was succesful");
304 ESP_LOGW(TAG,
"Calibration was NOT succesful");
309 void AS3935Component::tune_antenna() {
310 ESP_LOGI(TAG,
"Starting antenna tuning...");
311 uint8_t div_ratio = this->read_div_ratio();
312 uint8_t tune_val = this->read_capacitance();
313 ESP_LOGI(TAG,
"Division Ratio is set to: %d", div_ratio);
314 ESP_LOGI(TAG,
"Internal Capacitor is set to: %d", tune_val);
315 ESP_LOGI(TAG,
"Displaying oscillator on INT pin. Measure its frequency - multiply value by Division Ratio");
316 this->display_oscillator(
true, ANTFREQ);
virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position)=0
const float DATA
For components that import data from directly connected sensors like DHT.
uint8_t get_distance_to_storm_()
uint8_t watchdog_threshold_
uint32_t get_lightning_energy_()
virtual uint8_t read_register(uint8_t reg)=0
void set_timeout(const std::string &name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
uint8_t read_register_(uint8_t reg, uint8_t mask)
virtual void setup()
Where the component's initialization should happen.
uint8_t read_interrupt_register_()
virtual bool digital_read()=0
uint8_t lightning_threshold_
Implementation of SPI Controller mode.
void IRAM_ATTR HOT delay(uint32_t ms)