Making things smarter – building a wireless digital picture frame

Taking mundane objects and connecting them to one another is one of the key intentions of the whole IoT phenomenon. In our next recipe, we will create a digital picture frame. However, ours is is not the typical off-the-shelf version where you load images from an SD card. Instead, we will use a combination of web services that are commonly used in IoT together with a wireless image player to update the digital picture frame dynamically and seamlessly from a smartphone.

Getting ready

The materials needed are as follows:

  • A smartphone with internet connectivity
  • The BBB powered by 5V
  • A Wi-Fi dongle
  • An LCD (5 inches or larger)

You will also need to set up two web service accounts:

Using IFTTT

For this recipe, we will incorporate the If This Then That (IFTTT) web service. Although not perfect, IFTTT is a fun, rapid development tool for prototyping IoT scenarios and use cases. There are other ways to perform what I will outline later; you could hook up other popular third-party apps, such as Instagram, Flickr, and Twitter. However, I prefer to keep the additional installations at a minimum and establish more of a least common denominator environment that provides a simpler development path.

For long-term serious product builds, IFTTT is not a good choice because it is too "black box", too opaque in its functionality to rely on for final designs. However, for rapid protoyping and idea generating, it is highly useful. With IFTTT, you can iterate quickly and nimbly using a host of different web services that will always lead to new ideas and directions for your development.

How to do it...

Part one: The smartphone setup

Perform the following steps:

  1. On your smartphone, shoot a photograph with a camera app.
  2. Save the photo to a custom album on your phone that you need to create. Name it NowPix.
  3. Set up the IFTTT trigger on your phone to send the saved album file to Dropbox.

Setting up and testing the Dropbox script on the BBB

Perform the following steps:

  1. If you do not already have a Dropbox account, sign up for a free account at https://www.dropbox.com/home.
  2. Download and install a custom bash script called Dropbox Uploader. This is a wonderfully useful open source tool created by developer Andrea Fabrizi. The script provides an easy method to interact directly with the Dropbox's API authentication process, so providing your username and password is not necessary.
  3. Now, make sure that you are in your home directory:
    $ cd
    
  4. Then, clone the Dropbox Uploader code using the following command:
    $ git clone https://github.com/andreafabrizi/Dropbox-Uploader/
    
  5. Make sure that the directory was created, as shown in the following command:
    $ cd Dropbox-Uploader
    
  6. Set permissions with the following command:
    $chmod +x dropbox_uploader.sh
    $./dropbox_uploader.sh
    
  7. Then, run the script as follows:
    ~/Dropbox-Uploader$ ./dropbox_uploader.sh
    

    You should see the screen output with a set of instructions:

    This is the first time you run this script.
    

Back to your desktop client

Before responding to the terminal prompts, you will now need to set up a Dropbox App via your desktop client (PC or Mac). This app will hook to the Dropbox Uploader script.

Perform the following steps:

  1. Open the following URL on the desktop in your browser and log in using your Dropbox account at https://www2.dropbox.com/developers/apps.
  2. Click on Create App and then select the Dropbox API app.
  3. Select Files and Datastores. The rest of the fields should be filled out in the following manner:

    Can your app be limited to its own folder?

    No

    What type of files does your app need access to?

    Specific file types—My app only needs access to certain file types, like text or photos.

    Last question, what type of files will your app use?

    Images

  4. Give the app a name in the last text field. Mine is called NowPix to remain consistent with the directory/album name that we set up on the iPhone. Then, finish by clicking on the Create App button.
  5. Now, generate an App key.

Back to the BBB

The next series of steps continue with the verification/authentication process using Dropbox. Perform the following steps:

  1. In the open terminal window and at the appropriate prompt, paste the App Key generated in the previous step and follow the onscreen prompts.
  2. When your new app is successfully created, enter the App Key, App Secret, and the permission type shown in the confirmation page as follows:
    # App Key: YOUR_API_KEY_HERE
    
  3. Hit the return (Enter) key after you input the appropriate data, given the next several terminal prompts.

    Now, App Secret has been generated. Cut and paste that string from the Dropbox to the terminal window, as shown in the following code:

    # App secret abcdefghijklmnop1234
    Permission type, App folder or Full Dropbox [a/f]:
    App folder (f)
    App key is abcdefghijklmnop1234, App secret isabcdefghijklmnop1234 and Access level is Full Dropbox. Looks ok? [y/n]:
    [y/n]: y
    

    Hit return and you are finished at least with the installation.

    Note

    If you accidentally input the wrong App Key and App Secret (which I did since I have several Dropbox apps), you can reset the access by running the following script:

    $ ./dropbox_uploader.sh unlink

Testing and running the script

