Raspberry Pi Zero and Camera

RASPBERRY PI REFERENCE AND CAMERA SETUP

Raspberry Pi 3G Connection

Suggestions for getting 3G going with a Raspberry Pi and how to set up DDNS ...

Making 3G work on a Raspberry Pi

Sometimes, a Raspberry Pi needs to be installed out-of-range of a wi-fi network. This describes a way to replace the wi-fi connection with a 3G one by using a 3G dongle.

3G with a Raspberry Pi isn't always straightforward, so some experimentation may be required to get things going. The dongle, SIM card type and the ISP all have an impact on how successful the connection will be.

Most UK mobile broadband networks now only provide a private IP address so with these the Raspberry Pi cannot be accessed from elsewhere. If you need remote access you will need a modem-type dongle on Three with the ability to set the APN to 3internet rather than three.co.uk. Alternatively, you will need to obtain a fixed-IP SIM card appropriate for the dongle. This overview may change over time, so check the current situation before committing expenditure.

There are broadly two types of dongle:

If using an extension lead, adaptor or convertor shim with the micro USB connector on a Pi Zero or Pi Zero W ensure that it is the OTG ('On The Go') type or the dongle will power up but not pass data.

A simple alternative to connecting a 3G dongle directly to a Raspberry Pi is to use the Raspberry Pi's wi-fi to link to an adjacent MiFi device. However, the remote access restrictions will be the same as for a network connection dongle.

Watch the costs!

Because 3G broadband accounts are very expensive, the 3G connection should only be used when actually needed. Creating a data budget and operating scenario before activation can help.

For example, uploading one 928px x 576px camera image will 'cost' about 60KB meaning that a 1GB per month 3G package would support about 1024/.06 = 1,700 images per month, or 55 per day, or 2 or 3 per hour. It would be very easy for a few false movement triggers a day to exceed this, so 'lower cost' notification-only emails may be an option.

It may be better to have a pay-as-you-go dongle rather than a monthly or rolling account because the p-a-y-g dongle will switch off when it reaches its limit rather than building up an enormous bill. However, some dongles need to be powered down and up again to reactivate them once they have been re-credited which might be difficult at a remote location unless the relay option below is used.

What to do if you can't get a good 3G signal

If the connection works but data transfer is slow it may mean that the 3G signal level is too low or that the service is too busy. If you were using a mobile phone, you could just move around a bit until you found a better signal but this isn't always practicable with a camera when the viewing position is important. However, it might just be something blocking the signal between the camera and the mast, so a small change in position might improve things. If the data transfer rate is satisfactory at some times and not others then it's likely that too many people are trying to use the service so changing to another primary operator could perhaps be a solution.

Although you've probably checked your mobile broadband provider's coverage map on their website, they don't usually show you the actual base station locations and Ofcom have stopped publishing up-to-date information on their Sitefinder resource at http://www.sitefinder.ofcom.org.uk. Although you won't be able to determine much about the strength of the mobile phone mast signal, knowing where the signal comes from will help to ensure that the 3G dongle isn't hidden behind something that might block the signal. Remember that the signal might not be coming from a visible mast at all but from a tall building like a church tower.

If moving the camera position doesn't improve things, then one solution may be to physically separate the camera and dongle by using a USB extension lead. Alternatively, depending on the 3G dongle, it might be possible to plug in an external antenna.

Install 3G modem-type dongle software

This is not required for network connection-type dongles.

3G modem dongles often appear as two separate devices when first connected - a memory card and a 3G modem. This is so that the memory card section can automatically install a suitable modem driver on a Windows PC when it's first plugged in, however drivers are already installed on the Raspberry Pi so the dongle needs to be switched straight to modem mode before it will work and the correct driver needs to be activated. Two programs will be installed - usb-modeswitch will do the switching and wvdial will make the connection.

Install the usb-modeswitch package (if not already installed) by typing

sudo apt-get install usb-modeswitch

then press Enter.

If the 3G dongle is removed and inserted with the Raspberry Pi powered up, usb-modeswitch will automatically change it to modem mode. However, usb-modeswitch doesn't work if the dongle is in place when the Raspberry Pi is first powered up so a little extra effort is needed to make it suitable for a remote application.

What's needed is to call usb-modeswitch to change the setting when required but its command line mode with parameters doesn't work reliably. Instead, a small configuration file needs to be created containing the required information. The file shown here is for the Huawei E353 - see the boxout below for how to derive a configuration file for other dongles.

Create a new configuration file by typing:

sudo nano /etc/usb_modeswitch.d/E353.conf

then press Enter. Copy and paste the following:

DefaultVendor = 0x12d1
DefaultProduct = 0x1446

TargetVendor = 0x12d1
TargetProduct = 0x1506

CheckSuccess = 20

MessageContent = "55534243123456780000000000000011062000000100000000000000000000"

Hold down the Ctrl key and press X, then Y, then Enter to save the changes.

It's then only necessary to add a line to the cron table to call this every minute. Type:

sudo crontab -e

and press Enter then copy this line and paste it at the end of the existing text:

* * * * * sudo /usr/sbin/usb_modeswitch -I -c /etc/usb_modeswitch.d/E353.conf

Hold down the Ctrl key and press X, then Y, then Enter to save the changes.

Creating a configuration file for usb-modeswitch

With the dongle in place remove the power from the RPi then power up again. Once booted and logged in type

