Community discussions

MikroTik App
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

GPIO function for RBM33G

Tue Oct 31, 2023 3:52 pm

I wrote a function to work with GPIO RBM33G I/O lines. Probably not everything is rational in it, since I wrote with little time for optimization, just to work. But it works. Take who needs it. See the parameters in the function code. In general, this is $1 - action, $2 - line number (or all), $3 "direction" for $1 direction. If someone has a desire to optimize, or write their own better, I will say thank you.

P/S: Rex, come back, dear! It's boring without you.
:global iotLine do={
:global ioLineNONC
:local arrayPin {3; 5; 12; 13; 15; 16}
:local ioLine 6
:local act;

:if ([:len $ioLineNONC]=0) do={:set $ioLineNONC true}

:if ($1="help") do={
         /terminal style varname
         :put ""
         :put "Function works with GPIO RBM33G"
         :put "version 1.2"
         :put "by Sertik 30/10/2023"
         :put ""
         :put "\$1 - action: on/off/enable/disable/direction/info/help"
         :put "\$2 - line number, if not define - all"
         :put "\$3 - input/output for direction"
         :put "\$ioLineNONC - true (or not specified) - line for NO rele/ false - line for NC rele"
         :put ""
         /terminal style none}

 :if ([:len [/system routerboard settings get gpio-function]]!=0) do={
                              :local gpioanswer "Pin 12-16 is not set on GPIO.\n\rPlease reset routerboard gpio-function and reboot Router."
                              :put ("ERROR $0 function"."\r\n"."$gpioanswer")
                              :log error ("ERROR $0 function"."\r\n"."$gpioanswer")
                              :return $gpioanswer}       


     if (($1="info") and ([:len $2]=0)) do={
:local gio [/iot gpio digital print as-value]
:local count 0; local name; :local comment; :local script; :local dir; :local output; :local input;
:foreach i in=$gio do={
                            :set name ($gio->$count->"name")
                            :set comment ($gio->$count->"comment")
                            :set script ($gio->$count->"script")
                            :set dir ($gio->$count->"direction")
                            :set input ($gio->$count->"intput")
                            :set output ($gio->$count->"output")
                            :set count ($count+1)
         :put ("$count "."$name "."$dir "."$output "."$input "."$comment "."$script")
         :log warning ("$count "."$name "."$dir "."$output "."$input "."$comment "."$script")
  }
:return $gio}

:if (($1="info") && ([:len $2]=1)) do={
   :if (([:tonum $2]<=0) or ([:tonum $2]>$ioLine)) do={:return Error}
do {
                  :local state [/iot gpio digital get "pin$($arrayPin->([:tonum $2]-1))"]
                  :return $state
} on-error={:return Error}
}

:if (($1="enable") and ([:len $2]=1)) do={
  :if (([:tonum $2]<=0) or ([:tonum $2]>$ioLine)) do={:return Error}
do {
                               [/iot gpio digital set "pin$($arrayPin->([:tonum $2]-1))" disabled=no]
                               :local pinSet ([/iot gpio digital get "pin$($arrayPin->([:tonum $2]-1))"]->"disabled")
                               :if (!$pinSet) do={:return "Done"} else={:return "Not set"}
} on-error={:return Error}
}

:if (($1="disable") and ([:len $2]=1)) do={
  :if (([:tonum $2]<=0) or ([:tonum $2]>$ioLine)) do={:return Error}
do {
                               [/iot gpio digital set "pin$($arrayPin->([:tonum $2]-1))" disabled=yes]
                               :local pinSet ([/iot gpio digital get "pin$($arrayPin->([:tonum $2]-1))"]->"disabled")
                               :if ($pinSet) do={:return "Done"} else={:return "Not set"}
} on-error={:return Error}
}

:if (($1="disable") and ([:len $2]=0)) do={
   do {
                  :foreach Xpin in=$arrayPin do={
                  /iot gpio digital set "pin$Xpin" disabled=yes
                   }
        } on-error={:return Error}
:return Done}

:if (($1="enable") and ([:len $2]=0)) do={
   do {
                  :foreach Xpin in=$arrayPin do={
                  /iot gpio digital set "pin$Xpin" disabled=no
                   }
        } on-error={:return Error}
:return Done}


:if (($1="direction") and ([:len $3]=0)) do={
    :if (($2="input") or ($2="output")) do={
        do {
                  :foreach Xpin in=$arrayPin do={
                  /iot gpio digital set "pin$Xpin" direction=$2
                   }
        } on-error={:return Error}
  } else={:return Error}
:return Done}

:if (($1="direction") and ([:len $2]=1) and ([:len $3]>1)) do={
     :if (($3="input") or ($3="output")) do={
       :if ([:tonum $2]>$ioLine) do={:return "Error"}
            :do {
                  /iot gpio digital set "pin$($arrayPin->[:tonum $2])" direction=$3
                } on-error={:return Error}
     } else={:return Error}
:return Done}

     :if (($1="on") && ($ioLineNONC=true)) do={:set act 1}
     :if (($1="on") && ($ioLineNONC=false)) do={:set act 0}
     :if (($1="off") && ($ioLineNONC=true)) do={:set act 0}
     :if (($1="off") && ($ioLineNONC=false)) do={:set act 1}

:if ([:len $act]>0) do={

   :if ([:len $2]>0) do={
           :do {
                  :if (([:tonum $2]<=0) or ([:tonum $2]>$ioLine)) do={:return Error}
# check direction output - error
        :if ([/iot gpio digital get "pin$($arrayPin->([:tonum $2]-1))" direction]="input") do={:return "Error line $2 is set on INPUT"}
                  /iot gpio digital set "pin$($arrayPin->([:tonum $2]-1))" output=$act
                  } on-error={:return Error}
     } else={
                  :foreach Xpin in=$arrayPin do={
#check direction output for group line - sckip input line
         :if ([/iot gpio digital get "pin$Xpin" direction]="output") do={
                  /iot gpio digital set "pin$Xpin" output=$act}
                   }
                } 

 :return "Done"} else={:return "Error"}
}
# Examles (:global ioLineNONC true or non for NO line type rele, :global ioLineNONC false for line NC-type)

# [$iotLine help]
# [$iotLine info]
# :local R3 [$iotLine info 3]
# :put ([$iotLine info 2]->"disabled")
# [$iotLine direction 3 output]
# [$iotLine direction input]

# :log error ("Logical 1 on line 3 "."$[$iotLine on 3]")
# :delay 2s
# :log warning ("Logical 0 on line 3 "."$[$iotLine off 3]")

# [$iotLine on]
# [$iotLine off]

# [$iotLine enable/disable 1]
# [$iotLine enable/disable]
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: GPIO function for RBM33G

Tue Nov 28, 2023 1:46 pm

My new article about RBM33G https://habr.com/ru/articles/776908/

1. Control of the RBM33G Mikrotik via the second serial port
2. Connecting the remote control to the Microtik
3. Connecting the beeper module
4. Talking microtik - connecting the voice recording/playback module
5. Serial MP3 Player control via Laurent-5G
6. Connecting Arduino to Mikrotik (via USB) with port forwarding from the network to USB
7. Output of the GPIO connector to the router housing

Who is online

Users browsing this forum: No registered users and 10 guests