Protect your electronics from lightning strikes

WeatherPi Software

A big part of the WeatherPi project is the software. All of the Python software for this project is up on GitHub at the SwitchDoc Labs repository [25]. I have also included all of the various libraries for the I2C devices I am using.

You need to add the following software and libraries to your Raspberry Pi:

MySQL – If you need help installing MySQL, you can find tutorials online [26].

MatPlotLib – This graphing subsystem has a great interface to Python. It is a bit more complex to install, so I wrote a tutorial on how to install it on [27]. Note that the installation takes a long time, about eight hours on a Raspberry Pi A+ (mostly unattended).

The main WeatherPi Python software (Listing 4) is pretty simple. The application was much less complex than the Project Curacao software, so I decided just to use a simple loop with an "every 15 seconds" type of control. The beginning code sets up all the sensors on power-up.

Listing 4

WeatherPi Main Loop

01 SecondCount = 1
02 while True:
04   # process Interrupts from Lightning
06   if (as3935Interrupt == True):
08     try:
09       process_as3935_interrupt()
12     except:
13       print "exception - as3935 I2C did not work"
16   tca9545.write_control_register(TCA9545_CONFIG_BUS0)
17   # process commands from RasPiConnect
18   print "---------------------------------------- "
20   processCommand()
22   if ((secondCount % 10) == 0):
23     # print every 10 seconds
24     sampleAndDisplay()
25     patTheDog()      # reset the WatchDog Timer
26     blinkSunAirLED2X(2)
29   # every 5 minutes, push data to mysql and check for shutdown
31   if ((secondCount % (5*60)) == 0):
32     # print every 300 seconds
33     sampleWeather()
34     sampleSunAirPlus()
35     writeWeatherRecord()
36     writePowerRecord()
38     if (batteryVoltage < 3.5):
39       print "--->>>>Time to Shutdown<<<<---"
40       shutdownPi("low voltage shutdown")
43   # every 15 minutes, build new graphs
45   if ((secondCount % (15*60)) == 0):
46     # every 900 seconds
47     sampleWeather()
48     sampleSunAirPlus()
49     doAllGraphs.doAllGraphs()
52   # every 48 hours, reboot
53   if ((secondCount % (60*60*48)) == 0):
54     # reboot every 48() hours seconds
55     rebootPi("48 hour reboot")
57   secondCount = secondCount + 1
58   # reset secondCount to prevent overflow forever
60   if (secondCount == 1000001):
61     secondCount = 1
63   time.sleep(1.0)

Note that I reboot the Pi every two days, because I have noticed that after heavy usage of MatPlotLib or MySQL, I sometimes run out of resources, resulting in all sorts of odd behavior. Because the Raspberry Pi A+ has a small amount of RAM, rebooting is the easiest way to fix it.

Lightning Detector

I am using the Embedded Adventures MOD-1016 lightning sensor module with the AS3935 integrated circuit [1]. This great I2C breakout board has all the nasty little analog parts and circuitry that make the AS3935 work. The programmable IC detects the presence and approach of potentially hazardous lightning activity in the vicinity and provides an estimate of the distance to the head of the storm. The embedded lightning algorithm checks the incoming signal pattern to reject potential man-made disturbances and various noise sources. The AS3935 can also provide information on the noise level and inform the Raspberry Pi of high noise conditions.

When I found the AS3935 device, I was pretty excited about using it in a design. After quite a search and playing with a number of packages, I found a good software package for the IC by Phil Fenstermacher [28]. After a little tweaking and setting up interrupts, I got the software to work. The tweaked AS3935 software is available, along with the full WeatherPi software, on the SwitchDoc Labs GitHub site[25].

Buy this article as PDF

Express-Checkout as PDF

Pages: 8

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content