Kuna liini ülepinge tõttu kõrbes tehnikat juba mitu korda (ilmselt elektriku hooletuse tõttu), siis tekkis vajadus seadmeid kaitsta ja ka ülepinge kohta teateid saada.

Ülepinge eest kaitseb UPS. USB kaabli kaudu on võimalik ka liinipinget lugeda. Milline oleks aga selleks sobiv aparatuur? Loomulikult ei ole selleks vaja kallist arvutit. Kõne alla tuleb näiteks Orange Pi One, kuid tekkis huvi, kas saab ülesande lahendada ka OpenWrt ja TP-Link TL-MR3020 karbikesega.

Siin on siis kirjeldus, kuidas seda teha justnimelt TP-Link TL-MR3020-ga. Vaja on ka üht USB HUB-i, sest vastasel juhul UPS-i lihtsalt üles ei leita. Wiki lehel on ka niimoodi kirjas: The USB port on the TL-MR3020 is not compatible with USB devices that use low-speed (1.5Mbps) and only works properly with USB devices that use fast-speed (12Mbps) or high-speed (480Mbps).

Kuna TL-MR3020 flash mälu maht on vaid 4MB, siis tuleb kõik ebavajalik välja jätta.

Ubuntu käsurida:

cd ..
mkdir openwrt-imagebuilder
cd openwrt-imagebuilder
mkdir openwrt && cd openwrt
wget https://downloads.openwrt.org/chaos_calmer/15.05.1/ar71xx/generic/OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64.tar.bz2
tar -xvjf OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64.tar.bz2
cd OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64
make image PROFILE=TLMR3020 PACKAGES="kmod-usb-ohci -dnsmasq -kmod-gpio-button-hotplug -ppp -ppp-mod-pppoe -ip6tables -iptables  -wpad-mini"

Vajaliku faili leiame asukohast ~/openwrt-imagebuilder/openwrt/OpenWrt-ImageBuilder-15.05.1-ar71xx-generic.Linux-x86_64/bin/ar71xx/openwrt-15.05.1-ar71xx-generic-tl-mr3020-v1-squashfs-factory.bin

Nüüd siis ruuteri firmware flash mällu, kuid ligipääsuks on meil vaja oma arvutis panna staatiline ip aadress, sest dhcp teenust me mälu kokkuhoiu mõttes ruuterisse ei pannud.

Jätame siinkohal vahele, kuidas seadistada võrguparameetreid sobivaks. Pakettide installeerimine:

root@OpenWrt:~# opkg update
root@OpenWrt:~# opkg install kmod-usb-hid usbutils apcupsd wget-nossl

root@OpenWrt:~# lsusb
Bus 001 Device 003: ID 051d:0002 American Power Conversion Uninterruptible Power Supply
Bus 001 Device 002: ID 05e3:0608 Genesys Logic, Inc. USB-2.0 4-Port HUB
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
root@OpenWrt:~# opkg list-installed
apcupsd - 3.14.13-4
base-files - 157.2-r48532
busybox - 1.23.2-1
dropbear - 2015.67-1
firewall - 2015-07-27
fstools - 2016-01-10-96415afecef35766332067f4205ef3b2c7561d21
hostapd-common - 2015-03-25-1
iw - 3.17-1
jshn - 2015-11-08-10429bccd0dc5d204635e110a7a8fae7b80d16cb
jsonfilter - 2014-06-19-cdc760c58077f44fc40adbbe41e1556a67c1b9a9
kernel - 3.18.23-1-b2f200610f46d20ef52d269421369d0c
kmod-ath - 3.18.23+2015-03-09-3
kmod-ath9k - 3.18.23+2015-03-09-3
kmod-ath9k-common - 3.18.23+2015-03-09-3
kmod-cfg80211 - 3.18.23+2015-03-09-3
kmod-crypto-aes - 3.18.23-1
kmod-crypto-arc4 - 3.18.23-1
kmod-crypto-core - 3.18.23-1
kmod-hid - 3.18.23-1
kmod-hid-generic - 3.18.23-1
kmod-input-core - 3.18.23-1
kmod-input-evdev - 3.18.23-1
kmod-ipt-conntrack - 3.18.23-1
kmod-ipt-core - 3.18.23-1
kmod-ipt-nat - 3.18.23-1
kmod-ipv6 - 3.18.23-1
kmod-ledtrig-usbdev - 3.18.23-1
kmod-mac80211 - 3.18.23+2015-03-09-3
kmod-nf-conntrack - 3.18.23-1
kmod-nf-ipt - 3.18.23-1
kmod-nf-nat - 3.18.23-1
kmod-nf-nathelper - 3.18.23-1
kmod-nls-base - 3.18.23-1
kmod-usb-core - 3.18.23-1
kmod-usb-hid - 3.18.23-1
kmod-usb-ohci - 3.18.23-1
kmod-usb2 - 3.18.23-1
libblobmsg-json - 2015-11-08-10429bccd0dc5d204635e110a7a8fae7b80d16cb
libc - 0.9.33.2-1
libgcc - 4.8-linaro-1
libip4tc - 1.4.21-1
libip6tc - 1.4.21-1
libjson-c - 0.12-1
libjson-script - 2015-11-08-10429bccd0dc5d204635e110a7a8fae7b80d16cb
libnl-tiny - 0.1-4
libpcre - 8.38-1
libpthread - 0.9.33.2-1
librt - 0.9.33.2-1
libubox - 2015-11-08-10429bccd0dc5d204635e110a7a8fae7b80d16cb
libubus - 2015-05-25-f361bfa5fcb2daadf3b160583ce665024f8d108e
libuci - 2015-08-27.1-1
libusb-1.0 - 1.0.19-1
libusb-compat - 0.1.4-1
libxtables - 1.4.21-1
mtd - 21
netifd - 2015-12-16-245527193e90906451be35c2b8e972b8712ea6ab
odhcp6c - 2015-07-13-024525798c5f6aba3af9b2ef7b3af2f3c14f1db8
odhcpd - 2015-11-19-01d3f9d64486ac1daa144848944e877e7f0cb762
opkg - 9c97d5ecd795709c8584e972bfdf3aee3a5b846d-9
procd - 2015-10-29.1-d5fddd91b966424bb63e943e789704d52382cc18
swconfig - 10
uboot-envtools - 2014.10-2
ubox - 2015-11-22-c086167a0154745c677f8730a336ea9cf7d71031
ubus - 2015-05-25-f361bfa5fcb2daadf3b160583ce665024f8d108e
ubusd - 2015-05-25-f361bfa5fcb2daadf3b160583ce665024f8d108e
uci - 2015-08-27.1-1
usbutils - 007-1
usign - 2015-05-08-cf8dcdb8a4e874c77f3e9a8e9b643e8c17b19131
wget-nossl - 1.17.1-1
zlib - 1.2.8-1
root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
rootfs                    1.2M      1.1M    104.0K  91% /
/dev/root                 1.5M      1.5M         0 100% /rom
tmpfs                    14.0M    612.0K     13.4M   4% /tmp
/dev/mtdblock3            1.2M      1.1M    104.0K  91% /overlay
overlayfs:/overlay        1.2M      1.1M    104.0K  91% /
tmpfs                   512.0K         0    512.0K   0% /dev

Igaks juhuks on siin toodud kõigi installeeritud pakettide nimekiri. Peaaegu kogu mäluruum sai ka kasutatud.

APC UPS-i seadistamiseks muudan faili apcupsd.conf:

vi /etc/apcupsd/apcupsd.conf

UPSCABLE usb
UPSTYPE usb


/etc/init.d/apcupsd stop
apctest

apctest peab saama UPS-ga ühendust.

Käivitan uuesti apcupsd teenuse:

/etc/init.d/apcupsd start  

