---
title: "Early Seismic Warning."
url: "https://maker.wiznet.io/teju/projects/early-seismic-warning/"
markdown_url: "https://maker.wiznet.io/teju/projects/early-seismic-warning/md"
type: "UCC: User Created Content"
author: "SismoTico"
author_url: "https://www.instructables.com/Alerta-S%C3%ADsmica-Temprana/"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "SismoTico"
original_url: "https://www.instructables.com/Alerta-S%C3%ADsmica-Temprana/"
published: "2022-12-05"
language: "en"
hardware: ["WIZnet W5100 ethernet shield"]
likes: 3
views: 612
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Early Seismic Warning.

> During an earthquake, 3 types of waves are produced: the primary or P, which is important for this project, travels between 8 and 13 km/s an

Original author: SismoTico (source: https://www.instructables.com/Alerta-S%C3%ADsmica-Temprana/)

## Components

- **WIZnet W5100 ethernet shield** x 1 ([docs](https://www.wiznet.hk/en/open-source-hardware/102-w5100s-ethernetshield.html?search_query=W5100+ethernet+shield&results=84))

## Article

**Brief explanation**

During an earthquake, 3 types of waves are produced: the primary or P, which is important for this project, travels between 8 and 13 km/s and is recorded by seismograph equipment.

There are homemade sensors that give us an alert by detecting these P waves, giving us a few seconds before the earthquake happens.

This project seeks to create an early seismic warning system on twitter.

**The materials to use**

- 1 Arudino UNO (Cost approx. $25usd)

- 1 Arduino Shield Ehternet (Costo Aprox $24 usd)

- 1 [Quake Alarm](http://www.amazon.com/JDS-Earthquake-Alarm/dp/B001D8NMY4/ref=sr_1_1?ie=UTF8&qid=1459485731&sr=8-1&keywords=quake+alarm) (Costo Aprox. $30 usd)

- 1 Printed Circuit Board for prototypes (Approx. Cost $0.60 usd) or a breadboard

- 3 Jumper Cables of different colors (Approx. Cost $0.30 usd)

- 1 9V battery connector (Approx. Cost $0.30 usd)

- Soldering iron and welding

## **Step 1: ​How To Place The Cables.**

[![How to place the cables.](https://content.instructables.com/FPF/MYR7/IMGJD26Q/FPFMYR7IMGJD26Q.png?auto=webp&frame=1&width=291&height=1024&fit=bounds&md=fd007c35364c7c6b89279ff612b82206)](https://content.instructables.com/FPF/MYR7/IMGJD26Q/FPFMYR7IMGJD26Q.png?auto=webp&frame=1&height=1024&fit=bounds&md=fd007c35364c7c6b89279ff612b82206)

[![How to place the cables.](https://content.instructables.com/FDV/PEGZ/IMGJD780/FDVPEGZIMGJD780.jpg?auto=webp&frame=1&width=303&height=1024&fit=bounds&md=2657c7bfecd9fdeeccf6d63111bfb7bc)](https://content.instructables.com/FDV/PEGZ/IMGJD780/FDVPEGZIMGJD780.jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds&md=2657c7bfecd9fdeeccf6d63111bfb7bc)

[![How to place the cables.](https://content.instructables.com/FG7/GA69/IMGJD77K/FG7GA69IMGJD77K.jpg?auto=webp&frame=1&width=303&height=1024&fit=bounds&md=63e5907eb2fe88a8db1b7e119ce2517a)](https://content.instructables.com/FG7/GA69/IMGJD77K/FG7GA69IMGJD77K.jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds&md=63e5907eb2fe88a8db1b7e119ce2517a)

[![How to place the cables.](https://content.instructables.com/FVT/0I7B/IMGJD77M/FVT0I7BIMGJD77M.jpg?auto=webp&frame=1&width=303&height=1024&fit=bounds&md=47cd415687f7a50e153ab9ef8020f877)](https://content.instructables.com/FVT/0I7B/IMGJD77M/FVT0I7BIMGJD77M.jpg?auto=webp&frame=1&width=1024&height=1024&fit=bounds&md=47cd415687f7a50e153ab9ef8020f877)

In order to do our project without having to open the "Quake Alarm" sensor, we are going to use its 9v battery connector.

The Arduino will give 5v to the sensor with which it will be able to work fine.

The "Quake Alarm" has a speaker to emit an alert if it detects P waves, if this speaker sounds the voltage drops, so we will use Pin A0 to monitor a change in voltage.

The cables must be placed in the following order:

- **5v - A0 - Black** wire from the battery connector.

First the 5V cable that will power the sensor, then the A0 that will monitor the voltage, and finally the black cable from the connector.

In another row we connect the Black cable that comes from the Arduino and the red cable that comes from the connector.

once we have them in this order we weld so that the electricity passes between them.

It is important that when connecting the 9V connector to the sensor, the black cable becomes positive and the red cable becomes negative, be very careful with this.

## **Step 2: Arduino Code**

In the Arduino we are going to create a code that will allow us 2 things:

1. Read voltage to look for changes

2. If there is a variation in the voltage make a call to a web service

We are going to explain the code a bit, if you do not want to understand it in depth, after the explanation is the complete code and where to download it.

To start we are going to include the necessary libraries to be able to use the ethernet shield and the serial.

```
#include <SPI.h>
```

```
#include <Ethernet.h>
```

Then we are going to create a constant that will have the Analog PIN that we are going to use

```
#define QUAKEALARM A0
```

Next we define the variables that will help us to process the voltage variation and the time to call the web service.

```
static int difference;
```

```
int qaVal = 0;<br>int qaPreVal = 0;
```

```
int threshold       = 10;
```

```
int timeInterval = 5000; // Time interval between two calls to the web service
long
```

```
lastConnection = 0; // Time in milliseconds since the last connection to the web service
```

Now create the variables for the use of the ethernet, where we define the MAC of the network card, the IP that this card will use and the EthernetClient type class to be able to use the network card. We are also going to define the IP of the server that will receive the data.

```
byte mac[] = { <br>  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
```

```
byte ip[] = { 192,168,15,120 }; // IP address of the Arduino
byte
```

```
server[] = { 192,168,15,111 }; // IP address of the server
```

```
EthernetClient client;
```

With the variables, libraries and classes ready, we can start the program that our Arduino will have:

- We initialize the serial to 57600

- Then we tell the arduino that we are going to use pin A0 (QUAKEALARM constant) as an input pin.

- Finally we start the network card.

```
void setup () {   <br>  Serial.begin(57600);
  pinMode(QUAKEALARM, INPUT);
```

```
  Ethernet.begin(mac, ip); // Inicializamos el Ethernet Shield
```

```
}
```

And to finish the program, we put in a cyclic function that checks the voltage if it is less than 5v that makes a call to the server and by means of GET it sends the value 1 to the "earthquake" variable. It also filters so that there is a 5-second time lapse between one sending of information and another.

```
void loop () {
```

```
qaVal = analogRead(QUAKEALARM);<br> diferencia = qaPreVal - qaVal;
```

```
   if ((diferencia > 0) and (diferencia >= threshold) and ((millis() - ultimaConexion) > intervaloTiempo) ) {
```

```
     counter = counter + 1;
```

```
     Serial.print("Sismo! ");
```

```
     Serial.println(contador);
```

```
     ultimaConexion = millis();
```

```
     if (client.connect(server, 5002)>0) {
```

```
       Serial.println("Conecto");
```

```
       client.print("GET /?earthquake=1"); // We send the data by GET
```

```
       client.println(" HTTP/1.0");
```

```
       client.println("User-Agent: Arduino 1.0");
```

```
}
```

```
     client.stop();
```

```
     client.flush();
```

```
   
   
  }
```

```
  qaPreVal = qaVal;
```

```
}
```

You can download the code at [github.com/bettocr/alertasismo](https://github.com/bettocr/alertasismo)

### **Full code:**

```
#include <SPI.h>
```

```
#include <Ethernet.h>
```

```
// QuakeAlarm
#define QUAKEALARM A0
```

```
// Variable that holds the previous value of the QuakeAlarm
static int difference;
```

```
int qVal = 0;
int qaPreVal = 0;
int threshold       = 10;
int timeInterval = 5000; // Time interval between two calls to the web service
lastConnection long = 0; // Time in milliseconds since the last connection to the web service
```

```
// Variables Ethernet
```

```
//The values ​​corresponding to MAC, local IP, Gateway and Network Mask are entered
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,8,20 }; // IP address of the Arduino
byte server[] = { 192,168,8,11 }; // IP address of the server
EthernetClient client;
 
void setup () {   
  Serial.begin(57600);
  pinMode(QUAKEALARM, INPUT); 
  
  // ethernet 
  Ethernet.begin(mac, ip); // Inicializamos el Ethernet Shield 
  
  
}
```

```
void loop () {
```

```
  qaVal = analogRead(QUAKEALARM);
  diferencia = qaPreVal - qaVal;
```

```
  if ((diferencia > 0) and (diferencia >= threshold) and ((millis() - ultimaConexion) > intervaloTiempo) ) {
     counter = counter + 1;
     Serial.print("Sismo! ");
     Serial.println(contador);
     ultimaConexion = millis(); 
     if (client.connect(server, 5002)>0) {
       Serial.println("Conecto");
       client.print("GET /?key=1122334455&sismo=1"); // Enviamos los datos por GET
       client.println(" HTTP/1.0");
       client.println("User-Agent: Arduino 1.0");
     }
     client.stop();
     client.flush();
   
   
  } 
  qaPreVal = qaVal;
}
```

## **Step 3: Python code**

[![Python code](https://content.instructables.com/FUS/8PG8/IMGJDEE9/FUS8PG8IMGJDEE9.png?auto=webp&frame=1&fit=bounds&md=72d62cb5dd9be16b1eee84a6fa5108b4)](https://content.instructables.com/FUS/8PG8/IMGJDEE9/FUS8PG8IMGJDEE9.png?auto=webp&frame=1&fit=bounds&md=72d62cb5dd9be16b1eee84a6fa5108b4)

This code basically creates a web service on port 5002 of the server (I use it on a Raspberry Pi at home), which receives the message from the Arduino and then sends a Tweet.

I did it this way to be able to implement statistics and in the future to be able to use it in a PUSH notification service for mobiles.

In order for you to tweet, you must create an application on [apps.twitter.com](https://apps.twitter.com/) and obtain the Consumer Key (API Key), Consumer Secret (API Secret), Access Token and Access Token Secret data. and in the tweet function, change the xxxxx to their respective values.

You can download the complete code at [github.com/bettocr/alertasismo](https://github.com/bettocr/alertasismo) or copy it below:

```
#!/usr/bin/python2.7
```

```
# -*- coding: utf-8 -*-
```

```
import requests, urllib2, urllib, tweepy
```

```
from flask import Flask, request
```

```
from StringIO import StringIO
```

```
from datetime import datetime, timedelta
```

```
def tweet(mensaje):<br>
```

```
CONSUMER_KEY = 'xxxxxxx'
```

```
CONSUMER_SECRET = 'xxxxxxxx'
```

```
ACCESS_KEY = 'xxxxxxxx'
```

```
ACCESS_SECRET = 'xxxxxxxx'
```

```
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
```

```
api = tweepy.API(auth,parser=tweepy.parsers.JSONParser())
```

```
 tweetid = api.update_status(status=mensaje)
```

```
app = Flask(__name__)<br>
```

```
@app.route("/",methods=['GET'])
```

```
def seism():
```

```
	try:
```

```
		sismo = request.args.get("sismo")
```

```
		if (str(sismo) == "1" ):
```

```
			t = datetime.now()
```

```
			hora = t.strftime("%-I:%M %p")
```

```
			tweet("[In Tests] Possible earthquake in the next few seconds ("+hour+")")
```

```
except IOError as err:<br>                print 'Fallo'
```

```
pass
```

```
except Exception as e:
```

```
print "Exception "+str(e)
```

```
pass
```

```
     return "OK"
```

## **Step 4: Final Details**

[![Final details](https://content.instructables.com/FPE/HSO6/IMGJDD97/FPEHSO6IMGJDD97.jpg?auto=webp&frame=1&width=300&height=1024&fit=bounds&md=3e92146e512ba135aec9ec6b6f69a471)](https://content.instructables.com/FPE/HSO6/IMGJDD97/FPEHSO6IMGJDD97.jpg?auto=webp&frame=1&height=1024&fit=bounds&md=3e92146e512ba135aec9ec6b6f69a471)

[![Final details](https://content.instructables.com/FEK/UFV8/IMGJDDE8/FEKUFV8IMGJDDE8.jpg?auto=webp&frame=1&width=300&height=1024&fit=bounds&md=b06df34b0c9aedd289778e4dcbbf5a42)](https://content.instructables.com/FEK/UFV8/IMGJDDE8/FEKUFV8IMGJDDE8.jpg?auto=webp&frame=1&height=1024&fit=bounds&md=b06df34b0c9aedd289778e4dcbbf5a42)

[![Final details](https://content.instructables.com/F2G/RCGL/IMGJDG0U/F2GRCGLIMGJDG0U.jpg?auto=webp&frame=1&width=300&height=1024&fit=bounds&md=d6f3207a7a1f8d5cea1248481444bd14)](https://content.instructables.com/F2G/RCGL/IMGJDG0U/F2GRCGLIMGJDG0U.jpg?auto=webp&frame=1&height=1024&fit=bounds&md=d6f3207a7a1f8d5cea1248481444bd14)

[![Final details](https://content.instructables.com/FPV/OS17/IMGJDMT0/FPVOS17IMGJDMT0.jpg?auto=webp&frame=1&width=300&height=1024&fit=bounds&md=b7f4b3c3d0758317cb3b05d9aecf93af)](https://content.instructables.com/FPV/OS17/IMGJDMT0/FPVOS17IMGJDMT0.jpg?auto=webp&frame=1&height=1024&fit=bounds&md=b7f4b3c3d0758317cb3b05d9aecf93af)

---

Source: https://maker.wiznet.io/teju/projects/early-seismic-warning/
