Version 2

    With the new Raspberry Pi Cameras being launched today, I wanted to spend a bit of time trying them out and comparing the picture quality, and so forth. The thing is, here in the element14 office we have limited access to the wi-fi since our computer network is a pretty complicated, carefully controlled, and industrial-strength corporate set up. And this put me to thinking about how to access the imagery from a Raspberry Pi Camera.

     

    After some cursory deliberation, here's what I decided I wanted:

    • A Raspberry Pi Camera with quick and efficient access so I could snap photos or record videos.
    • The ability to view the image from a smartphone or tablet, rather than adding a screen to the set up.
    • Portability, so it wasn't restricted to places with a convenient wall socket.
    • Wireless access to the content it's recorded.

     

    It all proved to be relatively simple to accomplish, provides secure access to the image, and now that it's working I realise that this portable, smartphone-controlled Pi Camera is remarkably versatile for a lot of different applications.

     

    First things first, though. Here's how to make one.

     

    What You Need

    The list of materials is sweetly short, and easily available.

     

    Adding a Camera to a Raspberry Pi

    This is perhaps the simplest part, but for the sake of completion, let's quickly look it over.

     

    Pi-Camera-Cable.jpgSlide the ribbon cable (or FFC (flexible, flat cable), if we want to be precise) that comes with your Pi Camera V2 into the "CAMERA" connector on your Raspberry Pi. To do this, first lift the white collar along the top edge of the connector. You'll hear a little click as it comes free.

     

    The exposed side of the connector (the bit with the silver lines on it, as opposed to the blue stripe) should be facing the HDMI connector as you insert it into the "CAMERA" connector. Push the white collar back down into place, securing the ribbon cable in place.

     

    Start up your Raspberry Pi, with the monitor (or TV), keyboard and mouse connected. Open a Terminal window, and type:

    sudo raspi-config

    Hi enter, and this brings up the Pi's configuration options. We're assuming, at this point, that you've already got Raspbian installed on the card and set the Pi up, by the way.

     

    Select "Enable Camera", followed by "Enable", and then "Finish". When it asked you to reboot, say yes.

     

    And that's the camera set up as far as the Raspberry Pi 3 is concerned. However, for the sake of testing the next part, also connect your RPi3 to your wi-fi network.

     

    Accessing the Pi Camera from Your Smartphone

    Rpi-Cam-Web-Interface-screenshot.jpgThe work for this has been done already by the awesome Silvan Melchior, in the shape of his RPi Cam Web Interface app. This free application provides access to your Pi Camera image and its setup and controls from any web browser; including the one on your smartphone. And it does all this without your device needing any additional software, which is great news.

     

    To install RPi Cam Web Interface on your Pi, open a terminal and type the following commands:

     

    git clone https://github.com/silvanmelchior/RPi_Cam_Web_Interface.git

    cd RPi_Cam_Web_Interface

    chmod u+x *.sh

    ./install.sh

    Once it's finished installing, connect your smartphone or tablet to the same wi-fi network as the Raspberry Pi, launch web browser and go to the IP address of the Raspberry Pi.

     

    This shows you the Pi Camera image and control options, all within your browser window. How easy was that?

     

    I'm going to leave you to read through Silvan's instructions on things like motion detection, resolution and so forth, because those are project-specific settings for whatever you decide to do with the portable Pi Camera. That said, it's all pretty obvious when you look at them in the settings menus in your web browser, so have a fiddle and then come back and join us for the next part where we free the Pi Camera from the confines of a Wi-Fi network.

     

    Option 1: Wi-Fi Hotspot on your Smartphone

    Pi-Camera-Mobile-Hotspot.jpgThis option is simpler, but I'm not as keen on it as a solution for using RPi Cam Web Interface while on the move. But let's take a look anyway, given that it's quicker and easier.

     

    I used the option on my smartphone (a Samsung Galaxy S5) to create a Wi-Fi hotspot, and then connected the RPi3 to that network. You need to do this while the Pi is still hooked up to the keyboard and monitor and such.

     

    Start a Wi-Fi hotspot on your phone, and search for Wi-Fi networks on the Pi. Select your hotspot, and connect (with the password, if you included one when starting it up on your phone).

     

    My phone then told me a device had connected to its hotspot network, and gave me the IP address. Navigate to that IP address in the smartphone's web browser, and there you can access RPi Cam Web Interface, and thence the image and controls. Easy, but a little limited.

     

    One of the reasons I don't like this option as much is that I'm not sure whether the phone will always give the Pi the same IP address (I suspect it won't), and not every model of smartphone will work the same way. I can't say for sure, but I suspect an iPhone probably doesn't even let you do this, and if you're using a tablet you'll probably have similar difficulties. But it works, if you want to cut out a bit of work in option 2 below.

     

    Option 2: Set Up the Raspberry Pi 3 as a Wi-Fi Hotspot

    The alternative, which is preferable and more versatile, is to configure your Raspberry Pi 3 as a Wi-Fi hotspot (or access point, if we're being literal) instead of the smartphone. You then connect your smartphone, tablet, laptop or multiple devices to the RPi's network, and get to RPi Cam Web Interface that way. This is better because it's entirely cross platform in terms of devices, and makes it easy for multiple people to all view the image.

     

    Raspberry Pi guru Phil Martin helped out a lot with this setup, which was a bit trickier than anticipated. Potentially this is because I wanted to skip some stages, since we're not actually using our Raspberry Pi 3 as a Wi-Fi access point at all. At least, not for the point of sharing an internet connection. Potentially you guys can all help me refine this process, so do let me know in the comments if you can see a better way of doing this.

     

    In the meantime, here's the next stage in the project. With your Pi still hooked up to your monitor, keyboard and with Wi-Fi internet access, open terminal and type:

    sudo apt-get install dnsmasq hostapd

    This installs a DHCP and DNS server called "dnsmasq", which will hand out IP addresses to the devices you view the Pi Camera on, and the "hostapd" package that'll turn the Raspberry Pi 3 into a Wi-Fi access point.

     

    Before we start setting things up, we want to shut down the Raspberry Pi's Wi-Fi service so it allows you to make changes to what it's doing. In terminal, type:

    sudo ifdown wlan0

    We want to give the Raspberry Pi 3 a static IP address, so you don't have to change it every time you open your smartphone browser looking for RPi Cam Web Interface. In terminal we open the settings for dhcpcd to edit them. Type:

    sudo nano /etc/dhcpcd.conf

    Add these lines to the bottom of the file:

    interface wlan0

        static ip_address=172.24.1.1/24

    That doesn't have to be the IP address, if you prefer something different. Whatever it is, you just have to remember it for later.

     

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return.

     

    There are some existing settings that can clash here, so let's take care of those before we go much further. Open the existing interface configuration file in terminal by typing:

    sudo nano /etc/dhcpcd.conf

    Find the following line and add a "#" symbol to the beginning of the line, so your Raspberry Pi 3 ignores that command in the future.

    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

    So it now looks like this:

    #wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return. Enter the following in terminal to restart the dhcpd service, so your changes take immediate effect:

    sudo service dhcpcd restart

    Now we're on to setting up hostapd, and configuring the Wi-Fi hotspot. Let's open the configuration file for editing in terminal:

    sudo nano /etc/hostapd/hostapd.conf

    Put the following lines into the file:

    interface=wlan0

    driver=nl80211

    ssid=Pi3Camera

    hw_mode=g

    channel=6

    ieee80211n=1

    wmm_enabled=1

    ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]

    macaddr_acl=0

    auth_algs=1

    ignore_broadcast_ssid=0

    wpa=2

    wpa_key_mgmt=WPA-PSK

    wpa_passphrase=YourPasswordHere

    rsn_pairwise=CCMP

    You can change the "ssid" line to whatever you want. This is the name of the Wi-Fi network that you'll see on your smartphone when connecting to the Raspberry Pi 3.

     

    The "channel" can also be changed, if you prefer a different one.

     

    Finally, change "wpa_passphrase" to whatever password you want to use when people connect to the Raspberry Pi 3's Wi-Fi hotspot. In the text above you can see that it's set to "YourPasswordHere", so change that.

     

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return.

     

    Next we want to make sure the RPi3 uses this new configuration file whenever it start up and loads hostapd. Edit hostapd's setting by typing the following in terminal:

    sudo nano /etc/default/hostapd

    Find the following line:

    #DAEMON_CONF=""

    And change it to this (remembering to remove the # at the beginning of the line):

    DAEMON_CONF="/etc/hostapd/hostapd.conf"

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return.

     

    Even though we're using the Raspberry Pi 3 for slightly different purposes than sharing a wired internet connection via Wi-Fi, we still need it to hand out IP addresses to your smartphones and tablets when they connect. Phil recommends getting rid of the existing dnsmasq settings file, as it's over complicated, and creating a new one by typing the following into terminal:

    sudo mv /etc/dnsmasq.conf /etc/dnsmasq.conf.orig 

    sudo nano /etc/dnsmasq.conf 

    Add the following to the new, blank dnsmasq configuration file that you're now looking at in terminal:

    interface=wlan0

    bind-interfaces

    server=8.8.8.8

    domain-needed

    bogus-priv

    dhcp-range=172.24.1.10,172.24.1.19,12h

    The "dhcp-range" section are the range of IP addresses the RPi3 will hand out to devices. This can be used as a way to limit the number of devices that can simultaneously access your Pi Camera. In the text above, you can see that only 10 IP addresses are available, on a lease time of 12 hours. The first device to connect will get the IP address "172.24.1.10", the second will get "172.24.1.11", the third will get "172.24.1.12" and so on. Again, it's up to you how many you make available here, but think of it as something of a secondary security option, if you like.

     

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return.

     

    The following list of commands routes these Wi-Fi IP addresses to the ethernet port, which we probably don't need to do under our unique circumstances. However, I was having some difficulties in getting the hotspot working, so I added them anyway. Maybe you can tell me if I needed to or not?

     

    Type in:

    sudo nano /etc/sysctl.conf

    And remove the # from the beginning of this line:

    net.ipv4.ip_forward=1

    This command gets it started without a reboot:

    sudo sh -c "echo 1 > /proc/sys/net/ipv4/ip_forward"

    Enter the following to set up an NAT between the RPi3's Wi-Fi and wired connections:

    sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE 

    sudo iptables -A FORWARD -i eth0 -o wlan0 -m state --state RELATED,ESTABLISHED -j ACCEPT 

    sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT

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

    Open a new dhcpcd config file with:

    sudo nano /lib/dhcpcd/dhcpcd-hooks/70-ipv4-nat

    And add this line to it:

    iptables-restore < /etc/iptables.ipv4.nat

    Press CTRL+X to exit, press "Y" to save the changes to the file, and hit return. And we're there! Type in the following to start up the hostapd and dnsmasq services:

    sudo service hostapd start

    sudo service dnsmasq start

     

    Using The Ultimate Smartphone-Controlled Portable Raspberry Pi 3 Camera

    Okay, let's bring it all together.

     

    You can power the Raspberry Pi 3 from a mains socket if there's one available for your particular application, or if you're taking it out and about, why not use a powerbank? I've had the Portable Raspberry Pi 3 Camera running on an old, rather lame powerbank for over two hours at the time of writing, with just under half of the juice remaining.

     

    Open your smartphone's (or tablet's or laptop's) Wi-Fi settings, and scan for nearby networks. Select the one you set up as the SSID above, enter the password and hit connect.

     

    Launch your smartphone's web browser, and type the IP address we set up above into the address bar. In the settings above, that would be 127.42.1.1. And you should be looking at a live image from your Pi Camera without the need to have everything connected to a Wi-Fi network. Below is time lapse video test I ran for a couple of hours while writing this project up. It's not very interesting, admittedly, but I just needed something slow moving to test the time lapse function, and all I had was a cloudy day!

     

     

    What you do with The Ultimate Smartphone-Controlled Portable Raspberry Pi 3 Camera it is up to you, but I've put together a few ideas over here.