顯示具有 Raspberry 標籤的文章。 顯示所有文章
顯示具有 Raspberry 標籤的文章。 顯示所有文章

2016年2月21日 星期日

show chinese font on raspberry


1.
sudo apt-get -y install ttf-wqy-zenhei

2.

sudo apt-get -y install ttf-wqy-microhei

3.

sudo apt-get -y install fonts-arphic-ukai

4.

sudo apt-get -y install ttf-arphic-uming

2016年2月19日 星期五

在Raspberry 上面使用ADXL345



Step 1: Setting up the Raspberry pi for the ADXL 345

ADXL 345 pin out 圖

 RPI GPIO 圖

The ADXL345 supports both I2C and SPI connections, I used I2C, which requires some configuration on the Pi:
使用I2C 來連線.....

Use the GPIO diagram above to help with the wiring
GND - GND 
3V - 3V3
SDA - SDA
SCL - SCL
check 一下系統有沒有把I2C關掉

Add the I2C modules to the Pi's configuration:
sudo nano /etc/modules
Then add the following lines:
i2c-bcm2708
i2c-dev
Remove I2C from the blacklist:
sudo nano /etc/modprobe.d/fbdev-blacklist.conf
Change this:
blacklist i2c-bcm2708
To this:
#blacklist i2c-bcm2708
Reboot to make the changes:
sudo reboot

Step 2: Installing the Stuff


You will need to install smbus:
sudo apt-get install python-smbus i2c-tools git-core
Now test the ADXL345 is found on the I2C bus by running:
sudo i2cdetect -y 1

看的出來address 0x53的地方有device....

如果沒有看到0x53...代表你線接錯了...或是i2c關掉了

Now download from
不太會使用gitcore.....  所以我從網頁右上角有個 Download Zip..... 按下... 然後用ftp 傳到RPI...


cd adxl345-python-master
Then write:
sudo python example.py
If you get 0.000G for all axis then something probably isn't set up correctly.

看一下  example.py
from adxl345 import ADXL345

adxl345 = ADXL345()

axes = adxl345.getAxes(True)
print "ADXL345 on address 0x%x:" % (adxl345.address)
print "   x = %.3fG" % ( axes['x'] )
print "   y = %.3fG" % ( axes['y'] )
print "   z = %.3fG" % ( axes['z'] )

解釋如下 :



#import the adxl345 module
import adxl345
#create ADXL345 object 
accel = adxl345.ADXL345()
#get axes as g 
axes = accel.getAxes(True) 
# to get axes as ms^2 use  
axes = accel.getAxes(False)
#put the axes into variables 
x = axes['x'] 
y = axes['y'] 
z = axes['z']
#print axes 
print x 
print y 
print z
Change the program for fun!
The default range is 2g which means that the maximum G the ADXL345 can measure is 2.048, but at a high degree of sensitivity.
You can change the sensitivity of the ADXL345 by using the .setRange() method of the class.

這邊還有另外一個sample code

Firstly download their file from:
From there download the User manual(Super Kit for Pi) and the Code and Frizting(Super kit for Pi)

會看到兩個code ...


也是可以參考一下......  

Reference : http://www.instructables.com/id/how-to-use-the-ADXL345-on-Raspberry-pi/

2016年2月16日 星期二

ublox neo-6m gps raspberry pi


明天來實做看看



Reference : https://bigdanzblog.wordpress.com/2015/01/18/connecting-u-blox-neo-6m-gps-to-raspberry-pi/

Raspberry 使用固定ip..... pppoe

首先你要先去中華電信的網頁申請固定IP加浮動IP..... (比較便宜的方案... 但比較不穩定)

http://service.hinet.net/2004/adslstaticip.php

按下申請....不用錢....

然後他就會秀出你的固定ip.....

接下來就是在RPI那端使用pppoe的連線方式來連線....

使用 pppoeconf 套件


直接從官方套件庫安裝 PPPoE 撥接程式。
sudo apt-get -y install pppoeconf


