Tutorial:   Software! Building an IOT Lightning Detector with your Raspberry Pi – Part 3

Tutorial: Software! Building an IOT Lightning Detector with your Raspberry Pi – Part 3

At SwitchDoc Labs we have been building a number of prototype IOT (Internet Of Things) devices for a number of different videos, articles, products and books we have been working on.   Our latest Book,  “Raspberry Pi IoT Projects: Prototyping Experiments for Makers“, has been selling well on Amazon, published by APress-Springer/Daniel.  Our first O’Reilly Publishing tutorial video, “Introduction to the ESP8266 and the IOT” also just came out.  Time for a new project, the Raspberry Pi IOT Lightning Detector.

Part 2 shows you how to put together the hardware for the Raspberry Pi IOT Lightning Detector.  No soldering required!




The general idea of the Thunder Board IOT is to build a software platform to build more complex IOT sensors.   In this column and the next, we will be going through a complete,  IOT design.   As well as providing a test bed for the new Grove Thunder Board Lightning Detector.

The Software

The software required follows a familiar design pattern if you have looked at other SwitchDoc Labs projects.

The outline of the program is as follows.

A variety of Initialization occurs in the main line of the program.

First publish made PubNub

Next the scheduler is initialized for for four tasks. Note that this project doesn’t have an LED added but it could!

        # prints out the date and time to console
        scheduler.add_job(tick, 'interval', seconds=60)
        # blink life light
        scheduler.add_job(blinkLED, 'interval', seconds=5, args=[1,0.250])

        # add the Update to PubNub
        scheduler.add_job(publishLightningToPubNub, 'interval', seconds=120)

        # check configuration
        scheduler.add_job(readLightningStatus, 'interval', seconds=3600)

The job “tick” just prints out a time stamp every minute to the console.

The job “publishLightingToPubNub” sends a status of the machine to PubNub every two minutes, regardless of changes

The job “readLightningStatus” reads the current status from the Lightning Chip (Thunder Board) and updates internal status variables and prints the latest to the console.

“publishLightningToPubNub” is also called on any interrupt event from the Lightning Chip (AS3935) to immediately update your Internet Dashboard.


The Software Installation


Setting up your Raspberry Pi

(Step 1)  – Make sure you installed I2C as in this link:


(Step 2) – Install apscheduler


sudo apt-get install python-pip
sudo pip install setuptools --upgrade
sudo pip install apscheduler


(Step 3) – Install pubNub

sudo pip install pubnub

Getting the Thunder Board IOT software

(Step 4) – git clone the ThunderBoard IOT GitHub repository

git clone https://github.com/switchdoclabs/SDL_Pi_ThunderBoard_IOT


Testing your Setup

(Step 6) – Testing your I2C setup

run the following command:

sudo i2cdetect -y 1

You should see:

pi@RPi3-65:~/SDL_Pi_ThunderBoard_IOT $ 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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- 3e -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- 62 -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: 70 -- -- -- -- -- -- -- 

Depending on your Raspberry Pi, you may not see 0x03 or 0x70.  If you don’t see  0x3e and 0x62, then your LCD is not connected correctly.  Note, you will not see the ThunderBoard in this test.

(Step 6) – To test your LCD:

sudo python testLCD.py

You should see:

pi@RPi3-65:~/SDL_Pi_ThunderBoard_IOT $ sudo python testLCD.py
Testing LCD ------
Testing LCD Complete ------

You should see the countdown text on the LCD as well as color changes.

(Step 7) – to Test your ThunderBoard

sudo python testThunderBoard.py

You should definitely see the first line. It should say 0x02 or 0x03 depending on the version of the Thunder Board that you have. You may see more lines if your environment is especially electrically noisy. Hold it up against a monitor may trigger the noise system. If you have the Thunder Board Lightning Simulator, try it now
And you should see:

pi@RPi3-65:~/SDL_Pi_ThunderBoard_IOT $ sudo python testThunderBoard.py
Thunder Board present at address 0x02
Waiting for lightning - or at least something that looks like it

You should be able to get a noise trigger by holding it close to your phone or computer monitor, but not guarantees unfortunately.  Depends on too many factors.   The only way to fully test the devices is to wait for a thunderstorm or use the Thunder Board Lightning Simulator.

Configuring the Thunder Board IOT Software

Until you add the Internet dashboard (see Part 4), there is no configuration needed for the software.  Of course, feel free to modify the software to meet your needs!

Running the Thunder Board IOT Software

pi@RPi3-65:~/SDL_Pi_ThunderBoard_IOT $ sudo python ThunderBoardIOT.py

You should see the screens sequence thorough on the LCD and see something like the video below:


Thunder Board present at address 0x02
Waiting for lightning - or at least something that looks like it
ThunderBoard IOT

SwitchDoc Labs
Version:  Pi003

noise_floor= 0
min_strikes= 1
indoor= False
mask_disturber= False
disp_lco= False
Interrupt Count= 0
Publishing Data to PubNub time: 2017-12-15 10:56:37.356400
		LastResult:             None
status.is_error False
status.original_response [1, u'Sent', u'15133641978352182']
Scheduled Jobs
Jobstore default:
    blinkLED (trigger: interval[0:00:05], next run at: 2017-12-15 10:56:43 PST)
    tick (trigger: interval[0:01:00], next run at: 2017-12-15 10:57:38 PST)
    publishLightningToPubNub (trigger: interval[0:02:00], next run at: 2017-12-15 10:58:38 PST)
    readLightningStatus (trigger: interval[1:00:00], next run at: 2017-12-15 11:56:38 PST)
Press Ctrl+C to exit
Interrupt reason= 8
Lightning! 5km away.10:56:55 - 2017/12/15 PST
Interrupt reason= 4
Publishing Data to PubNub time: 2017-12-15 10:56:56.766724
		LastResult:             Disturber detected - masking
status.is_error False



Below is a video of the startup of the software on the Raspberry Pi IOT project:


Starting your Software on Bootup of the Raspberry Pi

If you want your IOT software to restart after boot up of your Raspberry Pi (a generally good idea), then do the following:

sudo nano /etc/rc.local

And add the following lines to the end of the file before the “exit 0” statement.


cd /home/pi/SDL_Pi_ThunderBoard_IOT/

sudo python ThunderBoardIOT.py > /null

You can test this by running:

sudo reboot


Final Assembly – Adding the Buzzer

(Step 1) – Shutdown your Raspberr Pi (sudo halt)

(Step 2) – Unplug the power to your Raspberry Pi

(Step 3)  – Take the Grove Cable (Part I) and connect it to the Buzzer (Part E) if you haven’t already.

(Step 4)  – Plug the other end of the Grove Cable (Part I) to the Pi2Grove board (Part B) in salt D13/16

(Step 5) – Power up your Raspberry Pi (Note the buzzer will beep until the program is running)


You are now done!    Next we take the data up to the Internet!



Coming in Part 4

Part 4 will go through the addition of a Internet IOT Dashboard to the Thunder Board IOT kit using PubNub, MQTT and Freeboard.


%d bloggers like this: