Git Product home page Git Product logo

raspberrypi-as3935's Introduction

RaspberryPi-AS3935

Build Status

A python library and demo script for interacting with the AMS Franklin Lightning Sensor.

This script will only work if the correct kernel modules are loaded on your Pi. Adafruit has a nice tutorial set up, though depending on the breakout board that you use, you may not see anything show up when you run i2cdetect.

Installation

You can install this module by running

pip install RPi_AS3935

or you can clone this repository and run

python setup.py install

Breakout Board

The AS3935 is a small chip, and rather than trying to solder it myself I purchased a (v2) breakout board from Embedded Adventures.

Connecting the AS3935

In my test setup I connected my breakout board to the Pi as shown

AS3935 Pin Raspberry Pi Pin
4 (GND) 25 (Ground)
5 (VDD) 1 (3v3 Power)
10 (IRQ) 11 (GPIO 17)
11 (I2CL) 5 (SCL)
13 (I2CD) 3 (SDA)

Known Issues

Addressing

You may need edit line 12 of demo.py so that the correct address is read.

Breakout Board Default Address
Embedded Adventures v2 0x00
Embedded Adventures v4/v8 0x03
Tautic Electronics (untested) 0x00

RaspberryPi Model

If you have one of the newer 512MB Pi boards, then you need to adjust line 12 in the demo script to read

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

raspberrypi-as3935's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

raspberrypi-as3935's Issues

Time delay before reading interrupt

Hi,
I have been waiting for lightning and in the mean while trying to understand the lightning module.
According to the datasheet:
"Whenever an interrupt is issued, the external unit should wait 2ms before reading the Interrupt register"

Would I be correct in suggesting this within the AS3935.py?

def get_interrupt(self):
    time.sleep(0.002)

AS3935 bus address

I'm getting the bus address 0x1a and not the standard 0x03 if I do i2cdetect. Then the demo script is stopping with
IOError [Errno 121] Remote I/O error...
I'm running raspberry PI 3 and of course adjustet the Bus to 1.
Any hints to solve the problem?
unbenannt

IOError: [Errno 121] Remote I/O error

Hi,

I can't get it to work, I am running it it with a raspberry A+ and as soon as I test it with a piezo I get this error:

Traceback (most recent call last):
File "demo.py", line 23, in handle_interrupt
reason = sensor.get_interrupt()
File "/home/pi/RaspberryPi-AS3935/RPi_AS3935/RPi_AS3935.py", line 48, in get_interrupt
self.read_data()
File "/home/pi/RaspberryPi-AS3935/RPi_AS3935/RPi_AS3935.py", line 234, in read_data
self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
IOError: [Errno 121] Remote I/O error

Thank you for your help !

Input/output error

Hi there,

I was testing out my sensor with your library, but I ran into an issue.
When I try to simulate lightning using a lighter, I mostly gave the following error:

Traceback (most recent call last):
File "lightning_web.py", line 38, in register_strike
reason = sensor.get_interrupt()
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 48, in get_interrupt
self.read_data()
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 228, in read_data
self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
IOError: [Errno 5] Input/output error

I thought of some fixes by myself, like changing 0x00 to 0x03 (i am using the Embedding Systems V8 version), but I doubt this would fix the issue.

Can you think of any solution to solve this problem?

Any chance you could add an SPI interface option as well?

With the known issues with this device on the I2C bus (according to sparkfun) its recommended to run it on the SPI bus instead

I can get it working on SPI with other available libraries, but no interrupt support. All the current libararies for this device that support SPI are written for CircuitPython, which doesn't support interrupts.... so makes it kind of difficult to conduct any asynchronous events on the Pi with it.

Help on sensor.set_indoors(True)

Please excuse me for this question as 99% it'll not be a code issue but only due to my newbiness.
I modified your demo.py to log lightnings and tested the sensor from Embedde Adventures in indoor conditions with pretty much success.
As my final installation is in a remote spot in the country, once set it back and tested once again successfully I wanted to set it for 'outdoor' installation.
My fault was thinking that just modifying sensor.set_indoors(True) into sensor.set_indoors(False) would have made the job.
Actually it gave me an error and didn't run. Now I reverted back to sensor.set_indoors(True) and it's giving me the following message.
I'm running it as root.

root@raspberrypi:/home/pi# Traceback (most recent call last):
File "/usr/local/bin/BlitzSensor.py", line 16, in
sensor.calibrate(tun_cap=0x05)
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 16, in calibrate
self.read_data()
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 110, in read_data
self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
IOError: [Errno 5] Input/output error

