Coder Social home page Coder Social logo

ironsheep / lightning-detector-mqtt2ha-daemon Goto Github PK

View Code? Open in Web Editor NEW
33.0 5.0 7.0 3 MB

Linux script to monitor AS3935 lightning detector and report detections to MQTT

License: GNU General Public License v3.0

Python 99.80% Shell 0.20%
home-assistant hassio as3935 lightning sensor mqtt daemon systemd-service raspberry-pi

lightning-detector-mqtt2ha-daemon's Introduction

lightning-detector-MQTT2HA-Daemon

Project Maintenance

GitHub Activity

License: GPL v3

GitHub Release

A simple Linux python script to monitor the AS3935 Lightning Detector attached to the Raspberry Pi on which it is running and report information from the detector via MQTT to your Home Assistant installation.

With this new sensor and a new Lovelace card (links below) you can monitor lightning presence in your local area.

When this script/daemon is run it will broadcast discovery topics via MQTT decribing this new sensor to home assistant. If you have MQTT Discovery enabled then this new sensor will appear as a new device in Home Assistant: (where the "Firmware: v...." will be the version number of this script that you've just set up.)

Discovered by Home Assistant

This script can be configured to be run in daemon mode continously in the background as a systemd service.

I2C or SPI

NOTE: if you are using an SPI version of the AS3935 sensor board, please refer to README-SPI.md instead of this page!

If you choose to use I2C, make sure that I2C is enabled on your Raspberry Pi:

sudo raspi-config

Then go to Interfacing Options -> I2C -> Yes -> Ok.

Upon saving these changes your RPi will reboot


If you like my work and/or this has helped you in some way then feel free to help me out for a couple of โ˜•'s or ๐Ÿ• slices!

coffee


Features

  • Tested on Raspberry Pi's 3 & 4 with Buster - but really should work on any.
  • Tested with Home Assistant v0.111.0
  • Tested with Mosquitto broker v5.1
  • Data is published via MQTT
  • MQTT discovery messages are sent so the detector is automatically registered with Home Assistant (if MQTT discovery is enabled in your installation)
  • MQTT authentication support
  • Supports MQTT LWT so you can tell when the script is running (or if for some reason it has stopped running)
  • No special/root privileges are required by this mechanism
  • Linux daemon / systemd service, sd_notify messages generated

Lightning Detector Device

The AS3935 Lightning Detector monitored by this script is reported as:

Name Description
Manufacturer (Austria Micro Systems) ams AG
Model AS3935
Name (fqdn) e.g., "mypi.home"
sofware ver Script Version (e.g., v1.0.0)
mac addr mac: 00:00:00:00:00:00
IP addr eth0: 00.00.00.00 -OR- wlan0: 00.00.00.00

(The addreseses reported are those of the Raspberry Pi running this script.)

Readings

This Lightning Detector as a sensor provides the following readings:

Name Description
timestamp date/time of report
energy "energy" for this report *
distance distance to storm front
count # detections since last report

* (from the datasheet: If the received signal is classified as lightning, the energy is calculated. This value is just a pure number and has no physical meaning.)

Prerequisites

Detector Board

You'll need an inexpensive lightning detector sensor (based on the AS3925 integrated circuit.) I picked up my AS3925 integrated circuit on a small circuit board from Sparkfun, but a similar board also avail. from Amazon

A NOTE of Caution! Please watch your prices... you should be able to get one for under $30 USD*. People are trying to sell them at much higher prices. Shop around for your best price.

MQTT Broker and Home Assistant

An MQTT broker is needed as the counterpart for this daemon.

MQTT is huge help in connecting different parts of your smart home and setting up of a broker is quick and easy. In many cases you've already set one up when you installed Home Assistant.

You'll need to know the hostname (or IP address) of the machine where the MQTT broker is running as well as the port it is listening to. This is typically the default MQTT port (1883). You will add this information to the config.ini for this script, as described below.

Connecting the AS3935 to your Raspberry Pi

You'll need the AS3935 Lightning sensor to be connected (via I2C for now) to your RPi. It is possible that your model RPi supports more than one I2C bus (numbered I2C0, I2C1, I2C2, etc.) The I2C1 device is exposed on our 40pin GPIO header for general use while I2C0 is generally used for a piHat EEPROM. Here's the pinout I use: (chose pins close together to make easy to wire up)

AS3935 Pin Module Pin Raspberry Pi: I2C1
4 (GND) GND 9 (Ground)
5 (VDD) 3V3 1 (3.3v)
10 (IRQ) INT 11 (GPIO 17)
11 (I2CL) SCL 5 (SCL)
13 (I2CD) SDA / MOSI 3 (SDA)

Make sure that if sensor_attached = ... in your config.ini is uncommented that it is set to I2C! If it is commented out this script defaults to I2C.

(You can use a different GPIO pin for the IRQ, but remember to change your config.ini:intr_pin = 17 value to the GPIO # you choose.)

Installation

On a modern Linux system just a few steps are needed to get the daemon working. The following example shows the installation on Debian/Raspbian below the /opt directory:

sudo apt install git python3 python3-pip python3-pigpio pigpio

sudo git clone https://github.com/ironsheep/lightning-detector-MQTT2HA-Daemon /opt/ISP-lightning-mqtt-daemon

cd /opt/ISP-lightning-mqtt-daemon
sudo pip3 install -r requirements.txt

Configuration

To match personal needs, all operational details can be configured by modifying entries within the file config.ini. The file needs to be created first:

cp /opt/ISP-lightning-mqtt-daemon/config.{ini.dist,ini}
vim /ISP-lightning-mqtt-daemon/config.ini

Execution

When you are ready to test your adjustments to the config.ini file you can start an MQTT monitor tool to see what your newly adjusted script will do. (I use MQTTBox to monitor all my MQTT testing.)

Once you are ready to test, start the PiGPIOd service

sudo systemctl start pigpiod.service

Then a test run is as easy as:

python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py

Using the command line argument --config, a directory where to read the config.ini file from can be specified, e.g.

python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --config /opt/ISP-lightning-mqtt-daemon

Antenna Fine Tuning

The AS3935 has a fine tuning adjustment setting for the 500KHz antenna. Our script has a special option we can use to determine the fine-tuning value our board needs. After running the script we then record the value in our config.ini.

Run the fine tuning option using:

python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap

the output will look something like (yours will be different!):

* Please allow a long time for this function to stop. It should take a little over 3 minutes to check test the 16 values
For tuning 0x0: average frequency of 511695.239310 Hz (diff: +731.0)
For tuning 0x1: average frequency of 511706.015761 Hz (diff: +731.6)
For tuning 0x2: average frequency of 507383.793496 Hz (diff: +461.5)
For tuning 0x3: average frequency of 507158.814087 Hz (diff: +447.4)
For tuning 0x4: average frequency of 503960.562001 Hz (diff: +247.5)
For tuning 0x5: average frequency of 504470.100857 Hz (diff: +279.4)
For tuning 0x6: average frequency of 502103.026784 Hz (diff: +131.4)
For tuning 0x7: average frequency of 500849.485221 Hz (diff: +53.1)
For tuning 0x8: average frequency of 498205.778733 Hz (diff: +112.1)
For tuning 0x9: average frequency of 495721.390191 Hz (diff: +267.4)
For tuning 0xa: average frequency of 497672.497232 Hz (diff: +145.5)
For tuning 0xb: average frequency of 494356.500465 Hz (diff: +352.7)
For tuning 0xc: average frequency of 490485.394396 Hz (diff: +594.7)
For tuning 0xd: average frequency of 491429.095599 Hz (diff: +535.7)
For tuning 0xe: average frequency of 489151.314855 Hz (diff: +678.0)
For tuning 0xf: average frequency of 490377.098284 Hz (diff: +601.4)
- Your best tuning capacitor value is 0x7: which is off by +53.1

now you need to add the 0x7 from the last line (above) to the config.ini. Look for the [Sensor] section and in there add a line:

tuning_capacitor = 0x7

(If you have qeustions about this take a look at the config.ini.dist file as it shows where the new entry should be.)

Save the file, and do a quick test again to make sure the script is ok with your config change.

Now you are ready to enable the detector script.

Run as Daemon / Service

You probably want to execute this script continuously in the background.

This can be done by running it as a daemon via the Systemd service - on systemd managed systems (the recommended option)

NOTE: Daemon mode must be enabled in the configuration file (default).

By default the isp-lightning.service file indicates that the script should be run as user:group daemon:daemon. As this script requires access to i2c and gpio you'll want to add access to them for the daemon user as follows:

# list current groups
groups daemon
$ daemon : daemon

# add i2c, gpio if not present
sudo usermod daemon -a -G i2c,gpio

# list current groups
groups daemon
$ daemon : daemon i2c gpio
#                 ^^^^^^^^ now they are present

Now that the 'daemon' user is configured to allow access the hardware you can setup the script to be run as a system service as follows:

  sudo ln -s /opt/ISP-lightning-mqtt-daemon/isp-lightning.service /etc/systemd/system/isp-lightning.service

  sudo systemctl daemon-reload

  # configure services so they start on reboot
  sudo systemctl enable pigpiod.service
  sudo systemctl enable isp-lightning.service

  # start services now
  sudo systemctl start pigpiod.service
  sudo systemctl start isp-lightning.service

  # see if services are running (ensure no start errors)
  sudo systemctl start pigpiod.service
  sudo systemctl status isp-lightning.service

NOTE: we use a symbolic link 'ln -s' so that when you list the files in /etc/systemd/system the link will point back to where your project in installed. You'll see that many other packages installed on your system already do this.

Integration with MQTT and Home Assistant

Detection values will be published to the (configurable) MQTT broker topic "{base_topic}/{sensorName}/detect" (e.g. home/nodes/lightning01/detect).

An example:

{
  "last": "2020-06-19T17:26:22-06:00",
  "energy": 96098,
  "distance": 1,
  "count": 1
}

This data can then be subscribed to and processed by your home assistant installation.

Additionally, the detector settings along with script settings are written to: "{base_topic}/{sensorName}/settings" with the following fields:

{
  "settings": {
    "timestamp": "2020-07-10T12:52:07-06:00",
    "hardware": {
      "min_strikes": 5,
      "afe_inside": true,
      "disp_lco": false,
      "noise_floor": 1
    },
    "script": {
      "period_in_minutes": 5,
      "end_storm_minutes": 30,
      "number_of_rings": 5,
      "distance_units": "km"
    }
  }
}

Lastly, there are two additional topics published which are used to drive our new Lovelace card. These are:

  • "{base_topic}/{sensorName}/crings" - which posts the live status of current period, updated at each new strike
  • "{base_topic}/{sensorName}/prings" - which posts the status of the preceeding full period, updated at the end of a period

Lovelace Card for Home Assistant

Want to go further? There is a Lovelace Lightning Detector Card built specifically for visualizing this lightning data.

Credits

Thank you to "Hexalyse" for providing the starting logic for this effort. His project which i had tweeting (yes, in french) locally here in Colorado when i was first bringing up my hardware is LightningTweeter

Thank you to also Thomas Dietrich for providing a wonderful pattern for this project. His project, which I use and heartily recommend, is miflora-mqtt-deamon

Thank you to Eloi Codina for providing the I2C source for AS3935 I2C access AS3935 v0.1.4

Disclaimer and Legal


Raspberry Pi is registered trademark of Raspberry Pi (Trading) Ltd.

This project is a community project not for commercial use. The authors will not be held responsible in the event of device failure or failure to detect any potentially damaging lightning.

This project is in no way affiliated with, authorized, maintained, sponsored or endorsed by Raspberry Pi (Trading) Ltd. or any of its affiliates or subsidiaries.

This project is also in no way affiliated with, authorized, maintained, sponsored or endorsed by Austria Micro Systems - ams AG, makers of the AS3935 Lightning Sensor.


License

Copyright ยฉ 2020 Iron Sheep Productions, LLC. All rights reserved.
Licensed under the GPL 3 License.

Follow these links for more information:

lightning-detector-mqtt2ha-daemon's People

Contributors

ironsheep avatar lightmaster avatar zebble avatar

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

Watchers

 avatar  avatar  avatar  avatar  avatar

lightning-detector-mqtt2ha-daemon's Issues

Problems with reading config.ini file

Hello

Thank you for this great project.
I use a Raspberry 3b

I tried to follow your instructions step by step,
The wiring for the sensor ist correct and I can detect the lightning sensor adress with i2cdetect.
I created and edit the config.ini file in the following path:
/opt/ISP-lightning-mqtt-daemon

But at the section "Execution", where I can test the code, I get this error:
no matter whether I specify the directory for the config file ("--config" ) or not

pi@raspi3:~ $ python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --config /opt/ISP-lightning-mqtt-daemon
[2021-07-12 10:43:05] - * init mqtt_client_connected=[False]
Traceback (most recent call last):
File "/opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py", line 155, in
config.read_file(config_file)
File "/usr/lib/python3.7/configparser.py", line 717, in read_file
self._read(f, source)
File "/usr/lib/python3.7/configparser.py", line 1079, in _read
raise MissingSectionHeaderError(fpname, lineno, line)
configparser.MissingSectionHeaderError: File contains no section headers.
file: '/opt/ISP-lightning-mqtt-daemon/config.ini', line: 1
'\ufeff# Configuration file for lightning-detector-MQTT2HA-Daemon\n'

My config.ini file code is here:

Configuration file for lightning-detector-MQTT2HA-Daemon

Source: https://github.com/ironsheep/lightning-detector-MQTT2HA-Daemon

Uncomment and adapt all settings as needed.

Some settings can be configured by environment variables.

If an env variable is set, it takes precedence over settings in this file

[General]

[Daemon]

Enable or Disable an endless execution loop (Default: true)

enabled = true

[MQTT]

The hostname or IP address of the MQTT broker to connect to (Default: localhost)

Also read from the MQTT_HOSTNAME environment variable

hostname = localhost

The TCP port the MQTT broker is listening on (Default: 1883)

Also read from the MQTT_PORT environment variable

port = 1883

Maximum period in seconds between ping messages to the broker. (Default: 60)

keepalive = 60

NOTE: The MQTT topic used for this device is constructed as:

{base_topic}/{sensor_name}

The MQTT base topic to publish the Lightning detector sensor data topics under.

base_topic = home/nodes

The MQTT name for this Lightning detector sensor

sensor_name = lightningdetector

The MQTT broker authentification credentials (Default: no authentication)

Will also read from MQTT_USERNAME and MQTT_PASSWORD environment variables

#username = dani
#password = pwd123

Enable TLS/SSL on the connection

tls = false

Path to CA Certificate file to verify host

#tls_ca_cert =

Path to TLS client auth key file

#tls_keyfile =

Path to TLS client auth certificate file

#tls_certfile =

Broker connection-failure recovery

Rety connection attempts [default 5]

retry_count = 5

Retry after waiting N seconds [default 30]

retry_wait_in_seconds = 30

[Behavior]

This script accumulates detections into buckets (rings if you will) for this period of time [2-10] in minutes [Default: 5]

period_in_minutes = 5

This script accumulates values into [3-7] rings [Default: 5]

number_of_rings = 5

This script can report distances in miles or kilometers (mi, km) [Default: km]

distance_as = km

This script determines that a storm has ended after this period of time [10-60] in minutes [Default: 30]

end_storm_after_minutes = 30

[Sensor]

decribe how your sensor is hooked up to your RPi

use values 'I2C' or 'SPI' - default is 'I2C'

sensor_attached = I2C

GPIO pin used for interrupts

intr_pin = 17

The SPI bus and device numbers come from the device file being used.

e.g., /dev/spidev0.0 would be bus=0, device=0

The defaults are 0 and 0 (SPI0 which appears at the GPIO 40pin header)

#spi_bus = 0
#spi_device = 0

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.

i2c_bus = 1
i2c_address = 0x03

Value to use for your board

Internal Tuning Capacitors (from 0 to 120pF in steps of 8pf) - A value of [0-15]

run the script with a --tune paramater to determine value for your board

NOTE: this runs for 3 minutes so be patient! Then record your best value here.

tuning_capacitor = 0x1

Indoors (True) = more sensitive (can miss very strong lightnings)

Outdoors (False) = less sensitive (can miss far away lightnings)

detector_afr_gain_indoor = false

#detector_noise_floor = 1

Prevent single isolated strikes from being logged => interrupts begin after

this number of strikes (def: 5, value 1,5,9,16), then are fired normally.

detector_min_strikes = 5

The mqtt connection fails.

Does someone have a idea, what could be the problem ?

Kind regards
Daniel

Home Assistant YAML configuration for sensors

Thank you Stephen for a great script and add on to Home Assistant.

It's been a year or so since I have configured the RPi script and it has been working fine.

I have the POE RPi mounted in the attic here in the midwest.

I see the data on MQTT explorer.

A week or so ago tried to configure HA config yaml for the sensors and for whatever reason I cannot properly configure the sensors.

Can you please post an example of your yaml configuration of the sensors used?

This is an example of what I have tried and it is not working.

sensor:

  • platform: mqtt
    state_topic: "homeassistant/sensor/lightningdetector/energy/config"
    name: "lightningdetector_energy"
    value_template: "{{ value_json.energy }}"

This is what I see on MQTT explorer.

{"name": "Lightningdetector Energy",
"uniq_id": "AS3935-b827ebd2c051_energy",
"stat_t": "/detect",
"val_tpl": "{{ value_json.energy }}",
"
": "homeassistant/nodes/sensor/lightningdetector",
"pl_avail": "Online",
"pl_not_avail": "Offline",
"avty_t": "~/status",
"dev": {"identifiers": ["AS3935-b827ebd2c051"]}}

[Improvement] Hardlink to system tools

gw = os.popen("ip -4 route show default").read().split()

ether = os.popen("ifconfig " + interface + "| grep ether").read().split()

I'd suggest looking in common system tool directories (/bin, /sbin, etc) and then hardlinking to system tools to make sure they work regardless of whether or not their directories are in the $PATH variable. Rasbian had a bug at one point where /sbin was not in the user's default $PATH and I just ran into this issue after installing zsh, with the script throwing an error that ifconfig was not found.

Tuning Frequency is Far Off

Checklist:

  • I updated to the latest version available
  • I double-checked my hardware connections
  • I checked that my MQTT broker is otherwise working

Release with the issue:
v2.2.7

Last working release (if known):

Hardware, Operating System, Python version:
Rasp Pi Zero W
AS3935

Description of problem:
Tuning lists average frequencies around 45,000 HZ, instead of 500,000. Yesterday it showed 125,000 average, but I don't believe I changed anything. No tuning cap gets it close to the ideal 500,000. Any ideas what could cause this? Here are the tuning logs:

* Please allow a long time for this function to stop. It should take a little over 3 minutes to test the 16 values
For tuning 0x0: average frequency of 42128.698511 Hz (diff: +28617.0)
For tuning 0x1: average frequency of 49636.536376 Hz (diff: +28147.7)
For tuning 0x2: average frequency of 49684.220732 Hz (diff: +28144.7)
For tuning 0x3: average frequency of 49599.874890 Hz (diff: +28150.0)
For tuning 0x4: average frequency of 49607.471047 Hz (diff: +28149.5)
For tuning 0x5: average frequency of 48872.206343 Hz (diff: +28195.5)
For tuning 0x6: average frequency of 49594.869901 Hz (diff: +28150.3)
For tuning 0x7: average frequency of 49128.727662 Hz (diff: +28179.5)
For tuning 0x8: average frequency of 48260.377657 Hz (diff: +28233.7)
For tuning 0x9: average frequency of 48562.278049 Hz (diff: +28214.9)
For tuning 0xa: average frequency of 49030.357431 Hz (diff: +28185.6)
For tuning 0xb: average frequency of 47609.750222 Hz (diff: +28274.4)
For tuning 0xc: average frequency of 47798.167907 Hz (diff: +28262.6)
For tuning 0xd: average frequency of 47485.117801 Hz (diff: +28282.2)
For tuning 0xe: average frequency of 45858.543565 Hz (diff: +28383.8)
For tuning 0xf: average frequency of 45551.386876 Hz (diff: +28403.0)
- Your best tuning capacitor value is 0x2: which is off by +28144.7

Python errors shown in the logs (if applicable):

* Please allow a long time for this function to stop. It should take a little over 3 minutes to test the 16 values
For tuning 0x0: average frequency of 42128.698511 Hz (diff: +28617.0)
For tuning 0x1: average frequency of 49636.536376 Hz (diff: +28147.7)
For tuning 0x2: average frequency of 49684.220732 Hz (diff: +28144.7)
For tuning 0x3: average frequency of 49599.874890 Hz (diff: +28150.0)
For tuning 0x4: average frequency of 49607.471047 Hz (diff: +28149.5)
For tuning 0x5: average frequency of 48872.206343 Hz (diff: +28195.5)
For tuning 0x6: average frequency of 49594.869901 Hz (diff: +28150.3)
For tuning 0x7: average frequency of 49128.727662 Hz (diff: +28179.5)
For tuning 0x8: average frequency of 48260.377657 Hz (diff: +28233.7)
For tuning 0x9: average frequency of 48562.278049 Hz (diff: +28214.9)
For tuning 0xa: average frequency of 49030.357431 Hz (diff: +28185.6)
For tuning 0xb: average frequency of 47609.750222 Hz (diff: +28274.4)
For tuning 0xc: average frequency of 47798.167907 Hz (diff: +28262.6)
For tuning 0xd: average frequency of 47485.117801 Hz (diff: +28282.2)
For tuning 0xe: average frequency of 45858.543565 Hz (diff: +28383.8)
For tuning 0xf: average frequency of 45551.386876 Hz (diff: +28403.0)
- Your best tuning capacitor value is 0x2: which is off by +28144.7

Additional information:

Add logic to post our storm as ended status

Our script has logic (no lightning for ~30mins) indicating a storm has passed...
We need to awaken special MQTT Topic posts of this information so our card can display it correctly...

MQTT lightningdetector/detect not updating count and distance

Checklist:

  • I updated to the latest version available
  • I double-checked my hardware connections
  • I checked that my MQTT broker is otherwise working

Release with the issue:
v2.0.3
Last working release (if known):

Hardware, Operating System, Python version:
RPi 3B, Raspberry Pi OS, Python 3.7.3

Description of problem:
MQTT messages to detect for count and distance always equal 1, even though the messages sent to crings and prings count properly and show reasonable distances.

MQTT Lightning Detect Count bug

Python errors shown in the logs (if applicable):


Additional information:

Spi bus tuning_cap Error

Python error log:

pi@raspberrypi:~ $ python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap
[2020-12-18 03:16:54] * Mode: Calculate Tuning Cap value and exit
[2020-12-18 03:16:54] - * init mqtt_client_connected=[False]
[2020-12-18 03:16:54] * Sensor on SPI bus
[2020-12-18 03:16:54] - - ip=[10.10.10.164], mac[d0:6f:4a:fe:64:f0], interface=[wlan0], uniq-id=[AS3935-d06f4afe64f0]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Can't connect to pigpio at localhost(8888)

Did you start the pigpio daemon? E.g. sudo pigpiod

Did you specify the correct Pi host/port in the environment
variables PIGPIO_ADDR/PIGPIO_PORT?
E.g. export PIGPIO_ADDR=soft, export PIGPIO_PORT=8888

Did you specify the correct Pi host/port in the
pigpio.pi() function? E.g. pigpio.pi('soft', 8888)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[2020-12-18 03:16:55] - - Testing AS3935 Communications...
[2020-12-18 03:16:55] - - TEST write=5, read-back=5
[2020-12-18 03:16:55] - - TEST write=2, read-back=2

  • Please allow a long time for this function to stop. It should take a little over 3 minutes to test the 16 values
    Traceback (most recent call last):
    File "/opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py", line 1171, in
    detector.calculate_tuning_cap()
    File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 442, in calculate_tuning_cap
    freqs.append(self.calculate_resonance_frequency(seconds_try))
    File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 463, in calculate_resonance_frequency
    cb = self.pi.callback(self.irq)
    File "/usr/lib/python3/dist-packages/pigpio.py", line 4920, in callback
    return _callback(self._notify, user_gpio, edge, func)
    File "/usr/lib/python3/dist-packages/pigpio.py", line 1210, in init
    self._notify.append(self.callb)
    AttributeError: 'NoneType' object has no attribute 'append'

I tried changing the intr_pin in the config.inf file but with no luck

Provide an MQTT subscription triggered version

The Lovelace component works great... but can't use it for my own sensor
So I've created my own remote sensor wired to an ESP32 powered by a solar panel and LION battery. It publishes strikes via MQTT. For me, Python makes my head spin (yet I my ESP32 is in C++... ain't that a kick).

How about making a version of this that is driven by an incoming MQTT subscription
Can you yank out the SPI/I2C code and make this purely driven by an MQTT subscription? I've found it quite easy to ETL my own published messages into your detect format based on an HA automation. All that missing is a way to feed it back into your process.

While your at it, can you make this script something that HA can manage either in the scripts/ or python_scripts/ subfolders?

Describe alternatives you've considered
None, really... I like your Lovelace component and would love to add it...

Additional context
Here's a stupid simple template on reformatting my own message into your detect message:

- alias: Republish Lightning Message
  trigger:
    platform: mqtt
    topic: 'sensor/weather/test'
  action:
    service: mqtt.publish
    data_template:
      topic: 'sensor/weather/lightning/detect'
      #retain: true
      payload: >-
        {"last":{{ trigger.payload_json["sensor"]["timeStamp"] }},"energy":{{ trigger.payload_json["weather"]["energy"] }},"distance":{{ trigger.payload_json["weather"]["distanceMiles"] }},"count":1}

Docker-Image available?

Is your feature request related to a problem? Please describe.
No, no Problem

Describe the solution you'd like
Docker-Image to run inside a container, Docker-Compose would be great

Describe alternatives you've considered
Native installation on old RPi1 is working fine but I want to consolidate the boards.

Additional context
I just want to ask if there is a docker based solution so the tool can run inside a container. As far as I see GPIO should be accessible in a container.

Not sure if --calc_tuning_cap is working right

Checklist:

  • I updated to the latest version available
  • I double-checked my hardware connections
  • I checked that my MQTT broker is otherwise working

Release with the issue:
v2.2.0

Last working release (if known):

Hardware, Operating System, Python version:

Description of problem:
I tried running the new "calculate tuning cap value" and my values were all identical, whereas the readme examples are all different. Not sure if its working on mine.

Python errors shown in the logs (if applicable):

pi@LightningPi [12:42:17 AM] [/opt/ISP-lightning-mqtt-daemon] [master *]                                                                                                    
-> % python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap                                                                                  
[2020-09-04 00:43:22] * Mode: Calculate Tuning Cap value and exit                                                                                                           
[2020-09-04 00:43:22] - * init mqtt_client_connected=[False]                                                                                                                
[2020-09-04 00:43:22] * Sensor on SPI bus                                                                                                                                   
* Please allow a long time for this function to stop. It should take a little over 3 minutes to test the 16 values                                                          
For tuning 0x0: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x1: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x2: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x3: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x4: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x5: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x6: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x7: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x8: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0x9: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xa: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xb: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xc: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xd: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xe: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
For tuning 0xf: average frequency of 0.000000 Hz (diff: +31250.0)                                                                                                           
- Your best tuning capacitor value is 0x0: which is off by +31250.0

Additional information:

i2c broken?

Checklist:

  • [X ] I updated to the latest version available
  • [ X] I double-checked my hardware connections
  • [ X] I checked that my MQTT broker is otherwise working

Release with the issue:
master

Last working release (if known):
unknown

Hardware, Operating System, Python version:
Pi 3B, python 3.7.3, raspbian 10.4

Description of problem:

When using i2c, a 'bad i2c bus' error is triggered.

Python errors shown in the logs (if applicable):

Traceback (most recent call last):
File "ISP-lightning-mqtt-daemon.py", line 937, in
detector = AS3935_I2C(interrupt_pin, i2c_address, i2c_bus)
File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 559, in init
self.device = self.pi.i2c_open(bus, address)
File "/usr/local/lib/python3.7/dist-packages/pigpio.py", line 2674, in i2c_open
self.sl, _PI_CMD_I2CO, i2c_bus, i2c_address, 4, extents))
File "/usr/local/lib/python3.7/dist-packages/pigpio.py", line 1009, in _u2i
raise error(error_text(v))
pigpio.error: 'bad I2C bus'


