ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
as3935.cpp
Go to the documentation of this file.
1#include "as3935.h"
2#include "esphome/core/log.h"
3
4namespace esphome::as3935 {
5
6static const char *const TAG = "as3935";
7
9 this->irq_pin_->setup();
10 LOG_PIN(" IRQ Pin: ", this->irq_pin_);
11
12 // Write properties to sensor
13 this->write_indoor(this->indoor_);
14 this->write_noise_level(this->noise_level_);
15 this->write_watchdog_threshold(this->watchdog_threshold_);
16 this->write_spike_rejection(this->spike_rejection_);
17 this->write_lightning_threshold(this->lightning_threshold_);
18 this->write_mask_disturber(this->mask_disturber_);
19 this->write_div_ratio(this->div_ratio_);
20 this->write_capacitance(this->capacitance_);
21
22 // Handle setting up tuning or auto-calibration
23 if (this->tune_antenna_) {
24 ESP_LOGCONFIG(TAG, " Antenna tuning: ENABLED - lightning detection will not function in this mode");
25 this->tune_antenna();
26 } else if (this->calibration_) {
27 this->calibrate_oscillator();
28 }
29}
30
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_);
36#endif
37#ifdef USE_SENSOR
38 LOG_SENSOR(" ", "Distance", this->distance_sensor_);
39 LOG_SENSOR(" ", "Lightning energy", this->energy_sensor_);
40#endif
41}
42
43void AS3935Component::loop() {
44 if (!this->irq_pin_->digital_read())
45 return;
46
47 uint8_t int_value = this->read_interrupt_register_();
48 if (int_value == NOISE_INT) {
49 ESP_LOGI(TAG, "Noise was detected - try increasing the noise level value!");
50 } else if (int_value == DISTURBER_INT) {
51 ESP_LOGI(TAG, "Disturber was detected - try increasing the spike rejection value!");
52 } else if (int_value == LIGHTNING_INT) {
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); });
58 }
59#endif
60#ifdef USE_SENSOR
61 uint8_t distance = this->get_distance_to_storm_();
62 if (this->distance_sensor_ != nullptr)
63 this->distance_sensor_->publish_state(distance);
64
65 uint32_t energy = this->get_lightning_energy_();
66 if (this->energy_sensor_ != nullptr)
67 this->energy_sensor_->publish_state(energy);
68#endif
69 }
70}
71
72void AS3935Component::write_indoor(bool indoor) {
73 ESP_LOGV(TAG, "Setting indoor to %d", indoor);
74 if (indoor) {
76 } else {
78 }
79}
80// REG0x01, bits[3:0], manufacturer default: 0010 (2).
81// This setting determines the threshold for events that trigger the
82// IRQ Pin.
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)) // 10 is the max sensitivity setting
86 return;
87 this->write_register(THRESHOLD, THRESH_MASK, watchdog_threshold, 0);
88}
89
90// REG0x01, bits [6:4], manufacturer default: 010 (2).
91// The noise floor level is compared to a known reference voltage. If this
92// level is exceeded the chip will issue an interrupt to the IRQ pin,
93// broadcasting that it can not operate properly due to noise (INT_NH).
94// Check datasheet for specific noise level tolerances when setting this register.
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))
98 return;
99
100 this->write_register(THRESHOLD, NOISE_FLOOR_MASK, noise_level, 4);
101}
102// REG0x02, bits [3:0], manufacturer default: 0010 (2).
103// This setting, like the watchdog threshold, can help determine between false
104// events and actual lightning. The shape of the spike is analyzed during the
105// chip's signal validation routine. Increasing this value increases robustness
106// at the cost of sensitivity to distant events.
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))
110 return;
111
112 this->write_register(LIGHTNING_REG, SPIKE_MASK, spike_rejection, 0);
113}
114// REG0x02, bits [5:4], manufacturer default: 0 (single lightning strike).
115// The number of lightning events before IRQ is set high. 15 minutes is The
116// window of time before the number of detected lightning events is reset.
117// The number of lightning strikes can be set to 1,5,9, or 16.
118void AS3935Component::write_lightning_threshold(uint8_t lightning_threshold) {
119 ESP_LOGV(TAG, "Setting lightning threshold to %d", lightning_threshold);
120 switch (lightning_threshold) {
121 case 1:
122 this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 0, 4); // Demonstrative
123 break;
124 case 5:
125 this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 1, 4);
126 break;
127 case 9:
128 this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 1, 5);
129 break;
130 case 16:
131 this->write_register(LIGHTNING_REG, ((1 << 5) | (1 << 4)), 3, 4);
132 break;
133 default:
134 return;
135 }
136}
137// REG0x03, bit [5], manufacturer default: 0.
138// This setting will return whether or not disturbers trigger the IRQ Pin.
139void AS3935Component::write_mask_disturber(bool enabled) {
140 ESP_LOGV(TAG, "Setting mask disturber to %d", enabled);
141 if (enabled) {
142 this->write_register(INT_MASK_ANT, (1 << 5), 1, 5);
143 } else {
144 this->write_register(INT_MASK_ANT, (1 << 5), 0, 5);
145 }
146}
147// REG0x03, bit [7:6], manufacturer default: 0 (16 division ratio).
148// The antenna is designed to resonate at 500kHz and so can be tuned with the
149// following setting. The accuracy of the antenna must be within 3.5 percent of
150// that value for proper signal validation and distance estimation.
151void AS3935Component::write_div_ratio(uint8_t div_ratio) {
152 ESP_LOGV(TAG, "Setting div ratio to %d", div_ratio);
153 switch (div_ratio) {
154 case 16:
155 this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 0, 6);
156 break;
157 case 22:
158 this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 1, 6);
159 break;
160 case 64:
161 this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 1, 7);
162 break;
163 case 128:
164 this->write_register(INT_MASK_ANT, ((1 << 7) | (1 << 6)), 3, 6);
165 break;
166 default:
167 return;
168 }
169}
170// REG0x08, bits [3:0], manufacturer default: 0.
171// This setting will add capacitance to the series RLC antenna on the product
172// to help tune its resonance. The datasheet specifies being within 3.5 percent
173// of 500kHz to get optimal lightning detection and distance sensing.
174// It's possible to add up to 120pF in steps of 8pF to the antenna.
175void AS3935Component::write_capacitance(uint8_t capacitance) {
176 ESP_LOGV(TAG, "Setting tune cap to %d pF", capacitance * 8);
177 this->write_register(FREQ_DISP_IRQ, CAP_MASK, capacitance, 0);
178}
179
180// REG0x03, bits [3:0], manufacturer default: 0.
181// When there is an event that exceeds the watchdog threshold, the register is written
182// with the type of event. This consists of two messages: INT_D (disturber detected) and
183// INT_L (Lightning detected). A third interrupt INT_NH (noise level too HIGH)
184// indicates that the noise level has been exceeded and will persist until the
185// noise has ended. Events are active HIGH. There is a one second window of time to
186// read the interrupt register after lightning is detected, and 1.5 after
187// disturber.
189 // A 2ms delay is added to allow for the memory register to be populated
190 // after the interrupt pin goes HIGH. See "Interrupt Management" in
191 // datasheet.
192 ESP_LOGV(TAG, "Calling read_interrupt_register_");
193 delay(2);
194 return this->read_register_(INT_MASK_ANT, INT_MASK);
195}
196
197// REG0x02, bit [6], manufacturer default: 1.
198// This register clears the number of lightning strikes that has been read in
199// the last 15 minute block.
201 // Write high, then low, then high to clear.
202 ESP_LOGV(TAG, "Calling clear_statistics_");
203 this->write_register(LIGHTNING_REG, (1 << 6), 1, 6);
204 this->write_register(LIGHTNING_REG, (1 << 6), 0, 6);
205 this->write_register(LIGHTNING_REG, (1 << 6), 1, 6);
206}
207
208// REG0x07, bit [5:0], manufacturer default: 0.
209// This register holds the distance to the front of the storm and not the
210// distance to a lightning strike.
212 ESP_LOGV(TAG, "Calling get_distance_to_storm_");
214}
215
217 ESP_LOGV(TAG, "Calling get_lightning_energy_");
218 uint32_t pure_light = 0; // Variable for lightning energy which is just a pure number.
219 uint32_t temp = 0;
220 // Temp variable for lightning energy.
222 // Temporary Value is large enough to handle a shift of 16 bits.
223 pure_light = temp << 16;
224 temp = this->read_register(ENERGY_LIGHT_MSB);
225 // Temporary value is large enough to handle a shift of 8 bits.
226 pure_light |= temp << 8;
227 // No shift here, directly OR'ed into pure_light variable.
228 temp = this->read_register(ENERGY_LIGHT_LSB);
229 pure_light |= temp;
230 return pure_light;
231}
232
233// REG0x03, bit [7:6], manufacturer default: 0 (16 division ratio).
234// This function returns the current division ratio of the resonance frequency.
235// The antenna resonance frequency should be within 3.5 percent of 500kHz, and
236// so when modifying the resonance frequency with the internal capacitors
237// (tuneCap()) it's important to keep in mind that the displayed frequency on
238// the IRQ pin is divided by this number.
239uint8_t AS3935Component::read_div_ratio() {
240 ESP_LOGV(TAG, "Calling read_div_ratio");
241 uint8_t reg_val = this->read_register_(INT_MASK_ANT, DIV_MASK);
242 reg_val >>= 6; // Front of the line.
243
244 if (reg_val == 0) {
245 return 16;
246 } else if (reg_val == 1) {
247 return 32;
248 } else if (reg_val == 2) {
249 return 64;
250 } else if (reg_val == 3) {
251 return 128;
252 }
253 ESP_LOGW(TAG, "Unknown response received for div_ratio");
254 return 0;
255}
256
257uint8_t AS3935Component::read_capacitance() {
258 ESP_LOGV(TAG, "Calling read_capacitance");
259 uint8_t reg_val = this->read_register_(FREQ_DISP_IRQ, CAP_MASK) * 8;
260 return (reg_val);
261}
262
263// REG0x08, bits [5,6,7], manufacturer default: 0.
264// This will send the frequency of the oscillators to the IRQ pin.
265// _osc 1, bit[5] = TRCO - System RCO at 32.768kHz
266// _osc 2, bit[6] = SRCO - Timer RCO Oscillators 1.1MHz
267// _osc 3, bit[7] = LCO - Frequency of the Antenna
268void AS3935Component::display_oscillator(bool state, uint8_t osc) {
269 if ((osc < 1) || (osc > 3))
270 return;
271
272 this->write_register(FREQ_DISP_IRQ, OSC_MASK, state, 4 + osc);
273}
274
275// REG0x3D, bits[7:0]
276// This function calibrates both internal oscillators The oscillators are tuned
277// based on the resonance frequency of the antenna and so it should be trimmed
278// before the calibration is done.
279bool AS3935Component::calibrate_oscillator() {
280 ESP_LOGI(TAG, "Starting oscillators calibration");
281 this->write_register(CALIB_RCO, WIPE_ALL, DIRECT_COMMAND, 0); // Send command to calibrate the oscillators
282
283 this->display_oscillator(true, 2);
284 delay(2); // Give time for the internal oscillators to start up.
285 this->display_oscillator(false, 2);
286
287 // Check it they were calibrated successfully.
288 uint8_t reg_val_srco = this->read_register_(CALIB_SRCO, CALIB_MASK_NOK);
289 uint8_t reg_val_trco = this->read_register_(CALIB_TRCO, CALIB_MASK_NOK);
290
291 // reg_val_srco &= CALIB_MASK;
292 // reg_val_srco >>= 6;
293 // reg_val_trco &= CALIB_MASK;
294 // reg_val_trco >>= 6;
295 if (!reg_val_srco && !reg_val_trco) { // Zero upon success
296 ESP_LOGI(TAG, "Calibration was succesful");
297 return true;
298 } else {
299 ESP_LOGW(TAG, "Calibration was NOT succesful");
300 return false;
301 }
302}
303
304void AS3935Component::tune_antenna() {
305 uint8_t div_ratio = this->read_div_ratio();
306 uint8_t tune_val = this->read_capacitance();
307 ESP_LOGI(TAG,
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);
314}
315
316uint8_t AS3935Component::read_register_(uint8_t reg, uint8_t mask) {
317 uint8_t value = this->read_register(reg);
318 value &= (~mask);
319 return value;
320}
321
322} // namespace esphome::as3935
virtual void setup()
Where the component's initialization should happen.
Definition component.cpp:84
ESPDEPRECATED("Use const char* or uint32_t overload instead. Removed in 2026.7.0", "2026.1.0") void set_timeout(const std voi set_timeout)(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a unique name.
Definition component.h:493
virtual void setup()=0
virtual bool digital_read()=0
virtual uint8_t read_register(uint8_t reg)=0
virtual void write_register(uint8_t reg, uint8_t mask, uint8_t bits, uint8_t start_position)=0
uint8_t read_register_(uint8_t reg, uint8_t mask)
Definition as3935.cpp:316
bool state
Definition fan.h:2
@ ENERGY_LIGHT_MMSB
Definition as3935.h:25
void HOT delay(uint32_t ms)
Definition hal.cpp:85
static void uint32_t