Subscribe via feed.

How-To: Raspberry Pi as a 3G/4G Router

Recently I have need to find an emergency alternative to my broadband due to the regional wide area network, Digital Region, being shut down, and the ISP Origin making a mess of getting all their cutomers onto ASDL. To get quickly back onto the internet, I have bought an ZTE MF823 4G Mobile Broadband Dongle as supplied by the badly named ‘three’ mobile phone company. As I have my own internal wired network, with multiple computers and ‘things’ there is a need to have something more sophisticated than just plugging the dongle into a single PC.

network diagram

Here is my recipe for setting up a Raspberry Pi as a router with an ZTE MF283 Dongle. In this setup all the computers are on a wired Ethernet connection using a switch for the network. The Pi has Raspbian Debian Wheezy installed (June 2014) with all the latest updates made. For testing, the dongle is plugged into the USB port via a powered hub, and the Pi connected to a switch with another PC running Linux Mint.

Setup the Dongle

This USB dongle has its own built in dialer so you do not need ppp or wvdial installed, it appears as a USB ethernet device on the Raspberry Pi. You will need a powered USB hub as the dongle can draw more power than the Pi can provide, the symptoms of too much of power being drawn will be the Pi behaving erratically or restarting unexpectedly.

With the dongle plugged in, check that it is recognised by the Pi with lsusb, it can be seen here as ‘ZTE WCDMA Technologies MSM’:

The device ID is 19d2. and 1405 is the mode, this should be 1405 – CDC ethernet. If it is not, try removing the micro-SD card and rebooting the Pi, the device modes available are:

  • 1225 – Default mode. USB Mass Storage Device + CD-ROM + card reader.
  • 1403 – Modem mode. RNDIS + Mass Storage Device.
  • 1405 – CDC ethernet
  • 0016 – Download mode

As the dongle also has a Mass Storage Device the Raspberry may not switch to CDC ethernet. If the mode does not change, try the following with usb-modeswitch:

$ sudo apt-get install usb-modeswitch
$ sudo usb_modeswitch -v 0x19d2 -p 0x1405 -d

I did not have to change the mode as it was correct already, and it didn’t change when I tried setting it as a Mass Storage device, I have not explored this any further.

When first plugged in the dongle was recognised as a ethernet device but it did not obtain an IP address:

if this is the case with you, add the following two lines to the end of sudo nano /etc/network/interfaces:

auto usb0
iface usb0 inet dhcp

the dongle provides its own address to the computer. Reboot, and you should see the obtained address:

The address 192.168.0.185 is now the internet address of the computer the dongle always assigns this address, there is also a useful web status page on http://192.168.0.1

Configuring the network

first of all enable ip4 forwarding, edit the file sudo nano /etc/sysctl.conf and uncomment the line:
net.ipv4.ip_forward=1
this will enable forwarding on reboot, you can also enable IP forwarding immediately with:
$ sudo sysctl -w net.ipv4.ip_forward=1

We now need to give the Pi a static IP address on the internal network. Edit sudo nano /etc/network/interfaces so you end up with a file that looks like this:

this gives the Pi a static IP address of 192.168.2.1.

DHCP

The next stage is to give the other computers on your network an IP address, this is done with a dhcp server:
$ sudo apt-get install isc-dhcp-server
you will need to configure dhcp sudo nano /etc/dhcp/dhcpd.conf, here is mine:

This will assign IP addresses in the range 192.168.2.50 to 192.168.2.150 to any computer connected to your network. I have used Open DNS for the Domain name Servers, if you wish to use google’s use:
option domain-name-servers 8.8.8.8, 4.4.4.4;instead. Reboot the Pi and then your test computer.

Your test computer should now have an IP address (192.168.2.51), and the gateway point to the Pi (92.168.2.1):

Accessing The Internet

The final part is to have the incoming traffic on the the Ethernet port eth0, go out on the dongle usb0. This is achieved with iptables, a firewall and traffic router. Install with:
$ sudo apt-get install iptables
and you need to setup Network Address Translation, NAT and forwarding. This short bash script clears any old settings before applying the new rules:

