A Proof of Concept of integration between Arduino and Sony Ericsson T610.

The purpose of this build was to evaluate the possibility to have power supply turned on and off depending on requests sent via SMS. A log (stored om micro SD Card) was also incorporated to trace which sender has requested what output.

The design criteria of the module was to

  1. Trigger relay (outlet) for a fixed period of time with an SMS
  2. Start generator (combustion engine) if not running
  3. Stop engine if no outlet triggered
  4. Log Usage per mobile phone number to SD card



Arduino Program

    1: #include <SD.h>
    2: #include <SoftwareSerial.h>
    3: #include <SimpleTimer.h>
    4:
    5: //DB Handler
    6: #include <DB.h>
    7: #include <EEPROM.h>
    8: #include <SD.h>
    9:
   10: //Init objects
   11: SimpleTimer timer;
   12: SoftwareSerial phone(8, 9);         //RX, TX pin for the phone
   13: File myFile;                        //Object for SD card
   14: char sms[128];
   15: char outlet;
   16: /*
   17: The source is distributed as an example with out any responsability of the author!
   18: Experimental program to handle the power management of the model aircraft club of Höganäs, SWEDEN
   19: */
   20:
   21: int outlet_0 = 4;                      //Outlet pin
   22: int outlet_1 = 5;
   23: int outlet_2 = 6;
   24: int outlet_3 = 7;
   25: int engineStart = 3;
   26: boolean engineRunning = false;         //Status on engine
   27: boolean cardSDPresent = false;
   28: int powerOutlet[] = {0,0,0,0};         //Minutes to let outlet in state ON
   29: int powerOutletTime = 10;              //Set time to 60 minutes
   30:
   31:
   32: void setup(){
   33:   //Init timer and set interval
   34:   timer.setInterval(6000, decreaseTimOnOutlet);
   35:   Serial.begin(9600);
   36:   phoneInit();
   37:   //Initialize the digital pin as an output.
   38:   pinMode(outlet_0, OUTPUT);
   39:   pinMode(outlet_1, OUTPUT);
   40:   pinMode(outlet_2, OUTPUT);
   41:   pinMode(outlet_3, OUTPUT);
   42:   pinMode(engineStart, OUTPUT);
   43:   //Init of file object
   44:   pinMode(10, OUTPUT);
   45:   if (SD.begin(10)) {
   46:     cardSDPresent = true;
   47:   }  
   48: }
   49:
   50: //Write last order to file for traceability
   51: void writeFile(){
   52:   // open the file with the name of the SMS date if init was successfull
   53:   if (cardSDPresent){
   54:     myFile = SD.open("hmfk.txt", FILE_WRITE);
   55:     // if the file opened okay, write to it:
   56:     if (myFile) {
   57:       myFile.println("-------------------- SMS Start --------------------");
   58:       for (int i = 0; i < 128; i++) {myFile.print(sms[i]);}
   59:       myFile.println("");
   60:       myFile.println("-------------------- SMS End   --------------------");
   61:       // close the file:
   62:       myFile.close();
   63:     }
   64:   }
   65: }
   66:
   67: //Decrease remaining time for outlet to be switched ON
   68: void decreaseTimOnOutlet(){
   69:   boolean anyOn = false;
   70:
   71:   //Check if any outlet is turned on
   72:   for( int i = 0 ; i < 4 ; i ++){
   73:     if (powerOutlet[i] > 0){
   74:       anyOn = true;
   75:     }
   76:   }
   77:
   78:   //If any outlet switched on and engine not running start
   79:   if (anyOn && !engineRunning){
   80:         startEngine();
   81:   }
   82:   //If no outlet switched on and engine running stop
   83:   if (!anyOn && engineRunning){
   84:         stopEngine();
   85:   }
   86:   //Walk through outlets and update output and engine
   87:   digitalWrite(outlet_0, (powerOutlet[0] > 0));
   88:   digitalWrite(outlet_1, (powerOutlet[1] > 0));
   89:   digitalWrite(outlet_2, (powerOutlet[2] > 0));
   90:   digitalWrite(outlet_3, (powerOutlet[3] > 0));
   91:  
   92:   //Decrease remaining time
   93:   for( int i = 0 ; i < 4 ; i ++){
   94:     if (powerOutlet[i] > 0){
   95:       powerOutlet[i] --;
   96:     }
   97:   }
   98: }
   99:
  100: //Start engine
  101: void startEngine(){
  102:   if (!engineRunning){
  103:     Serial.println("Start engine...");
  104:     //Open for fule/ignition
  105:     //Trigger starter for a couple of seconds and the release
  106:     //Starter On...
  107:     digitalWrite(engineStart, HIGH);
  108:     delay(3000);
  109:     //Starter Off...
  110:     digitalWrite(engineStart, LOW);
  111:     //Update status
  112:     engineRunning = true;
  113:     //Delay before any further action
  114:     delay(1000);
  115:   }
  116: }
  117:
  118: //Stop engine
  119: void stopEngine(){
  120:   if (engineRunning){
  121:     Serial.println("Stop engine...");
  122:     //Cut fule/ignition
  123:     //Update status
  124:     engineRunning = false;
  125:   }
  126: }
  127:
  128: //Init phone for SMS
  129: void phoneInit(){
  130:   //Serial.println("Init phone...");
  131:   // set the data rate for the NewSoftSerial port
  132:   phone.begin(9600);
  133:   delay(500);
  134:   phone.println("AT+CMGF=1\r\n");  // Set phone mode to text (vs pdu)
  135:   delay(500);
  136:   phoneReadResponse();
  137:   Serial.println("Init phone, DONE!");
  138: }
  139:
  140: //Get response from serial and print to hardware serial
  141: void phoneReadResponse(){
  142:   //Serial.println("Read response...");
  143:   int i;
  144:     // Debug Loop to print response
  145:   for (i = 0; i < 128; i++) {sms[i]=' ';}
  146:   for (i = 0; i < 128; i++) {
  147:     if (phone.available() > 0) {
  148:       sms[i] = phone.read();
  149:       //Serial.print(i);Serial.println(sms[i]);
  150:     }
  151:     delay(10);
  152:   }
  153:   //Serial.println("Read response, DONE!");
  154: }
  155:
  156: //Get index for start of SMS content
  157: int phoneGetSMSContentIndex(){
  158:   //Serial.println(sms);
  159:   int numFound = 0;
  160:   int ii = 0;
  161:   while (numFound < 8){
  162:     if (sms[ii] == '"'){numFound++;}
  163:     ii++;
  164:   }
  165:   ii=ii+2;
  166:   return ii;
  167: }
  168:
  169: //Get index SMS from phone
  170: boolean phoneGetSMS(int index){
  171:   boolean stat = false;
  172:   int contentIndex = 0;
  173:   Serial.print("Get SMS...");Serial.println(index);
  174:   //Get SMS at index in device  
  175:   phone.print("AT+CMGR="); phone.print(index); phone.println("\r\n");
  176:   delay(500);
  177:   //Read response
  178:   phoneReadResponse();
  179:   writeFile();
  180:   //Serial.println("Get SMS, Done!");
  181:   //Determine if valid SMS
  182:   contentIndex = phoneGetSMSContentIndex();
  183:   if(sms[contentIndex] == 'A' || sms[contentIndex] == 'a' ||
  184:       sms[contentIndex] == 'B' || sms[contentIndex] == 'b' ||
  185:       sms[contentIndex] == 'C' || sms[contentIndex] == 'c' ||
  186:       sms[contentIndex] == 'D' || sms[contentIndex] == 'd'){stat = true;}
  187:   return stat;
  188: }
  189:
  190: //Erase index SMS from phone
  191: void phoneDeleteSMS(int index){
  192:   //Serial.print("Delete SMS...");Serial.println(index);
  193:   //Get SMS at index in device  
  194:   phone.print("AT+CMGD="); phone.print(index); phone.println("\r\n");
  195:   delay(500);
  196:   //Read response
  197:   phoneReadResponse();
  198:   //Serial.println("Delete SMS, Done!");
  199: }
  200:
  201: //Update Status if more SMS in queue
  202: boolean phoneSMSToHandle(){
  203:   boolean stat = false;
  204:   //Set to handle device SMS memory and reply status
  205:   phone.println("AT+CPMS=\"ME\"\r\n");
  206:   delay (500);
  207:   //Read response
  208:   phoneReadResponse();
  209:   //Determine if any to handle
  210:   if(sms[20] != '0'){stat = true;  }
  211:   return stat;
  212: }
  213:
  214: //Main program that cycles continously
  215: void loop(){
  216:   //Trigger timer
  217:   timer.run();
  218:  
  219:   //If there is SMSM in queue => Handle them
  220:   int index = 1;
  221:   while (phoneSMSToHandle() && index < 15){
  222:     //Serial.println("Process SMS...");
  223:     //Go through all SMS currently  in queue and remove sequently
  224:     if (phoneGetSMS(index)){
  225:       Serial.print("Processing SMS...");Serial.println(index);
  226:       //Get outlet data
  227:       outlet = sms[phoneGetSMSContentIndex()];
  228:       //Update outlet 1-4
  229:       if (outlet == 'A' || outlet == 'a') {powerOutlet[0] = powerOutlet[0] + powerOutletTime;}
  230:       if (outlet == 'B' || outlet == 'b') {powerOutlet[1] = powerOutlet[1] + powerOutletTime;}
  231:       if (outlet == 'C' || outlet == 'c') {powerOutlet[2] = powerOutlet[2] + powerOutletTime;}
  232:       if (outlet == 'D' || outlet == 'd') {powerOutlet[3] = powerOutlet[3] + powerOutletTime;}
  233:       Serial.println("Processing SMS, DONE!");
  234:     }
  235:     phoneDeleteSMS(index);
  236:     index++;
  237:   }
  238: }