Maker Pro
Arduino

How to Make an Earthquake Early Warning Alarm

August 20, 2015 by Anmol Agarwal
Share
banner

Make an earthquake early warning system with an Arduino, littleBits, Ruby, and PubNub.

Earthquakes strike at the blink of an eye. Warning and alert systems need to deliver vital information just as quickly, in real-time. In this blog post, I am going to give a full insight on how I approached in building this earthquake alarm project, the hurdles I faced, resources I found and decisions I made.

Every invention or discovery is sparked with an idea. I never planned or knew how to build an Earthquake Alarm System until one day I saw a video of the tsunami that struck Japan in 2011.

The video drove me to do something about it. I thought this is the best time to put all the knowledge I have gained to real cause. My initial approach was to get some data through online resource like meteorological survey websites, as they are the ones with access to all the sensors, devices, machines, seismographs, etc to keep track of Earthquake activities. I planned on getting that data continuously and running the logic continuously like if earthquake is more than 5.3 Richter scale, send the warning.

I wanted to make this earthquake alarm system for India. First step, getting the data on Seismic activity in India. Fortunately, I came across this site – Indian Meteorological Department. There is a section in the website which lists the latest earthquake report mentioning the magnitude of the earthquake, latitude and longitude of the epicenter and the nearest region.

The problem with that was there is no API for that data. So only way I could get that was to crawl that page, every 10-20 seconds. Although that is a very inefficient method but I still wrote the script. I got it working but then I thought, is the data updated on the page in real-time or after couple of hours? If the earthquake comes somewhere at 6pm and it’s getting updated on the site at 8pm, then that data is of no use. I thought I gotta come up with something new and there has to be another way out.

I researched about it and I came across an Earthquake Early Warning System, which is installed in California. I was in US for sometime, I left US on 21 August 2014 and an earthquake came in California area on 23 August 2014. The system gave the warning 5-10 seconds in advance. Those seconds were “life-saving”.

As soon as I saw the system, I was like ‘This is amazing. So simple yet so effective.’ I decided to replicate it with littleBits, Ruby, and PubNub.

How Does the Earthquake Warning System Work?

I am using a pressure sensor to replicate or create the Earthquake effect. Whenever I press it, its value changes depending on how hard I press and that’s what we will be working with. I am using an LED as the warning signal. In real world application, we will have some kind of public announcement speakers. Continuing to work with Dino gem, let’s build the earthquake alarm. 

Installing Dino Gem

If you have already installed the Dino gem, you don’t need to reinstall it. If not, go to first part of RubyBits series to see how to install Dino gem

Writing the Code for the Earthquake Alarm

The code for earthquake alarm is very much similar to two other projects that I have done earlier, Smart Lighting and Package Delivery Detector. Only change is that we are continuously sending the data to PubNub. Then we are going to use freeboard.io to create a dashboard to continuously monitor our data in real-time. So this is a complete solution.

Let’s start with the code:

List all the gems that are required.

Setup the hardware with their specified pins. For this program, we just need an Arduino board, a Pressure Sensor an LED and nothing else.

Here, LED is connected to pin number 1 and sensor to pin ‘A0′(change to whatever you wish). I have named LED variable as buzzer because LEDs and buzzers work in the same way, they turn on and off the light/sound. You can connect the buzzer to it. Initially set the buzzer off.

Initialize and set up the PubNub variable, just like before. 

To get your unique pub/sub keys, you’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Dashboard. Our free Sandbox tier should give you all the bandwidth you need to build and test your messaging app with the web messaging API. 

Now we specify what message is to be sent to PubNub on a particular user’s event. (Here, when pressure sensor senses the change in it’s value).

When sensor receives any data, we turn it into Float type as it is String type by default and divide it by 100 to give us data just like a Richter scale, e.g. 6.51, 3.78 etc. We then put it on terminal and after that, send it to our PubNub instance using .publish method. 

The channel we are publishing to is earthquake frequency and the message payload is a key-value pair of key Magnitude and value as the data from sensor in Richter scale form. 

On the other end, we subscribe to the earthquake_frequency channel using our PubNub instances .subscribe method.

Here, we are running the logic on data received. If the value of Magnitude key in message payload is more than 5.3, put “EARTHQUAKE ALARM…” on terminal and turn on the buzzers (here we have LEDs). Otherwise nothing should happen and let everyone live in peace ?. We set the comparison value to be 5.3 because it is considered a moderate earthquake. 

Save the file as earthquake_alarm_freeboard.rb

 # List all gems that are required.
require 'rubygems'
require 'bundler/setup'
require 'dino'
require 'pubnub'


# Setup the hardware with their pins specified.
board = Dino::Board.new(Dino::TxRx::Serial.new)
sensor = Dino::Components::Sensor.new(pin: 'A0', board: board)
buzzer = Dino::Components::Led.new(pin: 1, board: board)


# Initially set the buzzer off.
buzzer.send(:off)


# Initializing and setting up the pubnub variable.
# ------------
@pubnub = Pubnub.new(
:subscribe_key    => 'sub-c-49d69172-3e94-11e4-8c81-02ee2ddab7fe',
:publish_key      => 'pub-c-67b64f52-9ac9-42df-9803-55fc0a646b22',
:error_callback   => lambda { |msg|
puts "Error callback says: #{msg.inspect}"
},
:connect_callback => lambda { |msg|
puts "CONNECTED: #{msg.inspect}"
}
)


# Subscribing the channel to/from which message are to be passed/collected.
@pubnub.subscribe(
:channel  => 'earthquake_frequency'
){|data|
# Logic of what to do on client side when message is analysed.
if data.message['Magnitude'] > 7.50
puts "EARTHQUAKE ALARM..."
buzzer.send(:on)
else
buzzer.send(:off)
end
}
# ------------


# Specifies what message is to be sent to pubnub on a particular user's event. (Here, when pressue sensor senses the change in it's value).
# ------------
sensor.when_data_received do |data|
mag = (data.to_f)/100
puts mag
@pubnub.publish(:message => {'Magnitude' => mag}, :channel => 'earthquake_frequency', :http_sync => true)
end


sleep

Running the Code in Terminal

Go into the directory where the file is located inside the terminal. Run ruby earthquake_alarm_freeboard.rb When you run it, you can see the changes in value on the terminal with the logic of printing “EARTHQUAKE ALARM…”. If you check out the PubNub’s Developer Console, you can see the data arriving in real-time with almost no delay (don’t forget to mention the same channel, publish key and subscribe key). Plus the LED (which we called buzzer in the code) also works accordingly.

Creating the Dashboard for the Earthquake Alarm System

Check out the above video to see how PubNub and freeboard.io works together. I created a pane of Text type including sparkline. I will leave this part for you figure it out. Don’t worry, it’s really easy, just follow the video.

And we just created a complete solution, from start to end, an Earthquake Alarm and Monitoring System, which could save lives.

Don't forget the source code from Github. That’s it! Congratulations. You have just programmed the Arduino with the modular power of Ruby and made an Earthquake Alarm system.

Related Content

Comments


You May Also Like