Community discussions

MikroTik App
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

script and json

Tue Aug 24, 2021 6:24 pm

I wanted to monitor with domoticz the temperature of a modem on a RBM11 routerboard.
I detect the temperature of the modem with a script that is saved every minute in the variable temp2.
with the command via browser http://10.100.0.202:8080/json.htm?usern ... &svalue=30
where 10...is the home automation server idx=27 the virtual sensor on domoticz and svalue=30 the temperature entered manually.
With this command from the browser everything works.

However, if I try via mikrotik script nothing works
/tool fetch http-method=post http-header-field="Content-Type: application/json" http-data="{username=xxx&password=xxx&type=command&param=udevice&idx=27&nvalue=0&svalue=$temp2}" url="http://10.100.0.202:8080"
what is the correct line to write?
There are no instructions in the mikrotik manual.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script and json

Tue Aug 24, 2021 7:59 pm

Search first:
viewtopic.php?f=9&t=177364&p=871386&hil ... on#p871287

and url must be ...202:8080/json.htm not only ...202:8080
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: script and json

Tue Aug 24, 2021 8:29 pm

and your command via browser is not even "post", but "get", equivalent with fetch would be to use http-method=get and the same full URL as in the browser.
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json

Tue Aug 24, 2021 11:45 pm

:local url "http://10.100.0.202:8080/json.htm?username=xxx&password=xxx&type=command&param=udevice&idx=27&nvalue=0&svalue="
/tool fetch http-method=get http-header-field="content-type:application/json" url=$url $temp2
I have modified it this way but the script does not work
the variable $temp2 seems to be ignored
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script and json

Wed Aug 25, 2021 12:00 pm

:local url "http://10.100.0.202:8080/json.htm?username=xxx&password=xxx&type=command&param=udevice&idx=27&nvalue=0&svalue="
/tool fetch http-method=get http-header-field="content-type:application/json" url=$url $temp2


$temp2 is writed at random position?
At least url="$url$temp2"


For terminal adding { } and \ before ?, removig anything you do not do in browser, like set content or specify get or post:
{
:local url="http://10.100.0.202:8080/json.htm?username=xxx&password=xxx&type=command&param=udevice&idx=27&nvalue=0&svalue=$temp2"
/tool fetch keep-result=no url=$url
}


For script (useless { } \ )
:local url="http://10.100.0.202:8080/json.htm?username=xxx&password=xxx&type=command&param=udevice&idx=27&nvalue=0&svalue=$temp2"
/tool fetch keep-result=no url=$url
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json

Wed Aug 25, 2021 6:37 pm

temp2.png
I modified the script like yours above but it still doesn't work.
temp2 is a global variable obtained from another script every minute
You do not have the required permissions to view the files attached to this post.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script and json

Wed Aug 25, 2021 6:59 pm

if you place, for test, a right value instead of $temp2, work?

post the temp2 script
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json

Wed Aug 25, 2021 7:06 pm

no if I enter a value it does not work.
I think it's not a problem with the variable but with the way fetch works.
Here is the script
:set temp1 55
:global temp (:tostr(:put [/ interface lte at-chat input="at+qtemp" lte1 wait=yes as-value]));
:global tempstring ([:pick $temp 0]);
:global temp2 ([:pick $tempstring 47] . [:pick $tempstring 48]);
:if ($temp2 > $temp1) do= {/tool fetch url="https://api.telegram.org/xxxxxx/text=EM160 Warning!!! Temperature is $temp2 Degrees" keep-result=no}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script and json

Wed Aug 25, 2021 7:21 pm

I do not know the format returned, but this is the right way to do that, :put is only for display on terminal the items, not for read results of operation.
The script is all it? You cut something (ignore censoring)???
# dice allo script di considerare la variabile temp1 gia' esistente
:global temp1
:set temp1 55
# mette in temp2 il risultato della richiesta, ma va tagliato con :pick, come hai già fatto, se non da un numero e basta
:global temp2 ([/interface lte at-chat input="at+qtemp" lte1 wait=yes as-value]->"output")
:if ($temp2 > $temp1) do={
    /tool fetch keep-result=nourl="https://api.telegram.org/xxxxxx/text=EM160 Warning!!! Temperature is $temp2 Degrees"
}
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json

Wed Aug 25, 2021 9:35 pm

yes that's it, that script sends a telegram message if the modem temperature is above 55 degrees and monitors the temperature every minute.
My problem, however, is to send that string http: //10.100.0.202 .... and retrieve the temperature from the temp2 variable. i can't find documentation for scripts with json .
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: script and json

Wed Aug 25, 2021 10:43 pm

but if pasted on browser work, without doing anything special, on fetch url must wokrs, but all non "a-z" and "0-9" on censored values must be url converted, browser do it automatically.
For example if you have & or ! on password, you must urlencode it and put on fetch url
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json

Thu Aug 26, 2021 12:32 am

the censored parts do not have any special characters or spaces they are only letters and numbers, I also tried to convert the whole string http://10.100.0.... with an on line urlencoder but it does not work. the script does not create any url variable in Environment. I have Ros 7.1rc1.
 
User avatar
frank333
Member
Member
Topic Author
Posts: 330
Joined: Mon Dec 18, 2017 12:17 pm
Location: S.Marino Router model: RB3011UiAS-RM+RBM11G

Re: script and json  [SOLVED]

Tue Nov 23, 2021 8:25 pm

I solved by forwarding port 8443 !!!!

script on the LTE router and should be scheduled every 4-5 minutes
---------------------------------------------------------------------
#temperature read by another script
:global temp2

#temp value transfer to domoticz on idx27 through NAT port 8443   
:global url "https://192.168.x.x:8443/json.htm?username=user&password=password&type=command&param=udevice&idx=27&nvalue=0&svalue=$temp2";
/tool fetch keep-result=no url=$url mode=https
-------------------
#forwarding rule on the main router
/ip firewall nat add chain=dstnat dst-port=8443 action=dst-nat protocol=tcp to-address=10.100.0.202 to-port=8443
Schermata del 2021-11-23 18.53.01.png
You do not have the required permissions to view the files attached to this post.
 
msatter
Forum Guru
Forum Guru
Posts: 2897
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: script and json

Tue Nov 23, 2021 9:51 pm

Nice work. Congratulations succeeding in this.

Who is online

Users browsing this forum: alexantao, rextended and 30 guests