Difference between revisions of "Smoke Alarm by Email"
Katherine.d (talk | contribs) (→Usage) |
Katherine.d (talk | contribs) (→Usage) |
||
Line 27: | Line 27: | ||
*5. During sending the mail, if long time no connection to the server, wifi will shut down automatically. Program has been added soft wdt. When wifi is shut down it will jump to OOOOH address of the program automatically. | *5. During sending the mail, if long time no connection to the server, wifi will shut down automatically. Program has been added soft wdt. When wifi is shut down it will jump to OOOOH address of the program automatically. | ||
+ | |||
+ | The sample Arduino code is provided here: | ||
+ | <syntaxhighlight lang="c"> | ||
+ | #include <MsTimer2.h> | ||
+ | #include <WiShield.h> | ||
+ | #define WIRELESS_MODE_INFRA 1 | ||
+ | #define WIRELESS_MODE_ADHOC 2 | ||
+ | //#define DEBUG | ||
+ | // Wireless configuration parameters ---------------------------------------- | ||
+ | byte local_ip[] = {192,168,3,178}; // IP address of WiShield | ||
+ | byte gateway_ip[] = {192,168,3,1}; // router or gateway IP address | ||
+ | byte subnet_mask[] = {255,255,255,0}; // subnet mask for the local network | ||
+ | prog_char ssid[] PROGMEM = {"SOLID"};// max 32 bytes | ||
+ | |||
+ | unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 | ||
+ | int wait = 500; //delay time | ||
+ | const prog_char security_passphrase[] PROGMEM = {"wxst2010"}; // max 64 characters | ||
+ | |||
+ | int WIFI_LED; //wifi connect LED | ||
+ | // WEP 128-bit keys | ||
+ | // sample HEX keys | ||
+ | prog_uchar wep_keys[] PROGMEM = { | ||
+ | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 | ||
+ | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 | ||
+ | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 | ||
+ | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3 | ||
+ | }; | ||
+ | |||
+ | //--------------------------------------------------------------------------- | ||
+ | unsigned char wireless_mode = WIRELESS_MODE_INFRA; | ||
+ | |||
+ | unsigned char ssid_len; | ||
+ | unsigned char security_passphrase_len; | ||
+ | byte server_ip[] = {202,165,103,162}; //yahoo | ||
+ | //byte server_ip[] = {219,142,78,189}; //sina | ||
+ | Client client(server_ip,25); //sina,yahoo | ||
+ | // MQ2 configuration parameters ------------------- | ||
+ | const int analogInPin = A0; // Analog input pin that the potentiometer is attached to | ||
+ | int sensorValue = 0; // value read from the pot | ||
+ | int outputValue = 0; | ||
+ | /********** jump to 0000H **********/ | ||
+ | void (*reset)(void)=0x0000; | ||
+ | /********** Time2 interrupt **********/ | ||
+ | void flash() { | ||
+ | MsTimer2::stop(); | ||
+ | reset(); | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | MsTimer2::set(60000, flash); // soft wdt period (1 minute) | ||
+ | WDT_reset(); // clear soft wdt | ||
+ | Serial.begin(9600); | ||
+ | delay(100); | ||
+ | Serial.println("BEGIN"); //begin mark | ||
+ | delay(100); | ||
+ | WiFi.begin(local_ip, gateway_ip, subnet_mask); //wifi begin | ||
+ | delay(1000); | ||
+ | Serial.println("first connecting..."); //wifi begin mark | ||
+ | client_connect(); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | WDT_reset(); //clear soft wdt | ||
+ | sensorValue = analogRead(analogInPin); // read the analog in value: | ||
+ | outputValue = map(sensorValue, 0, 1023, 0, 255); // map it to the range of the analog out: | ||
+ | Serial.print("sensor = " ); | ||
+ | Serial.print(sensorValue); | ||
+ | Serial.print("\t output = "); | ||
+ | Serial.println(outputValue); | ||
+ | delay(500); | ||
+ | if(outputValue>=50){ //check the vaule of MQ2 | ||
+ | WIFI_LED=digitalRead(9); //check the state of wifi connect led | ||
+ | if(WIFI_LED) | ||
+ | { | ||
+ | delay(500); | ||
+ | if(!client.connected()) //check the connected state of client | ||
+ | { | ||
+ | client_connect(); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | Serial.println("connected"); | ||
+ | delay(2000); | ||
+ | SMTP_email_command(); | ||
+ | } | ||
+ | } | ||
+ | else //if wifi off restar wifi | ||
+ | { | ||
+ | WDT_reset(); | ||
+ | Serial.println("WIFI_OFF"); | ||
+ | WiFi.begin(local_ip, gateway_ip, subnet_mask); | ||
+ | delay(1000); | ||
+ | Serial.println("WIFI_OFF restart..."); | ||
+ | client.connect(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | void SMTP_email_command(void) //smtp send mail | ||
+ | { | ||
+ | Serial.println("email"); | ||
+ | client.println("HELO"); /* say hello (statement after helo is needed but irrelevant)*/ | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("hello"); | ||
+ | client.println("auth login"); | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("auth login"); | ||
+ | |||
+ | client.println("c3VuemhhbnNoYW4="); //the name of yahoo,sina(Base64 encrypt) | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("name"); | ||
+ | |||
+ | //client.println("emhhbmdsaW4="); //the password of sina(Base64 encrypt) | ||
+ | client.println("c25vdzEzMTQ1MjA="); //the password of yahoo(Base64 encrypt) | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("password"); | ||
+ | |||
+ | //client.println("MAIL FROM:<sunzhanshan@sina.com>"); // identify sender, this should be the same as the smtp server you are using---sina | ||
+ | client.println("MAIL FROM:<sunzhanshan@yahoo.com.cn>"); //identify sender, this should be the same as the smtp server you are using---yahoo | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("mail form"); | ||
+ | |||
+ | client.println("RCPT TO:<sunzhanshan1985@163.com>"); /* identify recipient */ | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("mail to"); | ||
+ | |||
+ | client.println("DATA"); | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("data"); | ||
+ | |||
+ | //client.println("From: <sunzhanshan@sina.com>"); //sina | ||
+ | client.println("From: <sunzhanshan@yahoo.com.cn>"); //yahoo | ||
+ | delay(wait); | ||
+ | client.println("To: <sunzhanshan1985@163.com>"); | ||
+ | delay(wait); | ||
+ | client.println("Subject: Alarm"); /* insert subject */ | ||
+ | delay(wait); | ||
+ | client.println(); | ||
+ | client.println("the gas concentration tested by MQ2 is " ); /* insert body */ | ||
+ | client.println(outputValue); /* insert body */ | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("Message"); | ||
+ | client.println(); | ||
+ | client.println("."); | ||
+ | //client.println(); | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println(".the email end"); | ||
+ | //client.println(); | ||
+ | client.println("QUIT"); | ||
+ | delay(wait); | ||
+ | SMTP_ACK(); | ||
+ | Serial.println("QUIT"); | ||
+ | } | ||
+ | /********** WDT_reset **********/ | ||
+ | void WDT_reset(void) | ||
+ | { | ||
+ | MsTimer2::stop(); | ||
+ | delay(500); | ||
+ | MsTimer2::start(); | ||
+ | } | ||
+ | |||
+ | /********** server return data **********/ | ||
+ | void SMTP_ACK() | ||
+ | { | ||
+ | while(client.available()) { | ||
+ | char ACK = (char)client.read(); | ||
+ | Serial.print(ACK); | ||
+ | } | ||
+ | } | ||
+ | /********** client_connect **********/ | ||
+ | void client_connect() | ||
+ | { | ||
+ | WDT_reset(); | ||
+ | if (client.connect()) //connect client | ||
+ | { | ||
+ | Serial.println("connect"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | WDT_reset(); | ||
+ | WiFi.begin(local_ip, gateway_ip, subnet_mask); | ||
+ | delay(1000); | ||
+ | Serial.println("WIFI_OFF restart..."); | ||
+ | client.connect(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> |
Revision as of 02:02, 28 December 2012
Introduction
This is an open source alarm device.
When the concentration of toxic gas is high, MQ2 Shield alerts by sending email .This is used in gas leakage detecting equipments in family and industry, are suitable for detecting of LPG, i-butane, propane, methane ,alcohol, Hydrogen, smoke.
Features
- Wide detecting scope
- Fast response and High sensitivity
- Stable and long life
- Simple set up
Usage
Set Up
Debugging steps
- 1. Connect these three boards as shown in the above. Power it by USB.
- 2. Download programs to Arduino.
- 3. Open serial debugging tools. Use lighter or other tool that can distribute gas around MQ2 sensor. (Default setting is when output number is more than 50, send mails ) User and password have to be encrypted by Base64.
- 4. Pay attention to mail server IP and port. The setting has to be correct. Or the mail receiving will be failed. Mail is as following picture shows (test sina and yahoo). Here we suggest use yahoo mailbox.
- 5. During sending the mail, if long time no connection to the server, wifi will shut down automatically. Program has been added soft wdt. When wifi is shut down it will jump to OOOOH address of the program automatically.
The sample Arduino code is provided here: <syntaxhighlight lang="c">
- include <MsTimer2.h>
- include <WiShield.h>
- define WIRELESS_MODE_INFRA 1
- define WIRELESS_MODE_ADHOC 2
//#define DEBUG // Wireless configuration parameters ---------------------------------------- byte local_ip[] = {192,168,3,178}; // IP address of WiShield byte gateway_ip[] = {192,168,3,1}; // router or gateway IP address byte subnet_mask[] = {255,255,255,0}; // subnet mask for the local network prog_char ssid[] PROGMEM = {"SOLID"};// max 32 bytes
unsigned char security_type = 2; // 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2 int wait = 500; //delay time const prog_char security_passphrase[] PROGMEM = {"wxst2010"}; // max 64 characters
int WIFI_LED; //wifi connect LED // WEP 128-bit keys // sample HEX keys prog_uchar wep_keys[] PROGMEM = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, // Key 0 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 1 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Key 2 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 // Key 3
};
//--------------------------------------------------------------------------- unsigned char wireless_mode = WIRELESS_MODE_INFRA;
unsigned char ssid_len; unsigned char security_passphrase_len; byte server_ip[] = {202,165,103,162}; //yahoo //byte server_ip[] = {219,142,78,189}; //sina Client client(server_ip,25); //sina,yahoo // MQ2 configuration parameters ------------------- const int analogInPin = A0; // Analog input pin that the potentiometer is attached to int sensorValue = 0; // value read from the pot int outputValue = 0; /********** jump to 0000H **********/ void (*reset)(void)=0x0000; /********** Time2 interrupt **********/ void flash() {
MsTimer2::stop(); reset();
}
void setup() {
MsTimer2::set(60000, flash); // soft wdt period (1 minute) WDT_reset(); // clear soft wdt Serial.begin(9600); delay(100); Serial.println("BEGIN"); //begin mark delay(100); WiFi.begin(local_ip, gateway_ip, subnet_mask); //wifi begin delay(1000); Serial.println("first connecting..."); //wifi begin mark client_connect();
}
void loop() {
WDT_reset(); //clear soft wdt sensorValue = analogRead(analogInPin); // read the analog in value: outputValue = map(sensorValue, 0, 1023, 0, 255); // map it to the range of the analog out: Serial.print("sensor = " ); Serial.print(sensorValue); Serial.print("\t output = "); Serial.println(outputValue); delay(500); if(outputValue>=50){ //check the vaule of MQ2 WIFI_LED=digitalRead(9); //check the state of wifi connect led if(WIFI_LED) { delay(500); if(!client.connected()) //check the connected state of client { client_connect(); } else { Serial.println("connected"); delay(2000); SMTP_email_command(); } } else //if wifi off restar wifi { WDT_reset(); Serial.println("WIFI_OFF"); WiFi.begin(local_ip, gateway_ip, subnet_mask); delay(1000); Serial.println("WIFI_OFF restart..."); client.connect(); } }
} void SMTP_email_command(void) //smtp send mail {
Serial.println("email"); client.println("HELO"); /* say hello (statement after helo is needed but irrelevant)*/ delay(wait); SMTP_ACK(); Serial.println("hello"); client.println("auth login"); delay(wait); SMTP_ACK(); Serial.println("auth login");
client.println("c3VuemhhbnNoYW4="); //the name of yahoo,sina(Base64 encrypt) delay(wait); SMTP_ACK(); Serial.println("name");
//client.println("emhhbmdsaW4="); //the password of sina(Base64 encrypt) client.println("c25vdzEzMTQ1MjA="); //the password of yahoo(Base64 encrypt) delay(wait); SMTP_ACK(); Serial.println("password");
//client.println("MAIL FROM:<sunzhanshan@sina.com>"); // identify sender, this should be the same as the smtp server you are using---sina client.println("MAIL FROM:<sunzhanshan@yahoo.com.cn>"); //identify sender, this should be the same as the smtp server you are using---yahoo delay(wait); SMTP_ACK(); Serial.println("mail form");
client.println("RCPT TO:<sunzhanshan1985@163.com>"); /* identify recipient */ delay(wait); SMTP_ACK(); Serial.println("mail to");
client.println("DATA"); delay(wait); SMTP_ACK(); Serial.println("data");
//client.println("From: <sunzhanshan@sina.com>"); //sina client.println("From: <sunzhanshan@yahoo.com.cn>"); //yahoo delay(wait); client.println("To: <sunzhanshan1985@163.com>"); delay(wait); client.println("Subject: Alarm"); /* insert subject */ delay(wait); client.println(); client.println("the gas concentration tested by MQ2 is " ); /* insert body */ client.println(outputValue); /* insert body */ delay(wait); SMTP_ACK(); Serial.println("Message"); client.println(); client.println("."); //client.println(); delay(wait); SMTP_ACK(); Serial.println(".the email end"); //client.println(); client.println("QUIT"); delay(wait); SMTP_ACK(); Serial.println("QUIT");
} /********** WDT_reset **********/ void WDT_reset(void) {
MsTimer2::stop(); delay(500); MsTimer2::start();
}
/********** server return data **********/ void SMTP_ACK() {
while(client.available()) { char ACK = (char)client.read(); Serial.print(ACK); }
} /********** client_connect **********/ void client_connect() {
WDT_reset(); if (client.connect()) //connect client { Serial.println("connect"); } else { WDT_reset(); WiFi.begin(local_ip, gateway_ip, subnet_mask); delay(1000); Serial.println("WIFI_OFF restart..."); client.connect(); }
}
</syntaxhighlight>