Header Image - Kilo Charlie 4 Romeo Charlie Romeo

Monthly Archives

One Article

Lightning Detector and Email Notifier

I decided to post a draft of this page since I keep referencing it on a few nets. Watch for updates as I continue to update and refine the build and this page. Updated 16 July 2023.

This how-to instruction set will build a local lightning detection notification system which will send an email via Python script running on a Raspberry Pi. This of course, could be adapted to run on other hardware platforms easily.

Here’s my list of items:

* In 2022 Google stopped allowing “less secure apps” to access email. When they removed allowing the user to decide for themself to enable or disable, it rendered the email for many Raspberry Pi users to non-functioning (this would apply in other builds as well). It appears a work around was found, though I have not tried it. You can read about it here: G-Mail and less secure sign-in technology.

First, set up your Raspberry Pi if you have not already. There are many instruction sets on the Internet including this one: https://www.raspberrypi.com/tutorials/how-to-set-up-raspberry-pi/. Also, any Debian-based Operating System should work fine if you can’t source a Raspberry Pi. In my case I am using the Raspberry Pi 4 Model B Rev 1.2 running Raspbian GNU/Linux 10 (buster). I do plan to move this over to an Ubuntu machine one day to free this Pi up for other experiments.

Check and see if you have Python2 installed by running Python -V at the command prompt. You can use newer versions if you wish, but my code snippets in this example were written for the version noted below.

$ python -V
Python 2.7.16

If you do not have Python, it can be installed with:

$ sudo apt update
$ sudo apt-get install python2

