Jump to content






Photo

How to use a AS3935 Lightning sensor with a Raspberry Pi

Posted by pete_c , 03 July 2017 · 117 views

A repost of a Homeseer forum post.  I am impressed with the AS3935 lightning sensor chip.
 
AS3935 Lightning Sensor

 
I re purposed first Raspberry Pi purchased a few years ago.
 
Steps are basic
 
1 - build a Wheezy OS based Raspberry Pi
2 - wire the AS3935 lightning breakout board to the Raspberry Pi - 5 wires only.
 
Provides distance of lightning up to 40 Km.  Omni directional.
 
Step #1 - Preparing your RPi wiring

Step #2 - Preparing your AS3935 breakout board solder the wires - use a small tipped soldering iron, put a tiny drop of solder on the iron then touch the wire to solder.


Hardware:

1 - Raspberry Pi / original Homeseer Zee
2 - AS3935 Breakout board made by Playing with Fusion dot com.
3 - wires - preferred here is a starter wiring kit sold on Amazon, Ebay et al.
4 - soldering iron with a small tip and solder.

Connections are as follows
Note here you can instead solder a header to the lightning breakout board with straight or pins at 90 ° and just utilize single jumper cables.

1 - Board VDD ==> - 3.0VDC - PIN # 1 - RED
2 - Board GND ==> Ground - PIN # 6 - BLACK
3 - Board SCK / SCL ==> SCL - PIN # 5 - GPIO 3 - WHITE
4 - Board MOSI / SDA ==> SDA - PIN # 3 - GPIO 2 - GREEN
5 - Board IRQ ==> Pin #11 - GPIO 17 - YELLOW
6 - Board SI ==> 3.0VDC - Pin #17 for i2c - RED
 
7th of July, 2017 - some lightning passed today.
 
Overhead lightning detected - distance = 1 km at 14:19:53.838  2017-07-07
1 2017-07-07 19:21:48.004053 1 1 0

Overhead lightning detected - distance = 1 km at 14:21:47.993  2017-07-07
1 2017-07-07 19:22:42.710066 5 1 0

Lightning detected - distance = 5 kms at 14:22:42.699 2017-07-07
1 2017-07-07 19:23:48.668980 5 1 0

Lightning detected - distance = 5 kms at 14:23:48.658 2017-07-07
1 2017-07-07 19:25:49.172010 5 1 0

Lightning detected - distance = 5 kms at 14:25:49.161 2017-07-07
1 2017-07-07 19:38:49.285675 6 1 0

Lightning detected - distance = 6 kms at 14:38:49.275 2017-07-07
1 2017-07-07 19:47:19.987470 24 1 0

Lightning detected - distance = 24 kms at 14:47:19.977 2017-07-07
1 2017-07-07 19:54:57.464848 24 1 0

Lightning detected - distance = 24 kms at 14:54:57.454 2017-07-07

Step #3 - Preparing your RPi

A - Download Wheezy from here ==> Wheezy Download
B - Download SD Card Formatter from here ==> Windows SD Card Formatter
C - Format you SD card.  Here utilized Windows 7 and a 4Gb microSD card in an SD card holder
D - Download Win32 Disk Imager from here ==> Win32DiskImager
E - Write downloade
 
Step #4 - Preparing your RPi via SSH

A - Insert your SD card in to your RPi and plug in an Ethernet cable to network port on RPi and plug in power up.
B - Have a look see at your firewall router to find the DHCP address of your RPi.  It will be labeled as Raspberry.
C - Download Putty from here ==> Putty
D - Install it on your Windows PC
E - SSH to your RPi using user pi and password raspberry
F - type sudo su once in and hit enter
G - type raspi-config and hit enter - do what the picture shows. (Expand system, change password, internationalization options and advanced menu)
Go to advanced menu on raspi-config and do what the picture shows. (host name, memory split (go to 16) and i2c)
H - Exit and reboot.
I - SSH back to your RPi - adding one user
Type sudo su
add your name to users and sudo - note here using my name as an example
adduser pete *password and answer all of the questions - I use return on them
usermod -aG sudo pete
J - SSH back to your RPi - here we are going to create a static address - easy these days.
K - cd /etc
L - nano dhcpcd.conf
add your static IP to the end of the file.  Use your network information.  The example is my configuration.

interface eth0
static ip_address=192.168.244.165/25
static routers=192.168.244.129
static domain_name_servers=192.168.244.129

