GGshow reloaded GGshow reloaded

April 11, 2016

RPi RF Transmitter & Radio Controlled Sockets

Playing with my new toy – Pi-mote Control starter kit with 2 sockets.

Sample code – turn on all plugs for 5 seconds.

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(17,GPIO.OUT)
GPIO.setup(22,GPIO.OUT)
GPIO.setup(23,GPIO.OUT)
GPIO.setup(27,GPIO.OUT)
GPIO.setup(24,GPIO.OUT)
GPIO.setup(25,GPIO.OUT)

GPIO.output(17,True)
GPIO.output(22,True)
GPIO.output(23,False)
GPIO.output(27,True)
sleep(0.1)
GPIO.output(25,True)
sleep(0.25)
GPIO.output(25,False)

sleep(5)

GPIO.output(17,True)
GPIO.output(22,True)
GPIO.output(23,False)
GPIO.output(27,False)
sleep(0.1)
GPIO.output(25,True)
sleep(0.25)
GPIO.output(25,False)

Complicated? There is an easier way, by using Energenie library.

Installing Energenie library

For Python 3

sudo apt-get install python3-pip
sudo pip-3.2 install energenie

For Python 2

sudo apt-get install python-pip
sudo pip install energenie

* pip is a package management system used to install and manage software packages written in Python.

Sample code – turn on all plugs for 5 seconds

from energenie import switch_on, switch_off
from time import sleep
switch_on()
sleep(5)
switch_off()

It is much easier now, doesn’t it?

Updated: 11/9/2016
To control each switch separately, hold down the green button on one switch for 10-15 seconds, send channel 1 on command, hold down the green button on the other switch for 10-15 seconds, send channel 2 on command.
Now I can switch on/off the plug separately.

from energenie import switch_on, switch_off
from time import sleep
switch_on(1)
sleep(3);
switch_off(1)
sleep(3);
switch_on(2)
sleep(3);
switch_off(2)

Now I’ll be able to automatically turn on my radio every morning to wake me up, or remotely turn on my rice cooker before I arrive at home 😉

Reference
Controlling electrical sockets with Energenie Pi-mote – Raspberry Pi
Energenie Documentation
Energenie Python library source code

March 31, 2016

RPi 5 inch HDMI LCD

Bought a 5″ HDMI LCD screen for my Raspberry Pi, but it wasn’t working until I made some changes at /boot/config.txt file to force 800×480 screen resolution, edit /boot/config.txt and modify these lines:

hdmi_force_hotplug=1
hdmi_group=2
hdmi_mode=87
hdmi_cvt 800 480 60 6 0 0 0

Raspberry Pi 5inch HDMI LCD

 

Optionally to enable support of MJPEG, VP6, VP8, Ogg Theora & Ogg Vorbis, add these lines:

start_file=start_x.elf
fixup_file=fixup_x.elf
Filed under: Internet of Things (IoT),Linux,Raspberry Pi — Tags: , , , , — GG @ 2:08 pm

February 27, 2016

RPi camera

Enabling camera on your RPi

  • sudo raspi-config
    

Terminal commands

  • To take a picture
    raspistill -o filename.jpg
    
  • To play a recorded h264 video
    omxplayer filename.h264
    
  • To record a slow motion video (10 seconds, resolution 640×480 pixels, 90 frames per second)
    raspivid -w 640 -h 480 -fps 90 -t 10000 -o filename.h264

Installing Python API for RPi camera

  • sudo apt-get install python-picamera python3-picamera python-rpi.gpio
    