I checked everything I could
What I see now is that i2cdetect sometimes (rarely) is not reporting on the 03 address (while allways reporting other i2c connected devices). Can it be related to may code changing or it's just a case of connection/chip malfunctioning (it's raining heavily now up there and I can't check it phisically as I'm on a remote session).
Anyway what is the correct way of setting it for outdoor conditions?
Thanks for any help in advance!

Constant false detection

I just got my sensor ( MOD-1016 v6 ) and pi setup. It seems to connect up properly but it is giving lots of false positives.
It is a clear sunny day, definitely no lightning around. The box is inside at the moment.
Could it just be some sort of interference or a calibration issue?

pi@towerpi ~/lightning/RaspberryPi-AS3935 $ sudo python demo.py
Waiting for lightning - or at least something that looks like it
Noise level too high - adjusting
We sensed lightning!
It was 1km away. (02:35:44 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:37:04 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:38:08 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:38:12 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:38:20 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:38:34 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:42:19 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:42:57 - 2014/11/02)

We sensed lightning!
It was 1km away. (02:43:31 - 2014/11/02)

..and so on.

I am using a Raspberry Pi B+ with MOD-1016 v6.

I modified the sensor address to be sensor = RPi_AS3935(address=0x03, bus=1)

pi@towerpi ~/lightning/RaspberryPi-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: -- -- -- -- -- -- -- --

I have set tun_cap to match Tune 5 listed on the packet.
sensor.calibrate(tun_cap=0x05)

Is there something I have missed? or has something changed in version 6 of the board?

Possible Calibration Issue?

Just got the mod-1016 AS3935 module from Embedded Adventures. Found your code through Google (Thanks!), followed your readme, and was successfully running your code. Then I started looking through your AS3935 routines, and noticed something that doesn't agree with the documentation I got from Embedded Adventures. Here's what they say about calibration:
"These are the steps you need to take to start using the MOD-1016. Your MOD-1016 comes pre-calibrated–meaning you don’t need to worry about measuring frequencies and checking results. Wait a few milliseconds for the system to stabilise. Set the tune capacitor to the value indicated on the packaging, by setting the TUNE_CAP bits of register 8. Wait 2 milliseconds. Calibrate RCO by: Sending a calibrate RCO direct command (set memory location 0x3d to the value 0x96). Set Register 0x08, bit 5 to 1. Wait 2 milliseconds. Set Register 0x08, bit 5 to 0."

This is almost what you do, but you don't seed the TUNE_CAP bits with the value on the packaging, and the calibrate RCO direct command should be location 0x3D. I've modified your code to follow their instructions, and it still appears to work, although I've had no storms come through lately to verify this!

Was there some reason you're doing calibration differently, or should I shoot you a copy of my changes as a fix? Thanks again for providing an easy way to get this lightning module working with the Raspberry Pi! I look forward to adding lightning detection to my home weather station!

Output to file

Has anyone found the right code for redirecting the "demo.py" script to a file.
I'm looping around with writng to stdout stdin ev. piping or simply > to file..
I tested this
with open("blitze.dat", "w") as f:
sys.stdout = f
for global redirecting the print. But the file is not open for printing

Any help would be appreciated

GPIO Pin for IRQ

Hi looking at your connectivity table you state connect PIN 17 of the Pi P1 header to the IRQ of the Chip, however Pin 17 is 3.3V should this not read Pin 11 (GPIO 17)?

What about putting a 220 to 270 ohm resistor between the IRQ and the GPIO pin ? (not sure if this will work but might save the pi what do you think?)

Ps I'm using an embedded adventures V4 board, will comment when I get lightning..(so far the code is waiting for lightning) :-)

calibration methods added

Hi @pcfens - I have written two methods in python to put the module into calibration mode to find the frequency, tried committing, seems I have no rights.
Perhaps you would consider including these methods into RPi_AS3935.py

def display_res_freq_irq(self):
    # Set register to get resonant frequency
    # Note: multiply frequency reading by 16 for actual resonating frequency (target:500khz)
    self.read_data()
    self.set_byte(0x08, (self.registers[0x08] | 0x80))  
    time.sleep(0.002)
    # Good to measure IRQ pin for freq..

def display_res_freq_irq_off(self): 
    # Reset IRQ for no freq output"   
    #01111111 = 0x7F     = off
    self.read_data()
    self.set_byte(0x08, (self.registers[0x08] & 0x7F))
    time.sleep(0.002)

