Community discussions

MikroTik App
 
martc
just joined
Topic Author
Posts: 5
Joined: Sat Nov 26, 2022 12:33 pm

mqtt script, simple data

Sun Nov 27, 2022 2:17 am

Hi, im not familiar with scripting, i try send to mqtt modbus data.
all work fine
################################ Configuration ################################
# Name of an existing MQTT broker that should be used for publishing
:local broker "MQTT"

# MQTT topic where the message should be published
:local topic "modbuss/Temperature/DS18B20"

#################################### System ###################################


:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x1 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x2 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x3 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x4 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x5 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x6 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x7 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x8 reg-addr=0x0 as-value once]
:local modbus [:put [($modbusread->"values")]]

#################################### MQTT #####################################
:local message \
"{\"TEMP\":\"$modbus\"}"
###info LOG fails
  #  :log info "$message";
    :put ("[*] Total message size: $[:len $message] bytes")
    :put ("[*] Sending message to MQTT broker...")
    /iot mqtt publish broker=$broker topic=$topic message=$message
    :put ("[*] Done")
and i get - {"TEMP":"228"};{"TEMP":"32768"};{"TEMP":"32768"};{"TEMP":"227"};{"TEMP":"229"};{"TEMP":"32768"};{"TEMP":"32768"};{"TEMP":"227"}
how i can get just a simple string like - {228, 32768, 32768, 227, .....} ?
and if i understand right from script i can get only whole numbers? than i need make divide at other side not script..
Thanks.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: mqtt script, simple data  [SOLVED]

Sun Nov 27, 2022 4:49 pm

If you put "TEMP:" on message how can yu have only numbers without TEMP and without ":" ?
:local message "{$[:tostr $modbus]}"

i do not have "iot" but all this:
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x1 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x2 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x3 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x4 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x5 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x6 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x7 reg-addr=0x0 as-value once]
:local modbusread [/iot modbus read-holding-registers slave-id=0x08 num-regs=0x8 reg-addr=0x0 as-value once]
porobably is all useless because modbusread have the value of last "local" and all previous local are completely useless....


also what the hell you use :put???
is just this
:local modbus ($modbusread->"values")

# or all the "local" lines are simply replaced by:
:local modbus ([/iot modbus read-holding-registers slave-id=0x08 num-regs=0x8 reg-addr=0x0 as-value once]->"values")
 
martc
just joined
Topic Author
Posts: 5
Joined: Sat Nov 26, 2022 12:33 pm

Re: mqtt script, simple data

Mon Nov 28, 2022 12:43 am

If you put "TEMP:" on message how can yu have only numbers without TEMP and without ":" ?
:local message "{$[:tostr $modbus]}"
# or all the "local" lines are simply replaced by:
:local modbus ([/iot modbus read-holding-registers slave-id=0x08 num-regs=0x8 reg-addr=0x0 as-value once]->"values")
[/code]
Youre right, i just think read all address from device (8) but "num-regs=0x8" cover address from 1 to 8. as you say all this line was useless and maybe its reason why sometime i get "pool timeout". About TEMP i forget delete, i try get line like (TEMP1:230; TEMP2:220; TEMP3:240... etc) i dont more need this, i want just simple digit string (230, 220,230, ...) all 8 readings.
Your code " "{$[:tostr $modbus]}"[" is right what im searching for now im get "{206;32768;32768;205;205;32768;32768;205}", thanks!
 
Chiara
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Thu Jul 23, 2015 3:47 pm

Re: mqtt script, simple data

Thu Jan 19, 2023 11:22 am

May I ask you how did you managed to enable the port at startup?

# NAME START-DATE START-TIME INTERVAL ON-EVENT RUN-COUNT
0 modbus_poll jan/19/2023 09:51:31 1s modbus2mqtt 108
1 modbus_init startup 0s modbusinit 1

/system scheduler
add interval=1s name=modbus_poll on-event=modbus2mqtt policy=\
ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon start-date=jan/19/2023 start-time=09:51:31
add name=modbus_init on-event=modbusinit policy=ftp,reboot,read,write,policy,test,password,sniff,sensitive,romon \
start-time=startup

But it seems that "startup" is ignored and port is still disabled when start modbus_poll it got script error "Connection refused".

Who is online

Users browsing this forum: No registered users and 28 guests