Home‎ > ‎1-Arduino‎ > ‎

GSM Alarm

07 / 2012 - Arduino and GSM Siemens C75 phone.

    This project idea was given to me by a friend of mine, he explain me that was very nice to have an alarm that send SMS whenever the light was off (electricity power fails). Then I had this idea, we just have to reuse some old stuff to do it. The only cost that we may have is to buy an Arduino...
I decided to use an old Siemens C75 mobile phone, it is/was a good phone it never failed to me when I used it and the battery still is in good shape, so it was a waste being in my drawer , sleeping forever ...  
I will use an LDR (Light Sensor) but in practice it will be used other solution based on an optocoupler or even a current sensor, but for simplicity we will use an LDR solution, even so it could work if a light turns on when the power goes off, or ... hey , this is a great idea ,why not to use an emergency light, it solves me two problems, power fail detection, plus energy resource to power up the Arduino ... great !!! 

Worth mentioning that I still have left, 5 analog and 12 logic gates in Arduino, so why not to use it to detect doors, windows open, hight temperature , fire detection or flood detection, etc... and I can even use multiplexing chips to increase gates... ok, lets stay in light sensor for now ... ;)

Before I begin;  I'll not be responsible if you damage your phone ;)...

List of material I need:
  • Arduino Uno R3.
  • Siemens C75 and its power supply.
  • 2x 2K7 resistor.
  • 1x 3K3 resistor.
  • 1x 10K  resistor.
  • LDR sensor.
  • Emergency Light case with 12v battery.

STEP 1 --> Study on the Siemens C75 phone:
    First step was to check if this phone has a built in modem, and it has. Now I need to identify the pin output ( pinout ) from C75, to check where are the Tx/Rx pins. In fact It was not an hard task, there are several sites with this information, it seems that the best (old) phones for these type of tasks are the Siemens and the Sony-Ericsson phones.

Here it is what I found out about pin output: 
Siemens C75 pinout used:
  1.  > Vbatt - Power Charging (used for charging).
  2.  > Gnd - GND  (main ground).
  3.  > Tx   connected to arduino RX (using a resistor to limit current ).
  4.  > Rx  connected to arduino TX (using a voltage devider from 5v to ~2,75v).
  5.  > CTS  need to be connected to ground in order to work
  6.  > RTS 
  7.  > DCD 
  8.  > Audio P - AUDIO L 
  9.  > AUDIO - GND 
  10.  > Audio N - AUDIO R 
  11.  > GND Mic 
  12.  > EPP EPP 1 (EXT MIC.) 

    Marked in green colour are the pin that I will need for the project: 1,2,3,4,5 , pins 2 and 5 could be connected together , but because it was a very cumbersome task to solder some wires on the phone , I decided to just solder one wire per connector and then outside connect it to ground. 

in Figure 3 below we can see the solution used by me, I soldered the wires inside the phone connector and used an IDC connector with a 5 wire ribbon, hopefully will be OK for usage.

I did not use my SIM card with PIN authentication, this will avoid doing it by software (via Arduino), so I advice you to do the same.

STEP 2 --> Hardware assembly:
    Using a single breadboard the Arduino was attached to it, then all components were connected according to my plan.
Pictures are explicit , figure 1 is the schematic ,  figure 2 is the Fritzing breadboard design and the final assemblage on figure 3.
At  this stage the Emergency light was not yet used, but is just missing the part that assembles all these components inside the box. 


     
 
 
 

                            Figure 1: Schematic                                                 Figure 2 : Fritzing breadboard                                          Figure 3: System assembled



STEP 3 --> Software coding:

    Using the Arduino IDE all necessary code was made. My first sketch was a simple code to test all AT commands on the phone , this code still is on my final sketch , just change the variable "estado=3" instead of using "estado=0". 
I Used two Serial communication channels , one by software emulation using pins 2 and 3 used for phone communication (phone) and the Arduino hardware serial for debugging (Serial).

void loop(){ 
 if (phone.available())
      Serial.write(phone.read());
 if (Serial.available())
      phone.write(Serial.read());
}

AT-Commands for Siemens are attached in the bottom of this page, it is not for the C75 phone, since it is a phone and not a module, so most probably there are no documentation specifically for this phone, but we just need to test it and if it works, great!

Lets see how to send an SMS:

1- Send:  AT , received OK , it works!
2- Check the SMS format  (page 27 from AT reference pdf)  , only show a 0, it means that only supports PDU.
3- Just to be sure tried to force Text , but ERROR was received.

AT
OK

AT+CMGF?
+CMGF: 0
OK

AT+CMGF=1
ERROR

Unfortunately the Siemens C75 phone can only send SMS with PDU encoded and not Text mode, which difficult the message insertion, at least as human readable text message. after some reading I found an online PDU encode tool http://twit88.com/home/utility/sms-pdu-encode-decode , and start using it with the following code to send SMS:

