Category Archives: linux

Decode Raspberry vcgencmd get_throttled response with a PHP script

If you search for an interpretation to get a meaning out of the command

vcgencmd get_throttled

which might return something like:

throttled=0x50005

you will find many forum posts that basically tell you it is a bitcode and that you have to decode it following this table:

Bit Meaning
0 Under-voltage detected
1 Arm frequency capped
2 Currently throttled
3 Soft temperature limit active
16 Under-voltage has occurred
17 Arm frequency capped has occurred
18 Throttling has occurred
19 Soft temperature limit has occurred

(from this GitHub page)

yyyyeaaahhhh right.

Finally I found a comprehensible explanation here and I decided to write a script around it, and since I know PHP this is what I used.

So, from your home folder,

nano throttled.php

paste this inside:

<?php
$codes=array(
0=>"Under-voltage detected",
1=>"Arm frequency capped",
2=>"Currently throttled",
3=>"Soft temperature limit active",
16=>"Under-voltage has occurred",
17=>"Arm frequency capped has occurred",
18=>"Throttling has occurred",
19=>"Soft temperature limit has occurred");

$output=exec("vcgencmd get_throttled");
$output=explode("0x",$output);

if ($output[1]=="0") {
    echo "all fine, lucky you\n";
    exit();
}

$output=str_split($output[1]);
$bincode="";
foreach ($output as $hex) {
    $bincode.=str_pad(base_convert($hex,16,2),4,"0",STR_PAD_LEFT);
}
$bincode=array_reverse(str_split($bincode));
foreach ($bincode as $k=>$v) {
    if ($v) {
        echo $codes[$k]."\n";
    }
}

And then run:
php throttled.php

Display correct Raspberry CPU frequency in byobu status bar

I love byobu, since when I first discovered it existed me years ago. I didn’t even know screen was a thing back then, but byobu… oh byobu.

So anyway, I’ve come to rely a lot on it, and on its status bar, which with time allowed to display “accurately” both CPU temperature and clock.

On Raspberry Pi, I just discovered, byobu doesn’t always show the correct clock.

I noticed that the command:

vcgencmd measure_clock arm

returned a lower CPU clock, 0.6GHz (and that’s when I finally noticed that my Raspberry Pi 3B+ was permanently throttled because of low voltage).

Delving deeper, I realized that byobu takes the CPU temperature information from something like, if not exactly, this:

sudo cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq

which is the information given back by the Linux Kernel. I’ve read on RaspberryPi’s official forums (couldn’t find the link again now), and the consensus is that kernel is not necessarily correct, but the firmware always is.

The correct CPU clock frequency then must be asked to the firmware with the vcgencmd command above.

Now, how to display the correct clock in byobu status bar?

I used this guide:

http://blog.dustinkirkland.com/2010/01/byobu-custom-status-notifications-in-3.html

and with a litte help from RaspberryPi official forums:

https://www.raspberrypi.org/forums/viewtopic.php?f=91&t=251695

(those guys were REALLY fast in replying)

I created the $HOME/.byobu/bin/5_clk file, containing the following code:

#!/bin/bash
vcgencmd measure_clock arm | awk ' BEGIN { FS="=" } ; { printf("#[fg=#ffffff,bg=cyan]%.1fGHz#[default]\n", $2 / 1000000000) } '

made it executable with

chmod +x $HOME/.byobu/bin/5_clk

and enabled the “custom” row in the byobu toggles, while disabling the native info.

This replaces the cpu frequency info with the same color formatting, and refreshes every 5 seconds.

Control SIM800 with PHP through serial port

…or rather, control anything through serial port with PHP.

I must say, credit goes to this github repository which in turn is based on another known repository, and which I heavily simplified and stripped down because I don’t like having to deal with PHP classes and everything that has to do with it.

Let’s cut down the overhead, and to the chase.

This is an example code:

