12月 28

raspberry pi2 连接蓝牙键盘

B.O.W航世 无线蓝牙键盘 + raspberry pi 2 + 迷你USB蓝牙4.0适配器 无线接收器 CSR稳定芯片
CSR4.0
安装蓝牙程序
apt-get install -y bluetooth bluez-* blueman

使用命令lsusb,看看有没有识别usb蓝牙模块。我的信息如下:
Bus 001 Device 004: ID 0a12:0001 Cambridge Silicon Radio, Ltd Bluetooth Dongle (HCI mode)

重启操作系统或用命令开启蓝牙服务。
#/etc/init.d/bluetooth start

开启键盘蓝牙扫描,使用“hcitool scan”命令搜索到键盘。我的信息如下:
87:73:00:00:65:62 Bluetooth 3.0 Keyboard

开始在界面测试多次,都是4位配对码,总是失败。
在管理员模式下使用buletoothctl命令行进行连接操作。
# bluetoothctl
进入命令行控制台
[bluetooth]# power on // Power on the Bluetooth device
[bluetooth]# agent on // Start the agent
[bluetooth]# default-agent // Set agent as the default one
[bluetooth]# pairable on // Allow pairing
[bluetooth]# scan on // Enable scan so discoverable devices will be displayed
[bluetooth]# pair 87:73:00:00:65:62 // Pair with a discovered device
[bluetooth]# connect 87:73:00:00:65:62

根据提示,输入6位配对码。连接成功,键盘可用。
重启自动加载,编辑/etc/rc.local增加下面命令即可。
bluetoothctl connect 87:73:00:00:65:62

raspberrypi-bluekey

参考:
https://wiki.tizen.org/wiki/Bluetooth

1月 27

Raspberry pi 软件模拟PWM

  Raspberry pi 只有一个PWM。看到国外高手有人用软件模拟PWM。我也测试一下。以下部分只是测试记录,文档和代码均来自官方网站。

使用root用户安装softpwm,这个安装记录直接拷贝到系统里运行成功就行。
apt-get -y update
apt-get -y upgrade
apt-get install -y devscripts build-essential fakeroot dh-make mercurial dh-make
apt-get install -y devscripts build-essential fakeroot
mkdir temp
cd temp
hg clone http://hg.code.sf.net/p/raspberry-gpio-python/code raspberry-gpio-python -r 0.5.4
cd raspberry-gpio-python
python setup.py sdist
cd dist/
tar xvfz RPi.GPIO-0.5.4.tar.gz
mv RPi.GPIO-0.5.4 rpi.gpio-0.5.4
cd rpi.gpio-0.5.4
dh_make -s -f ../RPi.GPIO-0.5.4.tar.gz
rm -rf debian
cp -a ../../debian/ ./
debuild -us -uc
debuild clean
python setup.py install

测试代码如下,使用LED小灯测试:

#!/bin/env python
# -*-coding:utf-8 -*-
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(7, GPIO.OUT)

p = GPIO.PWM(7, 50)  #frequency=100Hz
p.start(0)
try:
    while 1:
        for dc in range(0, 101, 5):
            p.ChangeDutyCycle(dc)
            time.sleep(0.1)
        for dc in range(100, -1, -5):
            p.ChangeDutyCycle(dc)
            time.sleep(0.1)
except KeyboardInterrupt:
    pass
p.stop()
GPIO.cleanup()

来源
http://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/

11月 19

pcduino对比测试nginx和haproxy的负载效果

  在pcduino上对比haproxy和nginx的负载效果

  安装haproxy
  下载地址
http://www.haproxy.org/download/1.4/src/haproxy-1.4.24.tar.gz

  安装如下
make TARGET=linux26 ARCH=arm PREFIX=/program/haproxy
make install PREFIX=/program/haproxy

配置如下:

global
        log 192.168.1.132   local0
        #log 127.0.0.1  local1 notice
        #log loghost    local0 info
        maxconn 4096
        chroot /program/haproxy
        uid 0                          #所属运行的用户uid
        gid 0                         #所属运行的用户组
        daemon
        nbproc 1
        pidfile /program/haproxy/run/haproxy.pid
        #debug
        #quiet

defaults
        log     global
        log     192.168.1.132       local3        #日志文件的输出定向
        mode    http                            #所处理的类别
        option  httplog                        #日志类别
        option  httpclose
        option  dontlognull
        option  forwardfor
        option  redispatch
        retries 2                      #设置多个haproxy并发进程提高性能
        maxconn 2000
        balance roundrobin                     #负载均衡算法
        stats   uri     /haproxy-stats        #haproxy 监控页面的访问地址
        # 可通过 http://localhost:1080/haproxy-stats 访问
        contimeout      5000
        clitimeout      50000
        srvtimeout      50000

