PLEASE NOTE: This site is no longer being actively maintained. For frequently updated BeagleBone resources please visit Beaglebone.org.

Tuesday, April 29, 2014

On 2:19 PM by Anonymous


OVERVIEW

Subject: You have Mail (from BeagleBone)!

This alarm project hires a "watch dog" to keep track of your home or office. An email is sent to you by the BeagleBone when an ALARM condition is detected { bark...bark  }. We will use a flood alarm as an example project. Other types of  alarms can be derived from this tutorial.

What you will need

  • BeagleBone Black
  • BeagleBone expandable case Orange or Black (recommended)
  • AC adapter 5 Volts, 2 Amp (optional)
  • Proto Cape
  • Proto Parts Kit (and other components)
  • A network connection to the BeagleBone
  • Soldering iron, solder and insulated wires

  • Circuit components
    • (1) Transistor, Mosfet N-channel, 2N7000, TO-92
    • (1) Transistor, NPN general purpose,  2N2222, TO-92 (optional)
    • (1) Diode 1N4148 (optional)
    • (1) Red LED (optional)
    • (1) Push-button (optional)
    • (1) Resistor 330 Ohms  (optional)
    • (2) Resistor 1K Ohms
    • (1) Resistor 470K-to-10M  Ohms 
    • (1) Alarm Buzzer (optional)
    • (1) Screw terminal connector (optional)

Steps to follow

First we will install mail services on the BeagleBone.

STEP 1

Install a mail client. {Angstrom}.  We will be using SSH via USB  to login .

opkg install msmtp

STEP 2

Create a configuration file {use can use any editor nano, vim, emacs... }

nano /etc/msmtprc 

STEP 3

Enter the following mail client configuration information (for gmail account).
A mail client is required to properly send an email.
(remember to substitute your information for "user" and "password")

defaults
tls on
tls_starttls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt

account default
host smtp.gmail.com
port 587
auth on
user username@gmail.com
password mypass
from username@gmail.com
logfile /var/log/msmtp.log

Now save your file
Ctrl+o {to save file}

Enter { to confirm filename}

Ctrl+x {exit editor}

STEP 4

You can test the email client (open a terminal and enter the following)

sendmail user@somewhere.com
Subject: This is a test
Hello to me
{ctrl+D} 

STEP 5

Let's build the alarm circuit (P8-x, and P9-x) are the header pins of the BeagleBone Input/Output ports.
Schematic
Water Alarm Schematic Diagram 

Circuit Operation

Resistor R2 takes 3.3V from the BeagleBone and limits the current to a safe level. 
With water between the probe tips, an electric current flows from Probe-1 to Probe-2 and through R1. This creates a voltage on the gate terminal of  the mosfet transistor Q1. The output of Q1 goes low and this signal is read by the program.  Switch S1 is optional, and allows testing and debugging of the alarm without using water.

The alarm program will sense the alarm condition and send an email notifying you of the problem.  In addition LED1 and R4 can be used to indicate the alarm condition (flashing under program control).  No alarm would be complete, without a loud buzzer. Resistor R3 provides a safe signal to transistor T1, which switches ON the Buzzer.  Diode D1 protect the transistor from spikes that can occur during switching.  The behavior of the LED and buzzer is controlled by the software program.

R1: Resistor 470 kilohms (yellow,purple,yellow, gold) - to - 10 megohms (brown, black, blue, gold)
R2: Resistor 1 kilohms (brown, black, red, gold)
R3: Resistor 1 kilohms (brown, black, red, gold)
R4: Resistor 330 ohms (orange, orange, brown, gold)

photo components wiring

Here is a suggested component placement and wiring guide.

STEP 6

Create this BoneScript program in Cloud9. {copy-paste} or

Download Alarm1-email .js File

and save to /var/lib/cloud9

Program listing (Cloud9 IDE - BoneScript)


// Logic Supply Inc 
// Alarm and SendMail Demo Program for the BeagleBone Black ARM Cortex A8
// 
// Version  : 0.0   Apr 18, 2014 SendMail alarm using CBB-proto cape
//
// (2) Additional "Alarm message text files are required"  in your HOME directory
//     alarm_msg.txt     this file is sent when the alarm is triggered 
//     reset_msg.txt     this file is sent when the alarm is reset (normal)
//
// === Sample Alarm notification text file ===
// Subject: Alarm notification
//
// An alarm has been triggered
// Automated sytem do NOT reply 
//

var b = require('bonescript');
var exec = require('child_process').exec, child;

// define pins and variables

var LED_Red  = "P9_14";  // with 330 Ohms series resistor 
var BUZZER   = "P8_13";  // Optional
var Test_sw  = "P8_26";  // Push Button

var alarm1_state   = 0;
var LED_state      = 0;