TypeError: 'module' object is not callable with Python 3.7.3

Hi there
I tried to run demo.py with Python 3.7.3 and get the following error:

Traceback (most recent call last):
File "demo.py", line 13, in
sensor = RPi_AS3935(address=0x03, bus=1)
TypeError: 'module' object is not callable

When I run demo.py with Python 2.7.16 it works
Does someone can know, how I can modify the code to make it work with Python 3.7.3 ?

Kind regards, Daniel

AS3935.py; set_indoors missing set_byte command

Been tinkering with the code as we've had rain but no lightning lately, and spotted another little bug. In the set_indoors function, it's missing a self.set_byte(0x00, write_value) at the end to actually change the register. There's also a possibly spurious self.read_data() at the end of the next function in the code, set_mask_disturber.

Still not sure about things working right. I've changed the beginning of lightning.py to do a little more initialization: calibrate, set indoors true, noise floor to 0, and mask disturber to false. And, a "restore to defaults" direct command added to the beginning of the calibration function. I seem to get inconsistent results just running it repeatedly. Like, now I run it and immediately get a couple noise detections (believable), and then a disturber detection. If I comment out the automatic setting of the disturber mask, it just goes into a constant reporting of disturber detection. My reading of the docs make it sound like a disturber shouldn't necessarily be a constant thing once detected (as the noise interrupt is). And sometimes I'll start it up and it will say waiting, and then say nothing about noise or disturbers, when a minute ago it reported both. Makes me think that there's some "resetting" that may be needed if you're repeatedly stopping and starting the code. Wish the docs were a bit clearer about the software interaction with the chip, not just the hardware aspects.

Also fixed it to notice an interrupt of 0, which is supposed to mean the distance has changed.

I guess I'll be able to really know more once we get some known lightning around. Funny, wishing for a storm to come through....

several coments on AS3935 library

Thank you very much for your wonderful Python library about AS3935.

When I installed AS3935 library on RaspberryPi and experimented, there were several defects, so it'll be reported.

About class RPi_AS3935

  1. set_indoors(True)
    registers [0x00] isn't set right . It's better to change it as follows.

def set_indoors(self, indoors):
    self.read_data()
    if indoors:
        write_value = (self.registers[0x00] & 0xC1) | (0b10010 << 1) #0b10010:AEF indoor
    else:
        write_value = (self.registers[0x00] & 0xC1) | (0b01110 << 1) #0b01110:AEF outdoor
    self.set_byte(0x00, write_value)

  1. calibrate(tun_cap=0x0F)
    There is a defect . It's better to add 1 line.

def calibrate(self, tun_cap=None):
    """Calibrate the lightning sensor - this takes up to half a second and is blocking"""
    time.sleep(0.08)
    self.read_data()
    if tun_cap is not None:
        if tun_cap < 0x10 and tun_cap > -1:
            self.set_byte(0x08, (self.registers[0x08] & 0xF0) | tun_cap)
            time.sleep(0.002)
        else:
            raise Exception("Value of TUN_CAP must be between 0 and 15")
    self.set_byte(0x3D, 0x96)
    time.sleep(0.002)
    self.read_data() #add this line
    self.set_byte(0x08, self.registers[0x08] | 0x20)
    time.sleep(0.002)
    self.read_data()
    self.set_byte(0x08, self.registers[0x08] & 0xDF)
    time.sleep(0.002)

demo.py

from the datasheet: "The combination a0 = 0 (low) and a1 =0 (low) is explicitly
not allowed for I2C communication."

does this driver actually work? i'm having trouble getting this to run on address 3 and bus 1 on a raspi, while the hardware is confirmed to work using an arduino as a replacement for the raspi...

Help about new issue raised after latest update

Hope you could help me to better identify the problem.
I personalized your python example to receive strike alert notifications and it worked fine from november up to last week when I removed the Vantage Pro2 weather station and installed instead the Envoy console.
The Davis side worked fine, but now, a week later I'm realizing that the AS3935 is not working anymore.
Now I realized that since I finished the installation it started to try adjusting due to noise level too high nearly continuosly (1800 times in 1h30min!) and then stopped (and I think it was not able anymore to load on RasPi boot).
Now if I try to run it from telnet i get the following message:

