Advertisement

Sri Lanka's First and Only Platform for Luxury Houses and Apartment for Sale, Rent

Thursday, April 12, 2012

Low Cost Earth Quake Alarm using Arduino

Yesterday (11th of April 2012) was the first time I have ever experienced an Earth Quake in my life. It was strange because Sri Lanka is said to be a country less prone for Earth Quakes and there hasn't been one as violent as yesterday's one for at least 2 decades.

Even though most of the buildings were shaking along with the things inside for about 50 - 60 seconds, many people haven't noticed the shakes, may be due to the inexperience with Earth Quakes or due to confusion. But the most concerning thing is almost all Sri Lankan buildings are not Earth Quake safe like those in Japan. So feeling the Earth Quake and leaving the building as early as possible is crucial. Especially during night time where people will be sleeping.

For this reason I was inspired to create a Low Cost Earth Quake Alarm which will help those who can't feel an Earth Quake to get out in time and be safe.

I managed to build a small prototype using things lying around in my hack space which are;
  1. Arduino (Any Model)
  2. One Tilt Switch
  3. One Buzzer or Piezo Speaker
  4. One 1K Resistor
  5. 6 Jumper Cables
With the above components I built the below prototype


And used the following arduino sketch to detect changes in Tilt Switch which is connected to analog in pin A0 and if it exceeds a predefined threshold for a predefined period of time, alarm using the Piezo Speaker or Buzzer which is connected to pin no 10 which has PWM.

##############################
##     Shazin Sadakath      ##
##############################

#define TILT_SWITCH_PIN A0
#define BUZZER_PIN 10
#define MAX 100
int tiltValue = 0;
int previousTiltValue = 0;
int valueThreshold = 5;
int timeThreshold = 2 ;
int time = 0;

void setup() 
{
   pinMode(BUZZER_PIN, OUTPUT);
}

void loop() 
{
  tiltValue = analogRead(TILT_SWITCH_PIN);
  if(abs(tiltValue - previousTiltValue) >= valueThreshold)
  {
    time = time + 1;
  }
  else
  {
    reset();
  }
  if(time >= timeThreshold) 
  {
   analogWrite(BUZZER_PIN, MAX);
   delay(500);
   reset();
  }
  previousTiltValue = tiltValue;
  delay(500);
}

void reset() 
{
 time = 0;
 previousTiltValue = 0; 
 analogWrite(BUZZER_PIN, 0);
}

Finally I tested this with some minor shakes and it works. The alarm is going off until the shaking stops completely.



In Sri Lanka we can't buy off the shelf Commercial, Highly Accurate Earth Quake Detectors or Alarms yet so this prototype would be a life saver. Mounting this on a wall or on the roof where it is less like to be disturbed by people's movement and covering it in a box would make it less likely to produce false alarms.

Constructive Criticism is always welcome!

Saturday, April 7, 2012

reCaptcha Captcha in Open Cart Register Page

It has been quite a while since I posted anything on my blog. Recently I have been working on my small business web site and used opencart to create that web site. But by default Open Cart doesn't have a captcha for registering. Due to high no of bots available to create false accounts, it is a must to have captcha.

There is a post in Open Cart forum which shows how to enable opencart default captcha library in the registration page. But I wanted to use recaptcha which is much more hard to crack using image processing bots, has a built in audio playback and more importantly each time someone uses it in the back end they are helping to digitize scanned pages of books.

You need to register in recaptcha to get a public and private key for your domain name. After that you need to download the php library for recaptcha.

Step 1 :

Put the downloaded recaptchalib.php in /system directory

Step 2 :

Go to /catalog/view/theme/default or your custom theme folder /template/account/ and open register.tpl

Find the below code
    <h2><?php echo $text_newsletter; ?></h2>
        <div class="content">
          <table class="form">
            <tr>
              <td><?php echo $entry_newsletter; ?></td>
              <td><?php if ($newsletter == 1) { ?>
                <input type="radio" name="newsletter" value="1" checked="checked" />
                <?php echo $text_yes; ?>
                <input type="radio" name="newsletter" value="0" />
                <?php echo $text_no; ?>
                <?php } else { ?>
                <input type="radio" name="newsletter" value="1" />
                <?php echo $text_yes; ?>
                <input type="radio" name="newsletter" value="0" checked="checked" />
                <?php echo $text_no; ?>
                <?php } ?></td>
            </tr>
    </table>
    </div>


and put this below
<div class="content">
&nbsp;<span class="required">*</span>   <b><?php echo $entry_captcha; ?></b><br />
    <?php
          require_once('system/recaptchalib.php');
          $publickey = "<Your Public Key>"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>

    <span class="error"><?php echo $error_captcha; ?></span>
</div>


Step 3 :

Go to catalog/language/english/account.register.php and find this line
$_['entry_confirm']        = 'Password Confirm:';


and put this below
$_['entry_captcha']        = 'Enter the code in the box below:';


and find this line
$_['error_agree']          = 'Warning: You must agree to the %s!';


and put this below
$_['error_captcha']        = 'The captcha code was entered incorrectly, please try again!';


Step 4 :

Go to catalog/controller/account/register.php and find this line
$this->data['button_continue'] = $this->language->get('button_continue');


and put this line below
$this->data['entry_captcha'] = $this->language->get('entry_captcha');


find this line
if (isset($this->error['zone'])) {
    $this->data['error_zone'] = $this->error['zone'];
} else {
    $this->data['error_zone'] = '';
}


and put this below
if (isset($this->error['captcha'])) {
    $this->data['error_captcha'] = $this->error['captcha'];
} else {
    $this->data['error_captcha'] = '';
}


find this line
if (isset($this->request->post['newsletter'])) {
    $this->data['newsletter'] = $this->request->post['newsletter'];
} else {
    $this->data['newsletter'] = '';
}


and put this below
if (isset($this->request->post['captcha'])) {
    $this->data['captcha'] = $this->request->post['captcha'];
} else {
    $this->data['captcha'] = '';
}


find this line
private function validate() {
if ((strlen(utf8_decode($this->request->post['firstname'])) < 1) || (strlen(utf8_decode($this->request->post['firstname'])) > 32)) {
    $this->error['firstname'] = $this->language->get('error_firstname');
}


and put this below
require_once('system/recaptchalib.php');
$privatekey = "<Private Key>";
$resp = recaptcha_check_answer ($privatekey,
                      $_SERVER["REMOTE_ADDR"],
                      $_POST["recaptcha_challenge_field"],
                      $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
    this->error['captcha'] = $this->language->get('error_captcha');
}


That's it. Now you should see the recaptcha captcha in your registration page!