BASIC IDE Issues

I have a Basic IDE script running, including the report generation and MQTT script for talking to IOT Hub from AZURE, ever since I put in the MQTT Script, the report generating script is not working correcty.
A copy of my Basic IDE

TSET 1, 60
ONTIMER 1, “@CALC_VARS()”

FUNCTION CALC_VARS()
SCFM@ = FT2000@ * ((PT2027@ + 14.7)/14.7)*(528/(TE2032@+460))
ENDFN

TSET 3, 3600
ONTIMER 3, “@SETNAME()”
Function SETNAME()
name@= “Fleet 1- Orion”
ENDFN

//################" CONFIGURATION #################
DeviceId$=“FLT1_Orion”
IotHubName$ =“DPS-Turbines”
Changepushtime% = 10 //Timer to push only Tags that has changed
Fullpushtime% = 10// Timer to push all values
//Select the Tag Group to publish → 0 or 1
//Tag must be created and at least set in one of the groups.
GROUPA% = 1
GROUPB% = 1
GROUPC% = 1
GROUPD% = 1
// /usr directory operations to do :
// 1. Generate a Self-Signed certificate using
// openssl req -x509 -sha256 -nodes -days 3650 -newkey rsa:2048 -keyout ReplaceByDeviceID.key -out ReplaceByDeviceID.crt -config openssl.cnf
// 2. Rename the cert and the key with the DeviceID (.crt and .key)
// 3. Upload the self-signed cert + the key + the baltimoreCA certificate to the /usr
// 4. Start the script → You should see some “PUBLISH…” logs in the console.
// 5. Do not forget to select Run > Autorun in order to have the script running at boot
//################“END CONFIGURATION ##############
CLS
//Read number of Tags
NB%= GETSYS PRG,“NBTAGS”
DIM a(NB%,2)
MQTT “Open”,DeviceId$,IotHubName$ + “.azure-devices.net”
Mqtt “SetParam”,“Port”,“8883”
MQTT “setparam”, “log”, “1”
MQTT “setparam”, “keepalive”, “20”
MQTT “setparam”, “TLSVERSION”, “tlsv1.2”
MQTT “setparam”, “PROTOCOLVERSION”, “3.1.1”
MQTT “setparam”, “cafile”,”/usr/BaltimoreCyberTrustRoot.pem"
MQTT “setparam”, “CertFile”,"/usr/"+DeviceId$+".crt"
MQTT “setparam”, “KeyFile”,"/usr/"+DeviceId$+".key"
Mqtt “SetParam”,“Username”,IotHubName$+ “.azure-devices.net/”+DeviceId$+"/api-version=2016-11-14"
Mqtt “SetParam”,“Password”,“HostName=”+IotHubName$+";DeviceID="+DeviceId$+";x509=true"
SETSYS PRG,“RESUMENEXT”,1 //Continue in case of error at MQTT “CONNECT”
Mqtt “Connect”
ErrorReturned% = GETSYS PRG,“LSTERR”
IF ErrorReturned% = 28 THEN @Log("[MQTT SCRIPT] WAN interface not yet ready")
SETSYS PRG,“RESUMENEXT”,0
ONMQTT “GOTO MqttRx”
//a = table with 2 columns : one with the negative indice of the tag and the second one with 1 if the values of the tag change or 0 otherwise
IsConnected:
//Record the Tag ONCHANGE events into an array.
//Allows to post only values that have changed
FOR i% = 0 TO NB%-1
k%=i%+1
SETSYS Tag, “load”,-i%
a(k%,1)=-i%
a(k%,2) = 0
GroupA$= GETSYS TAG,“IVGROUPA”
GroupB$= GETSYS TAG,“IVGROUPB”
GroupC$= GETSYS TAG,“IVGROUPC”
GroupD$= GETSYS TAG,“IVGROUPD”

IF GroupA$ = “1” And GROUPA%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupB$ = “1” And GROUPB%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupC$ = “1” And GROUPC%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupD$ = “1” And GROUPD%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
NEXT i%