準備設定撥接參數。
sudo pppoeconf

輸入撥接帳號。
XXXXX@ip.hinet.net

輸入撥接密碼
your_password....

如果帳號跟密碼忘了....就是跑一趟中華電信吧....像我今天就是

最後執行結果如下:






http://blog.itist.tw/2015/02/raspberry-pi-pppoe-connection.html

2016年2月11日 星期四

2016年2月8日 星期一

打造你的下坡車測量系統


今天在吃麥當勞的時候....突然在想...如果把整個騎乘期間的避震器位移都記錄下來...

然後回去分析......再根據分析出來的結果去調整你的避震器來達到更符合你的設定....

讓你的整個Downhill 變得更好玩.....


step 1 :  紀錄你的避震器位移.....

             手邊剛好有一個超音波測距離模組.....他的原理是根據聲波反射的原理...

             如果你的待測物的距離比較遠....因為聲波的速度是固定的...所以接收到

             回傳聲波的時間比較久......反之你的待測物的距離比較近.....接收到

             回傳聲波的時間比較短...........有了這個模組.....配上Raspberry pi...就可以量測

             位移了....... 接下來是機構的問題.....不過下坡車的騎乘中...震動非常的激烈....

             所以機構必須要設定的非常穩固...... 這個可能要跟其他車友討論一下...看看要怎麼固定

           

   和車友  ian 討論.....目前初步的機構想法是使用gopro的夾具

 

      這個可以夾在外管上.....然後弄一塊反射板.....

       上橋部分.....可以考慮用下面這組......把 超音波模組固定在延長管的一端....大力夾

       夾在上橋.....

      

    

        但如果超音波模組使用起來問題太大(例如騎乘時的背景音干擾.....)
        
        可以考慮用紅外線模組



step 2  :   加裝  gps  sensor...

            這邊就比較簡單了..... 我過年前剛好買了一塊gps 的module...等貨到了...就可以加上去


step 3 :    加裝   3-axis 加速器

             http://www.seeedstudio.com/wiki/Grove_-_3-Axis_Analog_Accelerometer

             http://www.seeedstudio.com/wiki/Grove_-_3-Axis_Digital_Accelerometer(%C2%B116g)

             要研究一下要買那一種.....  digital 或是類比的

step  4 :    加裝   camera......

              這個也不難....買個  usb 的 camera....接上去 Raspberry pi 就可以使用....

               不過要來研究一下在 python 裡面怎麼啟動錄影.....

               下面的連結有些範例.....可以參考一下

                https://codeplasma.com/2012/12/03/getting-webcam-images-with-python-and-opencv-2-for-real-this-time/

step  5 :   把上述的裝置都弄好....寫好程式....帶到外面....啟動錄製....然後開始騎乘....

                騎完....結束錄製.....回家把檔案抓出來....寫個視窗程式秀出這些資料....


2016年2月6日 星期六

在 Raspberry 上面建立 php 的網頁server



sudo apt-get install apache2 -y

用web browse 去  http://192.168.1.9  (RPI 的ip.... 這不一定是你的...請用ifconfig 去查看)

如果有看到下面的畫面....代表apache php server 已經啟動了


  The default web is in 
/var/www/html/index.html.

cd /var/www/html
ls -al

ADDITIONAL - INSTALL PHP

To allow your Apache server to process PHP files, you'll need to install PHP5 and the PHP5 module for Apache. Type the following command to install these:
sudo apt-get install php5 libapache2-mod-php5 -y

Now remove the index.html file:
sudo rm index.html
and create the file index.php:
sudo nano index.php

<?php echo "hello world"; ?>


or show your PHP info:
<?php phpinfo(); ?>

apache2 server operation

  • stop it: sudo /etc/init.d/apache2 stop
  • start it with: sudo /etc/init.d/apache2 start
  • reload the config with: sudo /etc/init.d/apache2 reload
  • restart (stop followed by start): sudo /etc/init.d/apache2 restart