Additional information:

It appears the AS3935_I2C call has address and bus reversed. Changing it to:

detector = AS3935_I2C(interrupt_pin, i2c_address, i2c_bus)

Fixes this problem, but creates another:

Traceback (most recent call last):
File "ISP-lightning-mqtt-daemon.py", line 947, in
detector.set_noise_floor(testValue)
File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 194, in set_noise_floor
self.write_byte(0x01, (self.read_byte(0x01) & 0b10001111) + ((noise_floor & 0x07) << 4))
File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 576, in read_byte
return self.pi.i2c_read_byte_data(self.i2c_device, address)
AttributeError: 'AS3935_I2C' object has no attribute 'i2c_device'

Uncommenting line 42 in AS3935_i2c_spi.py fixes the issue:

    # Let our derived classes handle this!
    self.i2c_device = self.pi.i2c_open(bus, address)

Perhaps SPI has become the preferred choice and i2c got broken during the process/addition of adding SPI support?

Stupid Question from A New HA User

Hello Stephen,

Thank you so much for this repo! I am a newly-converted Homekit to HA user and am simply in love with the fine work all of you have done.

i have a silly question, as I am also brand new to raspberry PIs/Linux. I am using Home Assistant OS 5.13, and it took me a while to figure out how to activate I2C, since the method you indicate was not possible (due to the Home Assistant OS). Here is the info I found on how to activate it (you may want to include this in your instructions) https://www.home-assistant.io/common-tasks/os/

