AS3935 Lightning Detector Sensor to MQTT - 2020 Version

pete_c

Guru
Last time I did this was around 2017 and used an RPi 1.  This time around the author of the AS3935 to MQTT application wrote it for Home Assistant.  It works well too with Homeseer and mcsMQTT.
 
This is part one which uses an RPi2.  Part 2 is similiar and uses Tasmota and a Sonoff device.
 
This is a revisit of an old project that used an RPi a few years back.

Orginally tried to do this with Buster 64bit on the Pine64. Didn't work probably cuz the Buster 64bit libraries do not match or do not exist yet to the 32bit Buster libraries. When I used the 32 bit Buster on the RPi all worked well.

Trying this two ways. One is to connect to an RPI and the other is to use a XXX and Tasmota firmware.

Hardware and mod adds utilized:

1 - Rpi 2 with Buster 32 bit
2 - enable i2C via rasbi-config
3 - sudo apt install git python3 python3-pip python3-pigpio pigpio
4 - sudo git clone https://github.com/ironsheep/lightni...MQTT2HA-Daemon /opt/ISP-lightning-mqtt-daemon
5 - cd /opt/ISP-lightning-mqtt-daemon
6 - sudo pip3 install -r requirements.txt

2 - GY-AS3935 AS3935 Lightning Detector Sensor Thunder Storm Warning Distance Detection Module for Arduino Raspberry Pi Weather Station(Max 40km)- Amazon

Pins from Lightning detector module to RPI



AS3935 Lightning Detector Sensor to RPi wiring

This wiring part to the AS3935 Lightning sensor is missing in the original discussion here:

Lightning detector to MQTT2HA daemon.

Guessing author guesses that all of the lightning sensors are made the same way and wired the same way.

So referencing this page for my wiring of my Amazon purchased GY-AS3935 Board



And connecting the above to the RPi using authors wiring schema to RPi.



I almost returned my Amazon purchased GY-AS3935 Board thinking it was defective until I wired it correctly this morning after tinkering with it for 2 days and wondering why it wasn't working.

Testing it now and see the AS3935 Lightning sensor.
 

Code:
ICS-Lightning:~# 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: -- -- -- -- -- -- -- --

Code:
ICS-Lightning:~# python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py
[2020-09-16 08:19:38] - * init mqtt_client_connected=[False]
[2020-09-16 08:19:38] * Sensor on I2C bus
[2020-09-16 08:19:38] - log: Sending CONNECT (u0, p0, wr1, wq0, wf1, c1, k60) client_id=b''
[2020-09-16 08:19:38] - log: Sending PUBLISH (d0, q0, r0, m1), 'b'home/nodes/sensor/lightningdetector/status'', ... (6 bytes)
[2020-09-16 08:19:38] - * Wait on mqtt_client_connected=[False]
[2020-09-16 08:19:38] - log: Received CONNACK (0, 0)
[2020-09-16 08:19:38] MQTT connection established
[2020-09-16 08:19:38]
[2020-09-16 08:19:38] - on_connect() mqtt_client_connected=[True]
[2020-09-16 08:19:39] - - stopped MQTT timer
[2020-09-16 08:19:39] - - started MQTT timer - every 60 seconds
[2020-09-16 08:19:39] - - ip=[192.168.244.165], mac[b8:27:eb:d2:c0:51], interface=[eth0], uniq-id=[AS3935-b827ebd2c051]
[2020-09-16 08:19:39] Announcing Lightning Detection device to MQTT broker for auto-discovery ...
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m2), 'b'homeassistant/sensor/lightningdetector/last/config'', ... (539 bytes)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m3), 'b'homeassistant/sensor/lightningdetector/energy/config'', ... (301 bytes)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m4), 'b'homeassistant/sensor/lightningdetector/distance/config'', ... (336 bytes)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 2)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 3)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m5), 'b'homeassistant/sensor/lightningdetector/count/config'', ... (298 bytes)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m6), 'b'homeassistant/sensor/lightningdetector/settings/config'', ... (418 bytes)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m7), 'b'homeassistant/sensor/lightningdetector/crings/config'', ... (406 bytes)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 4)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 5)
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r1, m8), 'b'homeassistant/sensor/lightningdetector/prings/config'', ... (403 bytes)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 6)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 7)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 8)
[2020-09-16 08:19:39] - - Testing AS3935 Communications...
[2020-09-16 08:19:39] - - TEST write=5, read-back=5
[2020-09-16 08:19:39] - - TEST write=2, read-back=2
[2020-09-16 08:19:39] Publishing to MQTT topic "home/nodes/sensor/lightningdetector/settings, Data:{"settings": {"timestamp": "2020-09-16T08:19:39-05: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"}}}"
[2020-09-16 08:19:39] - log: Sending PUBLISH (d0, q1, r0, m9), 'b'home/nodes/sensor/lightningdetector/settings'', ... (240 bytes)
[2020-09-16 08:19:39] - log: Received PUBACK (Mid: 9)
[2020-09-16 08:19:39] << INTR(17) >> Noise level too high - adjusting
Tuning ...
 

