Back to Home

We connect Arduino to Mikrotik. Cheap and angry remote load management

Mikrotik Arduino NetPing KVM

We connect Arduino to Mikrotik. Cheap and angry remote load management

    I want to tell you about one integration "on the knee", which nevertheless has been working in my home laboratory for several months without any problems.

    I have one self-assembled iron server, let's call it NAS, which has one strange feature. Periodically during a regular reboot, it stops at some final stage of preparation for shutdown and does not reach a reset. It remains on and uncontrollable. I reboot it infrequently, but there is a “glitch” to be. To look for the reason to me, frankly, is a bit too lazy. Maybe someday I will update the BIOS or wait for some corrections in the OS with the next update. But, on the other hand, I still would like to have control over my infrastructure, so I decided to go the alternative way. Yes, I’ll immediately inform you that the server does not support HP iLO, Intel vPro, and other similar features that would be useful, but “didn’t.”

    So, if the server itself cannot turn off, then you need to connect some autonomous device with a relay and a port for network control to the power control circuits. There are many such devices with different capabilities, up to industrial controllers, and for different money. But you can collect this on your own with minimal time and money on the basis of what is. I used the following components:
    1. Mikrotik RB951G-2HnD router with free USB port, actively used on my network.
    2.
    Board for self-made Arduino Mega 2560 3. Optocoupler 4N35 for emulation of pressing the Power Server (ATX) button.
    We connect. The control circuit is ready!

    Mikrotik RB951G-2HnD <--USBSerial--> Arduino Mega 2560 <- optocoupler -> Power Button NAS

    And now more about how to make it work.

    What can Mikrotik


    Search the Mikrotik documentation ( http://wiki.mikrotik.com/wiki/Supported_Hardware) issued a list of devices and manufacturers supported in RouterOS USBSerial, they turned out to be chips from FTDI and Prolific. This inspires hope for a positive result. At my disposal were an Arduino Mega 2560 board with an ATmega8U2 chip and a limited-availability Arduino Mega 1280 with FTDI FT232RL. I did not particularly doubt the possibility of connecting the latter, but the first board was of more interest. The USB port of the Arduino Mega 2560 is connected to the ATmega8U2 microcontroller, in which the USBSerial firmware is spinning, which can be replaced with USB HID Keyboard firmware if desired. In general, it was not clear whether the USBSerial ATmega8U2 was determined by Mikrotik, but it turned out that RouterOS perfectly sees the connected Arduino with ATmega8U2 and installs the USBSerial port.



    And what can Arduino


    Reflected on several options for the Arduino control interface and peripherals. At first I thought to sew some Master-Slave protocol for remote control, for example, Modbus RTU. But for him, it would be necessary to forward the virtual port to the administrator's workstation (on the Mikrotik website there is a description of how to connect such a virtual port) and write, besides the Arduino firmware, another client Modbus application for management or use programs like Modbus Poll to directly edit Modbus registers. I did not really like this option because of the excessive complexity in operation. So pretty quickly, I came up with the idea of ​​a text command shell for Arduino, similar to the Linux Bash. And it turned out that such a command processor has already been written by kind people. I decided to use it. Project website:bitlash.net
    bitlash here! v2.0 (c) 2012 Bill Roy -type HELP- 942 bytes free
    > print "Hello, world!", millis()
    Hello, world! 11939
    >
    

    The bitlash command shell turned out to be quite developed, it covered my task with interest. In it, you can create command macros and a start macro, which will be written to a non-volatile EEPROM. Here's what my set for controlling the Power server button looks like, output from the ls command:
    > ls
    function off12 {d12=1;delay(8000);d12=0;};
    function startup {pinmode(12,1);};
    function on12 {d12=1;delay(250);d12=0;};
    >
    

    When Arduino starts, discrete pin 12 is configured for recording (startup macro). The administrator can write short commands “on12”, “off12” to the command line to turn the server on and off, as well as edit and create new macros.

    In my project, I used Arduino Mega, only because I had it, but I'm sure you can replace it with Arduino Due, or Nano with FTDI FT232RL, or ATmega16U2 with usbserial firmware.

    Peripherals Arduino. Optocoupler


    With the hardware of the control, everything is quite simple. You can connect a relay module, which will provide galvanic isolation and the ability to control a powerful load, this is the most universal option.
    But for myself, I did differently: I just needed to turn the server on and off (to be able to do this with guarantee), then I just connected the output of the 4N35 optocoupler at the terminals of the Power on / off button, which resulted in galvanic isolation.
    The scheme is approximately as follows:


    Output we connect in parallel to the server power button. The polarity matters, you can simply pick it up using the “poke method”. Between pin 12 of Arduino and the optocoupler, a resistance of about 500 Ohms is set.

    Forwarding serial port on Mikrotik in Telnet


    The final touch. Let's organize the console by forwarding the serial port to some TCP port of Mikrotik. I do not use telnet as a service of Mikrotik itself, so with a clear conscience I am giving this port to the Arduino console. Allow access only from local network addresses. Perhaps a couple more prohibition rules in the firewall.
    Port Settings:


    On the video, the process of connecting via Telnet and executing macro commands:


    Conclusion


    Shrink resistor optocoupler:


    Controller in container:


    Each time the Arduino connection is opened, it restarts because the serial port is reopened, which theoretically even positively affects the stability of work. You can manage many loads in the future. You can fasten the temperature sensor and write a command to output this temperature, you can monitor analog and discrete signals. Commands are entered in a regular terminal program (Putty, TeraTerm) into a Telnet (or Raw) connection. In addition, you can, in principle, send commands to the Arduino terminal and from RouterOS Mikrotik scripts.
    You can go even further. If you take a separate USB <-> UART converter and connect it to another Arduino UART (2) and Mikrotik, and fill in the HID firmware of the keyboard in the built-in USB ATmega16U2 (8U2) controller, you can get a similarity to KVM, only without V and M :) , i.e. hardware keyboard emulator with remote access. In general, much more can be done if desired.

    Read Next