My question is, how do I run the install script? I have no experience running an install script in Linux, and using the terminal in HA OS, the sudo command fails. Could you please assist me here (or better yet, point me to where I can read how to do it?) I thrive on finding things out for myself, but in this case, I am stumped!

thank you so much for any help in advance.

Eric

Could you add an SPI interface?

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

The current version (2) of SparkFun Lightning Detector - AS3935 comes with an SPI interface
The SparkFun clams the I2C interface is not reliable.
The I2C interface is still on the board, but looks like you have to cut traces on the board to enable it.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Add a SPI interface to the Daemon

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Cutting traces is a last resort and may cause the board to be unreliable due to the I2C chipset.
I would probably attempt to add the SPI interface.

Additional context
Add any other context or screenshots about the feature request here.

My board arrives tomorrow (July 20). Looking forward to playing with it in HA.
Thx

Sensor settings doesn't reflect what is configured in the config.ini

Checklist:

  • I updated to the latest version available
  • I double-checked my hardware connections
  • I checked that my MQTT broker is otherwise working

Release with the issue:

master branch

Last working release (if known):

Hardware, Operating System, Python version:

Python: Python 3.7.3
OS: raspbian light
Hardware: RaspberryPi ZeroW

Following the manual installation via source code.

Description of problem:

Overall the integration is working but when I try and adjust the following properties in the config.ini file they don't seem to take effect.