Where LAN is your internal network, and WAN is the internet. The final line allows you access to the Dongle’s built in web status page from any browser on your internal network, just use: http://192.168.2.1:2525

Save the file in your home directory as ~/ipt.sh, make it executable and run the script.

$ chmod +x ~/ipt.sh
$ sudo ~/ipt.sh

From your test computer, you will now be able to access the internet.

Finally, you now need to have iptables reload when you start the Pi. Export the iptables settings to a file with:

$ sudo sh -c "iptables-save > /etc/iptables.ipv4.nat"

and create a file sudo nano /etc/network/if-up.d/iptables with the following contents:

and make it executable sudo chmod +x /etc/network/if-up.d/iptables

after a reboot you can see your iptables with sudo iptables -L and sudo iptables -t nat -L.

Raspberry Pi - 4G Router

References and sources:

 

Rotary Encoders on the i2c bus

I have been getting to grips with rotary encoders on the Arduino, and to add a little drama I have gotten this working on the i2c bus. Here I will be showing how to set up the necessary hardware and demonstrate a program for the Arduino. I have used a similar setup to control an LED RGB light strip, with three rotary encoders to control the Red, Green and Blue and a fourth for special effects.

The i2c bus

The i2c bus allows connection of multiple devices to the Arduino on just two wires, these can be just about anything from temperature sensors to motor controllers with each device having its own address, up to eight of these can be used using just two wires from the Arduino.

For this project I’ll be using a single MCP23017 port expander, with which I can add sixteen digital I/O pins to the Arduino

MCP23017 pinout

The address for the expander is set on pins 15, 16 and 17 (A0, A1 and A2), for a single encoder set all of these to ground. Should you require more expanders the addresses can be set as in the table below, the MCP address in yellow is for the Arduino program.

chip
address
hardwired address i2c
address
MCP
address
A2 A1 A0
000 GND GND GND 0×20 0
001 GND GND 3v3 0×21 1
010 GND 3v3 GND 0×22 2
011 GND 3v3 3v3 0×23 3
100 3v3 GND GND 0×24 4
101 3v3 GND 3v3 0×25 5
110 3v3 3v3 GND 0×26 6
111 3v3 3v3 3v3 0×27 7

Rotary Encoders

The  encoders I’m using are the SparkFun 12-step rotary encoder with integrated push-button

rotary encoder

Inside they have mechanical contacts that output two square waves when rotated, A and B these are 90o out of phase with each other so when rotated clockwise output A is ahead of B, and counter-clockwise output B takes the lead, this is a two bit Grey code.

rotary encoder square wave
rotary switch outputs (source: datasheet)

So by comparing the two outputs we can determine the direction of rotation.

A Test Circuit

My test setup comprises of two rotary encoders, one Arduino Uno, one MCP23017 port expander, and a couple of resistors. External pull-up resistors are not required on the GPx input ports as the MCP23017 has these internally. Encoder A uses GPA0, GPA1 and GPA2 for the push button. Encoder B is on GPA4, GPA5, and GPA6.

rotary_test

Programming

My program uses the Adafruit MCP23017 and standard wire libraries. The Adafriut library addresses the GPx ports from 0-15, so GPA2 is 2, and GPB2 is 9. I have written this to output the state of rotation to the serial port at 9600 baud.

 

links:

Arduino Laser CNC Engraving Machine

Warning: This project uses a laser. It will hurt you if you are not careful. Please take care when handling the laser. Do not look at the beam, do not point it at yourself or anyone else. Remember that an IR Laser can damage the eye just as easily as a visible light one.


Laser CNC

I have built a Laser CNC machine out of a couple of old DVD-RW drives, an Arduino UNO with Grbl v0.8 installed, two EasyDriver Stepper Motor Controllers and bits of wood I have around the home. The laser came from one of the DVD-RW drives.

My Engraver is vaguely based upon the Pocket Laser Engraver by Groover, except I have used an Arduino UNO and have added limit switches. G-Code is a industry standard, of sorts, used to control CNC (Computer Numerical Control) Machines, such as lathes, routers, and in this case Laser Engravers.

