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