Breathe new life into your old home router

Viewing Router Data

Next, we want to create a simple Common Gateway Interface (CGI) Ash web page to view custom router data and pass the data to another device, such as a Raspberry Pi Node-RED server (Figure 7).

Figure 7: The setup for passing router data to other devices.

For this project, it's best to use dynamic data. If you don't have external data, then dynamic data from the router's system load data can be used (Figure 8). The command-line statement

cat /proc/loadavg
Figure 8: The router's system load data provides the dynamic data for our project.

will show the router's one-, five-, and 15-minute load averages.

By adding some AWK code, you can extract each of the data points directly. For example, to get the first data value, enter the following code:

## Show router load averages
cat /proc/loadavg
0.36 0.28 0.16 1/50 3307
## get the first data point
cat /proc/loadavg | awk '{print $1}'

Custom Router Web Page

OpenWRT runs the uHTTPd web server for its router configuration. This web server can also be used for custom CGI web pages that can use Ash scripting. You will find the OpenWRT custom CGI pages in the /www/cgi-bin directory. Listing 4 shows an example CGI page, test.cgi. This example shows the previous load average values along with some system summary information from the Linux monitoring tool vmstat.

Listing 4

Router Web CGI Example

01 #!/bin/ash
02 #
03 # test.cgi - show system load averages and vmstat
04 #
06 echo "Content-type: text/html"
07 echo ""
08 echo "<!DOCTYPE html>
09 <html>
10 <head><title>Router Points</title>
11 </head>
12 <body>
13 <h1>Router CGI Page</h1><hr>"
15 # -- show router system load averages --
16 echo "<h2> System Load Averages </h2>"
17 echo "1 minute load:"
18 cat /proc/loadavg |awk '{print $1}'
19 echo "<br>5 minute load:"
20 cat /proc/loadavg |awk '{print $2}'
21 echo "<br>15 minute load:"
22 cat /proc/loadavg |awk '{print $3}'
24 # -- show vmstat -- use <pre> formatting
25 echo "<h2> Vmstat Results </h2> <pre>"
26 vmstat
27 echo "</pre></body></html>"

CGI web pages use Ash/Bash echo statements to output HTML code. It is important to start the page by echo-ing out "Content-type: text/html" with an added new line (lines 6-7). For this example, including HTML heading tags such as <h2> in the echo string improves the presentation (lines 16 and 25).

The output from Ash/Bash statements such as

cat /proc/loadavg |awk '{print $1}'

will be shown directly on the web page.

Using the HTML <pre> tag provides a pre-formatted fixed format for the output from the vmstat monitoring utility (lines 25-27).

After creating the CGI page, the final step involves setting the file's execution rights as follows:

chmod +x test.cgi

You can now access your custom web page at http://router_ip/cgi-bin/test.cgi. Figure 9 shows the test CGI web page.

Figure 9: The custom router web CGI page.

Connecting to a Raspberry Pi

For our final project, we want to pass the data from the router to a Raspberry Pi. You could modify this project to pass data to a Home Assistant node or any PC on your home network.

The simplest protocol for passing data is to use TCP sockets. This approach doesn't require any added software to be loaded on either the router or on the Rasp Pi.

You can use the Bash nc (or netcat) to both send and receive TCP messages. To create an nc test, open two terminal windows: one on the router and the other on the Raspberry Pi (Figure 10).

Figure 10: Use nc to send and receive TCP messages.

To set up a listener on the Rasp Pi, define the Rasp Pi's IP address with a port (1234 in this example). The -l option sets listening mode, and the -k option will keep the connection open for multiple messages.

On the router sender side, an echo message is piped to the nc command with the Rasp Pi's IP address and port.

Next, you need to periodically send dynamic data out via TCP. Listing 5 shows an Ash script file that uses our earlier Ash/AWK code to get the router's load averages and then pipes the values to a TCP listener every two seconds.

Listing 5

Sending Router Data to a TCP Socket

# - Send Router Load Averages via TCP
#  - send 1 min avg to port 1111
#  - send 5 min avg to port 1115
while true
  cat /proc/loadavg | awk '{printf $1}'| nc 1111
  cat /proc/loadavg | awk '{printf $2}'| nc 1115
  sleep 2

Node-RED [5] is a great visual programming environment that comes preinstalled on the Raspberry Pi Raspbian image. To get TCP messages from the script in Listing 5 into Node-RED, two tcp in nodes can be configured with the required port numbers. To show the data graphically, two dashboard ui_gauge nodes can be connected to the outputs of the tcp in nodes. Figure 11 shows the Node-RED logic and web dashboard for the two router load average points.

Figure 11: Node-RED dashboard logic with TCP sockets.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus