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 conditionally NAS, which has one strange feature. Periodically, with a regular reboot, it stops at some final stage of preparation for shutdown and does not reach a reset. So it remains on and unmanaged. I restart it infrequently, but the "glitch" is the place to be. To be honest, to search for the cause is a little lazy. Maybe someday I will update the BIOS or wait for some fixes 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 take an alternative route. Yes, I immediately inform you that the server does not support HP iLO, Intel vPro, and other similar features that would be useful, but “not shmogli”.
So, if the server itself can not be turned off, then you need to connect to the power control circuits some kind of stand-alone device with a relay and a port for network control. There are many such devices with different capabilities, including industrial controllers, and for different money. But you can build 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 a free USB port, actively used in my network. 2. Fee for DIY Arduino Mega 2560 3. Optocoupler 4N35 to emulate 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
A search for Mikrotik documentation ( http://wiki.mikrotik.com/wiki/Supported_Hardware ) gave a list of USB devices supported by RouterOS and manufacturers, they turned out to be chips from FTDI and Prolific. This gives hope for a positive result. I had an Arduino Mega 2560 board with an ATmega8U2 chip and a limitedly available Arduino Mega 1280 c FTDI FT232RL. I didn’t doubt the possibility of connecting the latter, but the first motherboard was of more interest. The USB port of the Arduino Mega 2560 is brought into the ATmega8U2 microcontroller, in which USBSerial firmware is “spinning”, which you can optionally replace with the USB HID Keyboard firmware. In general, whether USBSerial ATmega8U2 in Mikrotik was determined, it was not clear, 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 software interface and peripherals. At first I was thinking of stitching up some Master-Slave protocol for remote control, for example, Modbus RTU. But for him it would be necessary to forward a virtual port to the administrator's workstation (on the Mikrotik website there is a description of connecting such a virtual port) and to write, in addition to the Arduino firmware, another client-based Modbus application to manage or use programs like Modbus Poll to directly edit Modbus registers. This option I did not like much because of the excessive complexity in operation. So pretty quickly I came to the idea of a text shell for Arduino, like Bash in Linux. And it turned out that such a command processor has already been written by good people. I decided to use them. 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 command shell of bitlash was fully developed, it covered my task with interest. It can create command macros and start macros to be written to a non-volatile EEPROM. Here is what my set looks like to control the server’s Power button, 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 you start the Arduino configured discrete output 12 to write (startup macro). The administrator can write short “on12”, “off12” commands 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 am sure you can replace it with Arduino Due, or Nano with FTDI FT232RL, or ATmega16U2 with usbserial firmware.
Peripherals Arduino. Optocoupler
With the hardware control, it's pretty 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 it differently: I just needed to turn the server on and off (to be able to do it guaranteed), then I just parallel to the Power on / off button at the terminals I connected the output of the 4N35 optocoupler, which gave a galvanic isolation. The scheme is approximately as follows:
Output is connected in parallel to the server power button. Polarity matters, you can just pick up "at random." Between the output of 12 Arduino and optocoupler set resistance of about 500 ohms.
Forwarding serial port to Mikrotik in Telnet
The final touch. Let's organize the console by forwarding the serial port to some TCP microtic port. I do not use telnet as a service of the microtic itself, so with a clear conscience I give this port under the Arduino console. Allow access from local network addresses only. Perhaps a couple more prohibitory rules in the firewall. Port Settings:
On the video process of connecting via Telnet and executing macro commands:
Conclusion
Optos with a resistor in heat shrinkage:
The controller in the container:
Each time the connection is opened, the Arduino restarts, because the serial port is reopened, which theoretically even positively affects the stability of work. You can manage in the future a variety of loads. You can screw the temperature sensor and write a command to output this temperature, you can monitor analog and discrete signals. Commands are entered in the usual 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 UART (2) Arduino and to Mikrotik, and add the HID keyboard firmware to the built-in USB ATmega16U2 (8U2) controller, you can get a KVM similarity, only without V and M :) i.e. emulator hardware keyboard with remote access. In general, if you wish, you can do a lot more.