eusotec@raspberrypi:/home/pi/logs$ sudo /etc/init.d/Run_BlitzS start
Starting BlitzSensor
eusotec@raspberrypi:/home/pi/logs$ Traceback (most recent call last):
File "/usr/local/bin/BlitzSensor.py", line 36, in
sensor.calibrate(tun_cap=0x05)
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 19, in calibrate
self.set_byte(0x08, (self.registers[0x08] & 0xF0) | tun_cap)
File "/usr/local/lib/python2.7/dist-packages/RPi_AS3935/RPi_AS3935.py", line 107, in set_byte
self.i2cbus.write_byte_data(self.address, register, value)
IOError: [Errno 5] Input/output error

if I

sudo i2cdetect -y 1

I see randomly 03 adress popping up, but is not costant

I have also a CO2 sensor collecting CO2 values every hour which is also i2c connected to the raspi and running with a separate python program which is still collecting data in a regular way and was totally unaffected (also in the interval when the AS3935 was continuosly adjusting). So I'm guessing the i2c wiring is still fine.

Maybe I made a also a raspi update/upgrade in that occasion (not sure 100%) which I never made before since the last september installation.

Do you have any idea or suggestion about the cause ot the issue?
(I'll be able to check it phisically only next week and I'm missing a very good time of the year for lightnings so any help would be much appreciated!)

Carlo

Can't get it to work..

Hi,

This seems extremely promising but I can't get it to work. I don't know if this is an issue with my installation or the code.

This is my pinout, how does this differ from your installation?

VDD -> 5V -> PIN 2
GND -> GROUND -> PIN 6

Pin 13 (I2CDATA) -> MOSI -> GPIO 2 -> PIN 3
Pin 11 (I2CL) -> SCLK -> GPIO 3 -> PIN 5

Pin 9 (SI) -> SI -> 3V3 -> PIN 17
Pin 10 (IRQ) -> IRQ -> GPIO 17 -> PIN 11

Kind Regards, Rickard.

automatic antenna tuning

You have the bones for someone to set the tuning capacitors, which is great for someone who knows what the value should be. In my case, there was no such info from the vendor.

Using the frequency output on the IRQ pin, you could simply iterate through all 16 possibilities and select the best one. Here is a version done in C++: https://github.com/SloMusti/AS3935-Arduino-Library/blob/master/AS3935/AS3935.cpp It uses a divisor of 16, but divisors of 32, 64, and 128 are also available. (I'm not sure how fast an interrupt handler in python on a RPi could count.)

This person claims that the tuning with this method does not agree with what they were told for their board, and they also claim it varies over time. https://github.com/evsc/ThunderAndLightning Beats me.

I'm hoping to have you do this work :-), but if you are not interested, I assume you would consider a PR if I coded it up.

IOError: [Errno 121] Remote I/O error when LAN cable unplugged

I have RPI 3.
I run ./demo.py after I've set adress to 0x03 and Bus to 0x01
Everything is fine.
As soon as I unplug LAN cable and connect via Wifi, I get

pi@raspberrypi:~/RaspberryPi-AS3935 $ ./demo.py
Traceback (most recent call last):
File "./demo.py", line 15, in
sensor.set_indoors(True)
File "/home/pi/RaspberryPi-AS3935/RPi_AS3935/RPi_AS3935.py", line 162, in set_indoors
self.read_data()
File "/home/pi/RaspberryPi-AS3935/RPi_AS3935/RPi_AS3935.py", line 234, in read_data
self.registers = self.i2cbus.read_i2c_block_data(self.address, 0x00)
IOError: [Errno 121] Remote I/O error

Beeing connected via Wifi and LAN and then pulling the cable also leads to an error.

Two sensors, not working.

The first sensor is mine.
It was incorrectly constantly sensing lightning when I had a fan going near the sensor. After disabling this fan I no longer picked up the false strikes. However 3 storms have passed over, including a strike that destroyed a tree a few streets away. Not a single strike has been detected.
I have checked the settings after each time and I cannot find anything obvious that would be wrong.
I broke the fan when removing it from the case so I cannot see if this will trigger the sensor again.
The board is inside a plastic box on a pole outside.

The second sensor is a friend who lives in the Tropics. They have regular storms. The sensor was working fine for a few weeks before suddenly stopping. The system has been rebooted, checked settings, but strikes are no longer detected.

Both sensors are from Embedded Adventures. I have emailed EA for advice but have not heard anything. Is anyone else having similar issues? I am wondering if we have faulty boards or if we are doing something wrong with the software.

Does anyone know of anything we can try? Is there anyway to test the boards?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.