ONTIMER 2,“goto MqttPublishAllValue”
TSET 2,Fullpushtime%
// TSET 2,Changepushtime%
END
//Compute the right time format for AZURE
Function GetTime$()
$a$ = Time$
$GetTime$ = $a$(7 To 10) + “-” + $a$(4 To 5) + “-” + $a$(1 To 2) + " " + $a$(12 To 13)+":"+$a$(15 To 16)+":"+$a$(18 To 19)
EndFn

//publish all tags
MqttPublishAllValue:
counter%=0
json$ = ‘{’
FOR i% = 0 TO NB% -1
SETSYS Tag, “load”,-i%
i$= GETSYS TAG,“Name”

  GroupA$= GETSYS TAG,"IVGROUPA"
  GroupB$= GETSYS TAG,"IVGROUPB"
  GroupC$= GETSYS TAG,"IVGROUPC"
  GroupD$= GETSYS TAG,"IVGROUPD"
 
  IF GroupA$ = "1" And GROUPA%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupB$ = "1" And GROUPB%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupC$ = "1" And GROUPC%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupD$ = "1" And GROUPD%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1

NEXT i%
json$ = json$ + ‘“time”: "’+ @GetTime$() +’"’
json$ = json$ + ‘}’

STATUS% = MQTT(“STATUS”)
//Is Connected
If (STATUS% = 5) Then
Print “[PUBLISH ALL TAGS TIMER] " + STR$ counter% + " tags selected and published”
MQTT “PUBLISH”,“devices/”+DeviceID$+"/messages/events/",json$, 0, 0
Else
Print “Not connected (” + STR$ STATUS% + “)”
Endif
End
FUNCTION Log($Msg$)
LOGEVENT $Msg$ ,100
PRINT $Msg$
ENDFN

ONDATE 1,“0 0 * * *”,“GOTO GENERATE_REPORT”
ONSTATUS “GOTO SCHEDULEDACTION_END”
GOTO GENERATE_REPORT
END

GENERATE_REPORT:
//Check if SD card is inserted or if there is enough space
SETSYS INF,“LOAD”
SDCardSize$ = GETSYS INF, “SDExtFree”
SDCardSize_Int% = Val SDCardSize$
IF SDCardSize_Int% > 5000 THEN // 5 MB remains
GOSUB READ_LASTTMS
LAST_TIME$ = @GetCurrentTimeStamp$()
EBD_STRING$ = “$dtHT$ftT$et” + LAST_TIME$ + “$st” + LASTTMS$
FILENAME$ = “/usr/sdext/DR_ORION_” + LAST_TIME$ + “.txt”
WRITEEBD EBD_STRING$, FILENAME$
ActionID% = GETSYS Prg, “ACTIONID”
ELSE
LOGEVENT “EUM Card full or not present”, -1
ENDIF
END
SCHEDULEDACTION_END:
CurID% = GETSYS Prg,“EVTINFO” : REM Get ActionID
REM : Check if it is the right schedule action to save last timestamp otherwise do nothing
If ActionID% = CurID% THEN
SETSYS Prg, “ACTIONID”, CurID%
CurID_Status% = GETSYS Prg,“ACTIONSTAT”
REM Action successfully done → Write last timestamp in file
IF CurID_Status% = 0 THEN
LOGEVENT “FILE " + FILENAME$ + " SUCCESSFULLY EXPORTED”, 100
@WRITETMS(LAST_TIME$)
ELSE
LOGEVENT “FILE " + FILENAME$ + " NOT SUCCESSFULLY EXPORTED”, 0
ENDIF
ENDIF
END
READ_LASTTMS:
SETSYS PRG,“RESUMENEXT”,1
REM GET LAST TIMESTAMP
OPEN “file:/usr/tms.dat” FOR BINARY INPUT AS 1
LASTTMS$ = GET 1,15
CLOSE 1
Cur_Err% = GETSYS PRG,“LSTERR”