detector_afr_gain_indoor = False

detector_noise_floor = 2

When the deamon starts the settings Data being logged via mqtt doesn't seem to match with the config I specified:

Publishing to MQTT topic "home/nodes/sensor/lightningdetector/settings, Data:{"settings": {"timestamp": "2021-01-06T19:59:49+00:00", "hardware": {"min_strikes": 5, "afe_inside": true, "disp_lco": false, "noise_floor": 1}, "script": {"period_minutes": 5, "end_minutes": 30, "number_rings": 5, "distance_units": "km"}}}"

The noise floor is explicitly logged as: "noise_floor": 1 and not 2 and after a while you can see it is busy adjusting the noise_floor:

[2021-01-06 20:09:54] << INTR(17) >> Noise level too high - adjusting
[2021-01-06 20:10:23] << INTR(17) >> Noise level too high - adjusting

Ideally I want both those settings to take effect when starting the daemon.

Python errors shown in the logs (if applicable):


Additional information:

My config.ini, passwords redacted.

# Configuration file for lightning-detector-MQTT2HA-Daemon
# Source: https://github.com/ironsheep/lightning-detector-MQTT2HA-Daemon
#
# Uncomment and adapt all settings as needed.
# Some settings can be configured by environment variables.
# If an env variable is set, it takes precedence over settings in this file

