INCLUDE rtcu.inc
VAR_INPUT
in : ARRAY[1..4] OF BOOL;
END_VAR;
VAR_OUTPUT
led : BOOL;
END_VAR;
VAR
AlarmState : BOOL := FALSE;
TrackState : BOOL := FALSE;
mxStateAl : MUTEX;
mxStateTr : MUTEX;
NextGps : DINT;
END_VAR;
FUNCTION SetAlarm;
VAR_INPUT
state : BOOL;
END_VAR;
mxLock(mx:=mxStateAl);
AlarmState := state;
mxUnlock(mx:=mxStateAl);
END_FUNCTION;
FUNCTION TestAlarm : BOOL;
mxLock(mx:=mxStateAl);
TestAlarm := AlarmState;
mxUnlock(mx:=mxStateAl);
END_FUNCTION;
FUNCTION SetTracking;
VAR_INPUT
state : BOOL;
END_VAR;
mxLock(mx:=mxStateTr);
TrackState := state;
mxUnlock(mx:=mxStateTr);
END_FUNCTION;
FUNCTION TestTracking : BOOL;
mxLock(mx:=mxStateTr);
TestTracking := TrackState;
mxUnlock(mx:=mxStateTr);
END_FUNCTION;
FUNCTION SetId;
VAR_INPUT
index : INT;
ID : STRING;
END_VAR;
VAR
oldIDs : STRING;
newIDs : STRING;
i : INT;
END_VAR;
IF index > 0 AND index < 6 THEN
oldIDs := LoadStringF(index:=3);
IF index > 1 THEN
FOR i := 1 TO index - 1 BY 1 DO
newIDs := strConcat(str1:=newIDs,str2:=strToken(str:=oldIDs,delimiter:=",",index:=i));
newIDs := strConcat(str1:=newIDs,str2:=",");
END_FOR;
END_IF;
newIDs := strConcat(str1:=newIDs,str2:=ID);
newIDs := strConcat(str1:=newIDs,str2:=",");
IF index < 5 THEN
FOR i := index + 1 TO 5 BY 1 DO
newIDs := strConcat(str1:=newIDs,str2:=strToken(str:=oldIDs,delimiter:=",",index:=i));
newIDs := strConcat(str1:=newIDs,str2:=",");
END_FOR;
END_IF;
SaveStringF(index:=3,str:=newIDs);
END_IF;
END_FUNCTION;
FUNCTION SendPos;
VAR_INPUT
phone : STRING;
END_VAR;
VAR
message : STRING;
END_VAR;
message := LoadStringF(index:=1);
gsmSendSMS(phonenumber:=phone,message:=message);
END_FUNCTION;
THREAD_BLOCK OWIBUTTON;
VAR
strID : STRING;
Valid : BOOL;
Count : INT := 0;
Blink : INT := 0;
END_VAR;
OWiButtonEnableLED(enable:=TRUE);
OWiButtonSetLED(state:=OFF);
WHILE TRUE DO
strID := OWiButtonGetID();
IF strLen(str:=strID) > 0 THEN
Count := 20;
Blink := 10;
IF NOT Valid THEN
IF strFind(str1:=LoadStringF(index:=3),str2:=strID) > 0 THEN
SetAlarm( state := NOT TestAlarm() );
END_IF;
OWiButtonSetLED(state:=ON);
Valid := TRUE;
END_IF;
ELSE
Valid := FALSE;
IF Count > 0 THEN
Count := Count - 1;
Sleep(delay:=100);
ELSIF TestAlarm() THEN
IF Blink = 0 THEN
Blink := 10;
OWiButtonSetLED(state:=ON);
ELSIF Blink = 9 THEN
OWiButtonSetLED(state:=OFF);
END_IF;
Blink := Blink - 1;
Sleep(delay:=100);
ELSE
OWiButtonSetLED(state:=OFF);
Sleep(delay:=100);
END_IF;
END_IF;
END_WHILE;
END_THREAD_BLOCK;
THREAD_BLOCK GPS;
VAR
gpspos : gpsFix;
str : STRING;
END_VAR;
gpsPower(power:=ON);
WHILE TRUE DO
gpspos();
IF gpspos.mode > 1 THEN
str := strFormat(format:="Time: \1.\2.\3, ",v1:=gpspos.day,v2:=gpspos.month,v3:=gpspos.year);
str := strConcat(str1:=str,str2:=strFormat(format:="\1:\2:\3 ",v1:=gpspos.hour,v2:=gpspos.minute,v3:=gpspos.second));
IF gpspos.latsouth THEN
str := strConcat(str1:=str,str2:=strFormat(format:="Lat: S\1*\2.\3 ",v1:=gpspos.latdeg,v2:=gpspos.latmin,v3:=gpspos.latdecmin));
ELSE
str := strConcat(str1:=str,str2:=strFormat(format:="Lat: N\1*\2.\3 ",v1:=gpspos.latdeg,v2:=gpspos.latmin,v3:=gpspos.latdecmin));
END_IF;
IF gpspos.lonwest THEN
str := strConcat(str1:=str,str2:=strFormat(format:="Long: W\1*\2.\3 ",v1:=gpspos.londeg,v2:=gpspos.lonmin,v3:=gpspos.londecmin));
ELSE
str := strConcat(str1:=str,str2:=strFormat(format:="Long: E\1*\2.\3 ",v1:=gpspos.londeg,v2:=gpspos.lonmin,v3:=gpspos.londecmin));
END_IF;
SaveStringF(index:=1,str:=str);
END_IF;
Sleep(delay:=1000);
END_WHILE;
END_THREAD_BLOCK;
THREAD_BLOCK SMS;
VAR
incoming : gsmIncomingSMS;
END_VAR;
gsmPower(power:=ON);
DebugMsg(message:="Ready to receive SMS");
WHILE TRUE DO
incoming();
led := gsmConnected();
IF incoming.status > 0 THEN
DebugMsg(message:=strConcat(str1:="SMS recieved => ",str2:=strConcat(str1:=incoming.phonenumber,str2:=strConcat(str1:=",",str2:=incoming.message))));
IF strCompare(str1:="OWI",str2:=strLeft(str:=incoming.message,length:=3)) = 0 THEN
SetId( index := strToInt(str:=strMid(str:=incoming.message,start:=4,length:=1)), ID := strMid(str:=incoming.message,start:=6) );
gsmSendSMS(phonenumber:=incoming.phonenumber,message:=strConcat(str1:=strMid(str:=incoming.message,start:=4,length:=1),
str2:=strMid(str:=incoming.message,start:=6)));
ELSIF strCompare(str1:="AON",str2:=incoming.message) = 0 THEN
SetAlarm(state:=ON);
gsmSendSMS(phonenumber:=incoming.phonenumber,message:="Alarm=ON");
ELSIF strCompare(str1:="AOFF",str2:=incoming.message) = 0 THEN
SetAlarm(state:=OFF);
gsmSendSMS(phonenumber:=incoming.phonenumber,message:="Alarm=OFF");
ELSIF strCompare(str1:="TON",str2:=incoming.message) = 0 THEN
NextGps := clockNow();
SetTracking(state:=ON);
gsmSendSMS(phonenumber:=incoming.phonenumber,message:="Tracking=ON");
ELSIF strCompare(str1:="TOFF",str2:=incoming.message) = 0 THEN
SetTracking(state:=OFF);
gsmSendSMS(phonenumber:=incoming.phonenumber,message:="Tracking=OFF");
ELSIF strCompare(str1:="POS",str2:=incoming.message) = 0 THEN
SendPos(phone:=incoming.phonenumber);
ELSIF strCompare(str1:="PHONE",str2:=strLeft(str:=incoming.message,length:=5)) = 0 THEN
SaveStringF(index:=4,str:=strMid(str:=incoming.message,start:=6));
gsmSendSMS(phonenumber:=incoming.phonenumber,message:=strConcat(str1:="Phone=",str2:=strMid(str:=incoming.message,start:=6)));
END_IF;
END_IF;
Sleep(delay:=2000);
END_WHILE;
END_THREAD_BLOCK;
PROGRAM thread_example;
VAR
thIbutton : OWIBUTTON;
thGps : GPS;
thSms : SMS;
Valid : BOOL;
END_VAR;
DebugMsg(message:="Initializing Mutex");
mxStateAl := mxInit();
mxStateTr := mxInit();
IF mxStatus(mx:=mxStateAl) = 1 THEN DebugMsg(message:="mxStateAl failed to init!"); END_IF;
IF mxStatus(mx:=mxStateTr) = 1 THEN DebugMsg(message:="mxStateTr failed to init!"); END_IF;
DebugMsg(message:="Starting Threads");
thIbutton();
thGps();
thSms();
IF NOT thIbutton._running THEN DebugMsg(message:="thIbutton failed to start!"); END_IF;
IF NOT thGps._running THEN DebugMsg(message:="thGps failed to start!"); END_IF;
IF NOT thSms._running THEN DebugMsg(message:="thSms failed to start!"); END_IF;
BEGIN
IF TestAlarm() THEN
IF (in[1] OR in[2] OR in[3] OR in[4]) AND NOT Valid THEN
gsmSendSMS(phonenumber:=LoadStringF(index:=4),message:="ALARM!");
Valid := TRUE;
END_IF;
ELSE
Valid := FALSE;
END_IF;
IF TestTracking() THEN
IF clockNow() >= NextGps THEN
SendPos(phone:=LoadStringF(index:=4));
NextGps := clockNow() + 60;
END_IF;
END_IF;
END;
END_PROGRAM;
|