Reference : https://www.raspberrypi.org/documentation/remote-access/web-server/apache.md

2016年2月4日 星期四

use usb2ttl to control raspberry or arduino



參考文章是用  raspberry 和 arduino  來做實驗


先從 raspberry 傳送到  arduino

在 arduino 

.寫程式如下 : (可以參考  File->Examples->01.Basics->Blink. )

const int ledPin = 13;

void setup(){
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
}

void loop(){
  if (Serial.available())  {
     blink(Serial.read() - '0');  // convert the character '1'-'9' to decimal 1-9
  }
  delay(500);
}

void blink(int numberOfTimes){
  for (int i = 0; i < numberOfTimes; i++)  {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}

最主要就是使用  Serial 這個物件

void Serial.begin (9600)  ->  鲍率 9600
bool Serial.available() -> check serial 有沒有message
int Serial.read()      -> 傳回接收到的數字


在Raspberry

use python

sudo vi main.py

#!/usr/bin/python

import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
ser.write('3')

大概就是RPI 傳送  3 出去....



從 arduino 傳送到  raspberry

在 arduino 

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println("Hello, World!");
  delay(1000);
}

多了一個function    :  Serial.println("Hello, World!");

在 raspberry


import serial
ser = serial.Serial('/dev/ttyACM0', 9600)
while 1 :
    ser.readline()</span>

多了一個function    serial .readline()  

執行的結果如下:

'Hello, World!\r\n'
'Hello, World!\r\n'
'Hello, World!\r\n'
...

不過我目前沒有arduino....所以我會用兩個RPI來互相傳送資料試試...



Reference : http://www.seeedstudio.com/recipe/166-basic-pi-lt-gt-arduino-communication-over-usb.html

2016年2月2日 星期二

python call c code on RPI

先做個筆記....有空再來好好研究


Ref : https://www.google.com.tw/search?q=python%20call%20c%20function

Ref : http://www.amazon.com/UNIX-Network....../dp/0132974290

Ref : http://stackoverflow.com/questions/145270/calling-c-c-from-python

Ref : https://docs.python.org/2/extending/extending.html







python socket programming

本文是依據Ref 1來的...

client site


lesson 1 : create a socket and add error handling

#handling errors in python socket programs
import socket   #for sockets
import sys  #for exit
try:
    #create an AF_INET, STREAM socket (TCP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();
print 'Socket Created'

lesson 2 :   connect to an server, get the ip address

import socket   #for sockets
import sys  #for exit
try:
    #create an AF_INET, STREAM socket (TCP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();
print 'Socket Created'
host = 'www.google.com'
try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
     
print 'Ip address of ' + host + ' is ' + remote_ip


指定了 host name...呼叫 gethostbyname() 來得到 ip address..

lesson 3 :  add connect

import socket   #for sockets
import sys  #for exit
try:
    #create an AF_INET, STREAM socket (TCP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();
print 'Socket Created'
host = 'www.google.com'
port = 80
try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
     
print 'Ip address of ' + host + ' is ' + remote_ip
#Connect to remote server
s.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip

   呼叫 socket.connect() 來連線到server



lesson 4 :  sending data

import socket   #for sockets
import sys  #for exit
try:
    #create an AF_INET, STREAM socket (TCP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();
print 'Socket Created'
host = 'www.google.com'
port = 80
try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
     
print 'Ip address of ' + host + ' is ' + remote_ip
#Connect to remote server
s.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
#Send some data to remote server
message = "GET / HTTP/1.1\r\n\r\n"
try :
    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()
print 'Message send successfully'

呼叫  socket.sendall() 來傳送資料....  "GET / HTTP/1.1\r\n\r\n"
執行內容如下:

因為傳送沒有發生error  (socket.error 沒有被觸發)
所以沒有印出 "Send failed"

lesson 5 :  receiving data

