Category: Linux (Page 2 of 3)

Joining GitHub

At the beginning of the year, I joined GitHub. I figured it might be useful for scripts etc I write when fiddling around with the Raspberry Pi. This month I actually created a repo (don’t worry, it’s nothing exciting- it’s just a script to stop the Raspberry Pi wifi disconnecting).

You can find me at https://github.com/Lynsay

Keeping the Raspberry Pi Connected via Wi-fi

A few months ago, I set-up my Raspberry Pi with a wi-fi adapter. More recently, I’ve noticed that the wi-fi connection occasionally drops out, and the adapter never attempts to reconnect to the network. This is a problem if I’m trying to access the Pi via SSH.

At this stage, I’m still unsure as to why the wi-fi drops out. Initially, I thought there was an issue with my interfaces file however, adding “auto wlan0” to it in an attempt to force a reconnect failed. Trawling through the syslog files revealed that when the wi-fi dropped, a “link beat lost” message was recorded. I was fairly certain it wasn’t a power supply issue messing with the USB ports so my next idea was to create a cron job to check the status of the wi-fi adapter. This solution worked and I have outlined the steps I used below.

First of all, you need to create a new bash script in the following location (note, I’m using nano as my text editor):

cd ~
sudo nano ../../usr/local/bin/wifiscript.sh

The file has the following permission (the user can execute the file)

sudo chmod 0100 ../../usr/local/bin/wifiscript.sh

Next up, here’s the script I used to check the status of the wi-fi connection. What does it do? The Pi attempts to ping Google. If the Pi cannot successfully ping Google, then the script ensures the wi-fi interface is down before bringing it back up. Else, the wi-fi is connected and no action needs to be taken (yay!).

#!/bin/bash
 
TESTIP=8.8.8.8
 
ping -c4 ${TESTIP} > /dev/null
 
if [ $? != 0 ]
then
    logger -t $0 "WiFi has gone down- run ifup"
    ifdown --force wlan0
    ifup wlan0
else
        logger -t $0 "WiFi is currently ok"
fi

Now we need to create a new cron job to schedule when the wifi script runs. The new cron job goes here:

cd ~
nano ../../etc/crontab

How to add the new cron job to crontab:

*/5 * * * * root    /usr/local/bin/wifiscript.sh >> /var/log/syslog 2>&1

The above line writes all the messages generated by the wifiscript.sh file to the syslog file (so “WiFi has gone down- run ifup” or “WiFi is currently ok”). The cron job is also scheduled to run every 5 minutes.

…and that should be it! Well, not quite. It’s probably a good idea to test if the script actually works.

Take the wi-fi interface down:

ifdown wlan0

Of course, this will break your SSH connection. Wait 5 minutes and the wi-fi interface should come back up, allowing you to SSH back into the Pi.

One last check. Have a look at the syslog file. You should see where the wi-fi disconnected and where it reconnected.

nano ../../var/log/syslog

Now you’re done!

Note: this is my first attempt at writing a bash script and a cron job so comments and criticism is welcome!  In the process of writing this blog post, I found Arne’s Blog to be particularly helpful.

Wireless Raspberry Pi- Edimax EW-7811Un Guide

I recently received a second Raspberry Pi as a gift, and decided to connect it to the internet via wi-fi. Initially, I was unsure as to which wi-fi adapters were compatible with the Pi however, the community suggested the Edimax EW-7811Un USB adapter. The adapter itself is tiny, and can be purchased for less than £10.

Whilst I was trying to set it up, I came across multiple guides offering various conflicting pieces of information, so I thought I’d share what worked for me. Before I go any further, I should state I’m running the Raspbian 2013-09-25-wheezy-raspbian.zip image.

First, update the repo

$ sudo apt-get update

Then, upgrade the system

$ sudo apt-get upgrade

Next, you need to generate a wpa passphrase based upon the name of your wi-fi network and the password you use to connect to it. Note the use of quotes around the name and password- I had spaces and certain awkward characters in both the name and the password- without the quotes, incorrect values were generated. The wpa_passphrase function will return a string- take a copy of this, you’ll need it later.

$ wpa_passphrase "name of your network" 'password for your network'

The interfaces file on the Pi now needs to be edited

sudo nano /etc/network/interfaces

The following code shows the contents of my interfaces file. There were many conversations online about what this file should contain In the end, this is what worked for me.

auto lo
 
iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
iface wlan0 inet dhcp
wpa-ssid "network name"
wpa-psk the_string_wpa_passphrase_generated