[General]


[Daemon]

# Enable or Disable an endless execution loop (Default: true)
#enabled = true


[MQTT]

# The hostname or IP address of the MQTT broker to connect to (Default: localhost)
# Also read from the MQTT_HOSTNAME environment variable
hostname = xx

# The TCP port the MQTT broker is listening on (Default: 1883)
# Also read from the MQTT_PORT environment variable
port = 1883

# Maximum period in seconds between ping messages to the broker. (Default: 60)
#keepalive = 60


# NOTE: The MQTT topic used for this device is constructed as:
#  {base_topic}/{sensor_name}
#
# The MQTT base topic to publish the Lightning detector sensor data topics under.
base_topic = home/nodes

# The MQTT name for this Lightning detector sensor
sensor_name = lightningdetector


# The MQTT broker authentification credentials (Default: no authentication)
# Will also read from MQTT_USERNAME and MQTT_PASSWORD environment variables
username = xx
password = xx

# Enable TLS/SSL on the connection
#tls = false

# Path to CA Certificate file to verify host
#tls_ca_cert =

# Path to TLS client auth key file
#tls_keyfile =

# Path to TLS client auth certificate file
#tls_certfile =

# Broker connection-failure recovery
#  Rety connection attempts [default 5]
#retry_count = 5

#  Retry after waiting N seconds [default 30]
#retry_wait_in_seconds = 30


