temp_gas_controller_email_GSM_sms_power_v1_1_fanboost_.ino

#include «SIM900.h»
#include <SoftwareSerial.h>

#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
#include «sms.h»

SMSGSM sms;

OneWire ds(8); // on pin 8

int smokepin = A2;
int fanboost = 7;
int poweron = 4;
int numdata;
int i=0;
char inSerial[40];
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0x00, 0x13, 0x77, 0x3D, 0x2E, 0x2C};
// assign an IP address for the controller:
IPAddress ip(192,168,1,210);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);
IPAddress server_ip(77, 5, 5, 5);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient client;

float temperature = 0.0;
float volsmoke = 0.0;
long lastReadingTime = 0;
long smsReadingTime = 0;
long adReadingTime = 0;

boolean started=false;
boolean monitoring=true;
boolean powerfail=false;

char smsbuffer[160];
char n[20];

///////////interrupt
volatile int rpmcount = 0;
int rpm = 0;
unsigned long lastmillis1 = 0;
/////////////////////

void setup() {

pinMode(fanboost, OUTPUT);

///////////interrupt
attachInterrupt(0, rpm_fan, FALLING);

/////////////////////

// start the SPI library:
SPI.begin();

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();

Serial.begin(9600);

pinMode(poweron, OUTPUT);
Serial.println(«GSM Shield POWER ON»);
digitalWrite(poweron, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(poweron, LOW); // turn the LED off by making the voltage LOW
Serial.println(«GSM Shield testing.»);
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(9600)){
Serial.println(«\nstatus=READY»);
started=true;
}
else Serial.println(«\nstatus=IDLE»);

if(started){
delsms();
}

// give the sensor and Ethernet shield time to set up:
delay(1000);

}

void loop() {

if (millis() — adReadingTime > 1000){

int failpowerValue = analogRead(A0);
if (failpowerValue > 1000){
powerfail = true;
}
else {
powerfail = false;
}

adReadingTime = millis();

}

/////////////////////////////////interrupt
///rpm = 0;

if (millis() — lastmillis1 >= 1000){ /*Uptade every one second, this will be equal to reading frecuency (Hz).*/

detachInterrupt(0); //Disable interrupt when calculating

rpm = rpmcount * 60; /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/

rpmcount = 0; // Restart the RPM counter
lastmillis1 = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}

/////////////////////////////////

if (millis() — smsReadingTime > 60000){

int pos=0;
//Serial.println(«Loop»);
if(started){
pos=sms.IsSMSPresent(SMS_ALL);
if(pos){
Serial.println(«IsSMSPresent at pos «);
Serial.println(pos);
sms.GetSMS(pos,n,smsbuffer,100);
Serial.println(n);
Serial.println(smsbuffer);
if(!strcmp(smsbuffer,»status»)){

sendsms();

}
if(!strcmp(smsbuffer,»kak dela?»)){

sms.SendSMS(«0639999999», «Otlichno!!»);

}
if(!strcmp(smsbuffer,»monon»)){

monitoring = true;
sms.SendSMS(«0639999999», «Monitoring ON»);

}
if(!strcmp(smsbuffer,»monoff»)){

monitoring = false;
sms.SendSMS(«0639999999», «Monitoring OFF»);

}
else
{
////////////////

}
delsms();
}
}

if (monitoring) {

////////////////////// RPM check
if (rpm < 1000) {
sendEmail();
///sendsms();
}
////////////////////////////////

////////////////////// POWER check
if (!powerfail) {
sendEmail();
sendsms();

}
////////////////////////////////

}

//////////////////////////////FANBOOST
if (temperature < 35) {
digitalWrite(fanboost, HIGH);
}
else {
digitalWrite(fanboost, LOW);
}
/////////////////////////////////////

smsReadingTime = millis();

}

// check for a reading no more than once a second.
if (millis() — lastReadingTime > 30000){
// if there’s a reading ready, read it:
// don’t do anything until the data ready pin is high:

getData();
getDataSmoke();
// timestamp the last time you got a reading:
lastReadingTime = millis();

}

// listen for incoming Ethernet connections:
listenForEthernetClients();
//Read for new byte on serial hardware,
//and write them on NewSoftSerial.
serialhwread();
//Read for new byte on NewSoftSerial.
serialswread();
}