Python code samples

  • Preview and capture a photo
    import time
    import picamera
    with picamera.PiCamera() as camera:
        camera.start_preview()
        time.sleep(10)
        camera.capture('filename.jpg')
        camera.stop_preview()
    
  • Capturing multiple photos
    import time
    import picamera
    with picamera.PiCamera() as camera:
        i = 0
        while True:
            camera.start_preview()
            time.sleep(1)
            camera.capture(str(i)+(".jpg"))
            i += 1
            time.sleep(5)
    
  • Capturing photo when push switch pressed
    * connect a push switch to GPIO2 & GND
    * use current timestamp as filename
    * save JPG when push switch pressed

    from time import gmtime, strftime, sleep
    import picamera
    import RPI.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)
    button=2
    GPIO.setup(button,GPIO.IN,pull_up_down=PIO.PUD_UP)
    while True:
        with picamera.PiCamera() as camera:
            camera.resolution = 1920,1080
            GPIO.wait_for_edge(button, GPIO.FALLING)
            camera.start_preview()
            sleep(2)
            camera.capture(strftime("%Y%m%d%H%M%S",gmtime())+'.jpg')
            camera.stop_preview()
    

    A photo posted by @le_g_end on

    #raspberrypi #camera & #pushswitch

    A video posted by @le_g_end on

  • Recording a 10 seconds video
    from time import sleep
    import picamera
    with picamera.PiCamera() as camera:
        camera.start_recording('filename.h264')
        sleep(10)
        camera.stop_recording()
    
    
  • Video recording and playback
    import os
    while True:
        os.system("raspivid -w 640 -h 480 -fps 90 -t 10000 -o filename.h264")
        os.system("omxplayer filename.h264")
    

Reference

RPi Chapter 1

Preparation

  1. Download Raspbian and install it on SD card.
  2. Login using default username: pi, and password: raspberry
  3. Run Xserver
    startx
    
  4. Update and cleanup
    sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get clean
    
  5. Installing packages
    sudo apt-get install {package}
    

June 6, 2015

When Pi open Windows

Microsoft released Windows 10 IoT Core Insider Preview today.

Download Windows 10 IoT Core Insider Preview with support for Raspberry Pi 2
Unzip Windows_IoT_Core_RPI2_BUILD.zip
Run Command Prompt as Administrator, navigate to the unzipped folder.

Insert SD card into card reader.
Install image file (flash.ffu) to your SD card.

dism.exe /Apply-Image /ImageFile:flash.ffu /ApplyDrive:\\.\PhysicalDriveN /SkipPlatformCheck

If your SD card is disk number 3, use /ApplyDrive:\\.\PhysicalDrive3
To find the disk number of your SD card:

diskpart
list disk
exit

Remove your SD card with Safely Remove Hardware. Your SD card is now ready to boot Windows 10. Insert SD card into your Pi, Connect HDMI monitor, network cable, power supply. Boot Windows 10 IoT Core Insider Preview.

Windows 10 IoT Core Insider Preview

Reference
PowerShell documentation

Filed under: Raspberry Pi,Windows — Tags: , , — GG @ 6:24 am

March 9, 2015

How to block a specific network port in Linux?

Question:

How to block a specific network port in Linux?

Answer:

Use iptables commands.
E.g. to block port 21 & 22:

iptables -A OUTPUT -p tcp --dport 21 -j REJECT
iptables -A OUTPUT -p tcp --dport 22 -j REJECT

To block a port permanently

  1. save iptables to a file,
    iptables-save > /etc/sysconfig/iptables
  2. add this line to /etc/rc.local
    iptables-restore < /etc/sysconfig/iptables
Filed under: Linux,Web Servers — Tags: , , — GG @ 5:27 pm

September 18, 2014

How to perform factory reset on MacBook Pro?

Question:

How to perform factory reset on MacBook Pro?

Answer:

  1. Hold down Cmd+R keys during startup to enter OSX Recovery
  2. Select Disk Utility, select Macintosh HD, click Erase.
  3. Once done, Quit Disk Utility, and select Reinstall OS X.

Reference

OS X: About OS X Recovery

Filed under: MacOS — Tags: , , , , — GG @ 5:22 pm

February 10, 2013

Interesting route of 216.81.59.173

Try run this command in your terminal:

traceroute -m 100 216.81.59.173

Or if you are using Windows:

tracert -h 100  216.81.59.173

It gives output like this:

