📜 ⬆️ ⬇️

Iron Budget Habramerka (ZhBH) karma, rating habratopika and other parameters on arduino + openwrt

Hi Habr!

I present to the public my Iron Budget Habramerka.


')
ZHBH allows you to measure and display the parameters of karma, rating and position in the overall rating. In addition, it compares favorably with software counterparts and allows you to keep track of fresh habratopic, showing who pledged, minusanul, added to favorites and total views. All this disgrace is implemented on arduin (yes, sad arduin, but there is a highlight, read on) and used the TP-link wr1043nd router with openWRT firmware as a gateway to the Internet (Any router that supports openwrt_ will use)

Have you already combed your hands to collect such a thing?



We need only a few things, the LCD screen 1602, a variable resistor of any rating, an arduino mini pro and a cheap usb-ttl adapter to pl-2303. Very budget set for creativity. The LCD screen is 80 rubles, the resistor is for nothing, the arduino about the mini is 100 rubles, usb-ttl is 30 rubles, experience is priceless.



The screen is connected via standard instructions in pictures found on the Internet, brightness is adjusted, USB is connected.

The second important thing is the router. A list of supported hardware can be found here. Wiki.openwrt.org/toh/start

The firmware of each router is individual, but it is detailed in the Wiki on the OpenWRT site, so I will not describe it. I note that it is best to immediately bring the internal UART connector to the outside, and you can bring it back to life, and it is easier to debug scripts via putty — ALL data is output via the serial port, while not everything is visible on ssh. During the tests, I have already restored the firmware several times through the COM + ttftp server. All manuals for working with iron and the initial installation is on the site.



After the firmware we set up the Internet, it is usually enough to insert the cable from the home router into the WAN router with openWRT and the Internet in the device already exists. I set up the Internet via wi-fi, using the Web interface. Not all the firmware immediately has a web interface, see the documentation for your router.

So, we will assume that half a day has passed, Openwrt is on the router, the Internet is configured on the router, we have entered Putty under the root and are ready to execute any commands.

Add the necessary packages. (I had to update wget - the version on the router did not support cookies - more on that later)

opkg update opkg install wget opkg install kmod-usb-acm opkg install kmod-usb-serial-pl2303 


If you use another usb adapter for arduin, you will need to put your package under this adapter. As a result of the actions, the device / dev / ttyUSB0 should appear (It may be called otherwise, depending on the firmware and the package for the USB-com adapter)

We go into / usr / and create a script, for example up.sh with the following content (respected habrovtsy, you need to remove 0000 (four zeros) in the code) it turned out that the text of the habratope parsed incorrectly due to the presence in the code of triggers by which the values ​​were selected, to redo laziness so that the habrameter worked added zeroes)

Hidden text
 wget --load-cookies /usr/cookies.txt -U "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0" http://habrahabr.ru/post/220893/ -O /tmp/index.html awk -F ';' '/&ua0000rr;/ {print substr($2,0,index($2," " ))}' /tmp/index.html>/usr/up.txt UP=`awk 'NR == 1' /usr/up.txt` echo 'u='$UP > /dev/ttyUSB0 awk -F ';' '/&ua0000rr;/ {print substr($3,0,5)}' /tmp/index.html>/usr/down.txt sed 's/\"/ /g' /usr/down.txt>/usr/down2.txt DOWN=`awk 'NR == 1{print$1}' /usr/down2.txt` echo 'd='$DOWN > /dev/ttyUSB0 VW=`awk -F '>' '/pageviews/ {print substr($2,0,index($2,"<" )-1)}' /tmp/index.html` echo 'v='$VW > /dev/ttyUSB0 FW=`awk -F '>' '/favs_count/ {print substr($2,0,index($2,"<" )-1)}' /tmp/index.html` echo 'f='$FW > /dev/ttyUSB0 wget http://habrahabr.ru/api/profile/romanvl/ -O /tmp/index2.html KARMA=`awk -F '>' '/karma/ {print int(substr($2,0,index($2,"<" )-1))}' /tmp/index2.html` echo 'k='$KARMA > /dev/ttyUSB0 RATE=`awk -F '>' '/rating>/ {print int(substr($2,0,index($2,"<" )-1))}' /tmp/index2.html` echo 'r='$RATE > /dev/ttyUSB0 POS=`awk -F '>' '/ratingPosition/ {print substr($2,0,index($2,"<" )-1)}' /tmp/index2.html` echo 'p='$POS > /dev/ttyUSB0 



Explanations are needed here.

1) Authorization on Habré is something with something. Without authorization, data on the rating of habratopika is not given, i.e. The guest simply will not see the necessary numbers. Therefore, cookies are used, saved from the browser, after successfully logging into Habr. I used Cookie Exporter 1.5 - an add-on for FF, which exports ALL cookies in a format understandable by Wget. I deleted everything that doesn’t belong to the Habra file from the file with all cookies I filled it via WinSCP to the router in the / usr folder / I highly recommend not sending my cookies to anyone.

2) Parsing the necessary values ​​is done through awk - a smart thing, but completely alien to the simple user =). For a very long time I suffered from awk, made cool crutches, as a result I got the result, I am sure, I can do better, but, for me, it works (c)

3) I took the values ​​of karma from an official source, although it could be parsed in the same way as the data on habratopic.

4) Data is transferred to arduin in a very interesting way! After connecting the arduine via USB to the router, communication in both directions becomes available through the port at 9600 speed (this is the default speed for my packet under pl-2303? I didn’t change, no need) It would be possible to send a string with data to arduin, arduin would parse the string and display it. But! In the most successful way I stumbled upon this bitlash.net (If someone sees it for the first time, look necessarily a smart thing.) In a nutshell, this is the command interpreter. Allows to program arduin via command line. In my case, pass the values ​​of variables and display them on the screen. I advise you to read the docks, for me it was a discovery. Before you run the code, download and install the bitlash library.

Code for arduine

Hidden text
 #include "bitlash.h" #include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); byte UP[8] = { 0b00100, 0b01010, 0b10001, 0b11011, 0b01010, 0b01010, 0b01010, 0b01110 }; byte DWN[8] = { 0b01110, 0b01010, 0b01010, 0b01010, 0b11011, 0b10001, 0b01010, 0b00100 }; byte FW[8] = { 0b00100, 0b10101, 0b11111, 0b01110, 0b01110, 0b11011, 0b10001, 0b00000 }; byte VW[8] = { 0b00000, 0b00000, 0b01110, 0b10001, 0b10101, 0b10001, 0b01110, 0b00000 }; void setup(void) { lcd.createChar(0, UP); lcd.createChar(1, DWN); lcd.createChar(2, FW); lcd.createChar(3, VW); initBitlash(9600); lcd.begin(16, 2); } void loop(void) { lcd.setCursor(0,0); // lcd.print("U"); lcd.write((uint8_t)0); lcd.print(getVar(20)); // lcd.print("D"); lcd.write((uint8_t)1); lcd.print(getVar(3)); // lcd.print("F"); lcd.write((uint8_t)2); lcd.print(getVar(5)); //lcd.print("V"); lcd.write((uint8_t)3); lcd.print(getVar(21)); lcd.setCursor(0, 1); lcd.print("K"); lcd.print(getVar(10)); lcd.print(" R"); lcd.print(getVar(17)); lcd.print(" P"); lcd.print(getVar(15)); runBitlash(); } 



It remains to run the execution of the up.sh script through kroner, connect the arduin to the router via USB and the habrommer is ready!

Source: https://habr.com/ru/post/220893/


All Articles