M - reboot and ssh back to your RPi with your static IP and new username and type sudo su.
N - type apt-get update
O - type apt-get upgrade (note this will run for a while)
P - when finished reboot.
Q - you are now finished with base configuration on your RPi
P - if you have not connected your lightning sensor then do it now.  Note type halt to shut down your RPi, unplug network and power cable, hook up your Lightning sensor.
Q - testing your i2C connection to you lightning sensor
R - cd /
S - type apt-get install i2c-tools
T - type  i2cdetect -y 1   * you will see the following.  If you see only 03 then you are connected to the AS3935
 
~# sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          03 -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Step #5 - Using the AS3935 Python library to test your lightning sensor AS3935 breakout board.

A - ssh to your RPi.
B - type cd /opt and mkdir lightning and cd lightning
C - type git - if it is not present then type apt-get install git
D - with your browser look at the following:  hxxps://github.com/pcfens/RaspberryPi-AS3935  read the text file
E - type git clone hxxps://github.com/pcfens/RaspberryPi-AS3935
F - cd to /opt/lightning/RaspberryPi-AS3935
G - type pip install -r requirements.txt
H - if there in a python module missing then type:  python -m pip install SomePackage
I - type again pip install -r requirements.txt
J-  type cd /opt/lightning/RaspberryPi-AS3935
K - type python setup.py
L - testing demo.py - make one change to the file here:

# Rev. 1 Raspberry Pis should leave bus set at 0, while rev. 2 Pis should set
# bus equal to 1. The address should be changed to match the address of the
# sensor. (Common implementations are in README.md)
sensor = RPi_AS3935(address=0x03, bus=1)

sensor.set_indoors(True)
sensor.set_noise_floor(0)
sensor.calibrate(tun_cap=0x0D) * changed to my capacitor value of 104

M - ssh to your pi, cd /opt/lightning/RaspberryPi-AS3935
N - type python demo.py
O - I've attached another more elaborate demo for lightning sensor at the end of this post.
The python script is called AS3935-monitor.py.

Step #6 - A3935 mini web server - located here ==> hxxps://github.com/pcfens/RPi-AS3935-Web
Made the web page a link from main Homeseer 3 server and one HSTouch page.

[attach]62022[/attach]

A - SSH to your Pi
B - type cd /opt/lightning
C - type git clone hxxps://github.com/pcfens/RPi-AS3935-Web
D - type cd RPi-AS3935-Web
E - type pip install -r requirements.txt
F - if there in a python module missing then type:  python -m pip install SomePackage
G - type again pip install -r requirements.txt
H - cp settings.cfg.sample to settings.cfg
I  - type nano settings.cfg
J - changed the following lines to match.

[pi]
; Rev. 1 Raspberry Pis should leave bus set at 0, while rev. 2 Pis should set
; bus equal to 1.
bus = 1

[as3935]
; Set the i2c address of lightning sensor
address = 0x03

; The value needed to tune the sensor
tuning_cap = 0x0F ==> 0x0D (x0D*8 =104) ==> tuning_cap = 0x0D

Hex value of 0x0F = 15 X 8 = 120 µF capacitor value
Hex value of 0x0D = 13 X 8 = 104 µF capacitor value

To calculate your tune your sensor value take your capacitor value and divide it by 8 and convert it to hex.

Here is another demo script called AS3935-monitor.py

VCT fork of RaspberryPi-AS3935 is located here:  hxxps://github.com/VCTLabs/py-sensor-test/blob/master/python/AS3935-monitor.py

# -*- coding: utf-8 -*-

#! /usr/bin/env python

"""

AS3935-monitor.py: A monitoring script for the AS3935 lightning

    sensor on the MOD-1016 v6 breakout board.  Requires both the

    RaspberryPi-AS3935 and RPi.GPIO Python modules.  Note that

    the reset function requires VCT fork of RaspberryPi-AS3935.





simple invocation:

    $ sudo python AS3935-monitor.py



error logging invocation:

    $ sudo python -u AS3935-monitor.py > >(tee -a output.log) 2> >(tee error.log >&2)



Licensed under the GNU General Public License (GPL) version 2 or greater.

Copyright 2014 Vanguard Computer Technology Labs, Inc.

"""



from RPi_AS3935 import RPi_AS3935

import RPi.GPIO as GPIO

import time

from datetime import datetime



GPIO.setmode(GPIO.BCM)



## Rev 1 Raspberry Pi: bus=0

## Rev 2 and later Raspberry Pi: bus=1

## All: address=<i2c add>