[Behavior]

# This script accumulates detections into buckets (rings if you will) for this period of time [2-10] in minutes [Default: 5]
#period_in_minutes = 5

# This script accumulates values into [3-7] rings [Default: 5]
#number_of_rings = 5

# This script can report distances in miles or kilometers (mi, km) [Default: km]
#distance_as = km

# This script determines that a storm has ended after this period of time [10-60] in minutes [Default: 30]
#end_storm_after_minutes = 30


[Sensor]

# decribe how your sensor is hooked up to your RPi
#  use values 'I2C' or 'SPI' - default is 'I2C'
sensor_attached = I2C

# GPIO pin used for interrupts
intr_pin = 17

# The SPI bus and device numbers come from the device file being used.
#   e.g., /dev/spidev0.0  would be bus=0, device=0
# The defaults are 0 and 0 (SPI0 which appears at the GPIO 40pin header)
#spi_bus = 0
#spi_device = 0

# 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.
#i2c_bus = 1
i2c_address = 0x03

# Value to use for your board
#   Internal Tuning Capacitors (from 0 to 120pF in steps of 8pf) - A value of [0-15]
# run the script with a --tune paramater to determine value for your board
#  NOTE: this runs for 3 minutes so be patient!  Then record your best value here.
#tuning_capacitor = 0x1