IF Cur_Err% = 33 THEN //REM File does not exist
LASTTMS$= “01011970_000000”
SETSYS PRG,“LSTERR”,0
ENDIF
SETSYS PRG,“RESUMENEXT”,0
CLS
RETURN
FUNCTION WriteTMS($LastTimeStamp$):
OPEN “file:/usr/tms.dat” FOR BINARY OUTPUT AS 1
PUT 1,$LastTimeStamp$
CLOSE 1
ENDFN
FUNCTION GetCurrentTimeStamp$()
REM TIME$ = 19/12/2014 10:03:01
$EWON_TIME$ = TIME$
$GetCurrentTimeStamp$ = $EWON_TIME$(1 To 2) + $EWON_TIME$(4 To 5) + $EWON_TIME$(7 To 10) + “_” + $EWON_TIME$(12 To 13) + $EWON_TIME$(15 To 16)+ $EWON_TIME$(18 To 19)
ENDFN
End

Hi Steve,

Are you getting any errors in the console when you run this code? Is the MQTT script running correctly after you entered it or does that not seem to be working either?

-Tim

Tim
MQTT is working as im getting the print out of the published tags. Im not getting any errors on the console. I just moved it to that order and will check if the other ontimers are working. But the ONDATE doesnt seem to be working.

I have seen some issues when you’re using functions as well as the sections that have the colon at the end of their name. Could you try and change it so that all of them are either functions or all of them are sections and see if that works?

Seems to have made it work, will keep an eye on it.
Thanks
Steve

1 Like

You’re welcome, let me know if it still seems to be having issues after the change

Tim
I was incorrect, it doesnt seem to be working correctly BASIC IDE Script Attached:

TSET 1, 60
ONTIMER 1, “@CALC_VARS()”

FUNCTION CALC_VARS()
SCFM@ = FT2000@ * ((PT2027@ + 14.7)/14.7)*(528/(TE2032@+460))
ENDFN

TSET 3, 3600
ONTIMER 3, “@SETNAME()”
Function SETNAME()
name@= “Fleet 1- Orion”
ENDFN

//################" CONFIGURATION #################
DeviceId$=“FLT1_Orion”
IotHubName$ =“DPS-Turbines”
Changepushtime% = 10 //Timer to push only Tags that has changed
Fullpushtime% = 10// Timer to push all values
//Select the Tag Group to publish → 0 or 1
//Tag must be created and at least set in one of the groups.
GROUPA% = 1
GROUPB% = 1
GROUPC% = 1
GROUPD% = 1
// /usr directory operations to do :
// 1. Generate a Self-Signed certificate using
// openssl req -x509 -sha256 -nodes -days 3650 -newkey rsa:2048 -keyout ReplaceByDeviceID.key -out ReplaceByDeviceID.crt -config openssl.cnf
// 2. Rename the cert and the key with the DeviceID (.crt and .key)
// 3. Upload the self-signed cert + the key + the baltimoreCA certificate to the /usr
// 4. Start the script → You should see some “PUBLISH…” logs in the console.
// 5. Do not forget to select Run > Autorun in order to have the script running at boot
//################“END CONFIGURATION ##############
CLS
//Read number of Tags
NB%= GETSYS PRG,“NBTAGS”
DIM a(NB%,2)
MQTT “Open”,DeviceId$,IotHubName$ + “.azure-devices.net”
Mqtt “SetParam”,“Port”,“8883”
MQTT “setparam”, “log”, “1”
MQTT “setparam”, “keepalive”, “20”
MQTT “setparam”, “TLSVERSION”, “tlsv1.2”
MQTT “setparam”, “PROTOCOLVERSION”, “3.1.1”
MQTT “setparam”, “cafile”,”/usr/BaltimoreCyberTrustRoot.pem"
MQTT “setparam”, “CertFile”,"/usr/"+DeviceId$+".crt"
MQTT “setparam”, “KeyFile”,"/usr/"+DeviceId$+".key"
Mqtt “SetParam”,“Username”,IotHubName$+ “.azure-devices.net/”+DeviceId$+"/api-version=2016-11-14"
Mqtt “SetParam”,“Password”,“HostName=”+IotHubName$+";DeviceID="+DeviceId$+";x509=true"
SETSYS PRG,“RESUMENEXT”,1 //Continue in case of error at MQTT “CONNECT”
Mqtt “Connect”
ErrorReturned% = GETSYS PRG,“LSTERR”
IF ErrorReturned% = 28 THEN @Log("[MQTT SCRIPT] WAN interface not yet ready")
SETSYS PRG,“RESUMENEXT”,0
ONMQTT “GOTO MqttRx”
//a = table with 2 columns : one with the negative indice of the tag and the second one with 1 if the values of the tag change or 0 otherwise
IsConnected:
//Record the Tag ONCHANGE events into an array.
//Allows to post only values that have changed
FOR i% = 0 TO NB%-1
k%=i%+1
SETSYS Tag, “load”,-i%
a(k%,1)=-i%
a(k%,2) = 0
GroupA$= GETSYS TAG,“IVGROUPA”
GroupB$= GETSYS TAG,“IVGROUPB”
GroupC$= GETSYS TAG,“IVGROUPC”
GroupD$= GETSYS TAG,“IVGROUPD”

