2016年8月30日 星期二

2016年8月27日 星期六

[OpenCV] 好站推

 "阿洲的程式教學" 分類不錯:http://monkeycoding.com/?page_id=12
"Notes of openCV-Python" 直接有範例看結果:http://blog.ganyutao.com/python/2016/01/11/Notes_of_python_openCV/

2016年8月26日 星期五

[RPi] 將Pi 3設成Wireless Router

1. 安裝必要軟體
$ sudo apt-get install isc-dhcp-server
$ sudo apt-get install hostapd


2. 備份阿
$ cd ~
$ mkdir bak
$ cp /etc/network/interfaces ~/bak
$ cp /etc/dhcp/dhcpd.conf ~/bak
$ cp /etc/hostapd/hostapd.conf ~/bak


3. 修改網路介面設定
$ sud vim /etc/network/interfaces
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

allow-hotplug wlan0
iface wlan0 inet manual


4. 修改 dhcp server 設定
$ sudo vim /etc/dhcp/dhcpd.conf
ddns-update-style none;
option domain-name "my.raspberry.pi";
option domain-name-servers 8.8.8.8, 8.8.4.4;
default-lease-time 600;
max-lease-time 7200;
authoritative;
log-facility local7;
subnet 192.168.55.0 netmask 255.255.255.0 {
        range 192.168.55.101 192.168.55.200;
        option routers 192.168.55.1;
}


5. 修改 hostapd 設定
$ sudo vim /etc/hostapd/hostapd.conf
# This is the name of the WiFi interface we configured above
interface=wlan0
# Use the nl80211 driver with the brcmfmac driver
driver=nl80211
# This is the name of the network
ssid=Pi3-AP
# Use the 2.4GHz band
hw_mode=g
# Use channel 6
channel=6
# Enable 802.11n
ieee80211n=1
# Enable WMM
wmm_enabled=1
# Enable 40MHz channels with 20ns guard interval
ht_capab=[HT40][SHORT-GI-20][DSSS_CCK-40]
# Accept all MAC addresses
macaddr_acl=0
# Use WPA authentication
auth_algs=1
# Require clients to know the network name
ignore_broadcast_ssid=0
# Use WPA2
wpa=2
# Use a pre-shared key
wpa_key_mgmt=WPA-PSK
# The network passphrase
wpa_passphrase=1234567890
# Use AES, instead of TKIP
rsn_pairwise=CCMP


6. 建立啟動腳本
$ vim ~/ap_mode.sh
#!/bin/bash

ifconfig wlan0 192.168.55.1 netmask 255.255.255.0
iptables -A INPUT -i wlan0 -j ACCEPT
iptables -t nat -A POSTROUTING -s 192.168.55.0/24 -o eth0 -j MASQUERADE
sysctl net.ipv4.ip_forward=1
hostapd /etc/hostapd/hostapd.conf -B
dhcpd wlan0


7. 修改權限 & 設定開機就啟動
$ chmod 755 ~/ap_mode.sh
$ sudo vim /etc/rc.local
新增這行
sudo ~/ap_mode.sh

Reference:
* Using your new Raspberry Pi 3 as a WiFi access point with hostapd

[Tips] 列出連到哪一台 Wireless Router?

困擾已久, 已解決

$ iwconfig
wlan0     IEEE 802.11abgn  ESSID:"OWEN"  
          Mode:Managed  Frequency:2.412 GHz  Access Point: XX:XX:XX:XX:XX:XX   
          Bit Rate=144.4 Mb/s   Tx-Power=15 dBm   
          Retry  long limit:7   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=70/70  Signal level=-17 dBm  
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:10429  Invalid misc:42   Missed beacon:0

2016年8月18日 星期四

[Python] Threading

好文:Python线程同步机制: Locks, RLocks, Semaphores, Conditions, Events和Queues

[Python] Tkinter Threading

這篇寫得很好
Tkinter: How to use threads to preventing main event loop from “freezing”


要用 python3 執行
 
import tkinter as tk
from tkinter import ttk
import threading
import queue
import time
                                                                                                                                                                        
class GUI:
    def __init__(self, master):
        self.master = master
        self.test_button = tk.Button(self.master, command=self.tb_click)
        self.test_button.configure(
            text="Start", background="Grey",
            padx=50
            )
        self.test_button.pack(side=tk.TOP)

    def progress(self):
        self.prog_bar = ttk.Progressbar(
            self.master, orient="horizontal",
            length=200, mode="indeterminate"
            )   
        self.prog_bar.pack(side=tk.TOP)


    def tb_click(self):
        self.progress()
        self.prog_bar.start()
        self.queue = queue.Queue()
        ThreadedTask(self.queue).start()
        self.master.after(100, self.process_queue)

    def process_queue(self):
        try:
            msg = self.queue.get(0)
            # Show result of the task if needed
            self.prog_bar.stop()
        except queue.Empty:
            self.master.after(100, self.process_queue)