# Indoors (True) = more sensitive (can miss very strong lightnings)
# Outdoors (False) = less sensitive (can miss far away lightnings)
detector_afr_gain_indoor = False

detector_noise_floor = 2

# Prevent single isolated strikes from being logged => interrupts begin after
# this number of strikes (def: 5, value 1,5,9,16), then are fired normally.
#detector_min_strikes = 5

Question on noise level

Checklist:

  • [x ] I updated to the latest version available
  • [ x] I double-checked my hardware connections
  • [x ] I checked that my MQTT broker is otherwise working

Release with the issue:
2.2.7
Last working release (if known):

Hardware, Operating System, Python version:
Raspberry Pi 3+, RaspiOS Debian Version 10.7, Python 3.7.3

Description of problem:

I'm getting a message saying

[2021-03-14 12:39:13] << INTR(17) >> Noise level too high - adjusting
[2021-03-14 12:39:13] << INTR(17) >> Noise level too high - adjusting
[2021-03-14 12:39:14] << INTR(17) >> Disturber detected. Masking subsequent disturbers

I'm wondering what these messages mean, and if I should change a setting. Also, my tunning values seem pretty far "off" compared to your examples. The closest one is 0xf which is still +519.9. Is this ok, or is there something wrong?

Python errors shown in the logs (if applicable):


