Showing posts with label Raspberry Pi. Show all posts
Showing posts with label Raspberry Pi. Show all posts

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.