Code:
ICS-Lightning:~# python3 /opt/ISP-lightning-mqtt-daemon/ISP-lightning-mqtt-daemon.py --calc_tuning_cap
[2020-09-16 08:26:12] * Mode: Calculate Tuning Cap value and exit
[2020-09-16 08:26:12] - * init mqtt_client_connected=[False]
[2020-09-16 08:26:12] * Sensor on I2C bus
[2020-09-16 08:26:12] - - ip=[192.168.244.165], mac[b8:27:eb:d2:c0:51], interface=[eth0], uniq-id=[AS3935-b827ebd2c051]
[2020-09-16 08:26:12] - - Testing AS3935 Communications...
[2020-09-16 08:26:12] - - TEST write=5, read-back=5
[2020-09-16 08:26:12] - - 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
For tuning 0x0: average frequency of 351817.047007 Hz (diff: +9261.4)
For tuning 0x1: average frequency of 364055.514169 Hz (diff: +8496.5)
For tuning 0x2: average frequency of 367525.060691 Hz (diff: +8279.7)
For tuning 0x3: average frequency of 350404.798561 Hz (diff: +9349.7)
For tuning 0x4: average frequency of 370185.383957 Hz (diff: +8113.4)
For tuning 0x5: average frequency of 358331.860467 Hz (diff: +8854.3)
For tuning 0x6: average frequency of 361992.328115 Hz (diff: +8625.5)
For tuning 0x7: average frequency of 349564.880004 Hz (diff: +9402.2)
For tuning 0x8: average frequency of 370973.106840 Hz (diff: +8064.2)
For tuning 0x9: average frequency of 344442.531351 Hz (diff: +9722.3)
For tuning 0xa: average frequency of 376547.161107 Hz (diff: +7715.8)
For tuning 0xb: average frequency of 405590.304774 Hz (diff: +5900.6)
For tuning 0xc: average frequency of 353628.876312 Hz (diff: +9148.2)
For tuning 0xd: average frequency of 423530.487778 Hz (diff: +4779.3)
For tuning 0xe: average frequency of 396381.250568 Hz (diff: +6476.2)
For tuning 0xf: average frequency of 398582.262969 Hz (diff: +6338.6)
- Your best tuning capacitor value is 0xd: which is off by +4779.3
root@ICS-Lightning:~#
Now adding capacitor value of 0xd in config.ini file

# 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 = 0xd

Configuring the daemons to auto start on boot:

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

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

reboot

Check MQTT status via MQTT explorer



2 - add to Home Assistant Lovelace cards



3 - Add to Homeseer mcsMQTT



4 - Text to speech via SAPI and via Alexa devices
5 - extend sensor using a pvc tube and round plastic ball for mounting RPi/ POe sensor in attic.
 
25th of September, 2020
 
Noticed that the MQTT Last Will and Testament (LWT) "Online" was "Offline" yesterday.  The Python script is tweaked down a bit.  It must have debended.  I rebooted and all is well.  
 
On an off side note tried originally to build this device on the old Pine64 2Gb computer running 64 Bit Ubuntu 20.X.  It did not work due to libraries not present for the Rock64 CPU.  Tried to install the Ring to MQTT application to same Pine64 64 bit computer with the same type of issues.  I was able to install it in Docker but only if I manually run it.  
 
Next similiar endeavor is to install the AS3935 Lightning sensor on a Tasmota firmware upgraded Sonoff basic device.  Tasmota now includes the AS3935 in their basic firmware.
 
I still like the old 1-Wire counter Lightning sensor more as it always worked just fine.  I have read some place that sometime soon the entire 1-Wire library will be incorporated in to the Tasmosta firmware (maybe under 1 Mb).
 
Back
Top