function serialread() {
   global $sim800;
   $chars = [];
   do {
     $char = fread($sim800, 1);
     $chars[] = $char;
   } while ($char != "\n" && $char != "");
   return join('', $chars);
}

exec("stty -F $tty cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts");
$sim800=fopen($tty,"w+b");
stream_set_blocking($sim800, false);
fwrite($sim800,"AT\r");
fwrite($sim800,"AT+DDET=1\r");
fwrite($sim800,"AT+CLIP=1\r");
fwrite($sim800,"ATS0=2\r");
fwrite($sim800,"AT+CMGF=1\r");
sleep(1);

while (true) {
   $data = trim(serialread());
   if (strlen($data)>1) {
     echo $data."\n";
   }
   sleep(1);
}

And now let’s do some explaining.

$tty is the string containing the value of the serial device file, in my case I defined it as $tty="/dev/ttyUSB0"; because I prefer using an external adapter and keeping more juicy GPIO pins to myself.

The $sim800=fopen($tty,"w+b"); instruction opens the serial stream as a writeable and readable file, and resets its content to empty, the b in "w+b" forces the connection to be established in binary format.

The stream_set_blocking($sim800, false); instruction allows the fread() command to immediately return a result, even if null, after calling it, instead of waiting for something to be output to the serial console.

The fwrite() instructions are to initialize the SIM800 module as I like it, you may want to search on the official AT commands of the SIM800 to find their meaning. What’s to be learnt here, is that, after you issue a command string, you want to end it with "\r" which is a carriage return, and sends the Enter key to the console; using "\n" which instead is a newline would not work to issue commands.

I use trim() on the serialread() function (which by the way is stolen from the repository linked above) because the SIM800 likes to add a lot of new lines around, and to parse and/or display the serial ouput of the module it’s better if you have a bare string to process.

That’s all folks.

Backup linux & raspbian important files on dropbox, automatically

I just create a repository on GitHub:

https://github.com/ephestione/bazidrop

It’s about a little tool I wrote, that I needed to have small, fresh backups made regularly of my raspberry pi’s operating system, that I could safely upload to the cloud (hence, encrypted).

I already use https://github.com/lzkelley/bkup_rpimage to make regular backups of my systems, but that creates 4GB .img files (even if sparse) that I cannot easily move around, and include all the contents of the sytem, even those files that can be restored with a new installation.

Instead, I needed to systematically backup my home folder, crontabs, apache files, mysql databases (maybe you have something else in mind), zip everything, and put it on dropbox.

This is the reason I wrote bazidrop.sh, which I think someone else could find useful.

Apache on Raspbian using older version of PHP

A few weeks ago I used this tutorial to install PHP 7.3 on my Raspbian.

Fast forward to today, I was bashing my head because after 2 hours of messing around, I could have exec() work from PHP CLI but not from the browser.

php -v from CLI correctly reported 7.3, so what gives?

After two hours I also decided to try phpinfo() from the browser, and guess what, it was still using 7.0!

So, after:

sudo a2dismod php7.0
sudo service apache2 restart

phpinfo() correctly reported 7.3 and I had my exec() working as intended.

Protect empty mountpoint from write access when drive is not mounted in Raspbian and Linux

I knew I had read something to this effect in the past since one of my raspi‘s had this thing where you couldn’t write to a mountpoint unless the drive/USB disk was actually mounted.

Since I spent some time in finding it again, here I add it to my blog as personal reference.

From this post:

Always set the attributes of mountpoint directories to immutable using chattr.

This is accomplished with chattr +i /mountpoint (with the mount unmounted).

This would error-out on new write activity and also protects the mount point in other situations.

lighttpd cannot install because of libssl1.1, how to fix

My situation happened on a Raspberry with Raspbian Stretch installed, I had added PHP7.0 repository from sury.org quite a while back and when by chance today I went to open PiHole admin page from my laptop it simply wouldn’t load.

Google what’s up, I found out lighttpd was maybe the culprit, look around and noticed it wasn’t installed, and couldn’t be installed with the error:

