Disable Session Restore Message in Chromium for Raspberry Pi

chromium + raspberry pi

I was using Raspberry Pi’s as a digital signage solution. I had a network of Pi’s that would simply point to a website displaying a slideshow. I set up each Pi to automatically open Chromium browser to that website on boot. It’s a great solution because its so simple and has the added benefit of consuming very little electricity, since its running on a Raspberry Pi.

A huge issue I ran into though, was when there was a power outage, or if the Raspberry Pi was improperly shut off, Chromium would open with a little pop-up bubble saying: Chromium did not shutdown properly. Do you want to restore your session? The only way to get rid of it was to plug in a mouse to the Raspberry Pi and dismiss the message. Not ideal if you are planning to manage multiple digital signs this way, and it looks kind of unprofessional.

chrome restore bubble

I spent days trying to find a fix. There were plenty of solutions on various forums and websites, but they were all outdated and no longer worked on recent versions of Chromium.

Through my research I narrowed down the cause of this problem to the chromium preferences file, found in this directory:

/home/pi/.config/chromium/Default/Preferences

There are two variables in that file that let Chromium know on startup whether or not it shutdown correctly.

We want to make sure that exit type is set to Normal and that exited cleanly is set to true.

So let’s look at this file and make sure these values are set correctly. To do this from the command line, we will open the Preferences file in Nano, which is a very basic command line text editor. Type the following into the terminal:

sudo nano /home/pi/.config/chromium/Default/Preferences

This will open Chromiums Preferences file in the terminal.

We need to find the two values to make sure they are correct. You can either scroll to the right with the arrow keys on your keyboard, or if you type ctrl+w on your keyboard it will open up nano’s search function. Then you can search for the word ‘exited’ and find the two variables.

Depending on how chromium shut down the last time you had it open, these values may already be correct. They should look like this:

terminal screenshot

If this line of text looks correct, exit the nano editor with ctrl+x on your keyboard.

Nano will ask you if you want to save the changes. Type in ‘y’ for yes and hit enter. This will save and take you back to the terminal.

Now we have our Preferences file set correctly, but as soon as Chromium crashes it will change these values and we will get the dreaded popups.

So what we need to do is make a copy of the Preferences file that we just edited with the proper settings. Then we will write a bash script to replace Chromium’s preferences file with our good copy each time the Raspberry Pi boots up.

First create a directory (folder) to store our copy. From the terminal, type:

mkdir /home/pi/chromium_prefs

Then copy our updated Preferences file into this new directory. Type the following command:

cp /home/pi/.config/chromium/Default/Preferences /home/pi/chromium_prefs

This is using the linux cp (copy) command, which takes two parameters, the path to the file we want to copy and the path to the folder we want to copy it too.

Now we just need to create the bash script to copy our Preferences file over Chromium’s version everytime the Raspberry Pi starts up.

We are going to use the nano editor again:

sudo nano start.sh

This creates a blank file and opens it in nano.

Let’s write our script. It should look like this:

#!/usr/bin/sh

cp /home/pi/chromium_prefs/Preferences /home/pi/.config/chromium/Default
chromium-browser --kiosk

The first line is just the standard header for bash scripts.

The next line is where we are copying our Preferences file and replacing the one in Chromium’s Default folder.

The last line actually launches Chromium in kiosk mode.

The final thing we have to do is tell the Raspberry Pi to run this script on start up.

Type the following in the command line:

sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart

Then add this line to the bottom of the autostart file.

@sh /home/pi/start.sh

That’s it! You can test this out by having Chromium open when shutting down your Raspberry Pi, and then booting it back up to see if you get the System Restore message.

It really shouldn’t be this difficult, as Chromium on a Raspberry Pi seems to be a popular solution for digital signage. But even though it takes a little effort, this solution works fine and I’ve used it for multiple digital signs. I hope this can help someone else.

How to Round to the Nearest 5 Cents (Nickel) With PHP

rounding with php

I was working on a pricing calculator in PHP and I needed to round the total to the nearest five cents, since my country got rid of pennies some years ago.

For example:

$2.23 should get rounded to $2.25

$2.21 should round to $2.20

$2.28 should round to $2.30

and so on…

Oddly enough, I didn’t find a whole lot of answers online explaining how to solve this seemingly simple problem.

So I decided to post my solution here in case someone comes across the same problem.

First we store the cost in the variable:

$cost = 27.2345567;

Then we pass the cost divided by 0.05 into the round function and multiply it by 0.05. This gives us our rounded value which we can store in a variable we’ll nameround_num.

$round_num = round($cost / 0.05) * 0.05;

That will round whatever number you assign to the cost variable to the nearest 5 cents.

This won’t display zero values, so 2.20 will be displayed as 2.2 and 2.00 will be displayed a 2.

To fix this, and make our numbers display consistently, we can use PHP’s number_format function like so:

number_format($round_num, 2);

The number_format function takes two arguments separated by a comma. The first argument is the number we want to format, which is our rounded number. The second argument is the number of decimal places we want to show. In this case we say 2 because that is the convention for pricing.

Here is the whole script, where we echo out the final cost at the end:

$cost = 27.2345567;
$round_num = round($cost / 0.05) * 0.05;
echo number_format($round_num, 2);

I am sure there is a more elegant way to do this, and by all means, post it in the comments.

But it works, and is pretty simple. So if you are like me and trying to solve this problem, maybe you will find this useful.

How to Install a LAMP Stack on a Raspberry Pi

 the lamp stack

A LAMP stack is a suite of development tools, which stands for Linux, Apache, MySQL and PHP. Each of these components serves its own purpose: Linux is the operating system, Apache is the web server, MySQL is the database and PHP is the programming language.

In this quick tutorial we’ll cover how to install a LAMP stack on a Raspberry Pi.

Note that I am using the latest (at the time of writing this) version of Raspbian: Stretch. If you are using older versions of Raspbian you may need to tweak some of these steps slightly, or upgrade your distribution.

1.) Update Raspbian Operating System

Type these commands in the terminal:

sudo apt-get update
sudo apt-get upgrade

Note that each of these commands will take a while to process. Especially on wifi. The upgrade in particular can take more than an hour, so go do some laundry or something and just keep checking back in.

2.) Install Apache

sudo apt-get install apache2 –y

3.) Test the Web Server

On successful installation, a default HTML page will appear in the root web folder:

/var/www/html

You can navigate to that directory in the terminal by typing:

cd /var/www/html

And then typing in the following command to see the files in that directory:

ls

You should then see the index.html file listed.

You can view this page on a web browser either from the Pi itself or from another computer on the same network.

From the Pi, open the web browser and type http://localhost in the address bar.

From another computer on the same network, type in the Pi’s IP address in the address bar of your web browser.

If you don’t know your Pi’s IP address, check out my quick tutorial on how to do this.

If everything went right, you should see a page like this:

Apache default page

 

4.) Install PHP

Run this command in the terminal:

sudo apt-get install php libapache2-mod-php –y

5.) Install MySQL

Run this command in the terminal:

sudo apt-get install mysql-server php5-mysql -y

6.) Restart Apache

sudo service apache2 restart

That’s it!

You now have your own local Raspberry Pi web server you can use to develop your web projects or tinker around.

If you have questions, let me know in the comments.