IF GroupA$ = “1” And GROUPA%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupB$ = “1” And GROUPB%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupC$ = “1” And GROUPC%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
IF GroupD$ = “1” And GROUPD%= 1 THEN Onchange -i%, “a(”+ STR$ k%+",2)= 1"
NEXT i%

ONTIMER 2,“goto MqttPublishAllValue”
TSET 2,Fullpushtime%
// TSET 2,Changepushtime%
END
//Compute the right time format for AZURE
Function GetTime$()
$a$ = Time$
$GetTime$ = $a$(7 To 10) + “-” + $a$(4 To 5) + “-” + $a$(1 To 2) + " " + $a$(12 To 13)+":"+$a$(15 To 16)+":"+$a$(18 To 19)
EndFn

//publish all tags
MqttPublishAllValue:
counter%=0
json$ = ‘{’
FOR i% = 0 TO NB% -1
SETSYS Tag, “load”,-i%
i$= GETSYS TAG,“Name”

  GroupA$= GETSYS TAG,"IVGROUPA"
  GroupB$= GETSYS TAG,"IVGROUPB"
  GroupC$= GETSYS TAG,"IVGROUPC"
  GroupD$= GETSYS TAG,"IVGROUPD"
 
  IF GroupA$ = "1" And GROUPA%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupB$ = "1" And GROUPB%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupC$ = "1" And GROUPC%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1
  IF GroupD$ = "1" And GROUPD%= 1 THEN json$ = json$ + '"' + i$+ '":"'+STR$ GETIO i$ + '",': counter% = counter% +1

NEXT i%
json$ = json$ + ‘“time”: "’+ @GetTime$() +’"’
json$ = json$ + ‘}’

STATUS% = MQTT(“STATUS”)
//Is Connected
If (STATUS% = 5) Then
Print “[PUBLISH ALL TAGS TIMER] " + STR$ counter% + " tags selected and published”
MQTT “PUBLISH”,“devices/”+DeviceID$+"/messages/events/",json$, 0, 0
Else
Print “Not connected (” + STR$ STATUS% + “)”
Endif
End
FUNCTION Log($Msg$)
LOGEVENT $Msg$ ,100
PRINT $Msg$
ENDFN

ONDATE 1,“0 0 * * *”,“GOTO GENERATE_REPORT”
ONSTATUS “GOTO SCHEDULEDACTION_END”
GOTO GENERATE_REPORT
END

FUNCTION GENERATE_REPORT()
//Check if SD card is inserted or if there is enough space
SETSYS INF,“LOAD”
SDCardSize$ = GETSYS INF, “SDExtFree”
SDCardSize_Int% = Val SDCardSize$
IF SDCardSize_Int% > 5000 THEN // 5 MB remains
GOSUB READ_LASTTMS
LAST_TIME$ = @GetCurrentTimeStamp$()
EBD_STRING$ = “$dtHT$ftT$et” + LAST_TIME$ + “$st” + LASTTMS$
FILENAME$ = “/usr/sdext/DR_ORION_” + LAST_TIME$ + “.txt”
WRITEEBD EBD_STRING$, FILENAME$
ActionID% = GETSYS Prg, “ACTIONID”
ELSE
LOGEVENT “EUM Card full or not present”, -1
ENDIF
END
FUNCTION SCHEDULEDACTION_END()
CurID% = GETSYS Prg,“EVTINFO” : REM Get ActionID
REM : Check if it is the right schedule action to save last timestamp otherwise do nothing
If ActionID% = CurID% THEN
SETSYS Prg, “ACTIONID”, CurID%
CurID_Status% = GETSYS Prg,“ACTIONSTAT”
REM Action successfully done → Write last timestamp in file
IF CurID_Status% = 0 THEN
LOGEVENT “FILE " + FILENAME$ + " SUCCESSFULLY EXPORTED”, 100
@WRITETMS(LAST_TIME$)
ELSE
LOGEVENT “FILE " + FILENAME$ + " NOT SUCCESSFULLY EXPORTED”, 0
ENDIF
ENDIF
ENDFN
FUNCTION READ_LASTTMS()
SETSYS PRG,“RESUMENEXT”,1
REM GET LAST TIMESTAMP
OPEN “file:/usr/tms.dat” FOR BINARY INPUT AS 1
LASTTMS$ = GET 1,15
CLOSE 1
Cur_Err% = GETSYS PRG,“LSTERR”

IF Cur_Err% = 33 THEN //REM File does not exist
LASTTMS$= “01011970_000000”
SETSYS PRG,“LSTERR”,0
ENDIF
SETSYS PRG,“RESUMENEXT”,0
CLS
RETURN
FUNCTION WriteTMS($LastTimeStamp$):
OPEN “file:/usr/tms.dat” FOR BINARY OUTPUT AS 1
PUT 1,$LastTimeStamp$
CLOSE 1
ENDFN
FUNCTION GetCurrentTimeStamp$()
REM TIME$ = 19/12/2014 10:03:01
$EWON_TIME$ = TIME$
$GetCurrentTimeStamp$ = $EWON_TIME$(1 To 2) + $EWON_TIME$(4 To 5) + $EWON_TIME$(7 To 10) + “_” + $EWON_TIME$(12 To 13) + $EWON_TIME$(15 To 16)+ $EWON_TIME$(18 To 19)
ENDFN

Are you available now to take a look at this over teamviewer?

Unfortunately I have a hourlong meeting in 10 minutes.
Thanks
Steve

Ok, after taking a look at the code I was going to try and change it so that the whole code would be done in sections instead of doing it through functions

If possible i could get your version and install it real quick on one of my devices.
Thanks
Steve

Hi Steve,

Without having access to your device it’s a little hard to try and debug this because I won’t have access to the tags and that will cause the script to error out. But I believe if you try and replace the functions with the goto command (and maybe gosub/return) depending on how the functions were originally working on.

-Tim

I could potentially set you into a test bed we have and give you access to just that device which is not in the operating pool

If possible I’d prefer if there was somebody available to view this with me and explain the previous code while we try and debug this, but let me know what you find out.

I am setting up a test system all the way in a similar fashion as a production unit so we can work on it.

Test system is setup, please email steve.rodriguez@dynamisps.com to coordinate the remote connectivity.

Hi Steve,

Does the device seem to be working since we made those changes?

-Tim

Tim
No, it does not seem to have worked, the last reports were from April 1st.

Are there any errors showing up in the console?

Tim,
For some reason it does not seem to want to run anything past the MQTT portion of the script, I put the TIme_Check funciton right after the MQTT script and made it run every 10 minutes, and the Time_Report variable had not changed from “23” until i manually ran the function. It seems like MQTT script just ends it goes back to the start of the Basic IDE Scripting area.