lighttpd : Depends: libssl1.1 (>= 1.1.0) but it is not going to be installed

I then discovered that said libssl1.1 library off sury.org, coming together with the PHP distro, broke lighttpd.

I removed the repository source from /etc/apt/sources.list.d/php.list (your filename may vary) and issued a sudo apt-get update followed by a dist-upgrade but that didn’t solve anything.

I couldn’t find for the life of me a way to simply exchange the libssl1.1 package (which was kept from the sury.org repository even after removing its source) to the original repository version, and

sudo apt-get remove libsll1.1

wanted me to remove also ALL the packages that depended on this library. And they were a LOT.

Like hell I wanted to do that!

Or not?

Well I tried

sudo apt-get install --reinstall libssl1.1

hoping it would get rid of the messy libssl1.1 exchanging it with the official repository one, but that also gave an error:

Reinstallation of libssl1.1 is not possible, it cannot be downloaded.

Sooooo I was getting out of options and very annoyed.

I thought, why not letting the frigging apt remove all those packages? I can always install them later on… I hope…

So I copied the list of the packages that were to be removed off putty window and into notepad++, crossed my fingers and let apt remove everything.

After that was done during an extremely long and suffered delay, I had to do

sudo apt-get install <paste big-ass list of packages here>

In my case, it didn’t work out that simply at the first try, I got some CRC mismatch somewhere so they were not installed all together.

But I issued the command with less packages listed, beginning with apache2 php7.0 mysql-server transmission-cli samba python somethingIcannotreallyrecallnow, and then reissued the first command with all the list again to fill in the blanks.

It installed everything piece by piece, and lighttpd with it as well.

Since apt-get actually removes those packages without purging them (unless you ask it to purge libssl1.1 which isn’t a good idea) their settings are kept and reinstalling the packages will restore the system to the previous state.

At least it did for me, since the system came back to normal after a reboot.

lftp and source: Is a directory error

I was running lftp with the -f switch to launch the usual mirror command, working fine with a few ftp servers at first, but when I added another sync with a pure-ftpd server something stopped working as intended: the message

source: Is a directory

appeared and nothing more happened, only after I pressed Enter the script went on with the execution.

What helped me was this discussion.

so I changed my script, and instead of having

open host
user username password
mirror blahblah

I used

open -u username,password host
mirror blahblah

and everything was magically fine!

Why? NO IDEA. Who cares. Works now.

Change user of transmission-daemon under Debian and Raspbian

UPDATE: Cristian commented adding a nice solution (which I didn’t personally test though, so it’s on you):

  1.  run chmod 775 on the download folder, with -R option (recursive on subfolders):
    sudo chmod -R ug+rw folderName
  2.  add your own user (the one you need to be able to access the downloaded files) to debian-transmission group, or any other group that the transmission daemon belongs to:
    sudo usermod -a -G groupName userName

And that would be all.

Though, after some tinkering that occurred as of june 2019, I found that my original, following, solution has a -probably- nicer “feature” for someone: the config folder of transmission is saved in your own home folder, instead of /var/lib/transmission-daemon/blahblah, in my case this saved some waste of time.

This is the original article with the original solution.

I have a raspi3 running transmission daemon downloading to an external USB drive shared via Samba. I don’t want to keep using debian-transmission user with the daemon since just switching to my user account having the access rights to the external USB is much simpler.

I had it already nice and running before, but updating the daemon with apt-get messed everything (and lost all the running torrents as well) so I had to rediscover the procedure once again, and for posterity (and myself for future occasions) I’m writing it down here.

By the way all the missing torrents appeared again after I solved the issue.

So, here are the things you need to do:

  1. run sudo service transmission-daemon stop just in case
  2. edit /etc/init.d/transmission-daemon to have USER=username
  3. edit /lib/systemd/system/transmission-daemon.service to the same effect
  4. run sudo chown -R user:user /etc/transmission-daemon/
  5. run sudo chown -R user:user /var/lib/transmission-daemon/