receiver number: 351987654321
message: hello world!
Output was: 
AT+CMGW=25
0011000C915391785634120000AA0CE8329BFD06DDDF72363904

Remark: AT+CMGW command is for saving a message on the phone draft folder , it is very good for testing since you will not spend money on SMS, you can see the messages created on the phone before sending it, the correct command to send immediately is : AT+CMGS .

Lets Code...
void createSMS(){
  phone.println("AT+CMGS=25");
  delay(2000);
  phone.print("0011000C915391785634120000AA0CE8329BFD06DDDF72363904");
  phone.write(0x1A); // Ctrl+Z
  phone.write(0x0D); //CR
  phone.write(0x0A); //LF
}
...and it works ! ...an SMS with "hello world!" on it was send by calling the createSMS() function.

    But, this is not the best way for sending SMSs, I have no idea what is the message send by Arduino, not to mention the fact that we must hardcode all messages so we are not allowed to use dinamically created messages, like sending actual values, etc... 
Fortunately there is a solution, thanks to Daniel Richman that made a function to convert simple text into PDU format, and also thanks to Tim Zaman  contribute to publish it in his site: http://www.timzaman.nl/?p=47&lang=en
    Nevertheless I introduce some changes on their code, add a new function to send readable destination phone number, instead of being hard coded in PDU format , but be careful because it was designed just to accept 12 digits phone number (like all Portuguese phone numbers), but can be easily adapted to others phone numbers length (check inside code comments).

With this code we can just send an SMS in PDU format like this:
 send_sms("351969999999","Light is OFF!");

So, my "new" code is doing the following:
Made an automate function, written in Portuguese "automato" , that basically changes state according normal program flow.

-State 0: (estado=0) --> this will check the LDR value and according with defined value , this case 900 (mean Ligh ON, it goes from 0 ->"no light" until 1023-> "maximum light") it will change for state 1 or if below 900 will change to state 2.
-State 1: Will set some flags to avoid sending several SMSs, and will send an SMS to saved destination with "Ligh is ON".
-State 2: Will send an SMS with "Light is OFF".
-State 3: DEBUG , used for testing phone connectivity, or testing all AT-Commands via serial com, only used if you change your code to start with estado=3 (state 3) , also documented inside the code.



Arduino code: GSMalarm.ino 
/***********************************************************
*                  SMS Alarm  
    This Software uses Arduino connected to a mobile phone(SIEMENS C75) 
    and it sends a SMS when a certain amount of light is off (LDR) and it sends 
    another (SMS) after light values are restored.
    
    Copyright (C) 2012  Carlos Oliveira  -- coli24200@gmail.com --

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
***********************************************************/
/*
HARDWARE SETUP

13 -Led pin Debug 13 (used for status)
A2 -Analog input A2 (Light Sensor LDR)
2 -RX (phone) 2
3 -TX (phone) 3
 
 */
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3


/***********************************************************
*                  PDU encoder setup       
*  Copyright (C) 2010  Daniel Richman 
***********************************************************/
/* From hexdump.h ALIEN Project Daniel Richman */
#define num_to_char(number)   ((number) < 10 ?                           \
                                               ('0' + (number)) :        \
                                               (('A' - 10) + (number)) )
/* To select the 4 bits we do this */
#define first_four(byte)       (0x0F & (byte))

/* Last four: Shift left to get to a number < 16 */
#define  last_four(byte)      ((0xF0 & (byte)) >> 4)

/* The MSB goes first. */
#define hexdump_a(byte)  num_to_char( last_four(byte))
#define hexdump_b(byte)  num_to_char(first_four(byte))
/*
 */
//**********************************************************
// End of PDU encoder setup

//init
SoftwareSerial phone=SoftwareSerial(rxPin, txPin);
int ledPin = 13; //Used for DEBUG
int sensorPin = A2;  
int sensorValue = 0; 
int estado=0; 
//estado=3 for testing serial communication with mobile
boolean flagLUZoff=false;
boolean flagSMS=false;

/***********************************************************
*                  Setup                                   *
***********************************************************/
void setup()  
{
  pinMode(ledPin,OUTPUT); // LED DEBUG
  //Serial ------------------------------------------------- 
  // Open serial communications and wait for port to open:
  Serial.begin(9600);  // DEBUG
  while (!Serial) {;
  // wait for serial port toconnect.Needed for Leonardo only
  }

  //C75 init ----------------------------------------------
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  phone.begin(9600);

}


/***********************************************************
*                  LOOP                                    *
***********************************************************/
void loop() // run over and over
  automatoMain();
}

/***********************************************************
*                  Automato                                *
***********************************************************/

void automatoMain() {

  switch(estado) {
  case 0://check light value
    digitalWrite(ledPin, HIGH);  
    delay(1000);    
    sensorValue = analogRead(sensorPin); 
    digitalWrite(ledPin, LOW); 
    delay(1000);
    if (sensorValue < 900){
      flagLUZoff=true;
      estado=1; 
    }
    else{
      flagLUZoff=false;
      estado=2; 
    }

    break;

  case 1:// SMS send Light OFF
    delay(1000);
    if(flagLUZoff && !flagSMS){
    Serial.println("Sending SMS light is OFF"); // DEBUG
    send_sms("351969999999","Light is OFF!");
    flagSMS=true;
    }
    delay(1000);
    estado=0;
    break;

  case 2:// SMS send Light ON
    if(!flagLUZoff && flagSMS){
    Serial.println("Sending SMS light is ON"); // DEBUG
    send_sms("351969999999","Light is ON!");
    flagSMS=false;
    }   
    delay(1000);
    estado=0;
    break;

  case 3://Phone communication tests (via Serial COM)
    if (phone.available())
      Serial.write(phone.read());
    if (Serial.available())
      phone.write(Serial.read());

    break;

  default:
    break;
  }
}

/***********************************************************
*                  SMS PDU encript  

    This part of the code is not written by me, nevertheless I had to 
    change some parts (documented) in order to work correctly under
    Arduino V1.0. And also I added a converter function for the phone
    number, but only works for phone number with 12 digits.
    
    code was taken from Tim Zaman site: 
    (http://www.timzaman.nl/?p=47&lang=en) 
    
    **********************************************************
    
    Copyright (C) 2010  Daniel Richman (/Timbobel) (/Oliveira)
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    For a full copy of the GNU General Public License, 
    see . <http://www.gnu.org/licenses/>.
*
***********************************************************/
void hexdump_byte(unsigned char byte)
{
  phone.write(hexdump_a(byte));
  phone.write(hexdump_b(byte));
}

/***********************************************************
*                  convert Phone   (C.Oliveira)                        *
*this only works if your phone number has 12 digits, if not* 
*use the old conversion method                             *
***********************************************************/
void convertPhone(char *pho){ 
  size_t data_length,y; 
  data_length = strlen(pho);
  for (y = 0; y < data_length; y=y+2)
    {
      phone.write(pho[y+1]);
      phone.write(pho[y]);
    }
//**********************************************************  

void send_sms(char *recPhone, char *data)
{
  size_t data_length, x;
  char l;
  long i;
  long n;

  data_length = strlen(data);
  i = data_length * 7;

  /* Round i up to a multiple of 8 */
  if (i & 0x07) i = (i & ~0x07) + 0x08;

  /* Calculate the number of message octets */
  i = i / 8;

  phone.println("AT+CMGF=0");
  delay(1500);
  phone.print("AT+CMGS="); // Send SMS Comand
  //phone.print("AT+CMGW=");  //Send SMS to mobile draft folder (DEBUG)
  delay(1500);
  phone.println(i + 14); //change it if necessary (see example below) Fixed number for 12 digits phone.
  delay(1500); 
  
/* Phone number is hardcoded if digits diferent from 12
   http://twit88.com/home/utility/sms-pdu-encode-decode
  use the utility to convert into pdu format, just fullfil
  the receiver number and press convert button.
  Hexadecimal PDU Message
  below the example for: 351987654321
  output:
  AT+CMGW=14  <--- size of, use above
  0011000C915391785634120000AA
  */
  // phone.print("0011000C915391785634120000AA"); // mobile: 351987654321 OLD METHOD for phones with digits !=12 
  
  phone.print("0011000C91"); // using the convertPhone function , for phones # with digits =12        
  convertPhone(recPhone);
  phone.print("0000AA");

  hexdump_byte(data_length & 0xFF);

  l = 0;
  n = 0;

  for (x = 0; x < data_length; x++)
  {
    if (data[x] == '$')  data[x] = 0x02;

    n |= (data[x] & 0x7F) << l;
    l += 7;

    if (l >= 8)
    {
      hexdump_byte(n & 0xFF);
      l -= 8;
      n >>= 8;
    }
  }

  if (l != 0)
  {
    hexdump_byte(n & 0xFF);
  }
  // Code changed for Arduino v1.0
  phone.write(0x1A); // Ctrl+Z
  phone.write(0x0D); // CR
  phone.write(0x0A); // LF
  delay(5000);
  
}

STEP 4 --> Final setup:

YouTube Video Demo





I will post a picture when all is assembled together with the Emergency Light.


Feel free to contact me if you have doubts :  coli24200@gmail.com




REFERENCES:




Project Files:
ċ
GSMalarm.ino
(8k)
Carlos Oliveira,
Aug 23, 2012, 3:16 PM
Ċ
Carlos Oliveira,
Jul 19, 2012, 10:45 AM
Comments