Now install the rtl_433 code (see official source code instructions here: https://github.com/merbanan/rtl_433/).

 $ sudo apt-get install rtl_433

rtl_433 is a very powerful tool! It can interpret numerous devices so be sure to check out the link above. Did you realize most cars can be tracked via the tire sensors which is now government mandated in many areas? Well now you do and this tool will also read cars as they drive by on the street! It’s all open and unencrypted. I make this point not to encourage snooping but to make folks aware of a pet peeve of mine which are new cars spewing data without knowledge or consent of most owners. Anyway, back to the lightning sensor discussion…

You will also need the msmtp package if you wish to send email notifications. Another package called mailutils is also helpful along with the mail transport agent. Those can be installed by running:

$ sudo apt-get install msmtp
$ sudo apt-get install mailutils
$ sudo apt-get install msmtp-mta

Which can also be combined as:
$ sudo apt-get install msmtp mailutils msmtp-mta

Here is the final setup of the Raspberry Pi with power (white cable), network (orange cable) and RTL-SDR Dongle in one of the USB ports. I also have a small band pass filter on the antenna connection (black cable at bottom with SMA connector). This additional filter isn’t generally needed but since I have a lot of radio activity going on in the area, I figured it couldn’t hurt. It doesn’t have great reviews but I tested it and it was a bit off frequency but not enough to impact my setup. Here is the link: https://www.amazon.com/Interface-Bandpass-Receiver-Dedicated-422-446MHz/dp/B0B1QSBZKR

Acurite 6045M Sensor placed outdoors. This can be found at: https://www.amazon.com/gp/product/B01LNALL6C/

The good thing about using this particular SDR Dongle is that it should be recognized by the Pi’s Operating System without installing anything additional. You can use the command ‘lsusb’ to be sure. Here you see it on the second output line below (Bus 001 Device 003).

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 0bda:2838 Realtek Semiconductor Corp. RTL2838 DVB-T
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

If you run rtl_433 with no options, you will start getting a lot of data displayed continuously until you press Ctrl-C in the terminal. Below you will see two different Acurite-6045M devices running (id 85 and id 127). There was no modification to the Acurite devices. This is right out of the box with batteries installed.

Screen capture of rtl_433 running

A couple things of note. Acurite appears to send the data three times. I presume this is their standard to ensure the transmission is received properly. Second, the lightning strike count is a rolling counter which wraps from 255 back to 0. That becomes important in the Python code because you will need to look for a change in the counter, not an absolute number. I also restrict the data collected to only the Acurite protocol needed (thus not filling up my log with tire sensors and many others). The -R option allows that selection. The protocol needed for the Acurite 6045M is “40”, i.e. running as this command only reads the Acurite 6045M and similar sensors and outputs formats (via -F option) json and http (more on that below). The protocol list can be found at https://github.com/merbanan/rtl_433.

/usr/local/bin/rtl_433 -R 40 -F json -F http

Another fun aspect of the rtl_433 package is the ability to format the output as json, csv, http, syslog and a few others. It has an integrated http server which defaults to port 8433 when invoked with rtl_433 -F http. Point your web browser to or whatever your Raspberry Pi’s IP is (if not local).

Screen capture of http data on port 8433

Now if all you want to do is view the data, you can stop here. The next steps are if you want to set up the Raspberry Pi to notify you via email or text that the lightning count has changed. I was hoping that the “active” tag in the data would be enough, but testing shows it is not. In my tests, the Acurite sensor goes into active mode regularly for unknown reasons. I suspect some is RFI, but I have not done enough testing. Ideally if the RFI field were true, the sensor should not go into active mode, but it’s hard to say what the thinking was in the design of the sensor. Even the author of rtl_433 states there are some transmitted bits in the message that have not been found to correlate to useful data. Check out the acurite.c code for an in-depth analysis.

For email sending, you use msmtp. I have never enjoyed working with msmtp. I find it rather clunky and usually have to fiddle around with it a bit. You must create a config file name msmtprc. For lots more information, try searching for msmtprc configuration. Here’s one example: https://centurio.net/2020/09/21/configure-mail-transport-agent-on-raspbian-with-external-smtp-server/. My example config file is below, redacted of real account login information of course. Make sure you have the /etc/ssl/certs/ca-certificates.crt as well. If you don’t have it properly installed, you might have to research how to get it on your particular system. My hosted email account has port 465 available for sending mail. You will have to modify the settings below based on your email provider.

$ cat /etc/msmtprc
auth on
tls on
tls_starttls off
tls_trust_file /etc/ssl/certs/ca-certificates.crt
logfile /var/log/msmtp.log
aliases /etc/aliases
auto_from off
# Account details
account yourusername
host mail.yourserver.com
port 465
from yourusername@yourserver.com
user yourusername@yourserver.com
password S0m3P@$$w0RD
# Default Account
account default : yourusername

You can try a test email using the following (this is one line, be careful with copy paste):

$ echo "Notification System Test" | mail --debug-level=6 -s "Test" -a "From:Notification System sendinguser@domain.com" recievinguser@domain.com

I find the “-a From” header helps get though some spam filters, especially to a cellular phone text gateway. You should see the following output after sending the mail command and get an email for the specified recipient.

mail: sendmail binary: /usr/sbin/sendmail
mail: mu_mailer_send_message(): using From: sendinguser@domain.com
mail: Sending headers…
mail: Sending body…
mail: /usr/sbin/sendmail exited with: 0

Now we move on to the Python code. Let me be clear here! This code will work but it is a quick approach. You can tailor it anyway you want and certainly clean it up to be more efficient. I am still working on the code to add other features and multiple sensors, etc. What that means is this is an earlier version that ran for many months. It does work, just not as clean and neat as it could be. Remember, be careful with copy and paste.

import json
import sys
import datetime
import subprocess

k = 0
start = 0
strike_count_old = 0

outputfile = open(r"/home/pi/lightning.txt", "a")

   for line in iter(sys.stdin.readline, b''):
      k = k + 1
      if k > 9:
          signal_string = json.loads(line)
          if signal_string["model"] == "Acurite-6045M":
               #print signal_string
               strike_count_new = signal_string["strike_count"]
               if strike_count_new != strike_count_old:
                    distance = signal_string["storm_dist"]
                    text = "{} - LIGHTNING STRIKE {} mi away. Historical count is now {}.".format(datetime.datetime.now().strftime("%m/%d/%Y %H:%M:%S"), distance, strike_count_new)
                    if start > 0:
			subprocess.call(['echo "{}" | mail -s "LIGHTNING STRIKE" -a "From:Notification System <sendinguser@domain.com>" receipient@domain.com'.format(text)], shell=True)
                        start = 1
                    strike_count_old = strike_count_new
                    #text = "No strike at time but data is {}.\n".format(signal_string)

except KeyboardInterrupt:

I’ve left some of the testing lines in there as well which are commented out plus I skip over the first number of lines read to make sure the input gets settled. (I will explain more on the code at a later time). This code reads the json format the rtl_433 will output from “-F json”. I saved this in a file called lightning.py in /home/pi. You will pipe the output from rtl_433 via the -F json command. In my example, I am also running the http server so I can view live data in a web browser. This code will send an email for every strike with the calculated distance (presumed in miles) along with the current rolling strike counter. With this snippet, it only looks for a device named “Acurite-6045M”. It would need more specificity if used with multiple sensors (what I am experimenting with now).

$ rtl_433 -R 40 -F json -F http | /usr/bin/python /home/pi/lightning.py

To run this at startup, you can add an entry in /etc/rc.local (on Raspian). If you are using Ubuntu 20.04+, rc.local is not enabled by default. If you wish to use this approach see https://marsown.com/wordpress/how-to-enable-etc-rc-local-with-systemd-on-ubuntu-20-04/. To use rc.local to start the script on boot, add the rtl_433 line below:

#!/bin/sh -e
# rc.local
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
# In order to enable or disable this script just change the execution
# bits.
# By default this script does nothing.

/usr/local/bin/rtl_433 -R 40 -F json -F http | /usr/bin/python /home/pi/lightning.py

exit 0

One final comment about the code. This will send ALL detected lightning strikes to email. Depending on your area, this could be a lot! I will eventually modify the code to compile the data, but again, this is proof of concept at this point. Besides adding diversity of sensors, my other planned addition is a commanded visual cue when lightning in the area.

To be continued…

Index: 5
PHP Code Snippets Powered By : XYZScripts.com