BASIC Code
Sartronics.com's IoT manager application has a simple on board BASIC interpreter to allow user code to be run. The code can drive control actuators given the state of other controls in a programmatic way. Once the code is entered or edited it is first parsed before being executed. If there are syntax errors than they will be prompted and the code will not operate. The code is case insensitive.
Examples of BASIC code is supplied at the end.
Comments
Comments always start with the quote character and continue to the end of the line.
Labels
Labels start at the beginning of a line. They are always followed by the colon ':' character.
Variables
The BASIC interpreter allows variables. Each variable can be assigned a constant numeric value or assigned the value of another variable or the result of a computation.
Example
foo = 123 ' Assigns the variable foo with the constant value of 123.
bar = foo ' Assigns the variable bar with the same value that foo has.
v1 = 2*foo + bar ' Assigned variable v1 with 2 times foo plus bar.
Statement Separator
To allow multiple statements on a single line the semicolon ';' character is used. This is particularly useful for grouping conditional statements, without the need for excessive GOTO statements.
Operators
The following table list the BASIC operators that are supported. The order of precedence is the same as standard mathematical symbols.
Operator |
Name |
Description |
= |
Assignment |
Assigns variables a new value |
+ |
Addition |
Adds 2 variables together |
- |
Subtraction |
Subtracts 2 variable |
* |
Multiplication |
Multiplies 2 variables. Note that it high precedence that addition and subtraction. |
/ |
Division |
Divides 2 variables. Note that it high precedence that addition and subtraction. |
> |
Greater than |
Used with 'IF' keyword, conditional executed if left side is greater than the right |
< |
Less than |
Used with 'IF' keyword, conditional executed if left side is less than the right |
>= |
Greater than or equal |
Used with 'IF' keyword, conditional executed if left side is greater than or equal to the right |
<= |
Less than or equal |
Used with 'IF' keyword, conditional executed if left side is less than or equal to the right |
<> |
Not equal |
Used with 'IF' keyword, conditional executed if left side is not equal to the right |
= |
Equal to |
Used with 'IF' keyword, conditional executed if left side is equal to the right |
Language KEY Words
The following table list the BASIC language key words that are supported.
Key Word |
Parameters |
Description |
GOTO |
1 numeric |
GOTO branches the program flow to another point in the code. It needs a constant numeric parameter that is the label of the line of code to jump to. |
IF |
1 conditional |
The IF keyword is used to conditional control the flow of the program. If the condition is true then the statement on the rest of the line is executed. The IF must be followed by a conditional expression.which is then followed by a THEN or a ';'. If the condition evaluates to true the THEN will execute. If THEN is followed by a constant number it is the line number that the program will jump to.other wise it will execute the statement following it. If multiple statements are on the same line with ':' separators they will also be executed. If the condition was false it program continues on the line below. |
FOR |
1 |
The FOR keyword is used for looping over the same lines of code repeated. A variable is used for counting. It can optionally be assigned an initial value. The code is executed until the counting variable is equal to the final constant value. The keyword TO is used to indicate the final constant value. It can be supplied from a variable, but final value remains the same even it this variable changes. The counting value is incremented by 1 or optionally by a supplied constant value. This value uses the key word STEP. |
TO |
1 |
Only used in FOR statement to indicate the final value in the loop |
STEP |
1 |
Only used in FOR statement if the increment size is not 1 |
NEXT |
1 |
When a NEXT keyword is encountered it branches to the last for loop that used the same variable as its counter. |
GOSUB |
1 |
GOSUB branches the program flow to another point in the code. It needs a constant numeric parameter that is the label of the line of code to jump to. When the END keyword is reached the program flow will return from where it was called. |
RETURN |
1 |
Returns from the subroutine back to continue after the GOSUB call. Executing a RETURN instruction without having been called from a GOSUB will end the program flow with an error. The return can optionally return a single number. |
END |
0 |
This will exit the BASIC program, however as this program always runs, it will always restart from the beginning after it ends.This is normally used to mark the end of the main routine and have the subroutines following. If there are no subroutines then the code can simply cease, an END statement is not required. |
|
... |
Print a message to the application's log. The statement is terminated at the end of the line or if a ':' is reached |
MID |
3 |
Copies the section of a string. The first parameter is the source string. The second parameter is the start count. The third parameter is the length. |
LEN |
1 |
This is the length of a string. |
Special Functions
The following table list the special functions that are supported.
Key Word |
Parameters |
Description |
Sleep |
1 |
Stops processing for a set period of time. The parameter specifies the time in milliseconds. Note however the resolution is only to 1/10 of a second. |
SetTimer |
1 |
Returns a numeric timer value for the TimedOut instruction The parameter is the number of seconds for the timer. |
TimedOut |
1 |
Returns 1 if timed out else 0. The first parameter is the returned value of the SetTimer instruction. |
TimeHour |
0 |
Returns hour of the day. |
TimeMin |
0 |
Returns minutes past the hour. |
TimeSec |
0 |
Returns seconds past the minute. |
TimeDay |
0 |
Returns 1 for Mondays to 7 for Sunday |
IsActive |
1 |
Returns 1 if control is currently active else returns 0. The parameter is a string type unique control name. |
IsInactive |
1 |
Returns 1 if control is currently inactive else returns 0. The parameter is a string type unique control name. |
Value |
1 |
Returns the level reading of the control. The parameter is a string type unique control name.. |
SetValue |
2 |
Sets control level. The first parameter is a string type unique control name. The next parameter is the level to set. |
SetOn |
1 |
Sets control output on. The parameter is a string type unique control name.. |
SetOff |
1 |
Sets control output off. The parameter is a string type unique control name.. |
TimedOn |
2 |
Sets control output on for a set duration. The first parameter is a string type unique control name.. The next parameter is the duration in seconds. |
TimedOff |
2 |
Sets control output off for a set duration. The first parameter is a string type unique control name.. The next parameter is the duration in seconds. |
SetGroupOn |
1 |
Sets control output on. The parameter is the name of a control group classification. |
SetGroupOff |
1 |
Sets control output off. The parameter is the name of a control group classification. |
TimedGroupOn |
2 |
Sets control output on for a set duration. The first parameter is a name of the control group classification. The next parameter is the duration in seconds. |
TimedGroupOff |
2 |
Sets control output off for a set duration. The first parameter is the name of a control group classification. The next parameter is the duration in seconds. |
DevCnt |
0 |
Returns the number of devices configured. |
DevCtrlCnt |
1 |
Returns the number of controls configured on a device. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices. |
IsDevCtrlActive |
2 |
Returns 1 if device control is currently active else returns 0. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices. The second parameter is the Control name or number 1 for the first up to how many are configured. |
IsDevCtrlInactive |
2 |
Returns 1 if device control is currently inactive else returns 0. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices. The second parameter is the Control name or number 1 for the first up to how many are configured. |
DevCtrlValue |
2 |
Returns the value of device control. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices. The second parameter is the Control name or number 1 for the first up to how many are configured. |
SetDevCtrlValue |
3 |
Sets the value of device control. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices. The second parameter is the Control name or number 1 for the first up to how many are configured. The third parameter is the value level. |
SetDevCtrlOn |
2 |
Sets control output on. The first parameter is the name or number of the device. The second parameter is the Control name or number. |
SetDevCtrlOff |
2 |
Sets control output off. The first parameter is the name or number of the device. The second parameter is the Control name or number. |
TimedDevCtrlOn |
3 |
Sets control output on for a set duration. The first parameter is the name or number of the device. The second parameter is the Control name or number. The next parameter is the duration in seconds. |
TimedDevCtrlOff |
3 |
Sets control output off for a set duration. The first parameter is the name or number of the device. The second parameter is the Control name or number. The next parameter is the duration in seconds. |
DevCtrlType |
2 |
Returns the configured device control type. The parameter is the name of the device or device's number in order from 1 to the maximum number of devices.The second parameter is the Control name or number. The return value is one of 0-Unknown, 1-BinaryInput, 2-AnalogInput, 3-OnOffOutput, 4-RelayOutput, 5-LevelOutput, 6-RangeInput, 7-RangeOutput. |
Sample code
The following show what Sartronics.com's IoT BASIC interpreter looks like. It is not a practical example of code that would control and monitor IoT devices but does show what BASIC code looks like The print statement produce a message that appears in the IoT manager application log.
PRINT "Hello world!"
PRINT "Lets calculate some areas"
FOR i=2 TO 6 STEP 2
FOR j=3 TO 4
PRINT " Area of \" i \"x\" j \" is \" i*j
NEXT j
NEXT i
Example 1
' This is a code example for Sartronics.com's IoT BASIC interpreter.
' Control named "Relay 1" is turned on only when controls name "Input 2" and "Input 3" are active
' It will only turn back off when both "Input 2" and "Input 3" are reset
10:
inp2Act = IsActive "Input 2"
inp3Act = IsActive "Input 3"
rlyOn = IsActive "Relay 1"
IF rlyOn = 1 THEN 30 ' If relay is on jump to line 30
' Relay is off, set it on if both inputs are active
IF inp2Act = 0 THEN 20 ' If input 2 is inactive jump to line 20
IF inp3Act = 0 THEN 20 ' If input 3 is inactive jump to line 20
SetOn "Relay 1" ' Both input 2 and 3 must be active so set relay output on
20:
SLEEP 1000 ' Can rest for a short while in milliseconds
GOTO 10 ' Check it all again
30:
' Relay is, on set it off if both inputs are inactive
IF inp2Act = 1 THEN 40 ' If input 2 is active jump to line 40
IF inp3Act = 1 THEN 40 ' If input 3 is active jump to line 40
SetOff "Relay 1" ' Both input 2 and 3 must be inactive to reset relay output off
40:
SLEEP 1000 ' Can rest for a short while in milliseconds
End ' Reached the end, it will restart again from the beginning
Example 2
' This is a code example for Sartronics.com's IoT BASIC interpreter.
' Siren is turned on for 2 minutes only when at least 2 inputs are active.
' It will not sound again for 10 minutes.
tmr1 = 0 ' Use this as a timer, set it to zero so it will already be timed out
inpCnt = DevCtrlCnt "Inputs"
10:
IF (TimedOut tmr1) <> 1 THEN SLEEP 1000 ; GOTO 10' Wait for timer
cnt = 0 ' Clear active count
FOR i=1 TO inpCnt STEP 1 ' Loop through all device's controls
on = IsDevCtrlActive "Inputs" i ' on is set to 1 if control input i is in the active state
IF on==1 THEN cnt = cnt + 1 ' Count number of active controls
NEXT i
IF cnt<2 THEN 20
TimedOn "Siren" 120 ' Turn on siren for 2 minute
tmr1 = SetTimer 600 ' Start timeout now for 10 minutes
20:
SLEEP 1000 ' Can rest for a short while in milliseconds
GOTO 10 ' Do it all again
Example 3
' This is a code example for Sartronics.com's IoT BASIC interpreter.
' Use a cancel button acknowledged s temperature alarm.
' Whenever an input has reached a warning level it needs a cancel button to be pressed within 2 minutes or relay output siren will be set on.
10:
level = Value "Temperature"
IF level>100 THEN 30 ' If an input has reached a maximum level start a 2 minute timer before sounding a alarm
Sleep 1000 ' Can rest for a short while in milliseconds
GOTO 10 ' Do it all again
30:
PRINT "Warning temperature level has reached " level
SetOn "Warning light" ' Turn the warning light output on
tmr = SetTimer 120 ' Start timeout now for 2 minutes
32:
' Wait here for 2 minutes checking in case the cancel button is pressed
IF (GOSUB 100)=1 THEN 70 ' If canceled pressed then wait for the temperature to restore
IF (TimedOut tmr)=1 THEN 50 ' If timed out go and set the siren
Sleep 1000 ' Can rest for a short while in milliseconds
GOTO 32 ' Check again it all again
50: ' Start siren and wait for a cancel button press
TimedOn "Siren" 600 ' Turn the output on for 10 minutes
52: ' Wait here while the siren is on
IF (GOSUB 100)=1 THEN 70 ' If canceled pressed then wait for the temperature to restore
IF (IsInactive "Siren")=1 THEN 10 ' If the siren has stopped then go back to checking inputs
Sleep 1000 ' Can rest for a short while in milliseconds
GOTO 52 ' Check again it all again
70: ' Wait here for the temperature to restore
Sleep 1000 ' Can rest for a short while in milliseconds
IF (Value "Temperature")>95 THEN 70 ' Check if temperature is lower
GOTO 10 ' Temperature has lowered so back to checking for high temperature again
100: ' Check if cancel button is pressed
PRINT "Warning Acknowledged"
IF (IsInactive "Cancel button")=1 THEN RETURN 0 ' Return 0 to indicate canceled button was not pressed
SetOff "Siren" ' Cancel button is pressed so disable siren output
SetOff "Warning light"
IF (IsActive "Cancel button")<>1 THEN 108 ' Wait for cancel button to be released
Sleep 1000 ' Can rest for a short while in milliseconds
108
RETURN 1 ' Return 1 to indicate canceled button was pressed