Kui teenus töötab, siis käivitan:

root@OpenWrt:~# apcaccess
APC      : 001,034,0833
DATE     : 2016-10-27 13:08:02 +0000
HOSTNAME : OpenWrt
VERSION  : 3.14.13 (02 February 2015) unknown
UPSNAME  : ups1
CABLE    : USB Cable
DRIVER   : USB UPS Driver
UPSMODE  : Stand Alone
STARTTIME: 2016-10-27 08:01:44 +0000
MODEL    : Back-UPS ES 700G
STATUS   : ONLINE
LINEV    : 232.0 Volts
LOADPCT  : 0.0 Percent
BCHARGE  : 100.0 Percent
TIMELEFT : 38.4 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
SENSE    : Medium
LOTRANS  : 180.0 Volts
HITRANS  : 266.0 Volts
ALARMDEL : 30 Seconds
BATTV    : 13.5 Volts
LASTXFER : No transfers since turnon
NUMXFERS : 0
TONBATT  : 0 Seconds
CUMONBATT: 0 Seconds
XOFFBATT : N/A
STATFLAG : 0x05000008
SERIALNO : 5B1633T44043
BATTDATE : 2016-08-19
NOMINV   : 230 Volts
NOMBATTV : 12.0 Volts
FIRMWARE : 871.O4 .I USB FW:O4
END APC  : 2016-10-27 13:08:13 +0000

Siin "LINEV    : 232.0 Volts" on just meid huvitav liinipinge.

Nüüd siis pisike skript, mis hakkab meie liinipinget jälgima:

#!/bin/sh
limit=235.0

while true; do
  result=$( apcaccess | grep "LINEV    :" )
  voltage="$( echo $result | awk -F":" '{print $2}' | awk '{print $1}' )"
  var=$(echo "$voltage $limit" | awk '{print ($1 > $2)}')
  if [ "$var" -eq 1 ]
  then
     data="overvoltage=$voltage"
     echo $data
     wget --http-user=user --http-password=password -q -O /dev/null --post-data $data http://www.infokiir.ee/postups/
  fi
  sleep 3
done

Loomulikult soovime, et meie skriptikene ruuteri käivitumisel ise käima läheks.

Olgu selle nimeks /root/lineinweb.sh. Sellisel juhul lisame faili /etc/rc.local:

root@OpenWrt:~# cat /etc/rc.local
# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.
/root/lineinweb.sh &
exit 0
root@OpenWrt:~#

Serveri poolel index.php:

<?php
$valid_passwords = array ("user" => "password");
$valid_users = array_keys($valid_passwords);

$user = $_SERVER['PHP_AUTH_USER'];
$pass = $_SERVER['PHP_AUTH_PW'];

$validated = (in_array($user, $valid_users)) && ($pass == $valid_passwords[$user]);

if (!$validated) {
  header('WWW-Authenticate: Basic realm="My Realm"');
  header('HTTP/1.0 401 Unauthorized');
  die ("Not authorized");
}

$date = date('Y-m-d H:i:s', time());
$overvoltage = $_POST["overvoltage"];
file_put_contents('postlog.txt', $date.':overvoltage:'.$overvoltage."\r\n", FILE_APPEND | LOCK_EX);

See php skript on vaid näidis. Ilmselt soovime endale saata ülepinge puhul SMS-i või maili. Jätame siinkohal ära kirjelduse, kuidas seda teha. 

Linke:

https://wiki.openwrt.org/toh/tp-link/tl-mr3020

http://www.orangepi.org/orangepione/

https://wiki.openwrt.org/doc/howto/obtain.firmware.generate

https://forum.openwrt.org/viewtopic.php?id=16094

http://stackoverflow.com/questions/16072351/how-to-assign-an-output-to-a-shellscript-variable

http://stackoverflow.com/questions/11237794/how-to-compare-two-decimal-numbers-in-bash-awk

http://php.net/manual/en/features.http-auth.php