Connecting a lightning detector to your Pi that warns you by Email

There is a nice little electronic kit from Franzis available, where you can build a lightning detector that shows you roughly the distance of thunderstorms around you and that may approach your location. The kit itself is powered by two AA batteries, but can also be powered by the Raspberry Pi’s 3.3V supply. The lightning detector includes four LEDs, from which one is flashing every time a lightning is detected. The other LEDs three gives you a warning level (green, yellow and red) for detected thunderstorms in a distance of about 50km, 30km and 10km.

Gewitterwarner

Besides the LEDs, there are also the connections M1 and M2 which provides a binary output of the warning level (00, 01, 10 and 11). You can connect these outputs  directly to the GPIOs of the Raspberry Pi, since both of them use 3.3V as a HIGH level. By checking the output of the lightning detector periodically with a Raspberry Pi, you are able to show the current warning level on the website of your weather station. For that, I use the combination of two icons, a bar indicator and a smiley/exclamation mark.

Stufe0 Happy     No thunderstorm within detection range

Stufe1 Neutral     Thunderstorm about 50km away

Stufe2 Sad     Thunderstorm about 30km away

Stufe3 Attention     Thunderstorm about 10km away

This is what the warning level looks like in the status bar of the website of my weather station, together with the weather forecast, the current season, sunset and sunrise times and the current moon phase.

Hechingen

Of course, you are not looking at the website of your weather station all days long, so it would make sense to send an instant notification, if a certain warning level has been reached. I do this by sending an Email to my personal Email address which then will be received on my Smartphone immediately.

Since the script is running periodically, you have to remember, if there was an active warning, the last time the script was running, otherwise you would get an Email every time the script is running and conditions for a warning are met. Also, you would not be able to send an all-clear message, if  the warning level went down again to a safe level.

A simple file can be used as a token, to tell the script running next time that the script running before was setting the system into the warning state, so that Emails are only sent when the system enters the warning state (warning) and when it leaves the warning state (all-clear). The token file has to be created when the systems goes to the warning state and the warning Email is sent, and deleted when the system leaves the warning state and the all-clear Email is sent.

Below, you can find a Python script that reads the warning level from the lightning detector, copies the icons to the website directory accordingly to the warning level, and sends an Email (warning), if the highest warning level has been reached. An Email will also be send (all-clear), if the warning level drops below the second highest warning level. If you want to use the script, don’t forget to replace YOURSENDEREMAILADDRESS, YOURPASSWORD, YOURMAILSERVER and YOURRECEIVEREMAILADDRESS with your personal information.


#!/usr/bin/python

import RPi.GPIO as GPIO
import sys
import os
import shutil
import smtplib
import socket
import time
from email.mime.text import MIMEText

Sender = 'YOURSENDEREMAILADDRESS'
Password = 'YOURPASSWORD'
smtpserver = smtplib.SMTP('YOURMAILSERVER', 587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN) # GPIO_GEN2
GPIO.setup(22, GPIO.IN) # GPIO_GEN3

M1 = 0
M2 = 0

if GPIO.input(27) == GPIO.HIGH:
	M1 = 1
if GPIO.input(22) == GPIO.HIGH:
	M2 = 1

if (M1 == 0 and M2 == 0):
	shutil.copyfile('/home/pi/lightning/Level0.png', '/var/www/Lightning.png')
	shutil.copyfile('/home/pi/lightning/Happy.png', '/var/www/Smiley.png')
	if os.path.isfile("warningactive"):
		os.remove("warningactive")
		smtpserver.login(Sender, Password)
		Receiver = 'YOURRECEIVEREMAILADDRESS'
		Wert = "All-clear, thunderstorm is over!"
		msg = MIMEText(Wert)
		msg['Subject'] = 'All-clear! - %s' % time.strftime("%d.%m.%Y %H:%M:%S")
		msg['From'] = Sender
		msg['To'] = Receiver
		smtpserver.sendmail(Sender, [Receiver], msg.as_string())
		smtpserver.quit()
elif (M1 == 0 and M2 == 1):
	shutil.copyfile('/home/pi/lightning/Level1.png', '/var/www/Lightning.png')
	shutil.copyfile('/home/pi/lightning/Neutral.png', '/var/www/Smiley.png')
	if os.path.isfile("warningactive"):
		os.remove("warningactive")
		smtpserver.login(Sender, Password)
		Receiver = 'YOURRECEIVEREMAILADDRESS'
		Wert = "All-clear, thunderstorm is over!"
		msg = MIMEText(Wert)
		msg['Subject'] = 'All-clear! - %s' % time.strftime("%d.%m.%Y %H:%M:%S")
		msg['From'] = Sender
		msg['To'] = Receiver
		smtpserver.sendmail(Sender, [Receiver], msg.as_string())
		smtpserver.quit()
elif (M1 == 1 and M2 == 0):
	shutil.copyfile('/home/pi/lightning/Level2.png', '/var/www/Lightning.png')
	shutil.copyfile('/home/pi/lightning/Sad.png', '/var/www/Smiley.png')
	smtpserver.quit()
elif (M1 == 1 and M2 == 1):
	shutil.copyfile('/home/pi/lightning/Level3.png', '/var/www/Lightning.png')
	shutil.copyfile('/home/pi/lightning/Attention.png', '/var/www/Smiley.png')
	if (os.path.isfile("warningactive") == False):
		os.mknod("warningactive")
		smtpserver.login(Sender, Password)
		Receiver = 'YOURRECEIVEREMAILADDRESS'
		Wert = "Attention, thunderstorm is coming!"
		msg = MIMEText(Wert)
		msg['Subject'] = 'Warning! - %s' % time.strftime("%d.%m.%Y %H:%M:%S")
		msg['From'] = Sender
		msg['To'] = Receiver
		smtpserver.sendmail(Sender, [Receiver], msg.as_string())
		smtpserver.quit()