...
 17     *        *        *     Request timed out.
 18   595 ms   619 ms   608 ms  Episode.IV [206.214.251.1]
 19   566 ms   605 ms   703 ms  A.NEW.HOPE [206.214.251.6]
 20   580 ms   658 ms   629 ms  It.is.a.period.of.civil.war [206.214.251.9]
 21   587 ms   648 ms   658 ms  Rebel.spaceships [206.214.251.14]
 22   564 ms   629 ms   619 ms  striking.from.a.hidden.base [206.214.251.17]
 23   565 ms   658 ms   610 ms  have.won.their.first.victory [206.214.251.22]
 24   611 ms   622 ms   619 ms  against.the.evil.Galactic.Empire [206.214.251.25]
 25   564 ms   600 ms   618 ms  During.the.battle [206.214.251.30]
 26   643 ms   641 ms   624 ms  Rebel.spies.managed [206.214.251.33]
 27   575 ms   638 ms   598 ms  to.steal.secret.plans [206.214.251.38]
 28   614 ms   645 ms   612 ms  to.the.Empires.ultimate.weapon [206.214.251.41]
 29   639 ms   688 ms   702 ms  the.DEATH.STAR [206.214.251.46]
 30   596 ms   639 ms   629 ms  an.armored.space.station [206.214.251.49]
 31   562 ms  1452 ms   378 ms  with.enough.power.to [206.214.251.54]
 32   405 ms   389 ms   409 ms  destroy.an.entire.planet [206.214.251.57]
 33   426 ms   438 ms   438 ms  Pursued.by.the.Empires [206.214.251.62]
 34   442 ms   441 ms   449 ms  sinister.agents [206.214.251.65]
 35   436 ms   559 ms   439 ms  Princess.Leia.races.home [206.214.251.70]
 36   417 ms   448 ms   441 ms  aboard.her.starship [206.214.251.73]
 37   430 ms   438 ms   438 ms  custodian.of.the.stolen.plans [206.214.251.78]
 38   452 ms   450 ms   449 ms  that.can.save.her [206.214.251.81]
 39   463 ms   430 ms   427 ms  people.and.restore [206.214.251.86]
 40   444 ms   469 ms   439 ms  freedom.to.the.galaxy [206.214.251.89]
 41   535 ms   438 ms   449 ms  0-------------------0 [206.214.251.94]
 42   437 ms   498 ms   439 ms  0------------------0 [206.214.251.97]
 43   426 ms   438 ms   449 ms  0-----------------0 [206.214.251.102]
 44   436 ms   439 ms   478 ms  0----------------0 [206.214.251.105]
 45   444 ms   438 ms   449 ms  0---------------0 [206.214.251.110]
 46  2227 ms   387 ms   388 ms  0--------------0 [206.214.251.113]
 47   392 ms   581 ms   399 ms  0-------------0 [206.214.251.118]
 48   406 ms   399 ms   398 ms  0------------0 [206.214.251.121]
 49   416 ms   448 ms   449 ms  0-----------0 [206.214.251.126]
 50   416 ms   438 ms   438 ms  0----------0 [206.214.251.129]
 51   435 ms   438 ms   438 ms  0---------0 [206.214.251.134]
 52   445 ms   458 ms   449 ms  0--------0 [206.214.251.137]
 53   437 ms   438 ms   435 ms  0-------0 [206.214.251.142]
 54   612 ms   448 ms   448 ms  0------0 [206.214.251.145]
 55   443 ms   480 ms   448 ms  0-----0 [206.214.251.150]
 56   455 ms   469 ms   494 ms  0----0 [206.214.251.153]
 57   402 ms   398 ms   398 ms  0---0 [206.214.251.158]
 58   415 ms   399 ms   388 ms  0--0 [206.214.251.161]
 59   428 ms   448 ms   433 ms  0-0 [206.214.251.166]
 60   458 ms   440 ms   439 ms  00 [206.214.251.169]
 61   436 ms   458 ms   459 ms  I [206.214.251.174]
 62   429 ms   449 ms   449 ms  By.Ryan.Werber [206.214.251.177]
 63   437 ms   458 ms   447 ms  When.CCIEs.Get.Bored [206.214.251.182]
 64   475 ms   456 ms   471 ms  read.more.at.beaglenetworks.net [206.214.251.185]
 65   437 ms   438 ms   438 ms  FIN [216.81.59.173]

Trace complete.
Filed under: Linux,Windows — Tags: , , , , , , — GG @ 6:37 pm

December 27, 2012

Linux command – 1.6 – File size & disk space

df – report file system disk space usage

Show information about the file system on which each FILE resides, or all file systems by default.

Usage:

df [OPTION]... [FILE]...

Options:

