Facebook
From Muhammad Iqbal, 3 Years ago, written in Visual Basic.
Embed
Download Paste or View Raw
Hits: 375
  1. 'Declare Public Variables
  2.  
  3. 'PreserveVariables
  4. SequentialMode
  5. 'ComPortIsActive (COMRS232)
  6.  
  7. '=========================================== GENERAL VARIABLES
  8. 'Dim bool As Boolean
  9. Public systemTimeZone As Long = 7
  10. ' Public systemStatusTableUpdateInterval As Long = 60 ' second
  11.  
  12. '=========================================== FTP VARIABLES
  13. Public FTPEnabled As Boolean = 1
  14. Const FTP_HOST = "someftpserver.com"
  15. Const FTP_USER="ftpuser"
  16. Const FTP_PASS="ftppass"
  17.  
  18. '==========================================
  19. ' ALARM TRIGGER
  20. '==========================================
  21. Public Battery_Is_Low As Boolean
  22. Public AlarmTriggerEnabled As Boolean = 0
  23. Public BatteryAlarmTrigger As Boolean
  24. Public Test_BatteryAlarmTrigger As Boolean
  25.  
  26. '==========================================
  27. ' NTP VARIABLES
  28. '==========================================
  29. Public timeIsSynced As Boolean
  30. Public timeDiff As Long
  31. Dim NTPSyncInterval As Long
  32. Dim fastNTPSyncInterval As Long = 60
  33. Dim slowNTPSyncInterval As Long = 21600
  34. Dim lastNTPSync As String
  35. Public triggerSyncNTP As Boolean = false
  36.  
  37. '=========================== Timing Vars
  38. Public Masterscan(9) As Long
  39. Alias Masterscan(1) = Year         'assign the alias Year to Masterscan(1)
  40. Alias Masterscan(2) = Month        'assign the alias Month to Masterscan(2)
  41. Alias Masterscan(3) = Date         'assign the alias Date to Masterscan(3)
  42. Alias Masterscan(4) = Hour         'assign the alias Hour to Masterscan(4)
  43. Alias Masterscan(5) = Minute       'assign the alias Minute to Masterscan(5)
  44. Alias Masterscan(6) = Second       'assign the alias Second to Masterscan(6)
  45. Alias Masterscan(7) = uSecond      'assign the alias uSecond to Masterscan(7)
  46. Alias Masterscan(8) = WeekDay      'assign the alias WeekDay to Masterscan(8)
  47. Alias Masterscan(9) = Day_of_Year  'assign the alias Day_of_Year to Masterscan(9)
  48.  
  49. '=========================== Data Tables
  50.  
  51. Public Result(10) As Long
  52.  
  53. '=========================================== DMM VARIABLES
  54. Public DMM_mode As String = "Auto"
  55.  
  56. 'If CR6
  57. Const DMM_Com_Port = ComC3
  58. 'If CR1000
  59. 'Const DMM_Com_Port = Com3
  60.  
  61. Const Number_of_DMMs As Long = 1
  62.  
  63. Dim DMM_Address(Number_of_DMMs) As Long  = {1}
  64. ' Public DMM_Is_Installed(Number_of_DMMs) As Boolean  = {false,false,false,false,false}
  65. Dim DMM_Zero(Number_of_DMMs) = {62.5} ' This is where you enter the zero Reading for the DMMS
  66.  
  67. 'DMM Modbus scan interval
  68. Const DMM_Poll_Interval = 150 ' if faster than this, skip scans seems increasing
  69. Const DMM_Poll_Interval_Units = msec
  70.  
  71. 'Logging interval
  72. Public DMM_Logging_Interval As Long = 900 ' in seconds
  73. Units DMM_Logging_Interval = Second
  74.  
  75. Public PTemp, Batt_volt(4)
  76. Units PTemp = °C
  77. Units Batt_volt = Volt
  78. Alias Batt_volt(1) = BatteryV
  79. Alias Batt_volt(2) = ChargeIn
  80. Alias Batt_volt(3) = ChargeOut
  81. Alias Batt_volt(4) = LithiumV
  82.  
  83.  
  84.  
  85. Dim Measure_DMMs_Trigger As Boolean = false
  86. Public Elapsed As Long
  87. Public stage As Long = 0
  88. Public Solenoid_Power_On As Boolean = false
  89. Public DMM_Power_On As Boolean = false
  90.  
  91. Public ModbusResultStr(Number_of_DMMs) As String
  92. Public DMM_Raw(Number_of_DMMs) As Long
  93. Public DMM_Reading(Number_of_DMMs)
  94. Units DMM_Reading(1) = mm
  95. Public DMM_Calc(Number_of_DMMs)
  96. Units DMM_Calc(1) = mm
  97. ' Public Laser_Switch As Boolean
  98. 'Public measureIsTriggered As Boolean
  99. Dim i As Long
  100. 'Dim j As Long, m As Long
  101.  
  102. 'Public PROCESS_STAGE As String * 36         ' keeping processing stage
  103. Public SLOWSCAN_STAGE As String * 36         ' keeping processing stage
  104.  
  105. '23:14 to 23:31, every day
  106. Public Trigger_StartHour1 As Long = 23
  107. Public Trigger_StartMinute1 As Long = 15
  108. Public TotalStartMinute1 As Long
  109.  
  110. Public Trigger_EndHour1 As Long = 23
  111. Public Trigger_EndMinute1 As Long = 30
  112. Public TotalEndMinute1 As Long
  113.  
  114. Public Trigger_StartHour2 As Long = 21
  115. Public Trigger_StartMinute2 As Long = 15
  116. Public TotalStartMinute2 As Long
  117.  
  118. Public Trigger_EndHour2 As Long = 21
  119. Public Trigger_EndMinute2 As Long = 15
  120. Public TotalEndMinute2 As Long
  121.  
  122. Public file_handle
  123. Public http_header As String * 100
  124. Public http_post_response As String * 200
  125. Public http_post_tx
  126.  
  127. Const CRLF = CHR(13) + CHR(10)
  128.  
  129. Public Message As String * 250
  130. Public EmailSuccess
  131. Public ServerResponse As String * 50
  132.  
  133. Dim battVStr As String
  134. Dim PTempStr As String
  135. Dim dateStr As String
  136. Dim timeStr As String
  137. Dim DMM_CalcStr As String
  138. Dim LVDT_mm_CalcStr As String
  139. Dim DMM_Power_OnStr As String
  140.  
  141. Public FTPResult(4) As Long
  142. Alias FTPResult(1) = FTP_Diagnostic
  143. Alias FTPResult(2) = FTP_DMM_Data
  144. Alias FTPResult(3) = FTP_LVDT_Data
  145. Alias FTPResult(4) = FTP_BattTemp
  146. Dim rootPath As String
  147.  
  148. Dim strNotInstalled As String = "Not installed"
  149. Dim strSuccess As String = "Success"
  150. Dim strTimeout As String = "Timeout"
  151. Dim strError As String = "Error"
  152. Dim strNotPolling As String = "Not polling"
  153.  
  154. Dim goodReading(10)
  155. Dim j
  156. Dim idx = 1
  157.  
  158. Dim k
  159. Dim sum
  160. Dim avg
  161.  
  162. Public PeriodicMode As Boolean = false
  163. Dim ManualMode As Boolean
  164. Dim Update_DMM_Table As Boolean = false
  165.  
  166. Dim strDateTimeFormat As String  * 48 = "%04u-%02u-%02u %02u:%02u:%02u"
  167.  
  168. 'Declare Private Variables
  169. 'Example:
  170.  
  171. 'Public OutStat(6)
  172. 'Public LastFileName(6) As String * 36
  173.  
  174. 'If CR6
  175. Const pinRelaySolenoid = C2
  176. Const pinRelayDMM = C1
  177. 'If CR1000
  178. 'Const pinRelaySolenoid = 7
  179. 'Const pinRelayDMM = 8
  180.  
  181. Sub READ_DMM
  182.  
  183.  
  184.   For i = 1 To Number_of_DMMs
  185.     DMM_Raw(i) = NAN
  186.     DMM_Reading(i) = NAN
  187.     DMM_Calc(i) = NAN
  188.     If DMM_Address(i) <= 0 Then
  189.       ModbusResultStr(i) = strNotInstalled
  190.     ElseIf DMM_Address(i) > 0 Then
  191.       If DMM_Power_On = true Then
  192.  
  193.         For j = 1 To 5
  194.           SerialFlush(DMM_Com_Port)
  195.           Delay (0,100,mSec)
  196.           ModbusMaster (Result(i),DMM_Com_Port,38400,DMM_Address(i),3,DMM_Raw(i),1,1,2,2,3)
  197.           If Result(i) = 0 Then
  198.             ModbusResultStr(i) = strSuccess
  199.             DMM_Reading(i) = DMM_Raw(i)/2
  200.             If DMM_Reading(i) > 0 Then
  201.               goodReading(idx) = DMM_Reading(i)
  202.               idx = idx+1
  203.               'AvgRun (goodReading(1),1,DMM_Reading(i),1)
  204.             EndIf
  205.           ElseIf Result(i) > 0 Then
  206.             ModbusResultStr(i) = strTimeout & " " & Result(i)
  207.           ElseIf Result(i) < 0 Then
  208.             ModbusResultStr(i) = strError & " " & Result(i)
  209.           EndIf
  210.         Next j
  211.        
  212.  
  213.          
  214.         For k = 1 To idx
  215.           sum = sum + goodReading(k)
  216.         Next k
  217.        
  218.         avg = sum/idx
  219.         sum = 0
  220.        
  221.         DMM_Reading(i) = avg
  222.         DMM_Calc(i)  = DMM_Reading(i) - DMM_Zero(i)
  223.        
  224.         ' reset idx
  225.         idx = 1
  226.       Else
  227.         ModbusResultStr(i) = strNotPolling
  228.       EndIf
  229.     EndIf
  230.   Next i
  231. EndSub
  232.  
  233.  
  234. 'Define Data Tables
  235. DataTable (DMM_Data,1,-1) 'Set table size to # of records, or -1 to autoallocate.
  236.   'DataInterval (0,DMM_Poll_Interval * Number_of_DMMs,DMM_Poll_Interval_Units,10)
  237.   'DataInterval(0,30,Min,10)
  238.   'TableFile ("CRD:DMM_Param_",8,-1,5,30,Min,0,0)
  239.   'Minimum (1,Batt_volt,FP2,False,False)
  240.   'Sample (1,PTemp,FP2)
  241.   'Sample (1,Elapsed_Solenoid_Power_ON,IEEE4)
  242.   'Sample (1,counter,Long)
  243.   'Sample (Number_of_DMMs,ModbusResultStr(),String)
  244.   'FieldNames("Modbus_Result")
  245.   Sample (1, DMM_Logging_Interval,FP2)
  246.   FieldNames("Reading_Interval")
  247.   Sample (Number_of_DMMs,DMM_Reading(),FP2)
  248.   FieldNames("DMM_Reading")
  249.   Sample (Number_of_DMMs,DMM_Calc(),FP2)
  250.   FieldNames("DMM_Calc")
  251. EndTable
  252.  
  253. DataTable (BattTemp,1,-1) 'Set table size to # of records, or -1 to autoallocate.
  254.   'DataInterval (0,DMM_Poll_Interval * Number_of_DMMs,DMM_Poll_Interval_Units,10)
  255.   DataInterval(0,30,Min,10)
  256.   'TableFile ("CRD:DMM_Param_",8,-1,5,30,Min,0,0)
  257.   Minimum (1,Batt_volt,FP2,False,False)
  258.   FieldNames("Logger_Volt")
  259.   Sample (1,PTemp,FP2)
  260.   FieldNames("Logger_Temp")
  261.   'Sample (1,Elapsed_Solenoid_Power_ON,IEEE4)
  262.   'Sample (1,counter,Long)
  263. EndTable
  264.  
  265. DataTable (Diagnostic,1,-1) 'Set table size to # of records, or -1 to autoallocate.
  266.   Minimum (1,Batt_volt,FP2,False,False)
  267.   FieldNames("Logger_Volt")
  268.   Maximum (1,PTemp,FP2,False,False)
  269.   FieldNames("Logger_Temp")
  270.   Sample (Number_of_DMMs,ModbusResultStr(),String)
  271.   FieldNames("DMM_Mbus_Result")
  272. EndTable
  273.  
  274.  
  275. Const Number_of_LVDT = 1
  276. Public Measure_LVDT_Trigger As Boolean
  277. Public LVDT_mV(Number_of_LVDT)
  278. Units LVDT_mV(1) = mV
  279. Public LVDT_mm_Raw(Number_of_LVDT)
  280. Units LVDT_mm_Raw(1) = mm
  281. Public LVDT_mm_Calc(Number_of_LVDT)
  282. Units LVDT_mm_Calc(1) = mm
  283. Public LVDT_Cal(Number_of_LVDT) = {30.17}    'in mV/mm
  284. Units LVDT_Cal(1) = mV/mm
  285. Dim LVDT_Zero(Number_of_LVDT) = {-2.371} ' This is where you enter the zero Reading for the DMMS
  286.  
  287. 'Dim LVDT_Measure_Interval      ' LVDT Measuring interval
  288. 'Dim LVDT_Measure_Units
  289.  
  290. DataTable (LVDT_Data,1,-1) 'Set table size to # of records, or -1 to autoallocate.
  291.   'Minimum (1,Batt_volt,FP2,False,False)
  292.   'Sample (1,PTemp,FP2)
  293.   Sample (1, DMM_Logging_Interval,FP2)
  294.   FieldNames("Reading_Interval")
  295.   Sample (Number_of_LVDT,LVDT_mV(),FP2)
  296.   FieldNames("LVDT_volt")
  297.   Sample (Number_of_LVDT,LVDT_mm_Raw(),FP2)
  298.   FieldNames("LVDT_mm_Raw")
  299.   Sample (Number_of_LVDT,LVDT_mm_Calc(),FP2)
  300.   FieldNames("LVDT_mm_Calc")
  301. EndTable
  302.  
  303.  
  304. Dim tsStr As String '2000-23-23 44:55:44
  305.  
  306. Function CombineYMDHMS(yLong As Long, moLong As Long, dLong As Long, hLong As Long, mLong As Long, sLong As Long) As String
  307.  
  308.   Sprintf(tsStr,"%04u-%02u-%02u %02u:%02u:%02u", yLong, moLong, dLong, hLong, mLong, sLong)
  309.   Return tsStr
  310. EndFunction
  311.  
  312. Dim ts As Long
  313.  
  314. Function ConvertDateTimeToEpoch1990 (dtStr As String) As Long
  315.  
  316.   ts = SecsSince1990 (dtStr,4)
  317.   Return ts
  318. EndSub
  319.  
  320. Public loggerBootTimestampStr As String '2000-23-23 44:55:44
  321. Sub loggerBootTimestampToString
  322.   RealTime (Masterscan())
  323.  
  324.   Sprintf(loggerBootTimestampStr,"%04u-%02u-%02u %02u:%02u:%02u", Year, Month, Date, Hour, Minute, Second)
  325. EndSub
  326.  
  327. Public loggerBootEpoch1990 As Long
  328. Sub loggerBootTimestampToEpoch1990
  329.   Call loggerBootTimestampToString
  330.  
  331.   'loggerBootEpoch1990 = SecsSince1990 (loggerBootTimestampStr,4)
  332.   loggerBootEpoch1990 = ConvertDateTimeToEpoch1990(loggerBootTimestampStr)  
  333. EndSub
  334.  
  335.  
  336. 'Main Program
  337.  
  338.  
  339.  
  340. BeginProg
  341.  
  342.   ' check logger date & time
  343.   ' should be between 1-Jan-2020 and 1-Jan-2050
  344.   Call loggerBootTimestampToEpoch1990
  345.   If loggerBootEpoch1990 > 946684800 AND loggerBootEpoch1990 < 1893456000 Then
  346.     timeIsSynced = true
  347.   Else
  348.     timeIsSynced = false
  349.     triggerSyncNTP = true
  350.   EndIf
  351.  
  352.   'SetStatus ("USRDriveSize",100000)
  353.  
  354.  
  355.  
  356.  
  357.  
  358.   '================== SETUP DMM ====================
  359.  
  360.   '  Turn_ONOFF_Solenoid(false)
  361.   '  Turn_ONOFF_DMM(false)
  362.  
  363.   'Measure_DMMs_Trigger = false
  364.  
  365.   ' If CR6
  366.   'SerialOpen (DMM_Com_Port,38400,16,0,128,4)
  367.   ' If CR1000
  368.   'SerialOpen (DMM_Com_Port,38400,16,0,128)
  369.  
  370.   '==================== END OF SETUP DMM
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.   Scan (1,Sec,0,0)
  378.  
  379.     '    Dim timerSecond, elapsedSecond
  380.     '    elapsedSecond = Timer (timerSecond,Sec,2  ) ' reset & start
  381.  
  382.     'Scan (DMM_Poll_Interval * Number_of_DMMs ,DMM_Poll_Interval_Units,3,0)
  383.     RealTime (Masterscan())
  384.     PanelTemp (PTemp,50)
  385.     Battery (Batt_volt())
  386.    
  387.    
  388.     ' ---------------------------------
  389.     ' Logic to monitor battery voltage
  390.     ' ---------------------------------
  391.     If BatteryV < 12.0 Then
  392.       If Timer(2,Sec,4) = 0 Then
  393.         Timer(2,Sec,0) 'start
  394.       ElseIf Timer(2,Sec,4) >= 604800 Then '604800 sec = 7 days
  395.         Timer(2,Sec,1) 'stop
  396.       EndIf
  397.     Else
  398.        Timer(2,Sec,3) 'stop & reset
  399.     EndIf
  400.    
  401.     If Timer(2,Sec,4) >= 600 Then ' 600 sec = 10 minute
  402.       Battery_Is_Low  = true
  403.     Else
  404.       Battery_Is_Low = false
  405.     EndIf
  406.    
  407.     ' Skip scan if batt voltage is low
  408.     If Battery_Is_Low Then
  409.       If TimeIntoInterval(15,24,Hr) Then
  410.         BatteryAlarmTrigger = true
  411.       EndIf
  412.      
  413.       If stage <> 0 OR Timer(1,Sec,4) > 0 Then        
  414.         PortSet (pinRelaySolenoid, true)
  415.         Delay (1,5,Sec)
  416.         stage = 0
  417.         Timer(1,Sec,3) ' stop & reset timer
  418.       EndIf
  419.      
  420.       ' Turn On/Off the relays
  421.       PortSet (pinRelaySolenoid, false)
  422.       PortSet (pinRelayDMM, false)
  423.            
  424.       ContinueScan
  425.     EndIf
  426.    
  427.  
  428.     ' Skip scan if time is not synced via NTP
  429.     If NOT timeIsSynced Then
  430.       ContinueScan
  431.     EndIf
  432.  
  433.     '----------------------- DMM SCAN SECTION
  434.    
  435.  
  436.  
  437.     TotalStartMinute1 = Trigger_StartHour1*60 + Trigger_StartMinute1-1
  438.     TotalEndMinute1 = Trigger_EndHour1*60 + Trigger_EndMinute1+1
  439.     TotalStartMinute2 = Trigger_StartHour2*60 + Trigger_StartMinute2-1
  440.     TotalEndMinute2 = Trigger_EndHour2*60 + Trigger_EndMinute2+1
  441.    
  442.  
  443.     If PeriodicMode Then
  444.       If TimeIsBetween (TotalStartMinute1,TotalEndMinute1,1440,min) Then
  445.         DMM_mode = "Manual"
  446.       EndIf
  447.     EndIf
  448.    
  449.    
  450.  
  451.     'If DMM_mode = "Manual" OR DMM_mode = "1" OR DMM_Logging_Interval < 60 Then
  452.     If DMM_mode = "Manual" OR DMM_mode = "1" Then
  453.       ManualMode = true
  454.     Else
  455.       ManualMode = false
  456.     EndIf
  457.  
  458.  
  459.  
  460.     Elapsed = Timer(1,Sec,4)
  461.  
  462.     '    Dim multiplier As Long
  463.     '    multiplier = DMM_Logging_Interval / (DMM_Poll_Interval * Number_of_DMMs)
  464.  
  465.     'If TimeIntoInterval(35,DMM_Poll_Interval * Number_of_DMMs * multiplier, Sec) Then Measure_DMMs_Trigger = true   'at 2 minute intervals, set PortOn to 1
  466.     'If TimeIntoInterval(DMM_Logging_Interval-35,DMM_Logging_Interval, Sec) Then Measure_DMMs_Trigger = true   'at 2 minute intervals, set PortOn to 1
  467.     If ManualMode Then
  468.       DMM_Logging_Interval = 1
  469.       Measure_DMMs_Trigger = true
  470.     Else
  471.       DMM_Logging_Interval = 900
  472.       If TimeIntoInterval((DMM_Logging_Interval-60)+35,DMM_Logging_Interval, Sec) Then
  473.         Measure_DMMs_Trigger = true
  474.       EndIf
  475.     EndIf
  476.  
  477.     'If DMM_mode = "Manual" Then Measure_DMMs_Trigger = false
  478.    
  479.  
  480.     If ManualMode Then
  481.  
  482.       DMM_Power_On = true
  483.  
  484.       If stage = 0 Then
  485.         If Solenoid_Power_On = false Then
  486.           Delay(1,5,sec)
  487.           Solenoid_Power_On = true
  488.           Timer (1,Sec,2) 'Reset & Start Off timer
  489.           stage = 11
  490.         ElseIf Solenoid_Power_On = true Then
  491.           Timer (1,Sec,2) 'Reset & Start Off timer
  492.           stage = 11
  493.         EndIf
  494.       ElseIf stage = 1 AND Timer (1,Sec,4 ) >= 1 Then
  495.         stage = 12
  496.         Solenoid_Power_On = false
  497.       ElseIf stage = 2 Then
  498.         stage = 12
  499.       ElseIf stage = 3 Then
  500.         stage = 12
  501.       ElseIf stage = 4 Then
  502.         stage = 12
  503.       ElseIf  stage = 5 AND Timer (1,Sec,4 ) >= 5 Then
  504.         stage = 0
  505.         Solenoid_Power_On = false
  506.       EndIf
  507.  
  508.  
  509.       '------ STAGE 11
  510.       If stage = 11 AND Timer (1,Sec,4 ) >= 1 Then
  511.         stage = 12
  512.         Solenoid_Power_On = false
  513.  
  514.         '------ STAGE 12
  515.       ElseIf stage = 12 AND Timer (1,Sec,4 ) >= 30 Then
  516.         ' make sure the laser have been fully turned On at this stage
  517.         ' because we want to take modbus reading
  518.  
  519.         Call READ_DMM
  520.  
  521.         'CallTable DMM_Param
  522.         Update_DMM_Table = true
  523.         Measure_LVDT_Trigger = true
  524.  
  525.  
  526.       EndIf
  527.  
  528.       '--------------------- AUTO MODE
  529.     Else
  530.       If stage >= 10 Then
  531.         Measure_DMMs_Trigger = false
  532.         stage = 5
  533.         Solenoid_Power_On = true
  534.         DMM_Power_On = false
  535.         Timer (1,Sec,2 ) 'Reset and Start timer
  536.  
  537.         '------ STAGE 0 And Mode is Auto
  538.       ElseIf stage = 0 AND Measure_DMMs_Trigger Then
  539.         Measure_DMMs_Trigger = false
  540.         stage = 1
  541.         Solenoid_Power_On = true
  542.         Timer (1,Sec,2) 'Reset & Start Off timer
  543.  
  544.         '------ STAGE 1
  545.       ElseIf stage = 1 AND Timer (1,Sec,4 ) >= 1 Then
  546.         stage = 2
  547.         Solenoid_Power_On = false
  548.  
  549.         '------ STAGE 2
  550.       ElseIf stage = 2 AND Timer (1,Sec,4 ) >= 20 Then
  551.         stage = 3
  552.         DMM_Power_On = true
  553.  
  554.         '------ STAGE 3
  555.         'ElseIf stage = 3 AND Timer (1,Sec,4 ) >= 25 Then
  556.       ElseIf stage = 3 AND Second = 0 Then
  557.         ' make sure the laser have been fully turned On at this stage
  558.         ' because we want to take modbus reading
  559.  
  560.         Call READ_DMM
  561.  
  562.         ' CallTable DMM_Param
  563.         Update_DMM_Table = true
  564.         Measure_LVDT_Trigger = true
  565.  
  566.         stage = 4
  567.  
  568.         '------ STAGE 4
  569.         'ElseIf stage = 4 AND Timer (1,Sec,4 ) >= 25 Then
  570.       ElseIf stage = 4 Then
  571.         'Measure_DMMs_Trigger = false
  572.         stage = 5
  573.         Solenoid_Power_On = true
  574.         DMM_Power_On = false
  575.         Timer (1,Sec,2 ) 'Reset and Start timer
  576.  
  577.         '------ STAGE 5
  578.       ElseIf  stage = 5 AND Timer (1,Sec,4 ) >= 5 Then
  579.         'Measure_DMMs_Trigger = false
  580.         stage = 0
  581.         Solenoid_Power_On = false
  582.         Timer (1,Sec,3) 'Stop & Reset timer
  583.  
  584.       Else
  585.         Measure_DMMs_Trigger = false
  586.       EndIf
  587.  
  588.     EndIf
  589.  
  590.     If Update_DMM_Table Then
  591.       If ManualMode Then
  592.         Update_DMM_Table = false
  593.         CallTable DMM_Data
  594.         CallTable BattTemp
  595.         CallTable Diagnostic
  596.       Else
  597.         Update_DMM_Table = false
  598.         CallTable DMM_Data
  599.         CallTable BattTemp
  600.         CallTable Diagnostic
  601.       EndIf
  602.     EndIf
  603.  
  604.  
  605.     If DMM_Power_On Then
  606.       SerialOpen (DMM_Com_Port,38400,16,0,128,4)
  607.     Else
  608.       SerialClose(DMM_Com_Port)
  609.     EndIf
  610.  
  611.  
  612.     ' Turn On/Off the relays
  613.     PortSet (pinRelaySolenoid,Solenoid_Power_On)
  614.     PortSet (pinRelayDMM, DMM_Power_On)
  615.  
  616.  
  617.  
  618.     '================ END OF DMM SCAN SECTION
  619.  
  620.     '    LVDT_Measure_Interval = 60
  621.     '    LVDT_Measure_Units = sec
  622.  
  623.     'If TimeIntoInterval(0,LVDT_Measure_Interval,sec) Then  Measure_LVDT_Trigger = true
  624.  
  625.     If Measure_LVDT_Trigger Then
  626.       Measure_LVDT_Trigger = false
  627.       'SW12 (SW12_1,1 )
  628.       'Delay(0,5,Sec)
  629.       For i = 1 To Number_of_LVDT
  630.         VoltDiff (LVDT_mV(1),1,mV5000C,U1,True ,0,50,1,0)
  631.         LVDT_mm_Raw(i)=(LVDT_mV(i)/LVDT_Cal(i))
  632.         LVDT_mm_Calc(i)= LVDT_mm_Raw(i) - LVDT_Zero(i)
  633.       Next i
  634.       'SW12 (SW12_1,0 )
  635.       CallTable LVDT_Data
  636.     EndIf
  637.    
  638.     SW12 (SW12_1,DMM_Power_On)
  639.  
  640.  
  641.  
  642.   NextScan
  643.  
  644.   SlowSequence    ' SlowSequence for anything slow
  645.  
  646.   Do
  647.     RealTime(Masterscan())
  648.  
  649.     If timeIsSynced = false Then
  650.       NTPSyncInterval = fastNTPSyncInterval
  651.     Else
  652.       NTPSyncInterval = slowNTPSyncInterval
  653.     EndIf
  654.  
  655.     If IfTime (0,NTPSyncInterval,sec) Then
  656.       triggerSyncNTP = true
  657.     EndIf
  658.  
  659.  
  660.     Sprintf(battVStr,"%.1f", BatteryV)
  661.     Sprintf(PTempStr,"%.1f", PTemp)
  662.     Sprintf(dateStr,"%04u-%02u-%02u", Year, Month, Date)
  663.     Sprintf(timeStr,"%02u:%02u:%02u", Hour, Minute, Second)
  664.     Sprintf(DMM_CalcStr,"%.2f", DMM_Data.DMM_Calc(1,1))
  665.     Sprintf(LVDT_mm_CalcStr,"%.2f", LVDT_Data.LVDT_mm_Calc(1,1))
  666.  
  667.    
  668.     If DMM_Power_On Then
  669.       DMM_Power_OnStr = "DMM ON"
  670.     Else
  671.       DMM_Power_OnStr = "DMM OFF"
  672.     EndIf
  673.  
  674.     If AlarmTriggerEnabled AND (BatteryAlarmTrigger OR Test_BatteryAlarmTrigger) Then
  675.  
  676.       'Message = "PERHATIAN !" + CRLF + CRLF
  677.       Message = Message + "[CR6 Logger] batt volt: " + battVStr + " V" + CRLF + CRLF
  678.       Message = Message + "Pls check ASAP." + CRLF + CRLF
  679.       Message = Message + "Logger date/time: "  + Status.Timestamp  + CRLF + CRLF
  680.       EmailSuccess = EmailRelay("[email protected]","[SHMS Kebumen Bridge] Warning Low Batt",Message,ServerResponse)
  681.       If EmailSuccess Then
  682.         BatteryAlarmTrigger = false
  683.         Test_BatteryAlarmTrigger = false
  684.       EndIf
  685.     EndIf
  686.     Erase(Message) 'Erase the message after sending
  687.  
  688.    
  689.  
  690.     If triggerSyncNTP Then
  691.       SLOWSCAN_STAGE = "Sync NTP"
  692.       ' synct time via NTP on regular basis
  693.       timeDiff = NetworkTimeProtocol ("pool.ntp.org", systemTimeZone * 3600, 1000)
  694.       If timeDiff <> NAN Then
  695.         triggerSyncNTP = false
  696.         timeIsSynced = true
  697.         NTPSyncInterval = slowNTPSyncInterval
  698.         RealTime(Masterscan()) ' check datalogger time
  699.         Sprintf(lastNTPSync, strDateTimeFormat, Year, Month, Date, Hour, Minute, Second)
  700.       Else
  701.         NTPSyncInterval = fastNTPSyncInterval
  702.       EndIf
  703.     EndIf
  704.    
  705.    
  706.     If IfTime(0,1, Min) Then
  707.       FTPEnabled = true
  708.     EndIf
  709.  
  710.     If FTPEnabled Then
  711.       FTPEnabled = false
  712.       '1st attempt
  713.       ' FTP
  714.       'Dim dt As String
  715.       'Sprintf(dt,"%04u-%02u-%02u.txt", Year, Month, Date)
  716.  
  717.  
  718.       rootPath = "2004_wika_lukulo"
  719.      
  720.       dateStr = "test"
  721.      
  722.       SLOWSCAN_STAGE = "FTP Diagnostic"
  723.       FTPResult(1) = FTPClient (FTP_HOST,FTP_USER,FTP_PASS,"Diagnostic",rootPath & "/Diagnostic/Diagnostic_" & dateStr & ".txt",9,0,0,Min,-1008)
  724.  
  725.       SLOWSCAN_STAGE = "FTP DMM_Data"
  726.       FTPResult(2) = FTPClient (FTP_HOST,FTP_USER,FTP_PASS,"DMM_Data",rootPath & "/DMM/DMM_Data_" & dateStr & ".txt",9,0,0,Min,-1008)
  727.  
  728.       SLOWSCAN_STAGE = "FTP LVDT_Data"
  729.       FTPResult(3) = FTPClient (FTP_HOST,FTP_USER,FTP_PASS,"LVDT_Data",rootPath & "/LVDT/LVDT_Data_" & dateStr & ".txt",9,0,0,Min,-1008)
  730.  
  731.       SLOWSCAN_STAGE = "FTP BattTemp"
  732.       FTPResult(4) = FTPClient (FTP_HOST,FTP_USER,FTP_PASS,"BattTemp",rootPath & "/BattTemp/BattTemp_" & dateStr & ".txt",9,0,0,Min,-1008)
  733.  
  734.       'This function returns:
  735.       ' -1 if successful,
  736.       ' 0 if it fails, or
  737.       ' -2 if execution did not occur when the instruction was called
  738.       ' (for instance, when using the optional parameters to stream data from a table and the number of records or time into interval conditions are not met).
  739.  
  740.     EndIf
  741.    
  742.  
  743.    
  744.     If IfTime(0,2, Sec) Then
  745.       'post current batt_volt to cosm.com (free service)
  746.       'see https://cosm.com/docs/ on API documentation
  747.  
  748.       http_header = "Content-Type:application/xml" 'enter unique ApiKey here
  749.       file_handle = FileOpen("CPU:Batt_Volt.csv", "w", -1)
  750.       FileWrite (file_handle, "1true" & DMM_CalcStr&"mm,"&LVDT_mm_CalcStr&"mm," & battVStr &"V,"& PTempStr &"°C,"& DMM_mode & "," & DMM_Power_OnStr&""  + CHR(10), 0)
  751.       FileClose (file_handle)
  752.    
  753.       http_post_tx = HTTPPut ("http://admin:[email protected]/ISAPI/System/Video/inputs/channels/1/overlays/text/1", "CPU:Batt_Volt.csv", http_post_response, http_header,,,,,500)
  754.     EndIf
  755.  
  756.     SLOWSCAN_STAGE = "End Of Slowscan"
  757.   Loop
  758.  
  759.   EndSequence
  760.  
  761. EndProg
captcha