Done.

 

According to a comment of this article, this might not work for you, so instead you might want to follow this guide instead:

https://askubuntu.com/questions/261252/how-do-i-change-the-user-transmission-runs-under/544185#544185

 

Raspberry Pi 3 vs Cubieboard 2 Samba NAS performance

I’ve been using a Cubieboard overclocked to 1.3GHz as my home little NAS server since a few years ago, and just recently I bought a Raspberry Pi 3 thinking it would have been surely faster.

I was especially looking to get better Samba performance.

I have additional ntfs-3g and encryption layers other than Samba, but both boards had their version of Debian installed, with the same packages and same configurations, so the starting configuration was basically the same.

Both were -alternatively- attached to the same Toshiba Canvio 3TB USB3 disk (working in USB2 mode on both boards), and on two adiacent ports on my TP-Link ADSL router.

I overclocked my Raspi3 to 1.5GHz, with sdram_freq set to 500.

For my test, both were alternatively running off the same AC powered USB adapter, under the same wattmeter, each with his own USB power cord.

Well, it was an overall disappointing experience.

Raspberry Pi 3 @ 1.5GHz

  • Download speed via Samba: almost 7MB/s
  • CPU temperature: about 42°C
  • System load: ~2
  • Power draw: 2.6W, with short spikes to 2.7W

Cubieboard 2 @ 1.3GHz

  • Download speed via Samba: almost 7MB/s in average, oscillating between 6.3 and 7.5MB/s
  • CPU temperature: about 42°C
  • System load: ~2
  • Power draw: average 3.4W, ranging from 3.3 to 3.5W

So, the only area where raspi3 is superior to cubie2 is the power draw, otherwise I would have expected way better performance.

Interesting to note, the transfer speed via samba was the same on the raspi3 at the default frequency of 1.2GHz (no benefit from overclocking whatsoever, if not maybe increased oscillations in speed with the same average value), while cubie2 seriously improved after I overclocked back then.

This might indicate a saturation of the USB bus, where the ethernet adapter resides in the raspberry pi 3, which might be the case since the system load of 2 means the 4 cores of the raspberry pi 3 are loaded half as much as the 2 cores on the cubieboard 2, so they could be waiting for the bus to free itself.

Investigating further, I ran

vmstat 3

on both the boards during the transfer… and averages were as follows:

  • Cubieboard 2: user 7%, system 52%, idle 39%, waiting 2%
  • Raspberry Pi 3: user 2%, system 28%, idle 55%, waiting 15%

I think it’s quite clear at this point that while the cubieboard 2 is crunching during that time (the idle should be due to one of the two cores not being optimized), the raspberry pi 3 is waiting on the bus to free itself before sending in other data (the higher idle also must be due to the 4 cores not being all used for the task, mostly 2 of which just don’t do anything at all).

I spent about half to buy the raspberry pi 3 than what I paid for the cubieboard 2 back then, but after so much time it’s not surprising; what is surprising is that the everyday performance appears to be the same 🙁

At this point I will probably keep the cubieboard 2, because it’s already configured as I want it (it’s got more amenities to it than just the NAS server based on Samba) and the different power draw would save me only about 2€ per year on the raspberry pi 3, so no point in upgrading.

UPDATE:

It’s been a couple months since i wrote this article, and for the last month, maybe more, I’ve been actually using the raspi3 as server (and have sold the cubie2 which wasn’t useful anymore).

I noticed that, on the long run, the NAS transfer speed didn’t keep up on the cubieboard after the uptime went, well, up 🙂 It got down to about 2MB/s, with spikes of 5MB/s, maybe less, but never quite reached the 7MB/s average which I got for the tests. No idea what caused this performance drop, but it was a fact: as days went by, the transfer speed was not on par, but far less.

The raspi3 on the other side looks more reliable, its uptime is 40 days as I write, and it still is right below the 7MB/s mark.