Announcement

Collapse
No announcement yet.

AS3935 Lightning Detector Sensor to MQTT - 2020 Version

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    AS3935 Lightning Detector Sensor to MQTT - 2020 Version

    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

    Click image for larger version  Name:	rp2_pinout.png Views:	8 Size:	403.2 KB ID:	1419224

    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

    Click image for larger version  Name:	GY_AS3935.jpg Views:	0 Size:	50.8 KB ID:	1419594

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

    Click image for larger version  Name:	lightningsensortorpi.jpg Views:	0 Size:	27.1 KB ID:	1419595

    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

    Click image for larger version  Name:	lightning.jpg Views:	0 Size:	32.8 KB ID:	1419633

    2 - add to Home Assistant Lovelace cards

    Click image for larger version  Name:	Lovelace.jpg Views:	0 Size:	80.7 KB ID:	1419728

    3 - Add to Homeseer mcsMQTT

    Click image for larger version  Name:	mcsMQTT.jpg Views:	0 Size:	108.1 KB ID:	1419757

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

    Auto mator
    Homeseer 3 Pro - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb- Mono 6.12.X - HSTouch on Intel tabletop tablets
    Homeseer Zee2 (Lite) - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro - Mono 6.12.X
    HS4 Pro - V4.1.10.0 - Ubuntu 18.04/VB W7e 64 bit Intel Kaby Lake CPU - 32Gb - Mono 6.12.x
    HS4 Lite -

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation-Tasmota-Espurna. OmniPro 2, Russound zoned audio, Smartthings hub, Hubitat Hub, and Home Assistant

    #2
    22nd of September, 2020

    Moved the RPi and Lightning sensor to the attic. It is sitting next to the ZWave RPi and the W800 device and the CheaperRFID device.

    Works better than my first attempt at this mostly due to the great Python scripused.

    Testing functionality

    1 - running application manually:

    sudo systemctl stop isp-lightning.service

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


    2 - tuning capacitor

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

    - Your best tuning capacitor value is 0xd: which is off by +3695.7

    Put this value in the configuration INI file. (IE: 13)

    # 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 = 13

    3 - running application manually one more time:

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

    4 - reboot device.



    - Pete

    Auto mator
    Homeseer 3 Pro - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU 16Gb- Mono 6.12.X - HSTouch on Intel tabletop tablets
    Homeseer Zee2 (Lite) - 3.0.0.548 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro - Mono 6.12.X
    HS4 Pro - V4.1.10.0 - Ubuntu 18.04/VB W7e 64 bit Intel Kaby Lake CPU - 32Gb - Mono 6.12.x
    HS4 Lite -

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation-Tasmota-Espurna. OmniPro 2, Russound zoned audio, Smartthings hub, Hubitat Hub, and Home Assistant

    Comment

    Working...
    X