Here I will be providing additional information about the electronics and Grbl configuration I discovered while building the Laser CNC. I have used the following software:

 

Electronics

Stepper Motor Controller

There are four electronic parts to this setup. The 5v power supply, a relay circuit to control the laser driver, the Laser Driver to control power to the laser, and the limit switch circuit used by Grbl to provide a stop indication on the axes.

The Power supply is a nothing glamorous, its a basic 7805 design giving a 5v output.
5v Power Supply
I use a 2A Switched Mode Power Supply plugged into the mains, similar to this 17W Switched Mode AC/DC Multi Voltage Power Supply from Maplins. Using 7.5v as the input is enough to keep the 7805 running without it having to convert too many volts into heat.

To control the laser from the Arduino, I have added a relay circuit. The 5v relay is powered by the power supply above, on one side of the normally open switches is a 5v fan to blow the smoke away, and on the other the Laser Driver, remember to switch the laser driver circuit rather than the laser diode.
relay circuit

The Laser driver using a LM317 Adjustable Regulator is a little more involved, as calculations have to be made to establish the output current. You want enough power to scorch or cut card, but not so much as to burn the laser diode out.
Laser Driver

The power output of the Laser Driver is set by the resistors R1 and R2. This video tutorial gives some explanation. Going above 500mA with a red laser will definitely cause it to blow, I have limited the power to 330mA as I only have a limited supply of lasers. I used this page for calculating resistors in parallel. Here are some milliamp output values using a couple of standard resistors.

Resistors volts / ohm mA
2 x 10R 1.25v / 5R 250mA
2 x 9R1 1.25v / 4R5 270mA
2 x 8R2 1.25v / 4R1 300mA
2 x 7R5 1.25v / 3R7 330mA

The final circuit is for the limit switches, and is based upon one found here:
limit switches
The switches need to be very sensitive, I found using that the motors were not strong enough to push button switches. I used those found in the one of the drives, and a couple from an old cassette deck. Check that you have connected up the switches correctly, X to X, Y to Y, confusion arises when they are the wrong way round and Grbl doesn’t say which switch has made contact.

In Grbl set $16=0 (hard limits, bool) if your image is larger than the allowed size then will just make your drawing look wrong, setting it to 1 (true) will cause the G-Code program to be aborted. $17=1 (homing cycle, bool) will cause Grbl to expect an $H (home) command when started, $H will cause your axes to move to their start positions.

Switch

 

Software Configuration

The only real issue with setting up Grbl, was getting the X and Y axes operating in the expected direction. There are two problems, getting the right start point, and having them go in the correct direction. You will be wanting the image in Inkscape to appear correctly. There are two settings within grbl v0.8 that need to be configured so that it works: $6 (step port invert mask) and $18 (homing dir invert mask). Having spent a while trying out various settings, I wrote a little mask calculator to assist me with this, to find your mask, click the checkboxes, or you can enter the current mask number and click set to find the binary code.

Current
Mask
Axis:
New
Mask
Invert Bit: $6=0
Set:

The documentation says that the first two bits are not used for inversion, but have found that there is a change in the direction of the motors.

Test with your favorite terminal program, on this Linux box I used: minicom -b 9600 -D /dev/ttyAMA0, turn local echo on with Ctrl-A E. Make the table go forward 1cm with X10, or in reverse X-10, Same for the Y axis, Y10, Y-10 further experimentation may be needed when you first start etching, I had a images and text appearing mirrored at first.

Now it is a case of loading your line-art into Inkscape, thin lines work best, creating the G-Code file using the laserengraver option in the extensions menu. Then sending the file to the Arduino with the Universal G-Code Sender. A Guide

My Grbl settings:

Etchnings

Links:

The Case of OpenCV and the Missing SURF

Posted under Computer, linux (no responses)

I have been wanting to have a play with the OpenCV computer vision framework on Python for a while and finally got some time to some experimenting, I am looking to have the computer recognise LEGO parts, after much research and mucking about it seems I should be using cv2.SURF and/or cv2.SIFT for what I want to do. However on Fedora 19 these are not included in the distribution RPM as they are nonfree in that they are not open source. Attempts to use SIFT or SURF result in the following error:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
2.4.6.1
>>> i = cv2.SURF()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
>>>
This is annoying as OpenCV will now need to be re-installed the old fashioned way, but there are instructions on the OpenCV site and I will be using them here with additional information I have discovered while following them.