lsusb

to get a list of all the devices connected to the USB port (a powered USB hub may be required for this stage).

The line for the dongle will contain a number in the style

14d1:1446

The first part of the number (14d1) identifies the manufacturer, in this case Huawei, and the second part (1446) identifies the product, in this case an E353 in memory card mode.

Remove and replace the dongle and type

lsusb

again. This time the dongle product number should have changed to the modem ID for the product, which shows that usb-modeswitch is working.

1446:1506

There are many configuration files already available in /usr/share/usb_modeswitch so type

cd /usr/share/usb_modeswitch

and press Enter. Then type

sudo tar -xzvf configPack.tar.gz

and press Enter. This will produce a list of files named as number pairs which should include the default dongle code found earlier (e.g. 12d1:1446). Open this file for editing by typing:

sudo nano 12d1:1446

and press Enter. Copy the displayed code. If the correct one cannot be found copy a similar one for the same vendor.

Create a new configuration file by typing:

sudo nano /etc/usb_modeswitch.d/XXXX.conf

the press Enter (change XXXX to something relevant first). Copy and paste the copied code then hold down the Ctrl key and press X, then Y, then Enter to save the changes.

Now test the configuration file by typing (replace XXXX as above):

sudo /usr/sbin/usb_modeswitch -I -c /etc/usb_modeswitch.d/XXXX.conf

and press Enter. Then typing

lsusb

and pressing Enter should show that the dongle has changed to modem mode. If it hasn't then the configuration file will need to be changed.

Next, install the wvdial program, which will actually make the connection, by typing

sudo apt-get install wvdial

and press Enter. The configuration file for wvdial needs to be edited by typing

sudo nano /etc/wvdial.conf

then press Enter. For example, for '3' edit the file to read as follows:

[Dialer Defaults]
Modem = /dev/ttyUSB0
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","3internet"
Stupid Mode = true
Modem Type = Analog Modem
Phone = *99#
ISDN = 0
Username = three
Password = three
Baud = 9600

Hold down the Ctrl key and press X, then Y, then Enter to save the changes. An internet search may be needed to find the configuration file details for other networks. Now typing

sudo wvdial

and pressing Enter should connect if the dongle is plugged in (a powered hub may be required for this). Ctrl C will disconnect.

Ensure the connection stays up by activating it every minute. Add an entry to the Cron table by typing

sudo crontab -e

and press Enter. Add this line at the end of the list:

* * * * * sudo wvdial

Hold down the Ctrl key and press X, then Y, then Enter to save the changes.

The connection status can be checked by typing

ifconfig

and pressing Enter. The 3G dongle will appear in the 'wwan0' section with the IP address in the ppp0 section.

Track the Raspberry Pi's IP address

This is not required for network connection-type dongles.

The Raspberry Pi's 3G IP address will change frequently so needs to be tracked if the camera is to be remotely accessed via a modem-type dongle. Installing ddclient on the Raspberry Pi will automatically update a DDNS service provider's fixed web address with the correct information. Several companies (e.g. Dyn, No-IP) provide this service and some are free of charge.

First set up a DDNS account so that the appropriate information is available for replies to the various questions which will appear during the following install, i.e. Service provider, Username and Password. Then type

sudo apt-get install ddclient

and press Enter. Select the option 'Find public IP' to extract the correct account IP address automatically. If you need to go back and change something after the install then use:

sudo dpkg-reconfigure ddclient

To check the current DDNS settings type:

sudo nano /etc/ddclient.conf

and press Enter. To check the current registered IP address type:

sudo ddclient -query

and press Enter.

It may take a minute or two for the IP address to be updated by the DDNS service.

Problem solving

The micro USB connector on a Pi Zero and Pi Zero W uses a system called OTG ('On The Go') where an extra pin switches between 'Host' and 'Device' (or 'Gadget') modes depending on whether the pin is grounded or not. If the wrong sort of extension or adaptor lead plug is used the dongle will power up but will not pass data. The solution is to replace the lead with an OTG type or dismantle the micro USB plug and solder pins 4 and 5 together, but this is not possible with all designs of micro USB plug.

The Raspberry Pi may take longer to boot up with a 3G dongle installed than without and it may take a minute after booting for the dongle to connect. If the connection is unsuccessful, here are a few things to try:

Powering a 3G dongle from some Raspberry Pi USB sockets can be unsatisfactory so a separate power feed may be required for the dongle. This could come from a powered USB hub but an alternative is to combine a modified USB extension lead with spare USB leads:

The modified power lead can be combined with UPS components.

Information included on this site is a personal opinion gathered from a variety of sources for information only and no responsibility can be accepted for any consequences that may arise from using the information contained here. No support is available with the practical implementation of code examples. Images are copyright and should not be downloaded or linked without permission and may be removed or changed without notice. References to products and software packages do not imply endorsement and their respective owners'/authors' rights and trade marks are acknowledged.
This site may be withdrawn at any time without notice.

The Picamera code used in this project to manage the camera board is that published at http://picamera.readthedocs.org which is Copyright Dave Hughes (no connection with this project).

© 2017 All rights reserved

First published August 2016

This website does not itself use cookies, however we may from time to time use third-party advertising companies to serve advertisements when you visit our website. These companies may use cookies to gather information about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. You can view our advertiser's privacy policy and your choices about not having this information used by clicking here. You can contact us by clicking here.