Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Friday, 15 May 2015

Adding USB to Ethernet to Arietta

Introduction
Arietta is a low cost linux embedded module: http://www.acmesystems.it/arietta I already talked about it, here:
This post talks about adding a usb to ethernet adaptor for arietta.
The adapter is based on asix hardware, you can find it in various shapes:


Modding the Kernel
My build enviroment is based on a debian linux, created with vagrant.
There is this very good tutorial on acmesystem website: http://www.acmesystems.it/compile_linux_3_16 on how to compile a new kernel for the board.
Instead of following the tutorial untill the end, when you reach "make ARCH=arm menuconfig"you have to enable the "Multi-purpose USB Networking Framework" and select the appropriate ethernet adapter:


You can find the section under Device Drivers / Network device support / USB Network Adapters.
After you have rebooted with the new kernel you can plug the adapter and see if the device is recognized typing "dmesg".

My ethernet device is recognized as "eth0", in order to configure it with dhcp you have to edit /etc/network/interfaces on arietta and insert these lines at the end:

auto eth0
iface eth0 inet dhcp
 
Plug the ethernet cable, reboot the board and... done!







Wednesday, 25 February 2015

Security Webcam: (Improve) Uploading Photo to Dropbox with a Linux Board


Recently I bought a used webcam Microsoft Lifecam HD3000 on ebay.
I was interested in this device because it is Linux compatible and it can be used with one of my linux boards: Arduino Yun.



I was interested to use the Webcam, the Yun and a Pir Sensor to make a rudimental security camera system for my home.

If you are interested in this type of activity, you can find a lot of material, just "googling" (and of course you can use another linux board, like a raspberry pi):
What I didn't like from these projects is the way they execute actions from the arduino side.

The components of my finished project are very similar to the first link from adafruit.com: a webcam, a pir sensor and the arduino yun.

The events on the Yun, Arduino side
The example that I've found works like this:
  1. wait for move event (eg. from pir sensor)
  2. use Process and call fswebcam to get a photo from the webcam
  3. use Process and call a python script to upload a photo on dropbox
  4. ... do something ...
  5. go back to 1.
Everytime in a yun's sketch you will use Process, you are blocking the execution of the arduino part until the command is completed.
As long as the step 3 is completed, it is impossible to capture another photo.
Some samples use Temboo for dropbox, but I don't like it: it makes the sketch more complicated to mantain and the Dropbox Api is easy to use.
Since I didn't want to wait the 3rd step or use Temboo api on my system, I have wrote some code.

My solution: Arduino part
In my solution the arduino part work like this:
  1. wait for move event (From pir sensor)
  2. use Process and call fswebcam to get a photo from the webcam saving it, in a temporary directory
  3. use Process to move the photo to a destination directory where will be picked by the linux part
  4. go back to 1
The step number 3 is needed to be sure that the photo is totally written before the upload on dropbox.
Now, I have again 2 call to Process... but move a photo of less than 100 kbyte from a directory to another is faster than uploading the same photo to dropbox.
The arduino side, it is indipendent from the dropbox upload: this is a job for the linux side.
(if you want the arduino's sketch, just ask in the comments).

Linino part
I wrote a python script upload_to_dropbox.py in /root, that is able to continually check the photos's destination directory and when it finds one new, it uploads it on my dropbox account and on a succesfull upload, it deletes the local photo from the yun.
For setup of python and dropbox library on arduino yun I have followed this tutorial.
What I have changed from it, it is the sketch and the python script.

The python script is here, just edit it with your generated dropbox key.

 import dropbox  
 import glob   
 import os  
 import json  
 from time import sleep  
 photodir = '/mnt/sda1/arduino/www/';  
 client = dropbox.client.DropboxClient('DropBoxGeneratedKeyGoHere')  
 #starting main loop here  
 while(1):  
      toupload = glob.glob(photodir+'*.jpg');  
      try:  
           for fname in toupload:  
                print "Uploading... " + fname;  
                f = open(fname, 'rb');  
                response = client.put_file(fname[len(photodir):], f);  
                print "uploaded" + fname[len(photodir):];  
                print response['bytes'];  
                if(response.has_key("bytes") and response['bytes'] > 0):  
                     os.remove(fname);   
           sleep(0.3);  
      except:  
           sleep(2);  

In order to start the script automatically on each boot of the Yun, you need to edit /etc/rc.local adding:

export PYTHONPATH=/mnt/sda1/python-packages
python /root/upload_to_dropbox.py &





Monday, 11 August 2014

Smart Charger: automatically backup your photos

[This is the second post about Arietta: the first is here]

The project
The idea behind this little project is to have a system that charge my phone and automatically backup the photo of my android phone while it is connected.

First of all, you need to attach an usb female adapter to Arietta and connect an usb (powered) hub to it: Follow this page

In order to get the photos on the android phone from Arietta you need to use MTP protocol and there are 2 ways:
a - install mtp-tools packages from repository
b - activate mtp filesystem

For my project the first solution is easier:  launch apt-get install mtp-tools

The solution b was better, because it allows you to mount the phone filesystem and copy files from it with normal linux commands like cp,rsync,mv.. but it's harder because you need to rebuild the arietta image.

External Usb Storage
The Arietta will copy my photos on an external storage instead of the internal sdcard of arietta because this helps to improve the life of the sdcard.

To automatically mount an external storage in a predefined directory:
- use blkid command to obtain an id that identify you external usb storage for the system
- change /etc/fstab and insert a line to specify where the storage mount point

My external usb storage is a simple pendrive formatted with fat32 with UUID FC4D-37AA.
At the end of /etc/fstab I've added this line to mount it on /mnt everytime is plugged in:

UUID=FC4D-37AA /mnt vfat defaults,umask=007,gid=46 0 0


The Backup Script ...
I've created a script called photobackup.sh that use some commands from mtp-tools package to copy and delete jpg photos from the phone:

#!/bin/bash

#check if external storage is present or abort
checkmount=`mount | grep /mnt | wc -l`
echo $checkmount;
if [ $checkmount -ne "1" ]; then
        echo "Usb External Storage not mounted...exiting";
        exit;
fi

echo "Usb External Storage Found";
mtp-files | grep .jpg | awk '{ print $2 }' > lista.txt
while read F ; do
    #there is enough space on device
    spacecheck=`df -h | grep /mnt | awk {'print $5'} | tr -d '%'`
    if [ $spacecheck -gt "95" ]; then
        echo "Not enough space left on device..exiting";
        exit;
    fi
    mtp-connect --getfile $F /mnt/$F # copy the file
    mtp-connect --delete $F          # delete the file from the phone
done

Explanation:

- it checks if Usb External Storage is mounted or it aborts the operation
- it creates a list of jpg files (supposing these are all photos) lista.txt


... and for every photo:
- it checks if there is enough space left on the Usb External Storage
- it reads the list of photos from lista.txt and it uses mtp-connect to copy file to the /mnt/ directory and it deletes the photo from the phone


..and the UDEV Rule!
In order to call photobackup.sh everytime you plug the phone to the hub you need a udev rule.

Create a script 98-my.rules with this content:
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666", RUN+="/root/photobackup.sh"

and copy it on /etc/udev/rules.d

Now everytime you attach the android phone to the hub, while the phone is charging, arietta copy your photos in the external pendrive, if present.

In the next article we will see how to add a led to monitor the status of the operation.