Change to root and add the rpmfusion.org free and nonfree repositories as ffmpeg and libv41 are unavailble from Fedoras, followed by an update (it seems that Fedora 19 always has something to update):

& sudo su
# yum localinstall --nogpgcheck http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-$(rpm -E %fedora).noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-$(rpm -E %fedora).noarch.rpm
# yum update

Remove the existing OpenCV packages:

# yum erase opencv*

There are a few packages to install in preparation to compiling the code, you may have some of all of these already but yum will skip those. Install the mandatory packages, these are for compiling the source and using GTK for the GUI:

# yum install cmake python-devel numpy gcc gcc-c++ gtk2-devel libdc1394-devel libv4l-devel ffmpeg-devel gstreamer-plugins-base-devel git wget

Install the optional dependencies, I have gone for the install everything approach, they may be useful later:

# yum install libpng-devel libjpeg-turbo-devel jasper-devel openexr-devel libtiff-devel libwebp-devel tbb-devel eigen3-devel python-sphinx texlive

Now, we are ready to get the latest source, here there are two ways to do this, install the latest development version using GIT, or download the latest stable version. My preference is to use the latest stable version.

With GIT, exit back to yourself from root, change to your home directory and download OpenCV:

# exit
$ cd ~
$ git clone https://github.com/Itseez/opencv.git
Cloning into 'opencv'...
$ cd opencv
$ mkdir build
$ cd build

Or using the latest build, at time of writing this is v2.4.7. Get this via the downloads page and save it to your home directory:

# exit
$ cd ~
$ tar -zxvf opencv-2.4.7.tar.gz
$ cd opencv-2.4.7
$ mkdir build
$ cd build

Now configure:

$ cmake -D CMAKE_BUILD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_XINE=ON -D WITH_V4L=ON D WITH_OPENGL=ON -D WITH_OPENCL=OFF -D CMAKE_INSTALL_PREFIX=/usr/local ..

When complete you should check that your options agree with those displayed. There are many others available. Support for other programming languages may be missed out if they are not already installed. In my case those for Java, if I were to try OpenCV in Java I would need to install the appropriate Java packages using yum, then recompile OpenCV.

Now build and install, this can take a while:

$ make
$ sudo make install