sensor = RPi_AS3935(address=0x03, bus=1)



## Uncomment to reset all registers to factory default values.

## sensor.reset()



sensor.calibrate(tun_cap=0x0d)

time.sleep(0.002)

sensor.set_indoors(True)

sensor.set_noise_floor## uncomment/set to filter out false positives

sensor.set_min_strikes(1)



def log_raw_data(data):

    # need to pass time as parameter and convert to UTC here

    utcnow = datetime.utcnow()

    version = 01

    timestamp = utcnow.strftime("%Y-%m-%d %H:%M:%S.%f")

    outstring = str(version)+" "+str(timestamp)+" "+str(data)+" "+str(min_strikes)+" "+str(noise_floor)+"\n"

    print(outstring)

    f.write(outstring)

    f.flush



def handle_interrupt(channel):

    time.sleep(0.003)

    global sensor

    reason = sensor.get_interrupt()

    if reason == 0x01:

        print("Noise level too high - adjusting")

        sensor.raise_noise_floor()

    elif reason == 0x04:

        print("Disturber detected - masking")

        sensor.set_mask_disturber(True)

    elif reason == 0x08:

        now = datetime.now()

        distance = sensor.get_distance()

        if 0 < distance < 63:

            log_raw_data(distance)

        if distance == 1:

            print("Overhead lightning detected - distance = " + str(distance) + " km at %s ") % now.strftime("%H:%M:%S.%f")[:-3],now.strftime("%Y-%m-%d")

        elif 40 < distance < 63:

            print("Distant lightning detected - distance = " + str(distance) + " kms at %s") % now.strftime("%H:%M:%S.%f")[:-3],now.strftime("%Y-%m-%d")

        elif 2 <= distance <= 40:

            print("Lightning detected - distance = " + str(distance) + " kms at %s") % now.strftime("%H:%M:%S.%f")[:-3],now.strftime("%Y-%m-%d")

        else:

            print("Invalid data; distance out of range.")



irq_pin = 17

cs_pin = 24



GPIO.setup(irq_pin, GPIO.IN)

GPIO.add_event_detect(irq_pin, GPIO.RISING, callback=handle_interrupt)



def read_settings():

    global min_strikes, noise_floor

    min_strikes = sensor.get_min_strikes()

    noise_floor = sensor.get_noise_floor()



def print_settings():

    print("Minimum allowed strikes is " + str(min_strikes))

    print("Current noise floor is " + str(noise_floor))



running = True



try:

    print("AS3935 Lightning Detection Monitor Script - v0.1")

    print("  Monitor Status: ONLINE")

    print("")

    ## log raw data to file

    f=open('AS3935-data.txt','a')

    read_settings()



    while running:

        time.sleep(1.0)



except KeyboardInterrupt:

    print("  Monitor Status: OFFLINE")

    print_settings()

    print("")



finally:

    GPIO.cleanup() # clean up GPIO on CTRL+C exit

    f.close()

18:18c 07/02/2017 testing while storm rolls through - hear thunder and seeing lightning

AS3935 Lightning Detection Monitor Script - v0.1
  Monitor Status: ONLINE

Disturber detected - masking
1 2017-07-02 23:16:00.074464 1 1 0

Overhead lightning detected - distance = 1 km at 18:16:00.067  2017-07-02
1 2017-07-02 23:16:01.165121 1 1 0

Overhead lightning detected - distance = 1 km at 18:16:01.158  2017-07-02
1 2017-07-02 23:16:03.188902 1 1 0

Overhead lightning detected - distance = 1 km at 18:16:03.178  2017-07-02

7th of July, 2017

Overhead lightning detected - distance = 1 km at 14:19:53.838  2017-07-07
1 2017-07-07 19:21:48.004053 1 1 0

Overhead lightning detected - distance = 1 km at 14:21:47.993  2017-07-07
1 2017-07-07 19:22:42.710066 5 1 0

Lightning detected - distance = 5 kms at 14:22:42.699 2017-07-07
1 2017-07-07 19:23:48.668980 5 1 0

Lightning detected - distance = 5 kms at 14:23:48.658 2017-07-07
1 2017-07-07 19:25:49.172010 5 1 0

Lightning detected - distance = 5 kms at 14:25:49.161 2017-07-07
1 2017-07-07 19:38:49.285675 6 1 0(0)
 




July 2017

S M T W T F S
      1
2345678
9101112131415
16171819202122
23 24 2526272829
3031     

Recent Comments