// === Define pin direction  ===

b.pinMode(LED_Red, b.OUTPUT);
b.pinMode(BUZZER, b.OUTPUT);
b.pinMode(Test_sw, b.INPUT);

// ===  Set initial pin state ===

b.digitalWrite(LED_Red, b.LOW); 
b.digitalWrite(BUZZER, b.LOW);    

console.log('Alarm Demo Program');

//=== Sample inputs at given rate ===

setInterval (inputHandler, 500);

//=== Handle input when called ===

function inputHandler() {     // detect alarm or test button push
    b.digitalRead(Test_sw, activate);

    function activate(x){
        if (x.value == '0') {           // pin is low - alarm condition
              
            if (alarm1_state == '0'){    // detected rising edge- was off
                alarm1_state = 1;        // store alarm state
                set_Buzzer();
                console.log("NORMAL");
                sendAlarmEmail();        // send alert via email (once only)
              }   
           else{                        // Toggle LED while alarm is ON
                LED_state = LED_state ? 0 : 1;
                set_LED();
                }    
        }
        else{                           // pin is high - reset condition
               
            if (alarm1_state == '1'){   // detected falling edge - alarm cleared
                alarm1_state = 0;      // store reset state
                LED_state = 0;
                set_LED();
                set_Buzzer();
                console.log("ALARM SET"); 
               sendResetEmail();
            }        
        }                    
        
    }
// === Set LED according to LED_state ===   

function set_LED (){
    if (LED_state=="1") b.digitalWrite(LED_Red, b.HIGH);
    else b.digitalWrite(LED_Red,b.LOW);
   }

// === Set LED according to LED_state ===   

function set_Buzzer (){
    if (LED_state=="1") b.digitalWrite(BUZZER, b.HIGH);
    else b.digitalWrite(BUZZER,b.LOW);
   }

// === Send Alarm Email notification ===

function sendAlarmEmail(){

child = exec('sendmail emailaddress@yourdomain.com < ~/alarm_msg.txt',
             function (error,stdout, stderr) {
                     console.log('stdout: ' + stdout);
                     console.log('stderr: ' + stderr);
                     if (error !== null) {
                         console.log('exec error: ' + error);
                     }
             });
}

// === Send Reset Email notification ===

function sendResetEmail(){

child = exec('sendmail emailaddress@yourdomain.com < ~/reset_msg.txt',
            function (error,stdout, stderr) {
                    console.log('stdout: ' + stdout);
                     console.log('stderr: ' + stderr);
                     if (error !== null) {
                         console.log('exec error: ' + error);
                     }
            });
}

}

Program Description

The first section of the program declares variables and initializes the pins so the BeagleBone can read and control the alarm signals.

var exec = require('child_process').exec, child;

The above statement allows us to make a system call as a child process. We will use this function to request the Linux operating system to send an email for us.

setInterval (inputHandler, 500);

The above line sets the rate at which we will scan the alarm signal. After the interval (500 milliseconds) the call-back function 'inputHandler' is called to check the status of the alarm signal.

If the alarm signal is low (active), we need to determine if this condition is new (or old news).
We do not want to send a email every second, only when the alarm event first occurs.

During the Alarm condition we want the red LED to be flashing and buzzer to sound the alarm. When the alarm condition is cleared (water retreats), again we are looking to send only one e-mail message.

To send an email message we make a system-call to "sendmail" and provide it a text file "alarm_msg.txt" which contains the message we want to communicate. This way it is very easy to change and customize the message and even create the text file from within a program.

STEP 7

Create an alarm notification text file "alarm_msg.txt"  (in your home folder)

 nano ~/alarm_msg.txt

Enter your text (example)

Subject: Alarm Notification
A flood alarm has been triggered.
Automated system - do NOT reply
Thank you.

Now create the back-to-normal  "reset_msg.txt" file

 nano ~/reset_msg.txt

Enter your text (example)

Subject: Alarm Notification
A flood alarm has been reset.
System is back to normal
Automated system - do NOT reply
Thank you.


STEP 8

Testing the alarm



  1. Verify your electrical connections, making sure wires are not shorting together.
  2. With power OFF, insert the prototype cable into the BeagleBone Header pins P9, P8.
  3. Insert the network or Wifi adapter for internet connectivity. Apply power using USB cable.
  4. Point your browser to the build-in Cloud9 development environment at
    192.168.9.2:3000
  5. Select the alarm program
  6. Click RUN

Press-and-hold the test push-button switch. The Buzzer will sound and the red LED will start to blink, a console message will be displayed and an email will be sent. Once the button released, a second email message will be sent to indicate that the water level has dropped. The red LED will also turn OFF.
Now insert the probe tips into a glass of water... Congratulations! (stay dry)