Additional information:

I2C Write Error

Checklist:

  • I updated to the latest version available
  • I double-checked my hardware connections
  • I checked that my MQTT broker is otherwise working

Release with the issue:
v2.2.3
Last working release (if known):

Hardware, Operating System, Python version:
RPi 3B
Rasbian
Python 3.7.3

Description of problem:
Probably related to #10, but getting an I2C read/write error.

Python errors shown in the logs (if applicable):

pi@LightningPi [03:11:24 PM] [/opt/ISP-lightning-mqtt-daemon] [master *]                                                                                                      [9/190]
-> % python3 ./ISP-lightning-mqtt-daemon.py -d -v
[2020-09-10 15:11:32] - ISP-lightning-mqtt-daemon.py v2.2.3
[2020-09-10 15:11:32] - Verbose enabled
[2020-09-10 15:11:32] - (DBG): Debug enabled
[2020-09-10 15:11:32] - (DBG): * init mqtt_client_connected=[False]
[2020-09-10 15:11:32] * Sensor on I2C bus
[2020-09-10 15:11:32] - * Connecting to MQTT broker ...
[2020-09-10 15:11:32] - (DBG): log: Sending CONNECT (u1, p1, wr1, wq0, wf1, c1, k60) client_id=b''
[2020-09-10 15:11:32] - (DBG): log: Sending PUBLISH (d0, q0, r0, m1), 'b'home/nodes/sensor/lightningdetector/status'', ... (6 bytes)
[2020-09-10 15:11:32] - (DBG): * Wait on mqtt_client_connected=[False]
[2020-09-10 15:11:32] - (DBG): log: Received CONNACK (0, 0)
[2020-09-10 15:11:32] MQTT connection established
[2020-09-10 15:11:32]
[2020-09-10 15:11:32] - (DBG): on_connect() mqtt_client_connected=[True]
[2020-09-10 15:11:33] - (DBG): - stopped MQTT timer
[2020-09-10 15:11:33] - (DBG): - started MQTT timer - every 60 seconds
[2020-09-10 15:11:33] Announcing Lightning Detection device to MQTT broker for auto-discovery ...
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m2), 'b'homeassistant/sensor/lightningdetector/last/config'', ... (534 bytes)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m3), 'b'homeassistant/sensor/lightningdetector/energy/config'', ... (301 bytes)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m4), 'b'homeassistant/sensor/lightningdetector/distance/config'', ... (336 bytes)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m5), 'b'homeassistant/sensor/lightningdetector/count/config'', ... (298 bytes)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 2)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m6), 'b'homeassistant/sensor/lightningdetector/settings/config'', ... (418 bytes)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 3)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 4)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m7), 'b'homeassistant/sensor/lightningdetector/crings/config'', ... (406 bytes)
[2020-09-10 15:11:33] - (DBG): log: Sending PUBLISH (d0, q1, r1, m8), 'b'homeassistant/sensor/lightningdetector/prings/config'', ... (403 bytes)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 5)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 6)
[2020-09-10 15:11:33] - * I2C configuration bus=1 - addr=3
[2020-09-10 15:11:33] - (DBG): - Testing AS3935 Communications...
[2020-09-10 15:11:33] - (DBG): - TEST write=5, read-back=5
Traceback (most recent call last):
  File "./ISP-lightning-mqtt-daemon.py", line 954, in <module>
    detector.set_noise_floor(testValue)
  File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 194, in set_noise_floor
    self.write_byte(0x01, (self.read_byte(0x01) & 0b10001111) + ((noise_floor & 0x07) << 4))
  File "/opt/ISP-lightning-mqtt-daemon/AS3935/AS3935_i2c_spi.py", line 588, in write_byte
    self.pi.i2c_write_byte_data(self.device, address, value)
  File "/usr/lib/python3/dist-packages/pigpio.py", line 2743, in i2c_write_byte_data
    self.sl, _PI_CMD_I2CWB, handle, reg, 4, extents))
  File "/usr/lib/python3/dist-packages/pigpio.py", line 979, in _u2i
    raise error(error_text(v))
pigpio.error: 'I2C write failed'
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 7)
[2020-09-10 15:11:33] - (DBG): log: Received PUBACK (Mid: 8)

Additional information:

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.