mbraccagni
Published © GPL3+

Mains Failure Detector

How avoid a freezer disaster while I am away.

IntermediateFull instructions provided9,931
Mains Failure Detector

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
Arduino NANO or UNO or PRO MINI
×1
GPRS shield SIM900
×1
12V switching wall power supply
×1
SparkFun Solder-able Breadboard - Mini
SparkFun Solder-able Breadboard - Mini
×1
Female Header 8 Position 1 Row (0.1")
Female Header 8 Position 1 Row (0.1")
One with short pins, two with long pins to stack on GPRS shield
×3
Male power Jack
×2
Female power jack
×2
9V battery (generic)
9V battery (generic)
×1
9V Battery Clip
9V Battery Clip
×1
Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 5,6K ohm or 6,8k ohm
×1
1N4007 – High Voltage, High Current Rated Diode
1N4007 – High Voltage, High Current Rated Diode
×2
Capacitor 100 µF
Capacitor 100 µF
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Code

SMS_1a.ino

Arduino
At lines 68, 85 and 102 you must replace XXXXXXXXXXXX with your phone number in international format.
You can compile the same code for Arduino UNO, NANO and PRO MINI. Required memory is very little.

Lines from 26 to 30 will switch on the shield, but, if it is just ON it will go OFF!!!! It's important to start the program with the shield OFF. So, ATTENTION:

1. Load software
2. Detach USB connector
2. Connect first the 12V power supply
3. Connect 9V battery

Now "Status" led on the shield will be on and "NetLight" led will be blinking and it will arrive the message "*** SISTEMA CONNESSO ***" (system connected).

If you want more informations about the GPRS shield, at https://randomnerdtutorials.com/sim900-gsm-gprs-shield-arduino/ you will find a very helpfull tutorial.
/*  

GPRS Shield with SIM900

Connections:
Arduino UNO pin 2 to Vin
Arduino UNO pin 7 to Shield TXD
Arduino UNO pin 8 to Shield RXD
Arduino UNO pin 9 to Shield pin 9 (shield ON/OFF)
Arduino UNO GND to Shield GND

*/

#include <SoftwareSerial.h>

// Configure software serial port
SoftwareSerial SIM900(7, 8); 

//Variable to save incoming SMS characters
char incoming_char=0;
//Allarm status
byte allarmSent = LOW;

void setup() {
  // Switch on the GSM shild
  pinMode(9,OUTPUT);
  digitalWrite(9, HIGH);
  delay(2000);
  digitalWrite(9, LOW);
  delay(5000);
  // Set pin 2 as input for Vin detection
  pinMode(2,INPUT);
  // Arduino communicates with SIM900 GSM shield at a baud rate of 19200
  SIM900.begin(19200);
  // Give time to GSM shield to log on to network
  delay(30000);
  // AT command to set SIM900 to SMS mode
  SIM900.print("AT+CMGF=1\r"); 
  delay(100);
  sendSMSready();
  // Set module to send SMS data to serial out upon receipt 
  // SIM900.print("AT+CNMI=2,2,0,0,0\r");
  // delay(100);
}

void loop()
{
  // Display any text that the GSM shield sends out on the serial monitor
  if(SIM900.available() >0) {
    //Get the character from the cellular serial port
    incoming_char=SIM900.read();
    //Print the incoming character to the terminal
    Serial.print(incoming_char);
  }
  while (digitalRead(2) == HIGH) {}
  if(digitalRead(2) == LOW && allarmSent == LOW) {
    // whait 1" more and send message if pin 2 is already LOW
    delay(1000);
    if(digitalRead(2) == LOW) sendSMSallarm();
  }
  while (digitalRead(2) == LOW) {}
  if(digitalRead(2) == HIGH && allarmSent == HIGH) sendSMSallarmOFF();
}

void sendSMSallarm()
{
  // Set recipient's mobile number in international format
  SIM900.println("AT + CMGS = \"+XXXXXXXXXXXX\""); 
  delay(100);
  // Set SMS message
  SIM900.println("*** MANCANZA TENSIONE RETE ***");
  delay(100);
  // End AT command with a ^Z, ASCII code 26
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  // Give module time to send SMS
  delay(5000);
  allarmSent = HIGH;
}

void sendSMSallarmOFF()
{
  // Set recipient's mobile number in international format
  SIM900.println("AT + CMGS = \"+XXXXXXXXXXXX\""); 
  delay(100);
  // Set SMS message
  SIM900.println("*** TENSIONE RETE OK ***");
  delay(100);
  // End AT command with a ^Z, ASCII code 26
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  // Give module time to send SMS
  delay(5000);
  allarmSent = LOW;
}

void sendSMSready()
{
  // Set recipient's mobile number in international format
  SIM900.println("AT + CMGS = \"+XXXXXXXXXXXX\""); 
  delay(100);
  // Set SMS message
  SIM900.println("*** SISTEMA CONNESSO ***");
  delay(100);
  // End AT command with a ^Z, ASCII code 26
  SIM900.println((char)26);
  delay(100);
  SIM900.println();
  // Give module time to send SMS
  delay(5000);
}

Credits

mbraccagni
0 projects • 3 followers

Comments