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.