Save the file and reboot the machine. Now the Pi should connect to your wireless home network.

Wireless Printing/AirPrint Server via the Raspberry Pi

NOTICE: Please read the entire post (and the comments) before proceeding.  This post was originally written at the end of 2012 and some of you have left comments noting fewer steps are required nowadays.  This guide should still work though. Updated guide (no Windows section) available at https://www.lynsayshepherd.com/blog/2015/10/18/wireless-printingairprint-server-via-the-raspberry-pi-updated-guide/.

NOTICE 2: Thank you for all of your emails regarding this post. Unfortunately, I can’t answer every one. If you’re struggling with printing, it might be worth reading through the comment section.

One of the first projects I wanted to attempt when I got my Raspberry Pi was to turn my wired Canon printer into a wireless printer. I managed to get it working so I thought I’d share the steps I went through.

Right, first things first, update the packages on your Pi:

sudo apt-get update

Next up, install all the packages required for printing. The following commands could be put on one line but I took the longer option of installing one package at a time.

sudo apt-get install avahi-daemon
sudo apt-get install avahi-discover
sudo apt-get install libnss-mdns
sudo apt-get install cups
sudo apt-get install cups-pdf
sudo apt-get install python-cups

It takes a while for the packages to install, so grab a cup of tea while you wait. Once everything has finished, you’ll need to add your username to the CUPS server so you’re able to add printers etc later on.

sudo usermod -aG lpadmin pi

Now, check that CUPS has installed correctly and that the service works (it stands for Common Unix Printing System and will let the Raspberry Pi act as a print server):

sudo /etc/init.d/cups start

You’ll need to check the Avahi service works too (it finds various devices on the network which are discoverable):

sudo /etc/init.d/avahi-daemon start

Next, you’ll need to edit the CUPS config file:

sudo nano /etc/cups/cupsd.conf

You’ll need to set-up the port we’re going to be listening on. Comment out the line that reads “Listen localhost:631”. Add in “Port 631”. It should look something like this:

#Listen localhost:631
Port 631

CUPS will need to be told to be used with any hostname, so it can work with AirPrint. The ServerAlias * directive needs to be added before the first occurence of <Location />

The config file will also need to be edited to control access to the server by adding “Allow @Local” in several places e.g.:

# Restrict access to the server...
Order allow,deny
Allow @Local

You need to include it in these areas of the config file:

# Restrict access to the server...
# Restrict access to the admin pages...
# Restrict access to configuration files...

Save the config file and restart the CUPS service:

sudo /etc/init.d/cups restart

Now, find the IP address of your Pi. It’ll be something like 192.168.1.x

ifconfig

Navigate to the CUPS configuration page by typing the IP address you just retrieved and the port number CUPS uses (so it’ll be an address like 192.168.1.x:631). A security exception message may pop up but that’s ok. Continue onwards!

Click on the Admin tab and view the server settings which are towards the right-hand side of the screen. Tick the box that says “share printers connected to this system”. At this stage, you’ll be asked for the username and password of the Raspberry Pi.

Plug your printer into one of the USB ports on the Pi. Click “add printer” in the CUPS web interface and it should appear under local printers. You’ll have to fill in details for the printer, such as name and location. You can enter whatever you want in here but the important part is ensuring you tick the “share this printer” box. Following this, you’ll have to select the appropriate printer driver from a (large) list that appears. Enter print and paper settings too. You’re now ready to click on maintenance > print test page. If everything has gone to plan, the test page will print successfully. You’re not done yet though….

You now need to set-up the Avahi service which will allow you to discover wireless printers on Apple devices. First, navigate to the opt directory, make a directory called airprint and move to it:

cd ../../opt
sudo mkdir airprint
cd airprint

Now, download the Python file which will help us generate the services we need (all one line):

sudo wget -O airprint-generate.py --no-check-certificate 
https://raw.github.com/tjfontaine/airprint-generate/master/airprint-generate.py

The permissions of the script need to be edited so it can be executed:

sudo chmod 755 airprint-generate.py

Generate the Avahi service and place the script in the appropriate folder:

sudo ./airprint-generate.py -d /etc/avahi/services

On generating the service I got the error shown below. This is fine if you don’t have any Apple devices running iOS6 but I’m willing to bet that most people with capable devices are running it.

