Earthquake monitor with Omega2+



  • Final assembled project

    Since the Omega2+ offers a pretty standard GPIO interface, I decided to “go hardware” and make a small project. My idea was to make a sound every time an earthquake is reported in Italy; the all idea is pretty simple, but it can be useful to learn basic principles of IoT.

    Image via onion.io

    First of all, the hardware part. I’m using an active buzzer (the only one I have) that is connected to the Omega2+ and controlled by the GPIO interface through a transistor. The components I used are:

    • Omega2+ with expansion board
    • A breadboard
    • An active buzzer
    • One PNP transistor (8550)
    • One Resistor (1KΩ)
    • Several jumper wires

    Here is the schematic for the circuit:

    Via Sunfounder.com

    I connected the positive pole of the buzzer to the 5V (2nd pin of the Omega2+’s board) and the negative one to the emitter of the transistor. The collector to the ground and the base to the GPIO0 of the expansion header, through the resistor. Pretty straightforward so far. You can test from the Omega2+ web interface that, setting the GPIO0 to output, the buzzer… will buzz.

    Omega2+ web interface is amazing to test GPIO before writing a single line of code!

    Now, it’s time to write the code: it will be a python script that fetches the QuakeML feed from the INGV website every minute and, if a new earthquake is found, makes a buzz — setting the GPIO0 to output for a certain time and then back in input. It’s not elegant at all, but again it shows some basic principles of coding with this kind of hardware. The full script is available on GitHub so here I will cover just the basic instructions.
    Using the GPIO interface with python requires python of course and the onionGpio module, so:

    opkg update
    opkg install python-light pyOnionGpio
    

    You can also choose to install the complete python package instead, it depends on the space you have available on the Omega2+. First of all, I set up the GPIO interface, using the 0 channel, via the onionGpio module:

    import onionGpio
    #Set GPIO
    gpioNum = 0
    gpioObj = onionGpio.OnionGpio(gpioNum)
    

    Now I’m ready to change the channel direction everytime I have to make a sound. Next, let’s fetch the QuakeML data. This is a special XML format for earthquakes’ data. There are a couple of modules for python but since my goal is pretty simple, I will deal with it like a standard XML input. I’ll use urllib2 and xmltodict (which you need to install via pip) to parse the file and create a dictionary. I built an easy function that download the data, check the field publicID of the last record and update the id if it represents a new earthquake. In this case, I also switch the channel direction to output and then back to input, after a small amount of time (default 0.5 seconds, the buzzer is really annoying!). I use gpioObj.setOutputDirection(0) and gpioObj.setInputDirection() to command the GPIO interface. The whole function now is like this:

    def checkLast():
     global lastId
     fsock = urllib2.urlopen(requestURL) #requestURL is the QuakeML handler from the INGV file
     data = fsock.read()
     fsock.close()
     data = xmltodict.parse(data)
     last = 0
     newId = data[‘q:quakeml’][‘eventParameters’][‘event’][0][‘@publicID’]
     
    
    if(lastId != newId):
      print “* Last earthquale:”,data[‘q:quakeml’][‘eventParameters’][‘event’][0][‘description’][‘text’],”{“,newId,”}”
      lastId = newId
      #Buzz it!
      status = gpioObj.setOutputDirection(0)
      time.sleep(0.5)
      status = gpioObj.setInputDirection()
     else:
      print “* No new earthquake, last id:”, lastId
     print ‘ — -’
    

    Then, I’ll call the function every minute with an amazing infinite loop (I warned you it was not elegant at all):

    while True:
    checkLast()
    time.sleep(60)
    

    The version of the script that you can find on GitHub contains some small adjustments, like the possibility to specify a minimum magnitude and the lenght of the buzz. Since I want to lunch the script via SSH and then keep it going without keep the session open, I recommend you to use nohup to keep the script running:

    nohup python earthquakes-italy.py </dev/null >quakes.log 2>&1 &
    

    In this way, I have a python process detached from the shell that is up and running and buzzing every time a new earthquake is reported by INGV.

    Ps. INGV is the Italian Institute for Geology and Volcanology and provides data related to Italian soil. You can easily adapt the script to fetch data from USGS web API which cover basically the whole world — however, I used the Italian source for my project since its data are more reliable for the country.

    (See original post on Medium)


Log in to reply
 

Looks like your connection to Community was lost, please wait while we try to reconnect.