2016年3月29日 星期二

Raspberry Pi: Turn It Into An Beacon Node

since: 2016/03/29
update: 2016/04/01

reference:
1. Raspberry Pi應用 - Bluetooth 4.0 使用 iBeacon 
2. Overview | piBeacon - DIY Beacon with a Raspberry Pi 
3. Raspberry Pi 3 as an Eddystone URL beacon 


A. Install Required Libraries
    $ sudo apt-get install libusb-dev libdbus-1-dev libglib2.0-dev libudev-dev libical-dev libreadline-dev

-----------------------------------------------------------------------------------------------

B. Download Bluez

    $ sudo mkdir bluez
    $ cd bluez
    $ sudo wget www.kernel.org/pub/linux/bluetooth/bluez-5.38.tar.xz

-----------------------------------------------------------------------------------------------

C. Unzip and Compile Bluez

    $ sudo unxz bluez-5.38.tar.xz
    $ sudo tar xvf bluez-5.38.tar
    $ cd bluez-5.38
    $ sudo ./configure --disable-systemd
   
    $ sudo make
    $ sudo make install
-----------------------------------------------------------------------------------------------

D. Check for your USB Module

    $ cd /home/pi/bluez/bluez-5.38
    $ tools/hciconfig

    hci0:    Type: BR/EDR  Bus: USB
    BD Address: 00:1A:7D:DA:71:13  ACL MTU: 310:10  SCO MTU: 64:8
    UP RUNNING
    RX bytes:652 acl:0 sco:0 events:43 errors:0
    TX bytes:1533 acl:0 sco:0 commands:43 errors:0


-----------------------------------------------------------------------------------------------

E. Enable the USB Device
    $ sudo tools/hciconfig hci0 up
    $ sudo tools/hciconfig hci0 leadv 3

      LE set advertise enable on hci0 returned status 12

    $ sudo tools/hciconfig hci0 noscan

-----------------------------------------------------------------------------------------------

F. Enter the Beacon Advertising Data
   $ sudo tools/hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00

< HCI Command: ogf 0x08, ocf 0x0008, plen 32
  1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F
  17 D1 AD 07 A9 61 00 00 00 00 C8 00
> HCI Event: 0x0e plen 4
  01 08 20 00


-----------------------------------------------------------------------------------------------

G. 測試軟體
     1. Mac: Beacon Scan

     2. iOS: Locate Beacon

     3. Android: Locate Beacon

-----------------------------------------------------------------------------------------------

H. 簡易開機自動啟動

     $ sudo nano /home/pi/iBeacon.sh
#!/bin/bash
sleep 5
cd /home/pi/bluez/bluez-5.38/
tools/hciconfig hci0 leadv 3
tools/hciconfig hci0 noscan
tools/hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00 00 00 00 C8 00


     $ sudo crontab -e
        ....
        @reboot /home/pi/iBeacon.sh

-----------------------------------------------------------------------------------------------
I. 測試結果:(iOS)








2016年3月3日 星期四

Raspberry Pi: Python Remote Development With PyCharm

since: 2016/03/03
update: 2017/01/21

reference:
1. Feature Spotlight: Python remote development with PyCharm | PyCharm Blog
2. https://www.mercurial-scm.org/wiki/Download

A. 在 Pi 上建立專案資料夾:
    $ cd
    $ mkdir project
    $ cd project
    $ mkdir routine_task

-----------------------------------------------------------------------------------------------

(選擇性)
B. 在  Mac 上安裝 Mercurial 版本控制
     

1. install brew: http://brew.sh
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

     

2. install mercurial
$ brew install mercurial


3. check PyCharm, add hg path: (later)

   
/usr/local/bin/hg


4. add configuration file
    $ cd
    $ vi .hgrc
[ui]
username = Lanli <yourID@gmail.com>


[tortoisehg]
ui.language = zh_TW


[auth]
bitbucket.org.prefix = bitbucket.org
bitbucket.org.username = lanli
bitbucket.org.password =
yourPass 

5. check again
    $ cat .hgrc

 
-----------------------------------------------------------------------------------------------

C. 在 Mac 上用 PyCharm 建立新的專案:

    1. Create New Project:

    2. Setting:
        Location: 本地專案位置
        Interpreter: 直譯器, 選取 remote 的 ssh 連線方式
         > Create