listen  localhost 0.0.0.0:1080                   #运行的端口及主机名
        mode    http
        option  httpchk GET /           #健康检测地址
        server  s1 192.168.1.132:9900 weight 3 check
        server  s2 192.168.1.80:8880 weight 3 check

简单操作如下:
启动服务:
# /program/haproxy/sbin/haproxy -f /program/haproxy/haproxy.cfg

重启服务:
# /program/haproxy/sbin/haproxy -f /program/haproxy/haproxy.cfg -st `cat /program/haproxy/logs/haproxy.pid` (没有换行)

停止服务:
# killall haproxy

#./haproxy -h 说明
-v 屏蔽版本
-vv 编译选项
-V 版本
-d 前台,debug模式
-db 屏蔽后台模式
-D daemon模式启动
-q 安静模式,不输出信息
-c 对配置文件进行语法检查
-s 显示统计数据
-l 显示详细统计数据
-ds 不使用speculative epoll
-de 不使用epoll
-dp 不使用poll
-sf 程序启动后向pidlist里的进程发送FINISH信号,这个参数放在命令行的最后
-st 程序启动后向pidlist里的进程发送TERMINATE信号,这个参数放在命令行的最后

  安装nginx
  下载地址
http://nginx.org/download/nginx-1.5.6.tar.gz
  首先安装pcre。
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
然后是configure、make、make install。
最后做个软连接。
ln -s /usr/local/lib/libpcre.so.1 /usr/lib/

  开始安装nginx编译参数如下
./configure –prefix=/program/nginx –user=root –group=root –with-pcre –with-select_module –with-poll_module –with-http_stub_status_module –with-http_ssl_module –with-http_realip_module –with-http_gzip_static_module
配置如下

user  root root;
worker_processes 1;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  5120;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" "$request_time" '
                      '"$upstream_cache_status"';
    access_log  logs/access.log  main ;
    sendfile        on;
    keepalive_timeout  120;
    tcp_nodelay on;
    tcp_nopush on;
    client_header_buffer_size 128k;
    large_client_header_buffers 4 64k;
    reset_timedout_connection on;
    proxy_ignore_client_abort on;
   ######################################
   upstream  deploy_tool {
            server 192.168.1.132:9900;
       }
   server {
        listen       80;
        server_name 192.168.1.132;
        location / {
        proxy_pass_header      Server;
        proxy_redirect          off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        Host $http_host;
        client_max_body_size   150m;
        proxy_connect_timeout  2000;
        proxy_send_timeout     2000;
        proxy_read_timeout     300;
        access_log off;
        proxy_pass              http://deploy_tool;
        }
        location /status {
             stub_status on;
             access_log  off;
             auth_basic_user_file    /lnmp/nginx/conf/htpasswd;
        }
     }
}

  WEB项目使用我的部署工具,只是连接到首页。python使用2.7。压力工具就是写了用urllib2多线程获取页面的代码,并发150个。测试结果如下:
haproxy:
haproxystatus
ubuntu
nginx:
nginxstatus
ubuntu-nginx

结论:
  如果只是简单反向连接,两者效率差不多。haproxy后端检测效果不错,对长连接的处理也很好,可做mysql等其他服务器的负载工作。nginx有更强大的模块,如缓存、uwsgi等功能一起用,nginx就方便了很多。以后WEB的服务使用nginx,其它需要负载的使用haproxy。

8月 29

pcduino使用心得对比raspberry pi

之前一直在用raspberry pi,为了试验方便今天又买了一个pcduino。两者做个对比。

优点:
1)pcduino使用TF卡,而raspberry pi使用SD卡。如果想raspberry pi美观点使用TF卡,还需要买相应的卡托。
2)pcduino的运行速度要比raspberry pi快。编译同样的go的版本raspberry pi用49分,pcduino用42分钟。
3)有python,升级安装工具是apt-get,直接用之前raspberry pi初始化安装的脚本,修改一下就跑起来了。
4)pcduino与arduino的链接会更方便一些。

缺点:
1)pcduino的系统安装比raspberry pi麻烦,需要先烧内核,然后再安装系统。
2)pcduino的体积比raspberry pi大出不少,宽度都是5.5cm,但是pcduino的长度是12.5,raspberry pi是8.5.
3)感觉pcduino的做工不如raspberry pi 原产的做工好,pcduino比较粗糙。
4) 板载的2G初步感觉有点鸡肋。如果要学习linux、python、golang、mysql、C等装上就没有空间了,必须要用SD。
以后再研究看看,是否能做个什么来辅助内存。