pcfens / RaspberryPi-AS3935

A basic library for working with the AS3935 that's connected to the Raspberry Pi.
Other
52 stars 24 forks source link

AS3935.py; set_indoors missing set_byte command #4

Closed lkomage closed 10 years ago

lkomage commented 10 years ago

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....

pcfens commented 10 years ago

Wow - I must have really been writing this in a hurry to have missed so much.

I fixed the set_byte in the set_indoors method, and removed the extra read_data in set_mask_disturber. I think it was there to make sure that the registers variable was up to date, but I decided to handle that differently while I was first writing everything.

The lightning.py file was meant as a proof of concept more than anything (it's very sloppy). If you have a better version that you'd like included in this repo feel free to send a pull request and I'll include it or replace the one I already have.

The RaspberryPi for a while has had issues with I2C repeated starts, something that the AS3935 is supposed to use for reading specific registers. It could be possible that the strange behavior is a result of this. There is a proposed fix, but I don't know that it's made it into any official distribution yet, and I haven't had time to do any detailed testing.

I've used it to detect lightning using the code here, though not as reliably as I would like. If you have any other thoughts, or pull requests please send them along.

lkomage commented 10 years ago

I long ago learned never to disparage anyone's code; been too many times when I looked at my own and thought, "I wrote what?!"

Your lightning.py is fine, and does what it's supposed to: allow a quick and dirty set up and test of the lightning module. I don't have anything better, and I've been using it to test this little device out. To that end, it's perfect; simple and to the point.

Interesting about the i2c issue. Not quite sure I understand what it is or how it relates, but I'll read up on it and learn.

Glad to hear you're detecting lightning! I'm hoping it will make an interesting addition to our weather station, and I'm not expecting it to perform like a $2000 Boltek lightning detector! My plan is to continue testing with your example, and when I'm a little more sure the interaction with the chip is correct, I'm going to write some code to integrate it into our existing weather station data. I'll keep you informed of anything I find out, and when I have something appreciably different from what you've done, I can submit it for addition to your library of tools. There really isn't much out there yet for this chip, so I'm glad your information is available, and that I can add something to it.

Again, thanks.

pcfens commented 10 years ago

The patched code is in the dev branch. Let me know if that seems to work better and I'll merge it in before closing the issue.

lkomage commented 10 years ago

It's all good, close it up!