We now need to verify that the script operates as expected: downloading images from Dropbox and placing them in a specified directory.

  1. First, we will verify that the content of our IFTTT directory on the Dropbox can be accessed and read properly, so we will list the images that are there:
    $ cd Dropbox-Uploader
    $ ./dropbox_uploader.sh list /IFTTT/iOS_Photos/NowPix
    
  2. Go get the actual images and download them to your directory using the following command:
    $ ./dropbox_uploader.sh download -s /Apps/NowPix /home/debian/NowPix
    

    We added the -s option that skips the already existing files when you download/upload; otherwise, the default parameter is to overwrite the files in the target directory.

  3. Verify that you did, in fact, get the images with the following command:
    $ cd NowPix
    $ ls
    
  4. Assuming that your script executed properly and the images were downloaded to the right directory, we now need to set up a process in order for the script to run transparently so that you don't have to run it each time you want to download images. To perform this, we will set up a cron job that automatically runs the script at specified intervals. We will begin this part by opening crontab in our default editor with the following command:
    $ crontab -e
    

    The -e option tells the shell to open our default editor, which in our case is nano.

    Note

    You should take note of these comments from the original script developer:

    ## Running as cron job

    Dropbox Uploader relies on a different configuration file for each system user. The default configuration file location is HOME_DIRECTORY/.dropbox_uploader. This means that if you do the setup with your user and then try to run a cron job as root, it will not work.

    So, when you run this script using cron, keep the following things in mind:

    Remember to set up the script that the user used to run the cron job.

    Always use the -f option to specify the full configuration file path because sometimes in the cron environment, the home folder path is not detected correctly.

    For security reasons, I recommend you to not share the same configuration file with different users.

  5. Add the following code to the end of the file that opens on your screen:
    */3 * * * * /home/debian/Dropbox-Uploader/dropbox_uploader.sh -s download /IFTTT/iOS_Photos/NowPix /home/debian/
    

    The number 3 is the frequency in which the cron job will run, which in our case is every 3 minutes. We have it set for such a high frequency only for testing purposes. When we are finished with our testing, you should change this number to a higher one, perhaps every few hours or longer, depending on how often you plan to add photos to your NowPix digital picture frame.

    The rest of the line with all the asterisks corresponds to the following layout for a cron job:

    minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command
    
  6. With cron, it is a good idea to verify that the job has run as expected. You can perform this easily with the following command that checks an e-mail file that crontab automatically runs once a cron job has been set up:
    $ tail -f /var/mail/debian
    

    You should see a log of the system e-mails that crontab will send, including the output from the downloader.sh script. This output should mirror the kind of output you saw when you were running the earlier bash script in the terminal window.

  7. Next, we need to install and set up a package that will open and cycle the images that we will download on the BBB. For this, we will use feh, a lightweight and powerful image viewer, as shown in the following code:
    $ sudo apt-get update
    $ sudo apt-get install feh
    
  8. feh requires -x so that you can run the app remotely via vnc as follows:
    $ feh -x
    
  9. Now, we can view all our photos taken on our smartphone as a slide show:
    $ feh -FD10 /home/debian/NowPix
    

    FD is the command to run a slideshow, whereas the number after FD is an adjustable play duration for the image.

    The program must also point to the directory with the images that will run as a slideshow. Use the Esc button on your desktop to exit from the slideshow.

    You may see another permissions-related error when you try to execute the feh program. The error may say something similar to the following code:

    Invalid MIT-MAGIC-COOKIE-1
    

    If so, run the following command to delete the .Xauthority file:

    $ rm .Xauthority*
    
  10. Next, we will create a systemd service (remember that?!) that runs the command to load the images and display them in a loop on bootup. So, log in as root, as shown in the following code:
    $ sudo -i
    
  11. Navigate to the systemd directory with the following code:
    # cd /lib/systemd/system
    
  12. Now, create a new service file as follows:
    # sudo nano nowpix.service
    
  13. Then, paste the following code to the editor window:
     [Unit]
    Description=Run NowPix images using Feh on boot
    
    [Service]
    Type=simple
    ExecStart=/usr/bin/feh -FD10 /home/debian/NowPix
    Environment=DISPLAY=:0
    SyslogIdentifier=fehslideshow
    Restart=on-failure
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
    
  14. Start the systemd service with the following code:
    # systemctl start nowpix.service
    
  15. Finally, run the following command that enables the service to start at bootup:
    # systemctl enable nowpix.service
    
  16. Now, reboot your BBB. If all is well, you should now have pictures cycling on your screen updated from your new photos on your smartphone.

See also

Crontabs is a bigger subject to learn and useful in many scenarios, where you want to time events on your BBB. For more information, refer to:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.189.177