27 April 2019

WiFi Dongle: Realtek RTL8188

I have a mini PC without any built-in WiFi, and I would like to use a WiFi dongle so that it can connect to the Internet easily. I have a D-Link DWA-110 WiFi dongle I bought with about RM100 about 10 years ago. Unfortunately, it does not work properly any more: It would give up the connection once I have used it for a few minutes and its temperature becomes high.

So I decided to buy a new one. I bought one with only RM10 (including shipping fee), but I neither knew what brand it was nor what chipset it used. I just trusted the description on the web page that it would just work perfectly in Linux. Can't ask for much with only RM10.

Here is a photo showing the old (on the left, of course) and the new one. Behold! Technology advances fast!


However, I was very disappointed when I first used it: A speed test showed that its download speed was 'only' 0.4 Mbps?!! And I got a higher (?) upload speed of 'only' 2.5 Mbps?!! I tested it on two computers running Ubuntu 18.04 and Ubuntu 19.04 but still obtained the same results. For once I thought that I was terribly cheated by the seller.
(Edited: I have later tested this driver r8188eu with options rtw_power_mgnt=0 rtw_enusbss=0. The speed does improve greatly to about but is still slower, in the range of 30 Mbps+.)

I decided to find out more. I checked the CD that it shipped with and found that it included a Linux driver. Weird, because Linux kernel has usually included all popular drivers, and the manufacturer does not have to provide one. Then I found something: This dongle embeds RealTek RTL8188ETV, and somehow the driver included by the current version of Linux kernel is not good enough. Better drivers are available out there.

Here's the procedure I follow to set it up, tested to work fine on Ubuntu 18.04 and Ubuntu 19.04:

  1. Check the existing driver and add it into the blacklist:
    $ lsmod | grep 8188
    r8188eu
      ...
    $ vi /etc/modprobe.d/50-blacklist-8188eu.conf
    blacklist r8188eu
  2. Obtain the latest source code from https://github.com/quickreflex/rtl8188eus:
    $ git clone -b v5.2.2.4 https://github.com/quickreflex/rtl8188eus.git
  3. Compile, install, and use the driver:
    $ cd rtl8188eus/
    $ make all
    $ sudo make install
    $ sudo rmmod r8188eu
    $ sudo depmod -a
    $ sudo update-initramfs -u
    $ sudo modprobe 8188eu
  4. Test it now, or to play safe, reboot the computer and test it again.
A speed test shows that it can achieve 54 Mbps now, which is also the WiFi speed limit of my modem. So it finally works fine!

