samomni
Newbie
Offline
Posts: 7
Thank You
-Given: 5
-Receive: 2
|
|
« Reply #13 on: April 07, 2008, 09:50:35 09:50 » |
|
Sony erricsson T230 is easy to use it support AT+CMGF=1(text mode instead of PDU) which is easier to program. Just make sure your microcontroller is powered from the connector of the cellphone or use a level converter because the serial connection is below ttl level (3.3V approximately ) just use a rs232 converter and back.
Posted on: April 07, 2008, 10:19:38 10:19 - Automerged
If there is anyone who needs an idea how to control a cellphone here is the code maybe some of you will find this quite long "MY APOLOGIES", I hope this can help even though the coding is not that good i've tested it using MikroPASCAL and a T230 cellphone.
program sms_switch; uses irq_usart,char_lib,eeprom_lib; const strln : byte = 128; RtrnLf : Array[3] Of char = (13,10,0); var alrm_active : boolean; cor,error,alrm : byte; alrm_status : byte; mphone : string[12]; port_switch : string[5]; mem : string[3]; TempBuff : string[strln]; //-------------------------main interrupt--------------------------------------- procedure interrupt; begin usart_interrupt; end; //------------------------------------------------------------------------------ procedure vdelay; begin vdelay_ms(2000); end; //--------------------------Modem Initialize------------------------------------ procedure wait_ok; var wait : byte; begin wait := 0; set_delimiter('OK'); repeat begin if wait = 10 then break else inc(wait); delay_ms(1000); end; until message_received = true;
if message_received = true then begin inc(cor); clear_rxbuf; lcd_out(2,10,'OK'); end else begin inc(error); lcd_out(2,10,'ERROR'); end; end; //------------------------------------------------------------------------------ function modem_init : boolean; begin cor := 0; error := 0; repeat begin lcd_cmd(lcd_clear); lcd_out(1,1,'ATE=0'); writeln('ATE=0'); wait_ok; lcd_out(1,1,'AT '); writeln('AT'); wait_ok; lcd_out(1,1,'AT+CMGF'); writeln('AT+CMGF=1'); wait_ok; lcd_out(1,1,'AT+CNMI'); writeln('AT+CNMI=2,1,0,0,0'); wait_ok; lcd_out(1,1,'AT+CPMS'); writeln('AT+CPMS="ME","ME","ME"'); wait_ok; end; until ((cor >= 5) or (error = 15)); if cor >=5 then result := true else result := false; end; //------------------------SMS Master Phone-------------------------------------- procedure text_mphone; begin clear_rxbuf; delay_ms(100); usart_write_text('AT+CMGS='); usart_write(34); // char " usart_write_text(mphone); usart_write(34); usart_write(13); usart_write(10); end; //----------------------------wait " > "---------------------------------------- function wait_3E : boolean; var wait : byte; begin wait := 0; set_delimiter('>'); repeat begin if wait = 6 then break else inc(wait); delay_ms(1000); end; until message_received = true; if message_received = true then begin inc(cor); result := true; end else begin inc(error); result := false; end; end; //------------------------------------------------------------------------------ procedure SUCCESS; begin lcd_cmd(lcd_clear); lcd_out(1,1,'Message Send '); lcd_out(2,3,'SUCCESS!'); smile(2,12); vdelay; end; //------------------------------------------------------------------------------ procedure FAILED; begin lcd_cmd(lcd_clear); lcd_out(1,1,'Message Send '); lcd_out(2,3,'FAILED!'); frown(2,12); vdelay; end; //------------------------------------------------------------------------------ procedure text_message(var text_msg : string[20]); begin text_mphone; error := 0; //clear OK error if wait_3E = true then begin usart_write_text(text_msg); usart_write(26); delay_ms(100); usart_write(13); usart_write(10); clear_rxbuf; wait_ok; end else inc(error); if error > 0 then FAILED else SUCCESS; end; //--------------------Phone Set ------------------------------------------------ procedure set_phone; // save phone number on eeprom var ii,ix : byte; begin // eg., SET NO *09165450917 ix := 0; ii := strchr(TempBuff,'*'); if ii <> $FF then // asterisk has been found begin ii := ii+1; //skip " , " char repeat begin mphone[ix]:= TempBuff[ii]; if ((mphone[ix] = 0) OR (ix = 11)) then break; inc(ix); inc(ii); end; until false; mphone[ix] := 0; // string terminated end; Eeprom_Write_Text($00, mphone); //mphone --> master phone Eeprom_Read_Text($00, mphone); serial_init; text_message('Number Registered'); end; //-----------------Check if there is a stored cellphone number in eeprom memory. function check_mphone : boolean; var ix,chk : byte; begin ix := 0; chk := 0;
Eeprom_Read_Text($00, mphone); if ((mphone[0] = 0) or (mphone[0] = $FF))then begin mphone := 'Error'; Eeprom_Write_Text($00, mphone); end; // 0 to 10 while ix <> 10 do // cellphone no. format eg., "09165450917" 11 digits begin //char '0' char '9' if ((mphone[ix] >= '0') and (mphone[ix] <= '9')) then begin inc(chk); inc(ix); end else begin lcd_cmd(lcd_clear); lcd_out(1,1,mphone); lcd_out(2,4,'Stored No.'); // Invalid Cell number; vdelay; break; end; end; lcd_out(1,1,mphone); if chk = 10 then result := true else result := false; mphone[11] := 0;
end; //--------------------------SEARCH STRING--------------------------------------- function search_string(var src : string[20]): boolean; var indx,ctr1,indx_rd : byte; dest : string[20]; begin // target string indx := 0; indx_rd := 0; ctr1 := 0; while src[indx] <> 0 do begin dest[indx] := src[indx]; inc(indx); end; // end target string
while true do // search string begin if TempBuff[ctr1] = dest[indx_rd] then inc(indx_rd) else indx_rd :=0; inc(ctr1); if ((indx_rd = indx) or (ctr1 = RxBufsize)) then break; end; if indx_rd = indx then result := true else result := false; end; //-----------------------------CMTI--------------------------------------------- procedure CMTI; var ii,ix : byte; begin ix := 0; ii := strchr(TempBuff,','); if ii <> $FF then begin ii := ii+1; //skip " , " char while true do begin mem[ix]:= TempBuff[ii]; if ((mem[ix] < '0') OR (mem[ix] > '9')) then break; inc(ix); inc(ii); end; mem[ix] := 0; end; memset(@TempBuff,0,strln); lcd_cmd(lcd_clear); lcd_out(1,1,'Read Memory '); lcd_out(1,13,mem); usart_write_text('AT+CMGR='); writeln(mem);
end; //------------------------------------------------------------------------------ procedure check_port; var nn : byte; begin //--------------------check POWER port status----------------------------- nn:=0; repeat // active low circuit = logic 1 input means ap is off begin if porta.nn = 1 then port_switch[nn] := '0' else port_switch[nn] := '1'; inc(nn); end; until nn = 4; port_switch[4]:= 0; // make sure string is terminated end; //-----------------------PORT DISPLAY------------------------------------------- procedure port_display; begin check_port; lcd_cmd(lcd_clear); lcd_out(1,1,'PORT Status'); lcd_out(2,1,'Switch = '); lcd_out(2,10,port_switch); end; //------------------------------------------------------------------------------ procedure port_status; begin check_port; error := 0; //clear OK error text_mphone; if wait_3E = true then begin usart_write_text('Status = '); usart_write_text(port_switch); usart_write(26); usart_write(13); clear_rxbuf; wait_ok; end else inc(error); if error > 0 then FAILED else SUCCESS; memset(@TempBuff,0,strln); set_delimiter(RtrnLf); // make sure return the delimiter to ent (13,10,0) port_display; end; //----------------------------POWER--------------------------------------------- procedure POWER; // example POWER ON*2 -- turn on port 3 var aa,bb,cc : byte; // POWER OFF*2 -- turn off port 3 pos : string[3]; // begin //---------------------------------------------------------------------- cc := $FF; aa := 0; bb := strchr(TempBuff,'*'); if bb <> $FF then begin bb := bb+1; //skip " * " char repeat begin pos[aa]:= TempBuff[bb]; if ((pos[aa] = $0D) OR (aa = RxBufsize)) then break; inc(aa); inc(bb); end; until false; pos[aa] := 0; end; cc := StrToword(pos); //---------------------------------------------------------------------- if search_string('ON') = true then begin if ((cc > 4) or (port_switch[cc] = '1')) then nop else begin lcd_cmd(lcd_clear); lcd_out(1,1,'Power ON Port'); lcd_out(2,1,pos); portc.cc := 1; //delay_ms(2000); //portc.cc := 0; end; end; //---------------------------------------------------------------------- if search_string('OFF') = true then begin if ((cc > 4) or (port_switch[cc] = '0')) then nop else begin lcd_cmd(lcd_clear); lcd_out(1,1,'Power OFF Port'); lcd_out(2,1,pos); portc.cc := 0; //delay_ms(2000); //portc.cc := 0; end; end; end;
//------------------------------------------------------------------------------ procedure alarm_set; begin lcd_cmd(lcd_clear); if search_string('OFF') = true then begin text_message('Alarm Disarmed!!!'); portc.0 := 0; alrm_status := 0; alrm_active := false; lcd_out(1,1,'Alarm Disarmed!!!'); vdelay; end; if search_string('ON') = true then begin alrm_active := true; text_message('Alarm ARMED!!!'); lcd_out(1,1,'Alarm ARMED!!!'); vdelay; end; end; //----------------------------CMGR---------------------------------------------- procedure CMGR; begin
if search_string('ALARM SET') = true then alarm_set; if search_string('POWER') = true then begin POWER; port_status; end; if search_string('STATUS') = true then port_status; if search_string('SET NO') = true then set_phone; if search_string('CMGR') = true then begin usart_write_text('AT+CMGD='); writeln(mem); end; port_display; clear_rxbuf; memset(@TempBuff,0,strln); set_delimiter(RtrnLf); end; //---------------------------ALARM---------------------------------------------- procedure ALARM; begin usart_write_text('ATD'); // miss call usart_write_text(mphone); wait_ok; text_mphone; if wait_3E = true then begin usart_write_text('ALARM Triggered '); usart_write(26); usart_write(13); clear_rxbuf; wait_ok; end else inc(error); if error > 0 then FAILED else SUCCESS; portc.0 := 1; //alarm ON lcd_cmd(lcd_clear); lcd_out(1,4,'WARNING!!!'); lcd_out(2,1,'Alarm Triggered'); vdelay; inc(alrm_status); set_delimiter(RtrnLf); end; //=======================MAIN ROUTINE=========================================== Begin adcon1 := %00000111; trisa := %1111111; trisb := %00000000; trisc := %11000000; PORTA := 0; PORTB := 0; PORTC := 0; port_switch := '0000'; // clear all POWER port status alrm_active := false; alrm_status := 0; lcd_Init(PORTB); //Initialize LCD lcd_cmd(lcd_clear); lcd_out(1,1,'SMS ALARM and '); lcd_out(2,1,'Wireless Switch'); vdelay; lcd_out(1,1,'System Starting'); lcd_out(2,1,'Initializing SMS'); vdelay; serial_init;
if modem_init = true then //Initialize cellphone-modem begin lcd_cmd(lcd_clear); lcd_out(1,1,'Modem OK'); vdelay; if check_mphone = true then begin serial_init; //Re-initialize serial communications, off during eeprom r/w text_message('System Ready'); // Send SMS System Ready end; end else begin lcd_cmd(lcd_clear); lcd_out(2,1,'Modem Error'); vdelay; reset; end;
port_display; serial_init; set_delimiter(RtrnLf);
while true do begin if message_received = true then begin transfer_message(TempBuff); if search_string('CMTI') then begin set_delimiter('OK'); CMTI; end; if search_string('CMGR') then CMGR; end; if ((porta.0 = 0) AND (alrm_active = true) AND (alrm_status < 3 )) then ALARM; end; end.
|