-a, --all include dummy file systems
-B, --block-size=SIZE use SIZE-byte blocks
-h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G)
-H, --si likewise, but use powers of 1000 not 1024
-i, --inodes list inode information instead of block usage
-k like --block-size=1K
-l, --local limit listing to local file systems
--no-sync do not invoke sync before getting usage info (default)
-P, --portability use the POSIX output format
 --sync invoke sync before getting usage info
-t, --type=TYPE limit listing to file systems of type TYPE
-T, --print-type print file system type
-x, --exclude-type=TYPE limit listing to file systems not of type TYPE
-v (ignored)
--help display this help and exit
--version output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Example: display available disk space

$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vzfs 30G 21G 9.3G 70% /
none 512M 4.0K 512M 1% /dev
/dev/vzfs 30G 21G 9.3G 70% /var/named/run-root/var/run/dbus

du – estimate file space usage

Summarize disk usage of each FILE, recursively for directories.

Usage:

du [OPTION]... [FILE]...
du [OPTION]... --files0-from=F

Options:

-a, --all
write counts for all files, not just directories

--apparent-size
print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in (`sparse') files, internal fragmentation, indirect blocks, and the like

-B, --block-size=SIZE
use SIZE-byte blocks

-b, --bytes
equivalent to `--apparent-size --block-size=1'
-c, --total
produce a grand total

-D, --dereference-args
dereference FILEs that are symbolic links

--files0-from=F
summarize disk usage of the NUL-terminated file names specified in file F

-H
like --si, but also evokes a warning; will soon change to be equivalent to --dereference-args (-D)

-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)

--si
like -h, but use powers of 1000 not 1024

-k
like --block-size=1K

-l, --count-links
count sizes many times if hard linked

-m
like --block-size=1M

-L, --dereference
dereference all symbolic links

-P, --no-dereference
don't follow any symbolic links (this is the default)

-0, --null
end each output line with 0 byte rather than newline

-S, --separate-dirs
do not include size of subdirectories

-s, --summarize
display only a total for each argument

-x, --one-file-system
skip directories on different file systems

-X FILE, --exclude-from=FILE
Exclude files that match any pattern in FILE.

--exclude=PATTERN
Exclude files that match PATTERN.

--max-depth=N
print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize

--time
show time of the last modification of any file in the directory, or any of its subdirectories

--time=WORD
show time as WORD instead of modification time: atime, access, use, ctime or status

