1+ // This sketch is to integrate the Playing With Fusion AXS3935 Lightning Sensor Breakout Board
2+ // with the MySensors environment and is based on a sketch provided by Playing With Fusion
3+ // http://playingwithfusion.com/productview.php?pdid=22&catid=1001
4+ //
5+ #include " MySensor.h"
6+ // the lightning sensor can communicate via SPI or I2C. This sketch uses the SPI interface
7+ #include " SPI.h"
8+ // include Playing With Fusion AXS3935 libraries
9+ #include " PWFusion_AS3935.h"
10+
11+ // setup CS pins used for the connection with the lightning sensor
12+ // other connections are controlled by the SPI library)
13+ int8_t CS_PIN = 8 ;
14+ int8_t SI_PIN = 7 ;
15+ int8_t IRQ_PIN = 3 ;
16+ volatile int8_t AS3935_ISR_Trig = 0 ;
17+
18+ // #defines
19+ #define AS3935_INDOORS 1
20+ #define AS3935_OUTDOORS 0
21+ #define AS3935_DIST_DIS 0
22+ #define AS3935_DIST_EN 1
23+ #define AS3935_CAPACITANCE 96 // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD
24+ // prototypes
25+ void AS3935_ISR ();
26+
27+ PWF_AS3935 lightning0 (CS_PIN, IRQ_PIN, SI_PIN);
28+
29+ #define CHILD_ID_DISTANCE 1
30+ #define CHILD_ID_INTENSITY 2
31+ MySensor gw;
32+ MyMessage msgDist (CHILD_ID_DISTANCE, V_DISTANCE);
33+ MyMessage msgInt (CHILD_ID_INTENSITY, V_VAR1);
34+
35+ void setup ()
36+ {
37+ gw.begin ();
38+
39+ // Send the sketch version information to the gateway and Controller
40+ gw.sendSketchInfo (" Lightning Sensor" , " 1.1" );
41+
42+ // Register all sensors to gw (they will be created as child devices)
43+ gw.present (CHILD_ID_DISTANCE, S_DISTANCE);
44+ gw.present (CHILD_ID_INTENSITY, S_CUSTOM);
45+ boolean metric = gw.getConfig ().isMetric ;
46+
47+ Serial.begin (115200 );
48+ Serial.println (" Playing With Fusion: AS3935 Lightning Sensor, SEN-39001" );
49+ Serial.println (" beginning boot procedure...." );
50+
51+ // setup for the the SPI library:
52+ SPI.begin (); // begin SPI
53+ SPI.setClockDivider (SPI_CLOCK_DIV4); // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!)
54+ SPI.setDataMode (SPI_MODE1); // MAX31855 is a Mode 1 device
55+ // --> clock starts low, read on rising edge
56+ SPI.setBitOrder (MSBFIRST); // data sent to chip MSb first
57+
58+ lightning0.AS3935_DefInit (); // set registers to default
59+ // now update sensor cal for your application and power up chip
60+ lightning0.AS3935_ManualCal (AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN);
61+ // AS3935_ManualCal Parameters:
62+ // --> capacitance, in pF (marked on package)
63+ // --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
64+ // --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2)
65+ // function also powers up the chip
66+
67+ // enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 1 -> pin 3 )
68+ attachInterrupt (1 , AS3935_ISR, RISING);
69+ // dump the registry data to the serial port for troubleshooting purposes
70+ lightning0.AS3935_PrintAllRegs ();
71+
72+ // delay execution to allow chip to stabilize.
73+ delay (1000 );
74+
75+ }
76+
77+ void loop ()
78+ {
79+
80+ // This program only handles an AS3935 lightning sensor. It does nothing until
81+ // an interrupt is detected on the IRQ pin.
82+ while (0 == AS3935_ISR_Trig){}
83+
84+ // reset interrupt flag
85+ AS3935_ISR_Trig = 0 ;
86+
87+ // now get interrupt source
88+ uint8_t int_src = lightning0.AS3935_GetInterruptSrc ();
89+ if (0 == int_src)
90+ {
91+ Serial.println (" Unknown interrupt source" );
92+ }
93+ else if (1 == int_src)
94+ {
95+ uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm ();
96+ uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw ();
97+
98+ Serial.print (" Lightning detected! Distance to strike: " );
99+ Serial.print (lightning_dist_km);
100+ Serial.println (" kilometers" );
101+ Serial.print (" Lightning detected! Lightning Intensity: " );
102+ Serial.println (lightning_intensity);
103+ gw.send (msgDist.set (lightning_dist_km));
104+ gw.send (msgInt.set (lightning_intensity));
105+ }
106+ else if (2 == int_src)
107+ {
108+ Serial.println (" Disturber detected" );
109+ }
110+ else if (3 == int_src)
111+ {
112+ Serial.println (" Noise level too high" );
113+ }
114+ }
115+
116+ // this is irq handler for AS3935 interrupts, has to return void and take no arguments
117+ // always make code in interrupt handlers fast and short
118+ void AS3935_ISR ()
119+ {
120+ AS3935_ISR_Trig = 1 ;
121+ }
0 commit comments