-----------------------------------------------------------------------------------------------

D. 設定將專案內容部署至遠端
     1. PyCharm > Preferences... > Build, Execution, Deployment > Deployment:

         Name: Rasberry Pi2
         Type: SFTP
          > OK


     2. 設定 Connection 頁籤內容:
         Type: SFTP
         SFTP host: 192.168.1.6
         Port: 22
         Root path: /       (建議設為 / )
         User name: pi
         Password: **

     3. 設定 Mapping 頁籤內容:
         Local path: 本地專案位置
         Development path on server 'Raspberry Pi2': 遠端專案目錄
         (備註: 會以
Connection 頁籤裡的 Root path 作為相對位置 )
         > OK


-----------------------------------------------------------------------------------------------

E. 測試:
     1. 將檔案部署至遠端

        a. 新增 python 檔案: HelloPython.py
      #!/usr/bin/python3      
      print('Hello Python')

    b. Tools > Deployment > Upload to Raspberry Pi2

    C. 檢查 pi 上的專案目錄: 

     2. 執行遠端程式:
         a. Run > Run 'HelloPython'


         b. 結果:

-----------------------------------------------------------------------------------------------

F. Create Project By Check out Version Control

    1. 確定已完成:
        B. 在  Mac 上安裝 Mercurial 版本控制


    2. Check out from Version Control

    3. Clone Repository

    4. Checkout From Version Control

2016年3月2日 星期三

Raspberry Pi: Connected Arduino Using I2C

since: 2016/02/25
update: 2016/03/03

reference:
1.Raspberry Pi and Arduino Connected Using I2C - OscarLiang.net
2. [I2C] onRequest and onReceive as slave with Raspberry Pi Master

A. 檢查 Raspberry Pi:
   
1. Linux 版本

    $ uname -a
Linux raspberrypi 4.1.17-v7+ #843 SMP Mon Feb 15 23:35:33 GMT 2016 armv7l GNU/Linux

    2. python 版本
    $ python -V
Python 2.7.9

-----------------------------------------------------------------------------------------------

B. 安裝 I2C 與 python相關套件:
    $ sudo apt-get install python-smbus
    $ sudo apt-get install python3-smbus // for python3
    $ sudo apt-get install i2c-tools

    // check
    $ sudo aptitude search smbus
p   pypy-smbus-cffi        - This Python module allows SMBus access through the I2C /dev              
i   python-smbus            - Python bindings for Linux SMBus access through i2c-dev                   
v   python2.7-smbus      -                                                                          
i   python3-smbus          - Python 3 bindings for Linux SMBus access through i2c-dev


-----------------------------------------------------------------------------------------------

C. 安裝 I2C 的 Kernel Support:
     $ sudo raspi-config
        > 9 Advanced Options

        > A7 I2C

        > Yes
       ....
        > Yes

-----------------------------------------------------------------------------------------------

D. 檢查設定(選擇性)
     $ cat /etc/modules
....
i2c-dev
    
     $ cat /etc/modprobe.d/raspi-blacklist.conf
# ...
# ...


    $ cat /boot/config.txt
....
dtparam=i2c_arm=on

    $ cat /etc/group | grep i2c
i2c:x:998:pi
      // $ sudo adduser pi i2c

-----------------------------------------------------------------------------------------------

E. I2C 測試:
    $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- -- 


    $ ls /dev/i2c*
/dev/i2c-1
-----------------------------------------------------------------------------------------------

F. Arduino Code:
#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int number = 0;
int state = 0;

void setup() {
    pinMode(13, OUTPUT);
    Serial.begin(9600); // start serial for output
    // initialize i2c as slave
    Wire.begin(SLAVE_ADDRESS);

    // define callbacks for i2c communication
    Wire.onReceive(receiveData);
    Wire.onRequest(sendData);
    //Serial.println(“Ready!”);
}

void loop() {
    delay(100);
}

// callback for received data
void receiveData(int byteCount){

    while(Wire.available()) {
        number = Wire.read();
        //Serial.print(“data received: “);
        //Serial.println(number);


        if (number == 1){

            if (state == 0){
                digitalWrite(13, HIGH); // set the LED on
                state = 1;
            }
            else{
                digitalWrite(13, LOW); // set the LED off
                state = 0;
            }
        }
    }
}

