Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Development
  3. Sketch for Lightning Sensor

Sketch for Lightning Sensor

Scheduled Pinned Locked Moved Development
58 Posts 14 Posters 31.6k Views 11 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SparkmanS Sparkman

    Hi all,

    Has anyone developed a MySensors sketch for the AS3935 Digital Lightning Sensor that's listed in the MySensors store? Playingwithfusion, the manufacturer of the sensor, has a sample sketch, but being new to MySensors, I need some help setting this up to send data to the MS gateway. Thanks in advance for any help.

    Cheers
    Al

    BulldogLowellB Offline
    BulldogLowellB Offline
    BulldogLowell
    Contest Winner
    wrote on last edited by
    #2

    @Sparkman said:

    Playingwithfusion

    cool sensor. Were you able to get it working with your arduino already?

    It looks like you have a few options, but you could try first getting it to work with SoftSPI.h library, and moving the MOSI, MISO and SCLK pins off of the MySensors Hardware SPI pins. Alternatively, you could try to implement using I2C, but I didn't see an example (maybe the manufacturer can provide that).

    Once you have it working with SoftSPI or I2C, it would be easy to help you get it to work with your MySensors Gateway.

    We can help you with the program, but testing would be yours to do, so try to get it working as above, and post that code.

    SparkmanS 1 Reply Last reply
    0
    • BulldogLowellB BulldogLowell

      @Sparkman said:

      Playingwithfusion

      cool sensor. Were you able to get it working with your arduino already?

      It looks like you have a few options, but you could try first getting it to work with SoftSPI.h library, and moving the MOSI, MISO and SCLK pins off of the MySensors Hardware SPI pins. Alternatively, you could try to implement using I2C, but I didn't see an example (maybe the manufacturer can provide that).

      Once you have it working with SoftSPI or I2C, it would be easy to help you get it to work with your MySensors Gateway.

      We can help you with the program, but testing would be yours to do, so try to get it working as above, and post that code.

      SparkmanS Offline
      SparkmanS Offline
      Sparkman
      Hero Member
      wrote on last edited by
      #3

      cool sensor. Were you able to get it working with your arduino already?

      Thanks for the response. I haven't hooked it up yet, hopefully tomorrow although not a lot of lightning this time of year where I live to test with. Is it possible to use "normal" SPI and just use a different pin for slave select for it? I was thinking about using pins 8 and 7 for the lightning sensor as to not conflict with the radio on pins 10 and 9, and using pin 3 for IRQ for the sensor versus 2 for the radio. Can both be connected to MOSI, MISO and SCLK? I was looking to use it with a Pro Mini and would need the following SPI parameters:

      SPI.begin();
      SPI.setClockDivider(SPI_CLOCK_DIV8);
      SPI.setDataMode(SPI_MODE1);
      SPI.setBitOrder(MSBFIRST);

      Do these conflict with what's needed for the radio? I guess using I2C would eliminate the conflicts, and will ask Playingwithfusion the details on how to make it work with that. I would like to learn how to get multiple devices playing together using SPI though. I'll read up on SoftSPI as well.

      Cheers
      Al

      1 Reply Last reply
      0
      • BulldogLowellB Offline
        BulldogLowellB Offline
        BulldogLowell
        Contest Winner
        wrote on last edited by
        #4

        presently, MySensors (and @hek can correct me if I am wrong) is still not using interrupt on pin2, so you should be OK there.

        SPI is a hardware implementation on pins 11, 12, 13 on your Arduino. MySensors is using hardware SPI to communicate with the radio.

        SoftSpi is a software implementation of SPI and will allow you to make SPI communication to your breakout board, but must use alternate pins on your Arduino as avoid the conflict.

        You should be able to get it to work with SoftSPI or I2C.

        1 Reply Last reply
        0
        • SparkmanS Offline
          SparkmanS Offline
          Sparkman
          Hero Member
          wrote on last edited by Sparkman
          #5

          Thanks, but isn't SPI designed to allow more than one slave to be connected? From what I understand, each slave requires a separate slave select pin, but should be able to share the other pins. I'm new to this so please correct me if I'm wrong.

          Thanks
          Al

          BulldogLowellB 1 Reply Last reply
          0
          • SparkmanS Sparkman

            Thanks, but isn't SPI designed to allow more than one slave to be connected? From what I understand, each slave requires a separate slave select pin, but should be able to share the other pins. I'm new to this so please correct me if I'm wrong.

            Thanks
            Al

            BulldogLowellB Offline
            BulldogLowellB Offline
            BulldogLowell
            Contest Winner
            wrote on last edited by BulldogLowell
            #6

            @Sparkman said:

            Thanks, but isn't SPI designed to allow more than one slave to be connected?

            yes, if they play well together... I've not been that lucky with Arduino. Since the breakout isn't on a shield, it may work OK.

            You can always just try it and let us know.

            SparkmanS 1 Reply Last reply
            0
            • BulldogLowellB BulldogLowell

              @Sparkman said:

              Thanks, but isn't SPI designed to allow more than one slave to be connected?

              yes, if they play well together... I've not been that lucky with Arduino. Since the breakout isn't on a shield, it may work OK.

              You can always just try it and let us know.

              SparkmanS Offline
              SparkmanS Offline
              Sparkman
              Hero Member
              wrote on last edited by
              #7

              @BulldogLowell said:

              yes, if they play well together... I've not been that lucky with Arduino. Since the breakout isn't on a shield, it may work OK.

              You can always just try it and let us know.

              Thanks, I did not realize that. The manufacturer got back to me already (great support over a weekend) and indicated that they don't have a sample I2C sketch, so I'll likely try getting it working with SPI first.

              Cheers
              Al

              1 Reply Last reply
              0
              • jeylitesJ Offline
                jeylitesJ Offline
                jeylites
                wrote on last edited by jeylites
                #8

                @BulldogLowell @Sparkman
                Hi guys, Have any of you made any developments? I was wondering, can I make changes to distance sensor sketch and use it for lighting?

                as3935_lightning_snsr_nocal.ino
                PWFusion_AS3935.cpp
                PWFusion_AS3935.h

                SparkmanS 1 Reply Last reply
                0
                • jeylitesJ jeylites

                  @BulldogLowell @Sparkman
                  Hi guys, Have any of you made any developments? I was wondering, can I make changes to distance sensor sketch and use it for lighting?

                  as3935_lightning_snsr_nocal.ino
                  PWFusion_AS3935.cpp
                  PWFusion_AS3935.h

                  SparkmanS Offline
                  SparkmanS Offline
                  Sparkman
                  Hero Member
                  wrote on last edited by Sparkman
                  #9

                  @jeylites I haven't done much with it as I was waiting for my NRF24l01's to show up, First shipment got lost and my replacement shipment seems to be stuck in customs for far too long. I'm hoping to get back to it in a couple of weeks. I did write a sketch that compiled and loaded properly, but I haven't been able to test it due to lack of radios :-(

                  Cheers
                  Al

                  1 Reply Last reply
                  0
                  • jeylitesJ Offline
                    jeylitesJ Offline
                    jeylites
                    wrote on last edited by
                    #10

                    @Sparkman OK, hope to hear from you soon. Cheers!

                    1 Reply Last reply
                    0
                    • SparkmanS Offline
                      SparkmanS Offline
                      Sparkman
                      Hero Member
                      wrote on last edited by Sparkman
                      #11

                      My radios showed up a few days ago and this is the sketch I'm using:

                      #include "MySensor.h"  
                      
                      // the sensor communicates via SPI or I2C. This example uses the SPI interface
                      #include "SPI.h"
                      // include Playing With Fusion AXS3935 libraries
                      #include "PWFusion_AS3935.h"
                      
                      // setup CS pins used for the connection with the sensor
                      // other connections are controlled by the SPI library)
                      int8_t CS_PIN  = 8;
                      int8_t SI_PIN  = 7;
                      int8_t IRQ_PIN = 3;                      
                      volatile int8_t AS3935_ISR_Trig = 0;
                      
                      // #defines
                      #define AS3935_INDOORS       1
                      #define AS3935_OUTDOORS      0
                      #define AS3935_DIST_DIS      0
                      #define AS3935_DIST_EN       1
                      #define AS3935_CAPACITANCE   96      // <-- SET THIS VALUE TO THE NUMBER LISTED ON YOUR BOARD 
                      // prototypes
                      void AS3935_ISR();
                      
                      PWF_AS3935  lightning0(CS_PIN, IRQ_PIN, SI_PIN);
                      
                      #define CHILD_ID_DISTANCE 1
                      #define CHILD_ID_INTENSITY 2
                      MySensor gw;
                      MyMessage msgDist(CHILD_ID_DISTANCE, V_DISTANCE);
                      MyMessage msgInt(CHILD_ID_INTENSITY, V_VAR1);
                      
                      void setup()  
                      { 
                        gw.begin();
                      
                        // Send the sketch version information to the gateway and Controller
                        gw.sendSketchInfo("Lightning Sensor", "1.0");
                      
                        // Register all sensors to gw (they will be created as child devices)
                        gw.present(CHILD_ID_DISTANCE, S_DISTANCE);
                        gw.present(CHILD_ID_INTENSITY, S_CUSTOM);
                        boolean metric = gw.getConfig().isMetric;
                      
                        Serial.begin(115200);
                        Serial.println("Playing With Fusion: AS3935 Lightning Sensor, SEN-39001");
                        Serial.println("beginning boot procedure....");
                        
                        // setup for the the SPI library:
                        SPI.begin();                            // begin SPI
                        SPI.setClockDivider(SPI_CLOCK_DIV4);    // SPI speed to SPI_CLOCK_DIV16/1MHz (max 2MHz, NEVER 500kHz!)
                        SPI.setDataMode(SPI_MODE1);             // MAX31855 is a Mode 1 device
                                                                //    --> clock starts low, read on rising edge
                        SPI.setBitOrder(MSBFIRST);              // data sent to chip MSb first 
                        
                        lightning0.AS3935_DefInit();                        // set registers to default  
                        // now update sensor cal for your application and power up chip
                        lightning0.AS3935_ManualCal(AS3935_CAPACITANCE, AS3935_OUTDOORS, AS3935_DIST_EN);
                                        // AS3935_ManualCal Parameters:
                                        //   --> capacitance, in pF (marked on package)
                                        //   --> indoors/outdoors (AS3935_INDOORS:0 / AS3935_OUTDOORS:1)
                                        //   --> disturbers (AS3935_DIST_EN:1 / AS3935_DIST_DIS:2)
                                        // function also powers up the chip
                                        
                        // enable interrupt (hook IRQ pin to Arduino Uno/Mega interrupt input: 0 -> pin 2, 1 -> pin 3 )
                        attachInterrupt(1, AS3935_ISR, RISING);
                      
                        lightning0.AS3935_PrintAllRegs();
                        
                        // delay execution to allow chip to stabilize.
                        delay(1000);
                      
                      }
                      
                      void loop()      
                      {     
                      
                        // This program only handles an AS3935 lightning sensor. It does nothing until 
                        // an interrupt is detected on the IRQ pin.
                        while(0 == AS3935_ISR_Trig){}
                       
                        // reset interrupt flag
                        AS3935_ISR_Trig = 0;
                        
                        // now get interrupt source
                        uint8_t int_src = lightning0.AS3935_GetInterruptSrc();
                        if(0 == int_src)
                        {
                          Serial.println("Unknown interrupt source");
                          //gw.send(msgDist.set(999));
                          //gw.send(msgInt.set(0));
                        }
                        else if(1 == int_src)
                        {
                          uint8_t lightning_dist_km = lightning0.AS3935_GetLightningDistKm();
                          uint32_t lightning_intensity = lightning0.AS3935_GetStrikeEnergyRaw();
                      
                          Serial.print("Lightning detected! Distance to strike: ");
                          Serial.print(lightning_dist_km);
                          Serial.println(" kilometers");
                          Serial.print("Lightning detected! Lightning Intensity: ");
                          Serial.println(lightning_intensity);
                          gw.send(msgDist.set(lightning_dist_km));
                          gw.send(msgInt.set(lightning_intensity));
                        }
                        else if(2 == int_src)
                        {
                          Serial.println("Disturber detected");
                          //gw.send(msgDist.set(998));
                          //gw.send(msgInt.set(0));
                        }
                        else if(3 == int_src)
                        {
                          Serial.println("Noise level too high");
                          //gw.send(msgDist.set(997));
                          //gw.send(msgInt.set(0));
                        }
                      }
                      
                      // this is irq handler for AS3935 interrupts, has to return void and take no arguments
                      // always make code in interrupt handlers fast and short
                      void AS3935_ISR()
                      {
                        AS3935_ISR_Trig = 1;
                      }
                      

                      It seems to be working (based on what I'm seeing using the serial monitor) and updating my controller (HomeSeer), although hard to know how well it's working without lightning in the area. I'll have to find a way to simulate lightning.

                      I have both the lightning sensor and the radio hooked up to the SPI bus.

                      Nano with NRF24L01 and AS3935_bb.png

                      Cheers
                      Al

                      EDIT: The diagram above is incorrect, I had to connect the lightning sensor breakout board to 5V, it did not seem to work at 3.3V in this configuration.

                      1 Reply Last reply
                      1
                      • hekH Offline
                        hekH Offline
                        hek
                        Admin
                        wrote on last edited by
                        #12

                        Sweet. Would you want to create a pull request when you're ready with this example (including AS3935 library)?

                        SparkmanS 3 Replies Last reply
                        0
                        • hekH hek

                          Sweet. Would you want to create a pull request when you're ready with this example (including AS3935 library)?

                          SparkmanS Offline
                          SparkmanS Offline
                          Sparkman
                          Hero Member
                          wrote on last edited by
                          #13

                          @hek Thanks and yes, will do.

                          1 Reply Last reply
                          0
                          • hekH hek

                            Sweet. Would you want to create a pull request when you're ready with this example (including AS3935 library)?

                            SparkmanS Offline
                            SparkmanS Offline
                            Sparkman
                            Hero Member
                            wrote on last edited by Sparkman
                            #14

                            @hek The AS3935 Sensor also registers the energy intensity. What sensor type would be the best way to send that data to the controller? I'm using S_DISTANCE for the calculated distance, but would also like to capture the energy intensity. The energy intensity is returned as a 32 bit integer.

                            Also, the AS3935 returns the distance in km, but I believe S_DISTANCE is in cm. Is there a way to specify different units or should I have the sketch convert it to cm and then have the controller convert back to km?

                            Thanks
                            Al

                            H 1 Reply Last reply
                            0
                            • hekH Offline
                              hekH Offline
                              hek
                              Admin
                              wrote on last edited by
                              #15

                              Oh.. than sensor is more advanced than I thought.

                              Send distance in "meter" for now. Stupid thing that the distance sensor example we're sending in cm in the first place.

                              Energy intensity is harder. What unit that is that? Maybe you could send it in VAR_1 for now?

                              SparkmanS 1 Reply Last reply
                              0
                              • hekH hek

                                Oh.. than sensor is more advanced than I thought.

                                Send distance in "meter" for now. Stupid thing that the distance sensor example we're sending in cm in the first place.

                                Energy intensity is harder. What unit that is that? Maybe you could send it in VAR_1 for now?

                                SparkmanS Offline
                                SparkmanS Offline
                                Sparkman
                                Hero Member
                                wrote on last edited by
                                #16

                                @hek The sensor has a number of registers that can be set and/or read, so depending what someone wants to do with it, additional parameters may be sent.

                                Ok, will do regarding the units. The energy intensity is just a relative value with no units associated with it. I saw one sketch for a different breakout board for the same chip that converted the intensity to a value in the range of 1-10. The actual value returned can be as large as 16843008. I'll take a look at VAR_1, but don't believe the HomeSeer plugin supports that yet. As a test I used I used V_WATT/S_POWER for now.

                                Cheers
                                Al

                                1 Reply Last reply
                                0
                                • jeylitesJ Offline
                                  jeylitesJ Offline
                                  jeylites
                                  wrote on last edited by
                                  #17

                                  @Sparkman

                                  If you can give me some time say around June or July, I'll be going to a tropical country with lots of lightning. I could test it out for you using Vera.

                                  SparkmanS 1 Reply Last reply
                                  0
                                  • jeylitesJ jeylites

                                    @Sparkman

                                    If you can give me some time say around June or July, I'll be going to a tropical country with lots of lightning. I could test it out for you using Vera.

                                    SparkmanS Offline
                                    SparkmanS Offline
                                    Sparkman
                                    Hero Member
                                    wrote on last edited by
                                    #18

                                    @jeylites Thanks, I appreciate the offer. July/August are prime thunderstorm season where I live and they are forecasting a possible thunderstorm for today, so I'm hoping to confirm that everything is working today :-)

                                    Cheers
                                    Al

                                    1 Reply Last reply
                                    1
                                    • sowardS Offline
                                      sowardS Offline
                                      soward
                                      wrote on last edited by
                                      #19

                                      I put one of these together a short while ago -- just missed a storm rolling though, but another round might come through later. There's a reference in the code to AS3935_CAPACITANCE and a comment suggesting it be set to "THE NUMBER LISTED ON YOUR BOARD", however I'm seeing no number listed on the board. Am I just over looking it or is it something you derive?

                                      thanx,
                                      JpS

                                      SparkmanS 1 Reply Last reply
                                      0
                                      • sowardS soward

                                        I put one of these together a short while ago -- just missed a storm rolling though, but another round might come through later. There's a reference in the code to AS3935_CAPACITANCE and a comment suggesting it be set to "THE NUMBER LISTED ON YOUR BOARD", however I'm seeing no number listed on the board. Am I just over looking it or is it something you derive?

                                        thanx,
                                        JpS

                                        SparkmanS Offline
                                        SparkmanS Offline
                                        Sparkman
                                        Hero Member
                                        wrote on last edited by Sparkman
                                        #20

                                        @soward So far the thunderstorms have skirted our area, but more to come tonight. Here's hoping ;-)

                                        The Capacitance value is written on the bag the sensor came in. If you don't have the bag anymore, send an email to TechnicalSupport@PlayingWithFusion.com and they should be able to cross reference your order and provide the value. How does your sketch compare to mine?

                                        Cheers
                                        Al

                                        sowardS 1 Reply Last reply
                                        0
                                        • SparkmanS Sparkman

                                          @soward So far the thunderstorms have skirted our area, but more to come tonight. Here's hoping ;-)

                                          The Capacitance value is written on the bag the sensor came in. If you don't have the bag anymore, send an email to TechnicalSupport@PlayingWithFusion.com and they should be able to cross reference your order and provide the value. How does your sketch compare to mine?

                                          Cheers
                                          Al

                                          sowardS Offline
                                          sowardS Offline
                                          soward
                                          wrote on last edited by
                                          #21

                                          @Sparkman great thanx, I do have the bag somewhere in the pile-of-parts-bags. So far I have only used the demo sketch from playingwithfusion, but once it appears to detect something I'll probably use yours as a base and branch out form there. I'm using a Vera so I can try to use VAR_1 as hek suggested.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          6

                                          Online

                                          11.7k

                                          Users

                                          11.2k

                                          Topics

                                          113.0k

                                          Posts


                                          Copyright 2019 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • MySensors
                                          • OpenHardware.io
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular