vegoil_bear
Published © GPL3+

Low cost earthquake detection & alerting

A very low cost earthquake detection & alerting system using esp8266, an analogue sensor and Cayenne.

IntermediateFull instructions provided2 hours4,212
Low cost earthquake detection & alerting

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
3-axis accelerometer breakout board
×1
ABS enclosure
×1
USB power supply
×1
0,5m micro USB cable
×1

Software apps and online services

Cayenne
myDevices Cayenne
Arduino IDE
Arduino IDE

Story

Read more

Schematics

Earthquake sensor

Code

Running average library

Arduino
No preview (download only).

Earthquake sensor

Arduino
// Reports detection of earthquake to Cayenne.

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <RunningAverage.h>

// WiFi network info.
char ssid[] = "ssid";
char wifiPassword[] = "wifiPassword";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

// sensor0 output is on A0

const int chipSelect = 4;

unsigned long lastMillis = 0;

int numsamp = 12; //window length is defined by the delay (4ms) and numsamp so approx. 48ms
//set up running average
RunningAverage myRA(1000);

int reportingint = 10000; //length of reporting interval in milliseconds

int sumsqrdiff = 0;  //sum of sqr differences over reporting interval

int avgsumsqrdiff = 0; //avg sum of sqr differences over period defined by numsamp. 

int abovethrcnt = 0; //for earthquake detection report the number of periods defined by numsamp that exceed earthquake threshold within each reporting interval

void setup() {
	Serial.begin(115200);
  // attempt to connect to WiFi network:
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);

  myRA.clear(); // explicitly start running average clean

}

void loop() {

  //define array size for the number of numsamp 
  int sensorarray[numsamp];
  int runAvg[numsamp];
  int sqrdiff[numsamp];
  
          for (int ana = 0; ana < numsamp; ana++) {
            //read ADC input A0 n times and stores in array of size n
            sensorarray[ana] = analogRead(A0); //read sensor voltage
            Serial.println (sensorarray[ana]);
            myRA.addValue(sensorarray[ana]); //use sensor sample in running average calculation
            runAvg[ana] = myRA.getAverage(),3;
            sqrdiff[ana] = (sensorarray[ana]-runAvg[ana])*(sensorarray[ana]-runAvg[ana]); //calculation of sum of square differences from running avergae
            //Serial.println (sqrdiff[ana]);
            sumsqrdiff = sumsqrdiff + sqrdiff[ana];
            // 4ms delay so that sample rate is approx. 250 samples/sec
            delay(4);

          } 
          avgsumsqrdiff = sumsqrdiff/numsamp; // calculate the peak of the avg of sum of square diffs
          Serial.println(avgsumsqrdiff);


              //if vibration levels exceed threshold then add 1 to the number of windows above threshold within the 10 second reporting interval
              if (avgsumsqrdiff > 89) {  //should be triggered by 'strong' earthquakes - min 0.092g
              abovethrcnt = abovethrcnt + 1;
              }
          sumsqrdiff = 0;
          avgsumsqrdiff = 0;


	//Publish data every reporting interval
	if (millis() - lastMillis > reportingint) {
    Cayenne.loop();
		lastMillis = millis();
		//Write data to Cayenne here. This example just sends the current uptime in milliseconds.
		Cayenne.virtualWrite(0, lastMillis); //Send number of milliseconds since startup to Cayenne. Used for diagnostics purposes
    Cayenne.virtualWrite(1, abovethrcnt); //Send number of periods above earthquake threshold of length defined by numsamples (~0.5secs) to Cayenne
    Serial.println(abovethrcnt);

        if (abovethrcnt>52) { //used this to trigger earthquake alerts - accelerationb must be above threshold for 25% of reporting interval
        Cayenne.virtualWrite(2, 2); //generate alert
      }
        else {
        Cayenne.virtualWrite(2,0); //cancel alert
      }

   //reset variables each publishing interval
   abovethrcnt = 0;

	}
}

Credits

vegoil_bear
0 projects • 0 followers

Comments