// callback for sending data
void sendData(){
    Wire.write(number);
}


-----------------------------------------------------------------------------------------------

G. Raspberry Pi's  Python Code:
#!/usr/bin/python
#encoding: utf-8

import smbus
import time

# for RPI version 1, use "bus = smbus.SMBus(0)"
bus = smbus.SMBus(1)

# This is the address we setup in the Arduino Program
address = 0x04

def writeNumber(value):
    bus.write_byte(address, value)
    # bus.write_byte_data(address, 0, value)
    return -1

def readNumber():
    number = bus.read_byte(address)
    # number = bus.read_byte_data(address, 1)
    return number

while True:
    var = input("Enter 1 – 9:")
    if not var:
        continue

    writeNumber(var)
    print "RPI: Hi Arduino, I sent you ", var
    # sleep one second
    time.sleep(1)

    number = readNumber()
    print "Arduino: Hey RPI, I received a digit ", number
    print


-----------------------------------------------------------------------------------------------

H. 備註:
When we use read_i2c_block_data(), It was too fast to write a command byte and read the data.

In that time, if we use Serial.println() to print log to serial. Arduino will can not follow the speed. and it missing the i2c read flag.

So, the dataReceive() funtion will not be run at all.

The solution is simple: just do not use Serial library until the i2c is free.

2016年2月19日 星期五

Raspberry Pi: Setting Up The Watchdog

since: 2016/02/19
update: 2016/02/19

reference:
1. 樹莓派硬體看門狗(Watchdog):當機時自動重新開機 - G. T. Wang
2. ssmtp to send emails – Raspberry Pi Projects


A. Sending Email From Your System with sSMTP
    1. 安裝套件:
        $ sudo apt-get install ssmtp
        $ sudo apt-get install mailutils

    2. 編輯設定檔:
        $ sudo nano /etc/ssmtp/ssmtp.conf
#
# Config file for sSMTP sendmail
#
# The person who gets all mail for userids < 1000
# Make this empty to disable rewriting.
#root=postmaster
root=lanlixxxx@gmail.com

# The place where the mail goes. The actual machine name is required no
# MX records are consulted. Commonly mailhosts are named mail.domain.com
#mailhub=mail
mailhub=smtp.gmail.com:587

# Where will the mail seem to come from?
#rewriteDomain=

# The full hostname
hostname=raspberrypi

# Auth
AuthUser=pythonicxxxx@gmail.com
AuthPass=pythonxxxx


# Are users allowed to set their own From: address?
# YES - Allow the user to specify their own From: address
# NO - Use the system generated From: address
FromLineOverride=YES
UseSTARTTLS=YES


    3. 降低寄發郵件的安全性設定: (AuthUser)
        > GMail 低安全性應用程式 - 帳戶設定

    4. 寄發測試郵件:
        $ echo "Hello world email body" | mail -s "Test Subject" lanlixxxx@gmail.com

    5. 寄發 "附加檔案" 測試郵件:
        $ sudo apt-get install mpack
        $ mpack -s "Test" somefile.txt lanlixxxx@gmail.com

-----------------------------------------------------------------------------------------------

B. 安裝 watchdog

    1. 啟用 bcm2708_wdog 核心模組
        $ sudo nano /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

i2c-dev

bcm2708_wdog

    2. 立即啟用:
        $ sudo modprobe bcm2708_wdog

    3. 安裝 watchdog 監控 Daemon
        $ sudo apt-get install watchdog

-----------------------------------------------------------------------------------------------

C. 編輯 watchdog 設定檔
    $ sudo nano /etc/watchdog.conf
#ping                   = 172.31.14.1
#ping                   = 172.26.1.255
#interface              = eth0
#interface              = wlan0
# 監控檔案是否可以正常存取

#file                   = /var/log/messages
#change                 = 1407

# Uncomment to enable test. Setting one of these values to '0' disables it.
# These values will hopefully never reboot your machine during normal use
# (if your machine is really hung, the loadavg will go much higher than 25)
max-load-1              = 24
max-load-5              = 18
max-load-15             = 12


# Note that this is the number of pages!
# To get the real size, check how large the pagesize is on your machine.
# 查看自己系統的 page size
# getconf PAGESIZE (4096)

#
#min-memory             = 1 (一張 page 的大小就是 4096 bytes)
#allocatable-memory     = 1

#repair-binary          = /usr/sbin/repair
#repair-timeout         =
#test-binary            =
#test-timeout           =

watchdog-device = /dev/watchdog

# Defaults compiled into the binary

// 檢查 CPU 溫度  (單位: 攝氏千分之一度)
// cat /sys/class/thermal/thermal_zone0/temp     (35780)
#temperature-device     =  /sys/class/thermal/thermal_zone0/temp
#max-temperature        = 120

#max-temperature        = 80000
# Defaults compiled into the binary
#admin                  = root
admin                   = lanlixxxx@gmail.com
#interval               = 1
#logtick                = 1
#log-dir                = /var/log/watchdog

# This greatly decreases the chance that watchdog won't be scheduled before
# your machine is really loaded
realtime                = yes
priority                = 1

# Check if rsyslogd is still running by enabling the following line
#pidfile                = /var/run/rsyslogd.pid

-----------------------------------------------------------------------------------------------

D. 調整: (for 新版 Pi OS: 2016-02-09-raspbian-jessie; still may not work)
     $ sudo nano /lib/systemd/system/watchdog.service
[Unit]
Description=watchdog daemon
Conflicts=wd_keepalive.service
After=multi-user.target
OnFailure=wd_keepalive.service

[Service]
Type=forking
EnvironmentFile=/etc/default/watchdog
ExecStartPre=/bin/sh -c '[ -z "${watchdog_module}" ] || [ "${watchdog_module}" = "none" ] || /sbin/modprobe $watchdog_module
ExecStart=/bin/sh -c '[ $run_watchdog != 1 ] || exec /usr/sbin/watchdog $watchdog_options'
ExecStopPost=/bin/sh -c '[ $run_wd_keepalive != 1 ] || false'

[Install]

WantedBy=multi-user.target

$ systemctl enable watchdog

-----------------------------------------------------------------------------------------------

E. 啟動看門狗
    1. 立即啟動 watchdog 常駐程式
        $ sudo service watchdog start

    2. 開機時自動啟動 watchdog
        $ sudo update-rc.d watchdog defaults

    3. 檢查
        $ ps aux | grep watchdog
root       786  0.0  0.3   1888  1760 ?        SLs  14:34   0:00 /usr/sbin/watchdog
pi         822  0.0  0.3   4276  1956 pts/0    S+   14:36   0:00 grep --color=auto watchdog


    4. 測試       
        kill 掉 watchdog 本身, 會重新開機
        $ sudo kill -9 786       

2016年1月22日 星期五

Raspberry Pi: Windows 10 IoT Memo

since: 2016/01/22
update: 2016/01/22

reference:
1. Windows IoT - Downloads
2. Windows IoT - Setup your Raspberry Pi 2
3. Windows IoT - Use SSH to connect to a Windows IoT Core device


A. 說明:
     1. 需要 Windows 10 才可以安裝

     2. 已在 Windows 上安裝過以下軟體:
         Windows 10 IoT Core
         Windows 10 IoT Core Dashboard

-----------------------------------------------------------------------------------------------

B. 可使用 ssh 登入
    > Windows 上安裝 putty

-----------------------------------------------------------------------------------------------

C. ID / pass:
    administrator / xxxx

Raspberry Pi: Setup For LED Pixel Art

since: 2016/01/22
update: 2016/01/22

reference:
1. Raspberry Pi and IOIO Board | PIXEL: LED ART
2. PIXEL Console App | PIXEL: LED ART
3. Overview | Web Enabled PIXEL on Raspberry Pi | Adafruit Learning System
4. Raspberry Pi and PIXEL | PIXEL: LED ART
5. RPiBlog: Installing Oracle JDK 8 on Raspberry Pi


A. Download the udev rules file.

    1. 到 Downloads · ytai/ioio Wiki , 下載 Linux 版的檔案: 50-ioio.rules (另存新檔)
       其內容為:
