This a guest post from Anmol Agrawal, an India-based UX/UI & Interaction Designer, and Full Stack Developer with Rails. Over the week, we’ll be publishing Anmol’s RubyBits, a blog series on hardware programming and the Internet of Things using Ruby.
Earthquakes strike at the blink of an eye. Warning and alert systems need to deliver vital information just as quickly, in realtime. In this blog post, I am going to give a full insight on how I approached in building this earthquake warning system project, the hurdles I faced, resources I found and decisions I made.
Every invention or discovery is sparked with an idea. I never planned to build an Earthquake Warning System (I didn’t even know about it nor did I know how to build one). One day I saw this video of Tsunami that came in Japan in 2011:
This 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. This is what we are going to end up building:
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.
Taking it a Step Further
I wanted to make this system for India. First step, getting the data on Seismic activity in India. Fortunately, I came across this site – India 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 something new, 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.
Two things to notice about this circuit:
- I am using 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 this.
1. Install Dino Gem
2. Write the Code
Package Delivery Detector that we created before. Only change is that we are continuously sending the data to PubNub, which we have used in this series before. Then we are going to use freeboard.io to create 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 and a LED, 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 as LED 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.
Initializing and setting 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 pressue sensor senses the change in it’s value).
When sensor receives any data, we are turning it into Float type as it is String type by default and dividing it by 100 to give as data just like a richter scale, e.g. 6.51, 3.78 etc. We are then putting it on terminal and after that, sending it to our PubNub instance using
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 instance’s
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 ALERT…” 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
3. Run the Code in Terminal
Go into the directory where the file is located inside the terminal. Run
When you run it, you can see the changes in value on the terminal with the logic of printing “EARTHQUAKE ALERT…”. If you check out the PubNub’s Developer Console, you can see the data coming in in realtime with almost no delay (don’t forget to mention the same channel, publish key and subscribe key). Plus LED (which we called buzzer in the code) also works accordingly.
4. Create Dashboard
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 Warning and Monitoring System, which could save lives.