INCLUDE rtcu.inc
VAR_INPUT
DIn : ARRAY[1..16] OF BOOL;
END_VAR;
VAR_OUTPUT
DOut : ARRAY[1..16] OF BOOL;
gsmOK: BOOL;
END_VAR;
PROGRAM Remote_IO;
VAR
command : STRING;
position : INT;
extract : strGetValues;
str : STRING;
statestr : STRING;
index : SINT;
DIn_old : ARRAY[1..16] OF BOOL;
sms : gsmIncomingSMS;
pdu : gsmIncomingPDU;
pdubuf : ARRAY[1..140] OF SINT;
gsm_linsec : DINT;
END_VAR;
gsmPower(power:=TRUE);
WHILE NOT gsmConnected() DO
Sleep(delay:=500);
DebugMsg(message:="Waiting for connect to GSM network...");
gsmOK:=NOT gsmOK;
UPDATEOUT;
END_WHILE;
gsm_linsec:=clockNow() + (24*60*60);
pdu(message:=ADDR(PDUbuf));
BEGIN
sms();
pdu();
IF pdu.status>0 THEN
DebugFmt(message:="PDU received. \1",v1:=pdu.length);
DebugFmt(message:=pdu.phonenumber);
END_IF;
str:="";
FOR index:=1 TO 16 DO
IF Din[index] <> Din_old[index] THEN
statestr:=strFormat(format:="S\1=\2;", v1:=index, v2:=SINT(Din[index]));
str:=strConcat(str1:=str, str2:=statestr);
Din_Old[index]:=Din[index];
END_IF;
END_FOR;
IF strLen(str:=str)>0 THEN
statestr:=LoadString(index:=1);
IF strLen(str:=statestr)=0 THEN
DebugMsg(message:="Phonenumber is empty, sending NO state change");
ELSE
DebugMsg(message:="Sending new state change:");
DebugMsg(message:=str);
gsmSendSMS(phonenumber:=statestr, message:=str);
END_IF;
END_IF;
IF sms.status > 0 THEN
position:=1;
WHILE TRUE DO
command:=strToken(str:=sms.message, delimiter:=";",index:=position);
position:=position+1;
DebugMsg(message:=command);
IF strLen(str:=command)=0 THEN
DebugMsg(message:="No more commands...");
EXIT;
END_IF;
IF strFind(str1:=command, str2:="S")=1 AND
(strFind(str1:=command, str2:="=")=3 OR strFind(str1:=command, str2:="=")=4) THEN
extract(str:=command, format:="S\1=\2");
IF extract.match THEN
IF extract.v1 >= 1 AND extract.v1 <= 16 THEN
DOut[extract.v1]:=BOOL(extract.v2);
END_IF;
END_IF;
ELSIF strFind(str1:=command, str2:="phone=")>0 THEN
SaveString(index:=1, str:=strMid(str:=command, start:=7));
str:=strConcat(str1:="New receiver set, number=", str2:=LoadString(index:=1));
gsmSendSMS(phonenumber:=sms.phonenumber, message:=str);
END_IF;
END_WHILE;
END_IF;
IF clockNow() > gsm_linsec THEN
DebugMsg(message:="GSM Module turned OFF");
gsmPower(power:=OFF);
DebugMsg(message:="GSM Module turned ON");
gsmPower(power:=ON);
gsm_linsec:=clockNow() + (24*60*60);
WHILE NOT gsmConnected() DO
Sleep(delay:=500);
DebugMsg(message:="Waiting for connect to GSM network...");
END_WHILE;
DebugMsg(message:="Now connected to GSM network");
END_IF;
gsmOK:=gsmConnected();
END;
END_PROGRAM;
|