ACTION=="add", SUBSYSTEM=="tty", SUBSYSTEMS=="usb", ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", SYMLINK+="IOIO%n", MODE="666"
ATTRS{idVendor}=="1b4f", ATTRS{idProduct}=="0008", ENV{ID_MM_DEVICE_IGNORE}="1"


    2.  將 50-ioio.rules 上傳到 pi 裡:
         $ scp 50-ioio.rules pi@192.168.1.24:/home/pi

    3. copy it to your rules directory (one time step)
       $ sudo cp 50-ioio.rules /etc/udev/rules.d

    4. restart udev
       $ sudo restart udev
       (or $ sudo /etc/init.d/udev restart)

-----------------------------------------------------------------------------------------------

B. Check IOIO recognized status
    1. Plug your IOIO into a free USB port on the Raspberry Pi

    2. check if it’s recognized using this command
        $ ls /dev/IOIO*
           /dev/IOIO0

-----------------------------------------------------------------------------------------------

C. Install Java on your Pi
    1. for pi 1:
        $ sudo apt-get install openjdk-7-jre

    2. for pi 2:
       $ sudo apt-get install openjdk-8-jre

    3. 備註:
        切換使用不同版本的 java 方式: (如果有安裝不同的版本)
        $ sudo update-alternatives --config java

-----------------------------------------------------------------------------------------------

D. 安裝 avahi-daemon
    $ sudo apt-get install avahi-daemon

-----------------------------------------------------------------------------------------------

E. Check PIXEL recognized status
    1.Move the toggle switch on the side of PIXEL from the “Bluetooth” position to the “PC” position

    2. Plug PIXEL into a free USB port on your Raspberry Pi using the included USB A-A cable.

    3. Then check if it’s recognized using this command:
        $ ls /dev/ttyACM0*
           /dev/ttyACM0

-----------------------------------------------------------------------------------------------

F. 下載 PIXEL Console App
        http://ledpixelart.com/console/
    1. 在此使用 PIXEL: Console V2.0 版本
    2. 上傳到 pi 裡:
        $ scp pixelc_2_0.jar pi@192.168.1.24:/home/pi

-----------------------------------------------------------------------------------------------

G. 測試:
     先將
pixelc_2_0.jar  更名成: pixelc.jar
     1. $ java -jar -Dioio.SerialPorts=/dev/IOIO0 pixelc.jar --gif=black.gif
     2. $ java -jar -Dioio.SerialPorts=/dev/IOIO0 pixelc.jar --gif=black.gif --write
     3. $ java -jar -Dioio.SerialPorts=/dev/IOIO0 pixelc.jar -–zip=95050
     4. $ java -jar -Dioio.SerialPorts=/dev/ttyACM0 pixelc.jar --gif=black.gif
     5. $ java -jar pixelc.jar --gif=fire.gif
     6. $ java -jar pixelc.jar -–zip=95050 

-----------------------------------------------------------------------------------------------

H. 其他:
     套件更新:
     $ sudo apt-get update
     $ sudo apt-get upgrade
     $ sudo apt-get dist-upgrade
     $ sudo rpi-update

     others:
     $ sudo apt-cache search openjdk
     $ sudo apt-get install openjdk-7-jdk
     $ sudo update-alternatives --config javac

2016年1月16日 星期六

Processing: An error occurred during startup

since: 2016/01/16
update: 2016/01/16


A. 錯誤:
     Processing 2.21 無法開啟

-----------------------------------------------------------------------------------------------

B. 解決方式:
    1. 可先開啟 Processing 3, 查看最下方偏好設定檔的位置.
        或看 C. 備註3.

    2. 刪除整個 "偏好設定檔目錄", 在此例為: /Users/Lanli/Library/Processing

    3. 重新開啟 Processing 2

-----------------------------------------------------------------------------------------------
  
C. 備註:
     1. WindowsMac 皆適用

     2. "偏好設定檔目錄" 預設為隱藏
          Windows: 目錄設定顯示所有隱藏檔
          Mac: Finder > 前往 > 前往檔案夾 ...

     3. Windows 與 Mac 預設的 "偏好設定檔目錄" 位置:
         Windows: C:\Users\Lanli\AppData\Roaming\Processing
         Mac: /Users/Lanli/Library/Processing