Community discussions

MUM Europe 2020
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

parse data from local file help please

Wed Aug 08, 2018 8:05 pm

I have a TM-Mikrotik temp logger ( papouch.com ) connected to the serial console of a RB2011.
it also can sens for a open/close sensor like a door sensor for example.

it creates an tm.dat file on the RB2011 with this contents :

+025.5C;H;

+025.5C;H;

+025.5C;H;

etc. etc.

where 25.5C is the temperature and H is the contact ( is L when pins are shortened )

Now i want to read this file by script and send me an alarm message when either the temperature is above 35C, and/or the contact is L

I have some experience with the Voltage / Temp alarms script here on the web, but now i am ran out of knowledge how to parse the data from this file.
can someone give me a little light on how to get the latest data line from this file with a script ?
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

Re: parse data from local file help please

Wed Aug 08, 2018 9:53 pm

ok i found i had to change the tm.dat file to tm.txt

no i can read the content with
( from terminal )

:put [/file get tm.txt contents]

will give me :

+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;
+025.6C;H;

so that command is working.

now i think i best can do a search within all the numbers and detect an higher than 35.5C and / or and L ?
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

Re: parse data from local file help please

Wed Aug 08, 2018 10:27 pm

one step at a time, found a solution

from script:

global check [/file get tm.txt contents];
:log info [:pick $check 2 4 ]
:log info [:pick $check 8 ]

will give me :
25
H

in log file.

i can proceed writing it into a basic email alarm script.
so its working, now maybe someone can think of a smarter solution.
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

Re: parse data from local file help please

Thu Aug 09, 2018 1:24 pm

seems harder then i thought.

i know of this sample code from Wiki. but have hard time to understand how to get the latest line/entry from a file.

can someone help me please

# i understand this gets the data from file
:global content [/file get [/file find name=tm.txt] contents] ;
:global contentLen [ :len $content ] ;

# now this i dont understand

:global lineEnd 0;
:global line "";
:global lastEnd 0;

# i understand this does get something done

:do {
       :set lineEnd [:find $content "\r\n" $lastEnd ] ;
       :set line [:pick $content $lastEnd $lineEnd] ;
       :set lastEnd ( $lineEnd + 2 ) ;

       :local tmpArray [:toarray $line] ;
	:if ( [:pick $tmpArray 0] != "" ) do={
	:put $tmpArray;
         /ppp secret add name=[:pick $tmpArray 0] password=[:pick $tmpArray 1] \
             local-address=[:pick $tmpArray 2] remote-address=[:pick $tmpArray 3] \
             profile=[:pick $tmpArray 4] service=[:pick $tmpArray 5];
}
} while ($lineEnd < $contentLen)
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

Re: parse data from local file help please

Mon Aug 13, 2018 4:30 pm

Alright with help of a friend ( Aalte Zuidema ), this works to get the last results from file.
now i only have to add a script to do something with the results, which can be found all over the internet.
:global content [/file get [/file find name=tm.txt] contents] ;
:global contentLen [ :len $content ] ;

:global lineEnd 0;
:global line "";
:global lastEnd 0;

:do {
  :set lineEnd [:find $content "\n" $lastEnd ] ;
  :set line [:pick $content $lastEnd $lineEnd ] ;
  :set lastEnd ( $lineEnd +2 ) ; 
  
  :global entry [:pick $line 0 $lineEnd ]
  
} while ($lastEnd < ( $contentLen -2 ) )

  :log info [:pick $line 2 7 ] 
  :log info [:pick $line 8 ]


also add a scheduler script to refresh the tm.txt once or twice a day day. else the file will be grown too big to load.
 
hengst
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 86
Joined: Sun Jan 03, 2010 3:04 pm

Re: parse data from local file help please

Mon Aug 13, 2018 4:51 pm

here the link to the thermometer with I/O contact. ( wish for a version with more I/O contacts )

https://www.papouch.com/en/shop/product ... -mikrotik/

i did connect it to the console port of Mikrotik ( disable console function in menu, and set routerboard to silent boot )

i used these to connect the Mikrotik console port to the db9 connector , in my case i had to remove the rj45 connector and put on a new one as the pinouts were mirrored.

https://www.amazon.co.uk/COOSO-Serial-F ... B01MF9F7HF

rj45-db9 male for the TM-mikrotik temp/contact reader.

1=where you normally put your orange/white

1=Gray
2=blue
3=green
4=orange
5=yellow
6=red
7=brown
8=black

but your cable colours could be different, so check it.

https://wiki.mikrotik.com/wiki/Manual:S ... al_Console

Who is online

Users browsing this forum: No registered users and 9 guests