#Socket client example in python
import socket   #for sockets
import sys  #for exit
#create an INET, STREAMing socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()
     
print 'Socket Created'
host = 'www.google.com';
port = 80;
try:
    remote_ip = socket.gethostbyname( host )
except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()
#Connect to remote server
s.connect((remote_ip , port))
print 'Socket Connected to ' + host + ' on ip ' + remote_ip
#Send some data to remote server
message = "GET / HTTP/1.1\r\n\r\n"
try :
    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()
print 'Message send successfully'
#Now receive data
reply = s.recv(4096)
print reply

呼叫 socket.recv() 並且把接收到的資料放在 reply 變數

執行結果如下:



lesson 6 :  close socket

s.close()

在最後加入這行就可以

==================================================

server site


lesson 1 : Bind a socket

import socket
import sys
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT)) # it must has two ()..
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'

一樣是先建立一個socket 物件的變數s..... 初始化的方式是呼叫  socket.socket()

然後呼叫 socket.bind(HOST,PORT)  函式來建立server.....

這邊PORT 設定為8888
HOST        設定為''

執行結果如下:



lesson 2 : Listen for incoming content

其實就是在後面多加兩行
s.listen(10)
print 'Socket now listening'


lesson 3 : accept connection

import socket
import sys
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#wait to accept a connection - blocking call
conn, addr = s.accept()
#display client information
print 'Connected with ' + addr[0] + ':' + str(addr[1])

呼叫  socket.accept()  ... 傳回值為 conn, addr......

  用 putty 登入那台電腦....記得port 要填8888

 就會看到下面的結果.... 我用ip : 192.168.1.4 的NB 來登入



lesson 4:  send and receive data

import socket
import sys
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#wait to accept a connection - blocking call
conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#now keep talking with the client
data = conn.recv(1024)
conn.sendall(data)
conn.close()
s.close()


為了測試這個server code....我用另外一台RPI ....然後把剛剛client 端的程式改寫

vi   client.py


import socket   #for sockets
import sys  #for exit

#create an INET, STREAMing socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print 'Failed to create socket'
    sys.exit()

print 'Socket Created'

host = '192.168.1.2'
port = 8888

try:
    remote_ip = socket.gethostbyname( host )

except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()

#Connect to remote server
s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip

#Send some data to remote server
message = "Hello world"

try :
    #Set the whole string
    s.sendall(message)
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()

print 'Message send successfully'


#Now receive data
reply = s.recv(4096)

print reply

s.close()

======================================================

從 client 端的程式來看....我預先知道server的ip address...(去server 那邊打ifconfig)

然後把 host = '192.168.1.2' .... port 改成 8888

把送出去的資料改成  Hello world

執行結果如下:

server 端的結果如下:




client 端的結果如下:



可以看到client 接收到剛剛傳給server的訊息

===============================================================

Live server


lesson 1 : add while loop
import socket
import sys
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 5000 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    data = conn.recv(1024)
    reply = 'OK...' + data
    if not data:
        break
     
    conn.sendall(reply)
conn.close()
s.close()


其實就是差在多了一個 while 迴圈......一直去執行 accept()

終止迴圈的條件是判斷有人送的空資料過來.....


lesson 2: handle connection

import socket
import sys
from thread import *
HOST = ''   # Symbolic name meaning all available interfaces
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()
     
print 'Socket bind complete'
#Start listening on socket
s.listen(10)
print 'Socket now listening'
#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
     
    #infinite loop so that function do not terminate and thread do not end.
    while True:
         
        #Receiving from client
        data = conn.recv(1024)
        reply = 'OK...' + data
        if not data:
            break
     
        conn.sendall(reply)
     
    #came out of loop
    conn.close()
#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])
     
    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))
s.close()

當一個server 不能只執行單一個thread... 為了要能夠多工.....我們必須使用 multi-thread

執行結果如下:





reference 1 : http://www.binarytides.com/python-socket-programming-tutorial/