If you run into the problem of losing connection after some time (so far I haven't encountered this problem), you may want to set some options for the driver:
$ cat /sys/module/8188eu/parameters/rtw_power_mgnt  # Check the current value
$ cat /sys/module/8188eu/parameters/rtw_enusbss  # Check the current value
$ vi /etc/modprobe.d/8188eu.conf
options 8188eu rtw_power_mgnt=0 rtw_enusbss=0

Here are the values defined for these options:
  • rtw_power_mgnt
    • 0: disable power saving
    • 1: power saving on, minPS
    • 2: power saving on, maxPS
  • rtw_enusbss
    • 0: disable auto suspend
    • 1: enable auto suspend
External References:

23 April 2019

Prove that \(2\lfloor y \rfloor > y\) when \(y \geq 1\)

Problem:
Prove that \(2\lfloor y \rfloor > y\) when \(y \geq 1\).

Proof:
Let \(y = \lfloor y \rfloor + r\), then \(0 \leq r < 1\).
Besides, since \(y \geq 1\), we have \(\lfloor y \rfloor \geq 1\), and thus \(\frac{1}{\lfloor y \rfloor} \leq 1\).
From \(r < 1\),
$$ \frac{r}{\lfloor y \rfloor} < \frac{1}{\lfloor y \rfloor} \leq 1 \\ \frac{r}{\lfloor y \rfloor} + 1 = \frac{r + \lfloor y \rfloor}{\lfloor y \rfloor} < 1+1 \\ \frac{y}{\lfloor y \rfloor} < 2 \\ y < 2 \lfloor y \rfloor \\ 2 \lfloor y \rfloor > y \quad \blacksquare $$

17 April 2019

USB Sound Card for Raspberry Pi 3B+ with Ubuntu Mate 18.04.2

I have bought and configured an external USB sound card for my Raspberry Pi.

After plugging in the sound card, here are what I have. Important lines are highlighted.

$ lsusb
Bus 001 Device 004: ID 1941:8021 Dream Link WH1080 Weather Station / USB Missile Launcher
Bus 001 Device 006: ID 8086:0808 Intel Corp. 
Bus 001 Device 005: ID 05e3:0608 Genesys Logic, Inc. Hub
Bus 001 Device 007: ID 0424:7800 Standard Microsystems Corp. 
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ sudo aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
  Subdevices: 7/7
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
card 0: ALSA [bcm2835 ALSA], device 1: bcm2835 ALSA [bcm2835 IEC958/HDMI]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

After disabling the onboard audio with:

$ sudo vi /boot/config.txt
...
dtparam=audio=off
...

I have these now.

$ cat /proc/asound/modules 
 1 snd_usb_audio
$ cat /proc/asound/cards
 1 [Device         ]: USB-Audio - USB PnP Sound Device
                      C-Media Electronics Inc. USB PnP Sound Device at usb-3f980000.usb-1.1.3.4, full
$ sudo amixer -c 1
Simple mixer control 'Speaker',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 151
  Mono:
  Front Left: Playback 44 [29%] [-20.13dB] [on]
  Front Right: Playback 44 [29%] [-20.13dB] [on]
Simple mixer control 'Mic',0
  Capabilities: pvolume pvolume-joined cvolume cvolume-joined pswitch pswitch-joined cswitch cswitch-joined
  Playback channels: Mono
  Capture channels: Mono
  Limits: Playback 0 - 127 Capture 0 - 16
  Mono: Playback 0 [0%] [0.00dB] [off] Capture 0 [0%] [0.00dB] [on]
Simple mixer control 'Auto Gain Control',0
  Capabilities: pswitch pswitch-joined
  Playback channels: Mono
  Mono: Playback [on]

Need to set the volume properly (to maximum).

$ sudo amixer scontrols
amixer: Mixer attach default error: No such file or directory
$ sudo amixer -c 1 scontrols
Simple mixer control 'Speaker',0
Simple mixer control 'Mic',0
Simple mixer control 'Auto Gain Control',0
$ sudo amixer -c 1 sset 'Speaker' 100%
Simple mixer control 'Speaker',0
  Capabilities: pvolume pswitch pswitch-joined
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 151
  Mono:
  Front Left: Playback 151 [100%] [-0.06dB] [on]
  Front Right: Playback 151 [100%] [-0.06dB] [on]

MPD works properly now!

References:



08 April 2019

MRTG on Raspberry Pi 3B+ with Ubuntu Mate 18.04

I have installed and configure MRTG for system monitoring on a Raspberry Pi 3B+ running Ubuntu Mate 18.04.

I have also tested a similar setup (without CPU temperature) on a PC running Ubuntu 18.10.

1. Install SNMP Daemon for Organizing and Collecting Device Information

Install the package.
  $ sudo apt install snmpd

Backup existing /etc/snmpd.conf.
  $ sudo mv /etc/snmp/snmpd.conf /etc/snmp/snmpd.conf.ORIG

Create /etc/snmpd.conf.
  $ sudo vi /etc/snmp/snmpd.conf
  agentAddress    127.0.0.1:161
  rocommunity     public
(Yes, only these two lines.)

Restart snmpd.
  $ sudo systemctl restart snmpd

2. Install SNMP Utilities

Install the package.
  $ sudo apt install snmp

Check whether the setup works by trying to get the free CPU percentage.
  $ snmpget -v 1 -c public localhost .1.3.6.1.4.1.2021.11.11.0
  iso.3.6.1.4.1.2021.11.11.0 = INTEGER: 97

There is no well known OID (Object Identifier) for CPU temperature, so we are going to write a custom script and feed the value to SNMP.
  $ vi ~/snmp-cpu-temp.sh
#!/bin/bash
if [ "$1" = "-g" ]
then
    echo .1.3.6.1.2.1.25.1.8
    echo gauge
    cat /sys/class/thermal/thermal_zone0/temp
    # The above is for Raspberry Pi 3B+. For Ubuntu PC,
    # I don't know a portable way applicable for all.
    # Tell me if you know one.
    # This ugly hack works for mine:
    #sensors -u 2>/dev/null | sed -n '4p' | sed -e 's/temp1_input: \(.*\)/scale=0; 1000 * \1 \/ 1/g' | bc -l
fi
exit 0
  $ chmod +x ~/snmp-cpu-temp.sh
  $ ./snmp-cpu-temp.sh -g
  .1.3.6.1.2.1.25.1.8
  gauge
  49768

Tell SNMP how to handle this OID by appending a line to /etc/snmp/snmpd.conf.
  $ sudo vi /etc/snmp/snmpd.conf
(append)
  pass .1.3.6.1.2.1.25.1.8 /bin/sh /home/username/snmp-cpu-temp.sh
  $ sudo systemctl snmpd restart
(Change username above to reflect yours.)

Check if it works.
  $ snmpget -v 1 -c public localhost .1.3.6.1.2.1.25.1.8
  iso.3.6.1.2.1.25.1.8 = Gauge32: 41856

3. Install MRTG

Install the package.
  $ sudo apt install mrtg

Backup existing /etc/mrtg.cfg.
  $ sudo mv /etc/mrtg.cfg /etc/mrtg.cfg.ORIG

Create /etc/mrtg.cfg. I am more interested in monitor the system health, including CPU load, memory usage, and CPU temperature.
  $ sudo vi /etc/mrtg.cfg
WorkDir: /var/www/mrtg
EnableIPv6: no 

Title[cpuload]: CPU Load
Target[cpuload]: 100 - .1.3.6.1.4.1.2021.11.11.0&.1.3.6.1.4.1.2021.11.11.0:public@localhost
Options[cpuload]: gauge, nopercent, growright, unknaszero, noo
MaxBytes[cpuload]: 100
YLegend[cpuload]: %
ShortLegend[cpuload]: %
LegendI[cpuload]: CPU
Legend1[cpuload]: CPU usage
PageTop[cpuload]: <H1>CPU Load</H1>

Title[memory]: Memory Usage
Target[memory]: .1.3.6.1.2.1.25.2.3.1.6.1&.1.3.6.1.2.1.25.2.3.1.6.3:public@localhost 
Options[memory]: gauge, nopercent, growright, unknaszero
Factor[memory]: 1024
kilo[memory]: 1024
kMG[memory]: ,,Mi,Gi,Ti,Pi,Ei,Zi,Yi
MaxBytes[memory]: 100000000000
YTicsFactor[memory]: 1024
YLegend[memory]: Bytes
ShortLegend[memory]: B 
LegendI[memory]: Physical 
LegendO[memory]: Virtual 
Legend1[memory]: Physical Memory
Legend2[memory]: Virtual Memory 
PageTop[memory]: <H1>Memory Usage</H1>

Title[cputemp]: CPU Temperature
Target[cputemp]: .1.3.6.1.2.1.25.1.7.0&.1.3.6.1.2.1.25.1.8:public@localhost 
Options[cputemp]: gauge, nopercent, growright, noi 
Factor[cputemp]: 0.001
MaxBytes[cputemp]: 100000
YTicsFactor[cputemp]: 0.001
YLegend[cputemp]: Celsius
ShortLegend[cputemp]: Celsius
LegendO[cputemp]: CPU temperature 
Legend2[cputemp]: CPU temperature in Celsius
PageTop[cputemp]: <H1>CPU Temperature</H1>

Create the corresponding web directory for MRTG. Here we use apache2, so install the apache2 package first if you haven't.
  $ sudo mkdir /var/www/mrtg
  $ sudo vi /etc/apache2/sites-available/mrtg.conf
Alias /mrtg "/var/www/mrtg/"
<Directory "/var/www/mrtg/">
  Options FollowSymLinks
  AllowOverride None
  Require all granted
</Directory>
  $ sudo a2ensite mrtg
  $ sudo systemctl restart snmpd

Use the indexmaker helper to create the main page index.html.
  $ sudo indexmaker /etc/mrtg.cfg --title='System Monitor' --columns=1 --output=/var/www/mrtg/index.html

It should work now! So surf http://address/mrtg/ and you should see something.
(Change address above to reflect yours.)



Notes:
  • The mrtg package installs /etc/cron.d/mrtg to run mrtg every 5 minutes.
  • The cfgmaker helper can create a draft /etc/mrtg.cfg based on what it detects, including network traffics. I don't have much luck with it, but you can try it if you want.
  • We can improve the MRTG setup further with RRDTool. But the CGI scripts I found that can work nicely with MRTG+RRDTool are outdated and do not run well with my setup.

05 April 2019

First Post from Blogger

Does Blogger for Android work? At least write a post with simple format?
If your see this, it works! Bold, italic, link.
Attaching image doesn't seem to work.