//////////////////////////interrupt
void rpm_fan()
{
rpmcount++;
}
//////////////////////////interrupt
void delsms(){
Serial.println(«delsms»);
for (int i=0; i<10; i++){ //do it max 10 times
int pos=sms.IsSMSPresent(SMS_ALL);
if (pos!=0){
Serial.print(«\nFind SMS at the pos «);
Serial.println(pos);
if (sms.DeleteSMS(pos)==1){
Serial.print(«\nDeleted SMS at the pos «);
Serial.println(pos);
}
else
{
Serial.print(«\nCant del SMS at the pos «);
Serial.println(pos);
}
}
}

}

void serialhwread(){
i=0;
if (Serial.available() > 0){
while (Serial.available() > 0) {
inSerial[i]=(Serial.read());
delay(10);
i++;
}

inSerial[i]=’\0′;
if(!strcmp(inSerial,»/END»)){
Serial.println(«_»);
inSerial[0]=0x1a;
inSerial[1]=’\0′;
gsm.SimpleWriteln(inSerial);
}
//Send a saved AT command using serial port.
if(!strcmp(inSerial,»TEST»)){
Serial.println(«SIGNAL QUALITY»);
gsm.SimpleWriteln(«AT+CSQ»);
}
else{
Serial.println(inSerial);
gsm.SimpleWriteln(inSerial);
}
inSerial[0]=’\0′;
}
}

void serialswread(){
gsm.SimpleRead();
}

byte sendEmail()
{
byte thisByte = 0;
byte respCode;

if(client.connect(server_ip,25)) {
Serial.println(F(«connected»));
} else {
Serial.println(F(«connection failed»));
return 0;
}

if(!eRcv()) return 0;
Serial.println(F(«Sending helo»));

// change to your public ip
client.println(F(«helo server.com»));

if(!eRcv()) return 0;
Serial.println(F(«Sending From»));

// change to your email address (sender)
client.println(F(«MAIL From: <notify@server.com>»));

if(!eRcv()) return 0;

// change to recipient address
Serial.println(F(«Sending To»));
client.println(F(«RCPT To: <user@server.com>»));

if(!eRcv()) return 0;

Serial.println(F(«Sending DATA»));
client.println(F(«DATA»));

if(!eRcv()) return 0;

Serial.println(F(«Sending email»));

// change to recipient address
client.println(F(«To: Igor <user@server.com>»));

// change to your address
client.println(F(«From: Server notify <notify@server.com>»));

client.println(F(«Subject: !!!Home server email notify!!!\r\n»));

client.println(F(«WARNING!»));

client.println();

client.println(F(«Temperature: «));
client.println(temperature);

client.println();

client.println(F(«Smoke: «));
client.println(volsmoke);

client.println();

client.println(F(«Fan RPM: «));
client.println(rpm);

client.println();

client.println(F(«Power state: «));
client.println(powerfail);
client.println(F(«.»));

if(!eRcv()) return 0;

Serial.println(F(«Sending QUIT»));
client.println(F(«QUIT»));

if(!eRcv()) return 0;

client.stop();

Serial.println(F(«disconnected»));

return 1;
}

byte eRcv()
{
byte respCode;
byte thisByte;
int loopCount = 0;

while(!client.available()) {
delay(1);
loopCount++;

// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F(«\r\nTimeout»));
return 0;
}
}

respCode = client.peek();

while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}

if(respCode >= ‘4’)
{
efail();
return 0;
}

return 1;
}
void efail()
{
byte thisByte = 0;
int loopCount = 0;

client.println(F(«QUIT»));

while(!client.available()) {
delay(1);
loopCount++;

// if nothing received for 10 seconds, timeout
if(loopCount > 10000) {
client.stop();
Serial.println(F(«\r\nTimeout»));
return;
}
}

while(client.available())
{
thisByte = client.read();
Serial.write(thisByte);
}

client.stop();

Serial.println(F(«disconnected»));
}
void getDataSmoke() {

int sensorValue = analogRead(smokepin);
volsmoke=(float)sensorValue/1024*5.0;
//Serial.print(» Smoke = «);
//Serial.println(volsmoke,1);

//////////////////////////////////////////////////

if (monitoring) {
if (volsmoke > 0.85) {
//sendEmail();
//gsm.SimpleWriteln(«ATD0639999999;»);
//delay(10000);
}
if (volsmoke > 1.5) {
sendEmail();
sendsms();
}
}
}
//////////////////////////////////////////////////////

void sendsms() {

char smsbuffer[50];

int d1 = volsmoke; // Get the integer part (678).
float f2 = volsmoke — d1; // Get fractional part (678.0123 — 678 = 0.0123).
int d2 = trunc(f2 * 100); // Turn into integer (123).
//int d2 = (int)(f2 * 10000); // Or this one: Turn into integer.

int t1 = temperature; // Get the integer part (678).
float tf2 = temperature — t1; // Get fractional part (678.0123 — 678 = 0.0123).
int t2 = trunc(tf2 * 100); // Turn into integer (123).

// Print as parts, note that you need 0-padding for fractional bit.
// Since d1 is 678 and d2 is 123, you get «678.0123».
sprintf (smsbuffer, «Smoke = %d.%02d\n Temp = %d.%02d\n Fan RPM = %d\n Power = %d», d1, d2, t1, t2, rpm, powerfail);

//sprintf(smsbuffer,»Smoke: %03d», volsmoke);
if (sms.SendSMS(«0639999999», smsbuffer))
Serial.println(«\nSMS sent OK»);

}
void getData() {
//Serial.println(«Getting reading»);
//Read the temperature data

byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius;

if ( !ds.search(addr)) {
// Serial.println(«No more addresses.»);
// Serial.println();
ds.reset_search();
delay(250);
return;
}

// Serial.print(«ROM =»);
//for( i = 0; i < 8; i++) {
// Serial.write(‘ ‘);
// Serial.print(addr[i], HEX);
// }

if (OneWire::crc8(addr, 7) != addr[7]) {
// Serial.println(«CRC is not valid!»);
return;
}
// Serial.println();

// the first ROM byte indicates which chip
type_s = 0;

ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end

delay(800); // maybe 750ms is enough, maybe not
// we might do a ds.depower() here, but the reset will take care of it.

present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

// Serial.print(» Data = «);
// Serial.print(present,HEX);
// Serial.print(» «);
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
// Serial.print(data[i], HEX);
// Serial.print(» «);
}
// Serial.print(» CRC=»);
// Serial.print(OneWire::crc8(data, 8), HEX);
// Serial.println();

// convert the data to actual temperature

unsigned int raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
// count remain gives full 12 bit resolution
raw = (raw & 0xFFF0) + 12 — data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw << 3; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw << 2; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw << 1; // 11 bit res, 375 ms
// default is 12 bit resolution, 750 ms conversion time
}
temperature = (float)raw / 16.0;
//fahrenheit = temperature * 1.8 + 32.0;

//Serial.print(» Temperature = «);
//Serial.print(temperature);
//Serial.print(» Celsius, «);
//Serial.print(fahrenheit);
//Serial.println(» Fahrenheit»);

ds.reset_search();

/////////////////////////////////////////
if (monitoring) {

if (temperature > 40.0) {
sendEmail();
}
if (temperature > 45.0) {
sendEmail();
sendsms();
//delay(10000);
}
}
/////////////////////////////////////////////
}

void listenForEthernetClients() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
//Serial.println(«Got a client»);
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you’ve gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == ‘\n’ && currentLineIsBlank) {
// send a standard http response header
client.println(«HTTP/1.1 200 OK»);
client.println(«Content-Type: text/html»);
client.println();
// print the current readings, in HTML format:

client.print(«Smoke: «);
client.print(volsmoke);
client.print(» index»);
client.println(«<br>»);

client.print(«Temperature: «);
client.print(temperature);
client.print(» degrees C»);
client.println(«<br>»);

client.print(«Fan RPM: «);
client.print(rpm);
client.print(» rpm»);
client.println(«<br>»);

client.print(«Power state: «);
client.print(powerfail);
client.println(«<br>»);

break;
}
if (c == ‘\n’) {
// you’re starting a new line
currentLineIsBlank = true;
}
else if (c != ‘\r’) {
// you’ve gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}