AS3935 Lightning sensor library

Dependents:   zeus

Fork of AS3935 by valentin spanu

include the mbed library with this snippet

#include "mbed.h"
#include "AS3935.h"

 // frdm-kl25z sd card connections for spi1 
 // ------------------------------------------------
 // Header -- kl25z -- SD/MMC          
 // J2-20  -- PTE1  -- MOSI
 // J9-13  -- PTE4  -- CS
 // J2-14  -- GND   -- Vss (GND) 
 // J9-9   -- PTE2  -- SCK
 // J9-11  -- PTE3  -- MISO
 
AS3935 ld(PTE1, PTE3, PTE2, PTE4, "ld", 100000); // MOSI, MISO, SCK, CS, SPI bus freq (hz) 
InterruptIn IntLightning(PTA12); //IRQ AS3935
DigitalOut led1(LED_RED);
Serial pc(USBTX, USBRX);

void DetectLightning()
{
    char OriginInt;
     wait_ms(2); 
    OriginInt = ld.interruptSource();
    if (OriginInt == 1) { //
        pc.printf(" Noise level too high\r\n");
        }
    if (OriginInt == 4) { //
        pc.printf(" Disturber\r\n");
        }
    if (OriginInt == 8) { // detection
        // pc.printf(" Lightning detection\r\n");
        pc.printf(" Lightning detection, distance=%dkm\r\n", ld.lightningDistanceKm());
        ld.clearStats();

        }
}

 
int main() {
    pc.baud(9600);
    pc.printf("\r\nstart lightning detector\r\n");
    
    //initialisations
    ld.reset();
    ld.setTuneCap(5); // 500kHz
    ld.powerUp();
    ld.setIndoors();  
    ld.setMinimumLightnings(1);
    //ld.setSpikeRejection(2);
    ld.setNoiseFloor(2);
    ld.disableDisturbers();
    //ld.enableDisturbers();
    ld.setWatchdogThreshold(2);
    wait_ms(10);
    IntLightning.rise(&DetectLightning);
    int MinBlysk = ld.getMinimumLightnings();
    int Noise = ld.getNoiseFloor();
    int TuneCap = ld.getTuneCap();
    int SpikeRej = ld.getSpikeRejection();
    int WatchDog = ld.getWatchdogThreshold();
     
    pc.printf(" Min wylad: %i", MinBlysk);
    pc.printf("\r\n");
    pc.printf(" Noise: %i", Noise);
    pc.printf("\r\n");
    pc.printf(" Tune CAP: %i", TuneCap);
    pc.printf("\r\n");
    pc.printf(" Spike rej: %i", SpikeRej);
    pc.printf("\r\n");
    pc.printf(" Watchdog: %i", WatchDog);
    pc.printf("\r\n");
    while(1) {
        led1 = ! led1;
        wait(0.2);
     }
    
 
 }

Files at this revision

API Documentation at this revision

Comitter:
cmkachur
Date:
Tue Jun 23 21:01:27 2015 +0000
Parent:
9:19a323b1c508
Child:
11:ee2e7a573227
Commit message:
Add method for reading energy of lightning strike. Add energy data to the log file. Remove call to powerUp() since this was only setting outdoor mode which was done elsewhere.

Changed in this revision

AS3935.cpp Show annotated file Show diff for this revision Revisions of this file
AS3935.h Show annotated file Show diff for this revision Revisions of this file
--- a/AS3935.cpp	Thu Jun 18 15:47:39 2015 +0000
+++ b/AS3935.cpp	Tue Jun 23 21:01:27 2015 +0000
@@ -118,6 +118,7 @@
     registerWrite(AS3935_PWD,1);
 }
 
+#if 0 
 void AS3935::powerUp()
 {
  #if 0
@@ -139,6 +140,7 @@
     wait_ms(2);
 #endif
 }
+#endif 
 
 #if 0 
 // replication of the acurite init sequence
@@ -441,3 +443,15 @@
     return measFreqBest; 
 }
 
+unsigned long AS3935::getEnergy(void)
+{
+    unsigned long retVal; 
+    unsigned char rxBuff[3];
+    
+    rxBuff[0] = registerRead(AS3935_S_LIG_L); 
+    rxBuff[1] = registerRead(AS3935_S_LIG_M); 
+    rxBuff[2] = registerRead(AS3935_S_LIG_MM); 
+    
+    retVal = ((unsigned long)rxBuff[2] << 16) | ((unsigned long)rxBuff[1] << 8) | (unsigned long)rxBuff[0]; 
+    return retVal; 
+}
--- a/AS3935.h	Thu Jun 18 15:47:39 2015 +0000
+++ b/AS3935.h	Tue Jun 23 21:01:27 2015 +0000
@@ -37,6 +37,9 @@
 #define AS3935_LCO_FDIV 0x03, 0xC0
 #define AS3935_MASK_DIST 0x03, 0x20
 #define AS3935_INT 0x03, 0x0F
+#define AS3935_S_LIG_L  0x04, 0xff
+#define AS3935_S_LIG_M  0x05, 0xff
+#define AS3935_S_LIG_MM 0x06, 0x1f
 #define AS3935_DISTANCE 0x07, 0x3F
 #define AS3935_DISP_LCO 0x08, 0x80
 #define AS3935_DISP_SRCO 0x08, 0x40
@@ -154,6 +157,7 @@
     int calibrateRCOs (InterruptIn &intrIn);
 
     unsigned long tuneAntenna(InterruptIn &intrIn); 
+    unsigned long getEnergy(void);
     
     /** Attach a function, lightning interrupt
      * @param fptr pointer to a void function, or 0 to set as none