class ThreadedTask(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
    def run(self):
        time.sleep(5)  # Simulate long running process
        self.queue.put("Task finished")

root = tk.Tk()
root.title("Test Button")
main_ui = GUI(root)
root.mainloop()                             

2016年8月17日 星期三

[Python] TKinter callback

# reference:
# http://stupidpythonideas.blogspot.tw/2013/10/why-your-gui-app-freezes.html?view=snapshot
# http://stackoverflow.com/questions/7498658/importerror-when-importing-tkinter-in-python
# nonlocal => python3
# required => sudo apt-get install python3-tk


try:
    # for Python2
    from Tkinter import *
except ImportError:
    # for Python3
    from tkinter import *

import time


def handle_click():
    win = Toplevel(root)
    win.transient()
    Label(win, text='Please wait...', font=("Helvetica", 48)).pack()

    i = 5 
    def callback():
        nonlocal i, win 
        print(i)
        i -= 1
        if not i:
            win.destroy()
        else:
            root.after(1000, callback)
    root.after(1000, callback)

root = Tk()
Button(root, text='Click me', font=("Helvetica", 48), command=handle_click).pack()
root.mainloop()
要用 python3 才能執行, 因為 nonlocal 關鍵字不適用 python2

[Tips] 時間單位換算

這重要性大概僅次於加減成除了吧

s (second,秒)
ms(millisecond,10^-3,毫秒,千分之一 秒)
μs(microsecond,10^-6,微秒,百萬分之一秒)
ns(nanosecond,10^-9,奈秒,十億分之一秒)


2016年8月15日 星期一

[科技新知] DuoSkin

剛剛在數位時代看到 DuoSkin 這個專案,介紹者是 Cindy Hsin-Liu Kao。做的東西挺有意思的,有 High-Low Tech 的感覺,令人反思 "人真的需要這麼多螢幕嗎?"

做的很好,加油!!

2016年8月9日 星期二

[Tips] Shutter

如何在 Ubuntu Linux 上快速修改圖片, 例如增加箭頭, 文字框或是縮放

可以用 shutter

安裝方式:
sudo add-apt-repository ppa:shutter/ppa
sudo apt-get update
sudo apt-get install shutter

如果可以 edit 就是安裝好了


參考:http://shutter-project.org/faq-help/ppa-installation-guide/

[OpenCV] Color and Shape Detection

趕快惡補一下:

Ball Tracking with OpenCV
http://www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/

OpenCV shape detection
http://www.pyimagesearch.com/2016/02/08/opencv-shape-detection/

Basic motion detection and tracking with Python and OpenCV
http://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/

Determining object color with OpenCV
http://www.pyimagesearch.com/2016/02/15/determining-object-color-with-opencv/

OpenCV Track Object Movement
http://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/

OpenCV and Python Color Detection
http://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/

Detecting Circles in Images using OpenCV and Hough Circles
http://www.pyimagesearch.com/2014/07/21/detecting-circles-images-using-opencv-hough-circles/

Home surveillance and motion detection with the Raspberry Pi, Python, OpenCV, and Dropbox
http://www.pyimagesearch.com/2015/06/01/home-surveillance-and-motion-detection-with-the-raspberry-pi-python-and-opencv/

Basic motion detection and tracking with Python and OpenCV
http://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/

2016年8月3日 星期三

[RPi] SD卡兩三件事

一直有人問在 Raspberry Pi 要選哪一種 SD 卡?

不要問了,就是 SanDisk,沒有之一。看看下面的測試結果,哪個品牌最多就用它吧。
http://elinux.org/RPi_SD_cards

同場加贈 benchmark。
https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=4076&start=250

[Security] USB Sniffer

要注意幾個地方:
1. $ sudo modprobe usbmon
2. filter 要從 USB 開始下手

參考:
http://twlinuxnotes.blogspot.tw/2013/01/wireshark-usb.html
https://wiki.wireshark.org/CaptureSetup/USB

2016年8月1日 星期一

[RPi] Raspberry Pi UART使用速記

Pi 3 內建 WiFi 和 Bluetooth, 完全是一片美意, 可是在使用上要注意幾個地方

1. 如果要能從序列埠登入到 Pi, 需要從 raspi-config 將 serial 選成 yes
並且在 /boot/config.txt 加入 dtoverlay=pi3-disable-bt 停用內建的 Bluetooth

2. 那如果停用了不就浪費錢了嗎?
可以在 /boot/config.txt 加入 dtoverlay=pi3-miniuart-bt 將 Pi 3 Bluetooth 移到 mini-UART (ttyS0)
另外還要加上 core_freq=250

3. 如果想用序列埠(UART)和其他硬體通訊, 例如 GSM, HC-05, LoRa, GPS 等怎麼辦?
要先從 raspi-config 將 serial 選成 no
再從 /boot/config.txt 加入 enable_uart=1, 就可以透過 /dev/ttyAMA0 通訊

以上環境是 Pi 3 + 2016-05-29.img

更多overlay 的說明可以參考:
https://github.com/raspberrypi/linux/tree/rpi-4.1.y/arch/arm/boot/dts/overlays

參考資料:
http://raspberrypi.stackexchange.com/questions/45570/how-do-i-make-serial-work-on-the-raspberry-pi3