You now need to move the module to anywhere on the Python Path, to find this:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print sys.path
['', '/usr/lib/python2.7/site-packages/PIL-1.1.7-py2.7-linux-x86_64.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/PIL', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg-info']

The directory /usr/lib/python2.7/site-packages looks suitable:

$ sudo mv /usr/local/lib/python2.7/site-packages/cv2.so /usr/lib/python2.7/site-packages

And add your new installation to the PYTHONPATH, and add the export to the end of your .bashrc so it survives a reboot:

$ export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
$ echo export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages >> ~/.bashrc

Now to test:

$ python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print cv2.__version__
2.4.7
>>> i = cv2.SURF()
>>>

Sorted. Now, perhaps, I can get on with what I actually want to do….

Sources:

Converting the Canon GPS log

Posted under Computer, linux (no responses)

Having been out and about with the Canon 6D, for once I had remembered to turn on the GPS logging built into the camera. Upon returning home after the seven mile walk I saved the GPS data in the cameras memory to the SD card. Now this LOG file found in the GPS directory of the SD card can be read directly into Google Earth but not into Memory Map for viewing on the far superior OS 1:25,000 Ordnance Survey map, also Memory Map does not read Google Earth’s KML or KMZ files.

So, the LOG file needs converting, but what format is it in? Canon are, it seems, rather unhelpful in revealing the secret but a little googeling found a reference to them using the NMEA-0183 format.

For conversion I found the free, excellent and very comprehensive gpsbabel to do the job (the PDF manual is over 200 pages), for me I convert the GPS files on a linux computer, but they do a Windows version and I assume the method and outcome will be the same. On Debian, install gpsbabel with:
$ sudo apt-get install gpsbabel

The basic use of gpsbabel for converting is as follows:
gpsbabel -i <input format> -f <input file> -o <output format> -F <output file>

As I know Memory Map reads the Garmin GPX format, I chose that as the output:
$ gpsbabel -i nmea -f 13120100.LOG -o gpx -F 13120100.gpx

And the rest is, as they say, Topographic.

walkabout

Infra-Red Coin Detector for Arduino

For something I’m building I need have the Arduino detect a coin being dropped trough a slot, for this I have built an IR detector, it comprises of an IR LED, IR Photo-Diode, Op-amp and ATtiny85 micro-controller.

IR Coin Detector (Mk3)

The circuit works by having the IR LED flood the Photo-diode so that when an object passes between them the Photo-diode stops letting current through, this is fed into the op-amp to provide a consistent output for the ATtiny85 micro-controller to detect the change in signal to then flash a couple of LED’s and provide a signal to an Arduino.

IR Coin Detector (Mk3)

Here is a program that flashes a couple of LED’s and makes output pin 4 high:

Sources:

  1. DIY Science: Measuring Light with a Photodiode II
  2. Pin Change Interrupt on the ATtiny
  3. Programming an ATtiny85 with an Arduino

Recording Sound on the Raspberry Pi

The Raspberry Pi does not have a microphone socket, which is inconvenient when you wish to record sound. To fix this you will need a USB Sound Card, for which I bought a Creative Sound Blaster Play! for about £20 and a short USB extension lead as the sound card is slightly too large and blocks the other USB port.

With the latest Raspbian “wheezy” installed on a Pi Model B with 512Mb of RAM and the overclocking set to High in raspi-config, here is a recipe for getting your Raspberry Pi to record sound from the command line. For the test setup I connected my iPod to the microphone port of the sound card, plugged everything in and powered up.

Raspberry Pi Records

After logging into the Pi, check that the computer can see the card, use lsusb to find it, here the card is highlighted in yellow:
$ lsusb
Bus 001 Device 002: ID 0424:9512 Standard Microsystems Corp.
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0424:ec00 Standard Microsystems Corp.
Bus 001 Device 004: ID 046d:c52e Logitech, Inc.
Bus 001 Device 005: ID 041e:30d3 Creative Technology, Ltd Sound Blaster Play!
remember that different makes of card will have different names and ID’s

Your user will need to be in the audio group, check this with groups <username>:$ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users netdev input
if not, then add them with:$ sudo usermod -a -G audio <username>

There is/was an issue with the Pi’s USB port that meant it can/could become overwhelmed1 with data which causes popping and bubbling noises to be included in your recordings, this can be fixed with an update of the Pi’s firmware:$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install rpi-update
$ sudo rpi-update

The Raspbian image already has the alsa-utils for sound already installed, the programs I am using for recording and playback are:

  • alsamixer – GUI for setting the recording and playback levels
  • amixer – Command Line for setting the recording and playback levels
  • alsactl – for saving the settings set in alsamixer or amixer to use again after a reboot
  • arecord – For recording the sound
  • aplay – For playing back your recording

All of these programs have a –help option.

There are two methods for setting up the the microphone port on the card, the first is alsamixer:

alsa mixer

$ alsamixerPress F6: Select Sound Card, and choose yours from the list, the bcm2835 ALSA is the on-board sound, for me the one to pick was: USB Device 0x41e:0x30d3 and take a note of the card number, in my case: 1. Now select the Mic and increase the volume to 52, or the first white blob, you’ll need to change it later, but its a good place to start. The Auto Gain Control wants to be off, select the gain control and press M to toggle so it displays [MM] for mute. Press Esc to exit and save the settings with:$ sudo alsactl store 1 where 1 is the card number.

Alternatively, you can use amixer. First find your sound card, in amixer there does not appear to be a method of listing the available cards, but on a Raspberry Pi I guess it will always be card 1, you can list the cards current status with:$ amixer --card 1 contents
numid=1,iface=MIXER,name='Mic Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=off
numid=2,iface=MIXER,name='Mic Playback Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=32,step=0
: values=21
| dBminmax-min=0.00dB,max=47.81dB
numid=5,iface=MIXER,name='Mic Capture Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=7
| dBminmax-min=0.00dB,max=23.81dB
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=3,iface=MIXER,name='Speaker Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=4,iface=MIXER,name='Speaker Playback Volume'
; type=INTEGER,access=rw---R--,values=2,min=0,max=151,step=0
: values=52,52
| dBminmax-min=-28.37dB,max=0.06dB

So I want to turn the Auto Gain Control off, and the recording volume to 14:
$ amixer -c 1 cset numid=7,iface=MIXER,name='Auto Gain Control' 0
numid=7,iface=MIXER,name='Auto Gain Control'
; type=BOOLEAN,access=rw------,values=1
: values=off
$ amixer -c 1 cset numid=6,iface=MIXER,name='Mic Capture Volume' 14
numid=6,iface=MIXER,name='Mic Capture Volume'
; type=INTEGER,access=rw---R--,values=1,min=0,max=16,step=0
: values=14
| dBminmax-min=0.00dB,max=23.81dB

again, store the settings so that they will be used again on a reboot:$ sudo alsactl store 1

Now we are ready to do a test recording, first check that arecord will see your card:$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: U0x41e0x30d3 [USB Device 0x41e:0x30d3], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0

and now for a ten second test recording, this will create a file called rectest.vav in your home directory. Remember to set the Device (-D plughw:1) number to the right card (card 1):arecord -D plughw:1 --duration=10 -f cd -vv ~/rectest.wav the -vv option displays extra information on the screen as well as a volume meter, this should be peaking at around 95% on the loudest sounds, if it is at 100% all a lot of the time then you are probably recording distortion.

Playback the recording with aplay:aplay ~/rectest.wav the default settings will play the wav fie through the TV if it is connected by HDMI, To playback through the USB sound card set the device to the card number, like in arecord:aplay -D plughw:1 ~/rectest.wav

Congratulations, you now have a fully working Pi Recording Device. Remember to experiment with the volume levels, too high and your recording will sound distorted.

References:

  1.  Raspberry Pi Usb Audio fix (10 May 2013)

Python and the Oracle Client

Posted under Computer, Database, linux (no responses)

Installing the python cx_Oracle extension module for connecting to Oracle databases on this Fedora 18 workstation turned out to be a bit of a faff by giving an assortment of unhelpful error messages, if you are having the same pain maybe this will help.

You will need two files from the Oracle Database Instant Client download site, the basic client package and the SDK (devel): http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html For Linux choose the correct flavour for your installed operating system: x86 or x86-64 for 64bit operating systems, you will need to register on the site to access the files. For my purposes I got the 11.2 version for x64 RPM files, install them using:
$ sudo rpm -i oracle-instantclient11.2-basic-11.2.0.3.0-1.x86_64.rpm
$ sudo rpm -i oracle-instantclient11.2-devel-11.2.0.3.0-1.x86_64.rpm

You will now need to tell the system where the libraries are:
$ sudo su
# echo /usr/lib/oracle/11.2/client64/lib/ > /etc/ld.so.conf.d/oracle.conf
# ldconfig
# exit

To install cx_Oracle you will need to set some environment variables otherwise you will get an “error: cannot locate an Oracle software installation” message. The easy_install program is found in python-setuptools I have included it in the recipe as a reminder if you have not installed it already.
$ sudo yum install python-setuptools
$ export ORACLE_HOME=/usr/lib/oracle/11.2/client64
$ export LD_LIBRARY_PATH=$ORACLE_HOME/lib:$LD_LIBRARY_PATH
$ export PATH=$ORACLE_HOME/bin:$PATH
$ sudo -E easy_install cx_Oracle

or by downloading the version from http://cx-oracle.sourceforge.net/ and installing it manually, you will still need to set the exports, as above, and do the following for installation:
$ sudo -E python setup.py build
$ sudo -E python setup.py install

The -E on the sudo takes your environment variables, including the three you just set, into your sudo session.

Success:
$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
>>> exit()

When the the library cannot be seen, you get this error:
$ python
Python 2.7.3 (default, Aug 9 2012, 17:23:57)
[GCC 4.7.1 20120720 (Red Hat 4.7.1-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cx_Oracle
Traceback (most recent call last):
File "", line 1, in
ImportError: libclntsh.so.11.1: cannot open shared object file: No such file or directory
>>> exit()

Attercliffe, Sheffield. Plastics recycling centre fire

Posted under Pictures (no responses)

Sunday 14th July 2013. The view from my window:

Plastics Recycling Fire

looks like a volcano

Taking Photos on the Canon EOS with an Arduino

For my Spangaly Stick project, more of which later, I have been wanting to add a remote control function for the camera for a while now and with the extended winter I found I had the time. I am using an Arduino micro-controller fitted with an XBee wireless shield to respond to a keyword sent by another Arduino with a similar setup, or from a computer with an XBee on a USB port.

take a picture - board

This board had been built to work with the Canon EOS DSLR range of cameras, but should work with other makes of camera fitted with an electronic remote socket. For basic use the camera should be set in Aperture Priority (Av) mode with the lens set to manual focus and if your using a tripod switch off any image stabilization.

For this project you will need:

  • A Windows (XP and above) Computer
  • An Arduino Uno R3
  • Two XBee’s
  • An XBee USB adapter
  • An XBee Shield for the Arduino
  • A battery to power the Arduino, I use a 12v 1.3Ah Sealed Lead Acid.
  • A remote lead for your camera
  • Ability to solder, read circuit diagrams, etc..

For this remote to work, you will need to configure two XBees to talk to each other. The easy way to do this is using the X-CTU tool (unfortunately it is Windows only) and a XBee USB Adapter. The XBees come in two main types, the Series One (S1) and Series Two (S2) they will need to be of both the same series to talk to each other, I have used Series One bees here, but not the ones without the sticking out antenna as I suspect they may be a bit delicate.

While the XbeeSheid configuration is documented on the arduino site, I shall summarise here. With the XBee plugged into the USB adapter and the adapter plugged into the computer wait for it to be detected by Windows then you can configure it. In X-CTU, on the PC Settings Tab, select the USB Serial Port. By default the XBee is set to baud: 9600, flow: None, data: 8, parity: None, stop: 1. Click the Test/Query button you should see some basic XBee settings, if it says it cannot communicate, try a different baud rate. Higher baud rates are available, but whats the rush?

Now click the Modem Configuration tab, under Modem Parameter and Firmware, click Read, after a pause at the top of the list The modem indicated will be whatever model you have, in my case XB24, select the function set we will be using: XBEE 802.5.4 RS485 ADAPTER and the firmware version: 13E8. You will need to set the Networking & Security Channel (CH) and the PAN ID (ID) to the same on both, as well as check the Serial Interfacing are set correctly, Interface Data Rate (BD): 3 – 9600, Parity (NB): 0 – NONE. Click Write to save the settings to the XBee.

For testing the XBee’s, I had one plugged into the USB adapter and the other placed into the the XBee Shield, an LED fitted across Digital Output 12 and Ground, and sent the following program to the Arduino, remembering to flick the little switch on the shield to USB for programming, and Micro for XBee emissions.

The circuit I made for taking photos includes the focus, this is optional but I have included it so the camera can be woken up before the picture is taken. An ILD74 opto-isolater is used to electrically separate the camera from the Arduino, it also simplified the circuit not having to use transistors. The LED’s are useful additions but are optional.

The diagram also shows the Canon Remote connections. Most use a 2.5mm stereo jack plug, but the more expensive cameras have a proprietary Canon N3 connector, to get the lead you will need to sacrifice a wired remote, one of these can be gotten of ebay very cheaply, about £3.00. The colours for the wires can be anything, so you will need check with a continuity tester.

The program on the Arduino listens for a keyword, in this case ‘PHOTO’ which then triggers the picture taking sequence first by setting the focus to wake the camera up then taking the picture. The Indicator LED is used to show activity on the serial port.