--time-style=STYLE
show times using style STYLE: full-iso, long-iso, iso, +FORMAT FORMAT is interpreted like `date'

--help
display this help and exit

--version
output version information and exit

SIZE may be (or may be an integer optionally followed by) one of following:
kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.

Examples: display size of a directory

$ du -hs /path/to/dir
9.2G	/path/to/dir

stat – display file or file system status

Usage:

stat [OPTION]... FILE...

Options:

-L, --dereference
follow links

-f, --file-system
display file system status instead of file status

-c --format=FORMAT
use the specified FORMAT instead of the default; output a newâ line after each use of FORMAT

--printf=FORMAT
like --format, but interpret backslash escapes, and do not outâ put a mandatory trailing newline. If you want a newline, include n in FORMAT

-t, --terse
print the information in terse form

--help
display this help and exit

--version
output version information and exit

The valid format sequences for files (without --file-system):
%a Access rights in octal
%A Access rights in human readable form
%b Number of blocks allocated (see %B)
%B The size in bytes of each block reported by %b
%C SELinux security context string
%d Device number in decimal
%D Device number in hex
%f Raw mode in hex
%F File type
%G Group name of owner
%h Number of hard links
%i Inode number
%m Mount point
%n File name
%N Quoted file name with dereference if symbolic link
%o I/O block size
%s Total size, in bytes
%t Major device type in hex
%T Minor device type in hex
%u User ID of owner
%U User name of owner
%w Time of file birth, human-readable; - if unknown
%W Time of file birth, seconds since Epoch; 0 if unknown
%x Time of last access, human-readable
%X Time of last access, seconds since Epoch
%y Time of last modification, human-readable
%Y Time of last modification, seconds since Epoch
%z Time of last change, human-readable
%Z Time of last change, seconds since Epoch

Valid format sequences for file systems:
%a Free blocks available to non-superuser
%b Total data blocks in file system
%c Total file nodes in file system
%d Free file nodes in file system
%f Free blocks in file system
%i File System ID in hex
%l Maximum length of filenames
%n File name
%s Block size (for faster transfers)
%S Fundamental block size (for block counts)
%t Type in hex
%T Type in human readable form

Example: display file system status

$ stat -f /
  File: "/"
    ID: 2b6896ea6511332f Namelen: 255     Type: ext2/ext3
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 2090580    Free: 1663166    Available: 1558309
Inodes: Total: 524288     Free: 421462

Alternative way to display file size using ls command

$ ls -lah filename | awk '{ print $5}'
Filed under: Linux — Tags: , , , , — GG @ 8:53 pm

December 19, 2012

Linux command – 1.2 – Searching for files

find – search for files in a directory hierarchy

Usage:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]

Options:

-P
Never follow symbolic links. This is the default behaviour. When find examines or prints information a file, and the file is a symbolic link, the information used shall be taken from the properties of the symbolic link itself.

-L
Follow symbolic links. When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine the file to which the link points). Use of this option implies -noleaf. If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken). Using -L causes the -lname and -ilname predicates always to return false.

-H
Do not follow symbolic links, except while processing the command line arguments. When find examines or prints information about files, the information used shall be taken from the properties of the symbolic link itself. The only exception to this behaviour is when a file specified on the command line is a symbolic link, and the link can be resolved. For that situation, the information used is taken from whatever the link points to (that is, the link is followed). The information about the link itself is used as a fallback if the file pointed to by the symbolic link cannot be examined. If -H is in effect and one of the paths specified on the command line is a symbolic link to a directory, the contents of that directory will be examined (though of course -maxdepth 0 would prevent this).

locate – find files by name

Usage:

locate [OPTION]... PATTERN...

Options:

-b, --basename
Match only the base name against the specified patterns. This is the opposite of --wholename.-c, --count
Instead of writing file names on standard output, write the number of matching entries only.

-d, --database DBPATH
Replace the default database with DBPATH. DBPATH is a :-separated list of database file names. If more than one --database option is specified, the resulting path is a concatenation of the separate paths. An empty database file name is replaced by the default database. A database file name - refers to the standard input. Note that a database can be read from the standard input only once.

-e, --existing
Print only entries that refer to files existing at the time locate is run.

-L, --follow
When checking whether files exist (if the --existing option is specified), follow trailing symbolic links. This causes broken symbolic links to be omitted from the output. This is the default behavior. The opposite can be specified using --nofollow.

-h, --help
Write a summary of the available options to standard output and exit successfully.

-i, --ignore-case
Ignore case distinctions when matching patterns.

-l, --limit, -n LIMIT
Exit successfully after finding LIMIT entries. If the --count option is specified, the resulting count is also limited to LIMIT.

-m, --mmap
Ignored, for compatibility with BSD and GNU locate.

-P, --nofollow, -H
When checking whether files exist (if the --existing option is specified), do not follow trailing symbolic links. This causes broken symbolic links to be reported like other files. This is the opposite of --follow.

-0, --null
Separate the entries on output using the ASCII NUL character instead of writing each entry on a separate line. This option is designed for interoperability with the --null option of GNU
xargs(1).

-S, --statistics
Write statistics about each read database to standard output instead of searching for files and exit successfully.

-q, --quiet
Write no messages about errors encountered while reading and processing databases.

-r, --regexp REGEXP
Search for a basic regexp REGEXP. No PATTERNs are allowed if this option is used, but this option can be specified multiple times.

--regex
Interpret all PATTERNs as extended regexps.

-s, --stdio
Ignored, for compatibility with BSD and GNU locate.

-V, --version
Write information about the version and licence of locate on standard output and exit successfully.

-w, --wholename
Match only the whole path name against the specified patterns. This is the default behavior. The opposite can be specified using --basename.

Filter contents using ls & grep

Example: list all mp3 files in current working directory

$ ls -B |grep ".mp3"
?????.mp3
?????.mp3
?????.mp3
Filed under: Linux — Tags: , , , — GG @ 5:41 pm
Older Posts »

© 2024 GGSHOW | Powered by WordPress