image/urf is not in mime types, what_the_printer_is_called may not be available on ios6
(see https://github.com/tjfontaine/airprint-generate/issues/5)

To fix this issue, you’ll have to create 2 files in the /usr/share/cups/mime directory. These files will be called airprint.types and airprint.convs. Let’s start with airprint.types.

Use nano to create a new file and edit it:

sudo nano airprint.types

Insert the following into the airprint.types file:

#
# "$Id: $"
#
# AirPrint type
image/urf urf string(0,UNIRAST)
#
# End of "$Id: $".
#

Again, use nano to create the second file:

sudo nano airprint.convs

Insert the following into airprint.convs

#
# "$Id: $"
#
# AirPrint
# Updated list with minimal set 25 Sept
image/urf application/pdf 100 pdftoraster
#
# End of "$Id: $".
#

Restart the CUPS service now that changes have been made:

sudo service cups restart

The Python file which helped us generate services earlier needs to be re-downloaded to the opt/airprint directory

sudo wget https://raw.github.com/tjfontaine/airprint-generate/master/airprint-generate.py

Ensure that you’re in the airprint directory and regenerate the Avahi service (like before):

sudo ./airprint-generate.py -d /etc/avahi/services

Various files have been edited so I’d recommend that you reboot the Raspberry Pi around now. If you only have Apple devices, that’s it, you’re done- hopefully, everything should work. In my experience, I’ve found that the printer can be a little laggy and sometimes, it just doesn’t bother printing certain jobs from my iPhone. Don’t let that put you off though- most of the time it works as expected. I’ve had no problems printing from my laptop or iPad.

 

INTERMISSION

 

Now, I’m aware some of you will want to use the Raspberry Pi to print from Windows machine. If you want to do this, you’ve got a few more steps to go. Before I go any further, I’ll say that I tested this with Windows XP only- things might be easier with Windows Vista/7/8. Maybe things will need to be done slightly differently, I don’t know.

Install Samba on the Raspberry Pi which allows cross-platform file/printer sharing (basically allows Linux and Windows file systems to connect to each other)

sudo apt-get install samba

Navigate to the samba config file which can be found at- /etc/samba/smb.conf. Ensure it contains the following data:

	# CUPS printing.  See also the cupsaddsmb(8) manpage in the
	# cupsys-client package.
	   printing = cups
	   printcap name = cups
	[printers]
	   comment = All Printers
	   browseable = no
	   path = /var/spool/samba
	   printable = yes
	   guest ok = yes
	   read only = yes
	   create mask = 0700
 
	# Windows clients look for this share name as a source of downloadable
	# printer drivers
	[print$]
	   comment = Printer Drivers
	   path = /usr/share/cups/drivers
	   browseable = yes
	   read only = yes
	   guest ok = no

 

INTERMISSION

 

You’re now finished with the Raspberry Pi. Again, I’d reboot it anyway, just to ensure that all appropriate files are running with the configuration that you’ve entered.

So, you want to print in Windows, eh? Here it goes-

I’ll reiterate, I used Windows XP. Things might be different in Vista/7/8

Install the drivers for your printer

Navigate to My Computer > View workgroup computers > Raspberrypi server > Printername

A few dialogue boxes may appear but they should be ok

Select the appropriate driver from the list of printer drivers that appears and click ok

The printer attached to the Raspberry Pi will now appear in the control panel under printers and faxes

My printer constantly displays “access denied, unable to connect” but it still works anyway (I’ve got no idea why)

Finally, try and print something- the new printer will appear in the list of available printers. Fingers crossed, you should now be able to print from Windows too!

This tutorial is based on content from a few other sources:
Configuring the Raspberry Pi as an AirPrint Server
iOS6 Doesn’t Recognise CUPS Print Shares
Printserver for Windows Clients

Edit- I’ve noticed this tutorial was ripped to shreds by a user on Reddit earlier in the year, so I thought I’d clarify a few things. The distro I used was Raspbian (my previous posts about my Raspberry Pi show the version I used). Technically the tutorial is not Raspberry Pi specific and should work on other Debian distros too however, the Pi is neat wee computer to turn into a print server. Yes, I typed “sudo apt-get install” multiple times, instead of installing packages using just the one line, but I’ve mentioned in previous posts that I was a Linux n00b at the time.

On the positive side, thank you to everyone who has posted comments about the guide on my blog :). I must have done something right if so many of you got wireless printing to work! A few of you have mentioned that some of the packages are no longer necessary when setting up the printer- I’m considering putting an updated guide together to make it easier for folks.

« Older posts Newer posts »

© L.Shepherd 2006-2022. All rights reserved.

Modified version of Hemingway Theme by Anders Noren