古人智慧

Just Do it!
上士聞道,勤而行之;中士聞道,若存若亡;下士聞道,大笑之。不笑,不足以爲道。
~ 道德經 41

「實現夢想不是追逐成功,而是在於賦予生命意義,人生中的每個決定與聲音都有其重要含義。」"The key to realizing a dream is to focus not on success but on significance — and then even the small steps and little victories along your path will take on greater meaning."
電視名人-歐普拉·溫芙蕾(OPRAH WINFREY)

搜尋此網誌

Translation

顯示具有 4.Banana pro 標籤的文章。 顯示所有文章
顯示具有 4.Banana pro 標籤的文章。 顯示所有文章

2018年3月18日 星期日

[Banana Pi] send email by SMTP Server(send only)

最近做了一個追蹤美金的python code可以每天從台灣銀行下載當天的匯率製作出歷史曲線圖

《美金匯率歷史曲線圖》
把這個code放到小電腦(樹莓派香蕉派),然後自動透過email把曲線圖寄出。
好玩的唄。。。。
當然,有人肯定會說這東西早就有人做好了,為何還要ooxx。。。。
~只能說~~~我就是想做好玩學點東西,阿不然?
這篇先記錄如何在小電腦BananaPi設置email server相關步驟做個筆記,不聊到如何coding這個python爬蟲:

Step1:

設定這台小電腦的ip 與 domain name。
sudo nano /etc/hosts
在nano裡面修改

127.0.0.1 localhost.localdomain localhost
aaa.bbb.ccc.ddd your.domain.name your_machine_name
aaa.bbb.ccc.ddd 填上真實對外的ip address
your.domain.name 填上你的domain name
your_machine_name 填上你這台小電腦的名稱(在/etc/hostname裡面)

Step2:

安裝email server相關套件與config,還有uuencode做附加檔的指令。
sudo apt install mailutils 
sudo dpkg-reconfigure exim4-config
有關exim4-config就參考前人筆記(底下有參考文章),簡述幾個重點。
  1. 選擇 “internet site; mail is sent and received directly using SMTP”。
  2. “The ‘mail name’ is ….”填上domain name,也就是yourname@[xxxx.xxx.xxx] xxx的部分。
    其他都按Enter跳過

Step3:

先找個jpeg或png做個附加檔,然後用這個語法把email送出去做測試。
mail -s"title" -A attach_file receiver@email -aFrom:sender@email < email_body_file
或者

echo "write down the email body here" | mail -s"title" -A attach_file receiver@email -aFrom:sender@email
注意:每個參數都必須存在,不能省略
title: email的主旨
attach_file:要附加的檔案
receiver@email:收件人的email
sender@email: 發信人的email,可以自己杜撰的email address
email_body_file:一個文字檔,要寫在email body的文字

Step4:

設定每日固定時間把email發出。
sudo crontab -l
sudo crontab -e
-l :list 目前所有例行性工作
-e :edit 例行性工作
我先把2個工作寫成script
#!/bin/bash
python3 /home/regis/python/currency/currency_trend_v3_2.py
echo "Today USD currency" | mail -A/home/regis/python/currency/currency.png -s"USD currency" xxxxxx@xxxx.com -aFrom:regis@xxxxx.xxxx.xxx
然後,將這個script帶入例行工作
sudo crontab -e

# 
# m h  dom mon dow   command
* 10 * * * /home/regis/python/currency/cy_pic_send
設定為每天早上10點發出
就是這麼簡單,但是我搞了一整天。。。。
時間就是不夠用
~~

參考文章:

2017年3月5日 星期日

[BananaPi] OpenVPN server & Client setup

最近在對岸用PPTP的VPN時常連不上我的BananaPi,但是用SSH卻非常順,我合理懷疑可能PPTP的port 1723被擋。

Google一連串先進同好的資料,一致認為OpenVPN應該是最佳的solution。OpenVPN主要是可以自己設定port,假設用https的port443應該非常適合,因為https是常用的protocol,一般不會去block。

把細節記錄下來避免忘記,而且可以分享同好。

設備與環境:
1. BananaPi with Ubuntu 14.04
2. Wifi AP
3. 用 sudo -i 進入root

Step1: Install OpenVPN
首先進入root,如果用ssh進入ubuntu,用sudo -i.

apt-get update
apt-get upgrade

apt-get install openvpn easy-rsa

2. config OpenVPN 
cd /etc/openvpn
gzip -d /usr/share/doc/openvpn/examples/sample-config-files/server.conf.gz .

產生server.conf
Modify the server.conf

nano server.conf

port 1194 => port 443

dh dh1024.pem => dh dh2048.pem

在ubuntu 16.04不用打開這一行
;push "redirect-gateway def1 bypass-dhcp" => 去掉;

;push "dhcp-option DNS 208.67.222.222" => 去掉;
;push "dhcp-option DNS 208.67.220.220" => 去掉;
增加一行,google的DNS server
push "dhcp-option DNS 8.8.8.8"

;user nobody  => 去掉;
;group nogroup  => 去掉; 

完畢,記得按ctrl-x存檔

3. config IP forward 
修改 /etc/sysctl.conf
nano /etc/sysctl.conf

#net.ipv4.ip_forward=1 => 去掉#

修改/etc/rc.local
nano /etc/rc.local
增加一行
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE

4. generate RSA key
copy easy-rsa 到openvpn,這是避免升級時把key蓋掉
cp -r /usr/share/easy-rsa/ /etc/openvpn
mkdir /etc/openvpn/easy-rsa/keys

修改RSA key屬性
nano /etc/openvpn/easy-rsa/vars

export KEY_COUNTRY="TW"
export KEY_PROVINCE="TW"
export KEY_CITY="TPE"
export KEY_ORG="My Company Name"
export KEY_EMAIL="aaaa@bbbb.com"
export KEY_OU="MYOrganizationalUnit"
export KEY_NAME="server"

完畢,記得按ctrl-x存檔

產生Diffie-Hellman parameters,這花掉大約10分鐘。。。。
openssl dhparam -out /etc/openvpn/dh2048.pem 2048

5. 製作server & client key
cd /etc/openvpn/easy-rsa

source ./vars
./clean-all
./build-ca

產生server key,細節參考 How To Set Up an OpenVPN Server on Ubuntu 14.04
./build-key-server server

產生client key,最好一次多做幾個備用
給phone1
./build-key phone1
給phone2
./build-key phone2
給pad1
./build-key pad1
給pad2
./build-key pad2
給PC1
./build-key pc1

這些步驟會在keys目錄下產生.crt, .key檔案。

6.修改 client.ovpn檔
先複製template
cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf /etc/openvpn/easy-rsa/keys/client.ovpn

cd /etc/openvpn/easy-rsa/keys

製作phone1的ovpn
cp client.ovpn phone1.ovpn

nano phone1.ovpn

remote my-server-1 1194 => remote xxxx.xxxx.xxxx(regis.xxx.xxx or ip) 443
;user nobody => 去掉;
;group nogroup => 去掉;

cert client.crt => cert phone1.crt
key client.key => key phone1.key

其他phone2,pad1/2,PC等等,幾個都是重複這個步驟。

7. 啟動OpenVPN server
cp /etc/openvpn/easy-rsa/keys/{server.crt,server.key,ca.crt} /etc/openvpn

service openvpn start
or
service openvpn restart

service openvpn status

《重要!!》記得在Wifi AP把port 443打開,並接到 banana pi 的IP與port 443

8. 設定 Android phone
將這些client 的.crt .key複製到regis的HOME目錄,並change own and group
cp -r /etc/openvpn/easy-rsa/keys/{phone1.*,ca.crt} /home/regis/vpn/phone1/.
cp -r /etc/openvpn/easy-rsa/keys/{phone2.*,ca.crt} /home/regis/vpn/phone2/.
cp -r /etc/openvpn/easy-rsa/keys/{pad1.*,ca.crt} /home/regis/vpn/pad1/.
....

cd /home/regis/vpn/phone1
chown regis *
chgrp regis *
(這個步驟是為了可以用SCP複製到Macbook)
其他都一樣

複製到Macbook
scp -P xxxx 192.168.x.xxx:/home/regis/vpn/phone1/* ./vpn_keys/phone1/.
主要4個檔案
ca.crt
phone1.crt
phone1.key
phone1.ovpn

scp -P xxxx 192.168.x.xxx:/home/regis/vpn/phone2/* ./vpn_keys/phone2/.

然後,複製到手機與PC

9. 設定PC

《心得》
我發現連接速度很快,其他等下次出差再看看效果。


參考文章:

2016年12月16日 星期五

Git架設-Banana-Pi M2+

因正職工作因素,時常出差在外地。在hotel寫Code時,需要參考以前的code都很困擾。如果把這些code全部上傳到GitHub又不太現實。
心中就想自己有個Git repository可以從外地進來取code,應該是很美好的事!
是的,在網路上已經有很多先進提供很好的方案,經過實作後完全符合我的需求,真是太棒了!

記錄實作流程:
設備:
1. Banana-Pi M2+
2. TP-Link Wifi分享器

安裝Git:
>> sudo apt-get install git-core

建立一個Git repository放在  /var/git 目錄內:
>> cd /var
>> sudo mkdir git

>> cd git
>> sudo mkdir regis_project.git

>> cd regis_project.git
>> sudo git --bare init

建立git的group:
>> sudo groupadd git
>> sudo usermod -a -G git your_login # your_login改成你自己的帳號。
>> sudo chgrp -R git /var/git
>> sudo chmod g+rwx -R /var/git

到這裡,已經在server(Banana-Pi)端完成Git的部署。
再來就是把所有的code上傳到這個repository。

我所有的code放在Mac的 ~/Documents/arduino/regis_project
>> cd ~/Documents/arduino/regis_project
>> git init

設定server端的short name,之後輸入比較容易,因為避免被駭,我把ssh的port換掉。
>> git remote add origin ssh://your_login@your.host:port/var/git/regis_project.git

這裡的host name,如果在內網就直接輸入banana-pi的IP,在外網就要輸入外網對應的IP。

把code上傳
git add .
git commit -m "說明remark"
git push origin master

完成所有code上傳。
再來,就是實驗下載code。

在PC上d槽
D:\test\t2\regis_project> git init
D:\test\t2\regis_project> git clone origin
就可以把code全部拉下來在這個目錄。

有用的命令:
git status -v
git log
git log --graph --oneline
git pull
git mergetool



參考先進文章:
簡易git server架設教學 (用Ubuntu linux)
伺服器上的 Git - 在伺服器上部署 Git

2016年10月8日 星期六

[BananaPi M2+] How to resize the partition

把BananaPi設定成VPN還蠻不錯用的,可以在外翻牆回家。。。。
每次出差必定將banana做個apt-get升級,讓banana保持最新狀態。

我是用emmc開機模式,這次升級發生root的空間不足,原來之前把image燒到emmc時並無做partition resize的動作,所以在系統上就只有1.6G,用了半年就爆了,XD。

在網上找了不少資料與做實驗,正確做法整理如下:
分析原因:
1. 先用df確定disk空間status - 發現/ (root)的空間只剩下0%,難怪無法升級。
2. 再用mount,確定/(root)是mount在/dev/mmcblk0p2

因為做partition resize必須umount,但是 /(root) 的partition是正在運行的partition,不能umount,所以必須要用SD card開機再做resize的動作。

做法:
1. 製作1張可開機的SD card,可參考這裡 。
2. 開機後,root賬號login
3. 用parted指令可以resize partition在emmc的disk,細節可參考這裡
4. reboot,還是用SD card開機。
5. 最後,用root下resize2fs指令把filesystem重新整理完成。

《code》
regis@bpi-iot-ros-ai:~ $ sudo parted /dev/mmcblk1 #mmcblk1 represent eMMC
GNU Parted 3.2
Using /dev/mmcblk1
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print                          #List partitions                                
Model: SD SL08G (sd/mmc)
Disk /dev/mmcblk1: 7948MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
  1      8389kB  58.7MB  50.3MB  fat16        primary  msftdata
  2      58.7MB  3670MB  3611MB  ext4         primary  msftdata

(parted) resizepart                    #Resize partition
Partition number? 2                    #Select partition 2
End?  [3670MB]? -1                     #Resize to the end                              
(parted) print                         #List partitons                                
Model: SD SL08G (sd/mmc)
Disk /dev/mmcblk0: 7948MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name     Flags
 1      8389kB  58.7MB  50.3MB  fat16        primary  msftdata
 2      58.7MB  7947MB  7888MB  ext4         primary  msftdata

(parted)  q                                                              
Information: You may need to update /etc/fstab.

regis@bpi-iot-ros-ai:~ $ sudo reboot
regis@bpi-iot-ros-ai:~ $ sudo resize2fs /dev/mmcblk1p2


重新用eMMC開機後,用df指令可以確認已經resize完成。

regis@bpi-iot-ros-ai:~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root        7155560 1525744   5266352  23% /
devtmpfs          380800       0    380800   0% /dev
tmpfs             512036       0    512036   0% /dev/shm
tmpfs             512036    6904    505132   2% /run
tmpfs               5120       4      5116   1% /run/lock
tmpfs             512036       0    512036   0% /sys/fs/cgroup

/dev/mmcblk0p1    261868  213032     48836  82% /boot

2016年8月16日 星期二

Linux重要command


透過 systemctl 管理單一服務daemon的啟動/開機啟動與觀察狀態
systemctl [command] [unit]
systemctl status

觀察power saving狀態
sudo powertop


Banana Pi M2+ case design

因緣手上有一片banana pi m2+,考慮用來做高階的robot。
利用幾天時間用SketchUp為Banana Pi M2+做個簡單的外殼,方便攜帶與避免短路弄壞板子。

美圖分享:
《Sketchup建模》
《Prusa i3還是老當益壯,印的很漂亮 》

《花了50分鐘,成功》

《先把板子裝到底殼,因為太合身,要稍微扳開側邊與壓住Reset鍵,斜放入》

《底視圖》

《側視圖》

《USB、SD,IR》

《組合圖》

《為了降低機身,把USB與網孔露出》


《上視圖》

《底視圖》

《旁邊多了 Banana Pi M2+字樣,不過印的不是很好》
0.5mm孔徑不適合印太細的字

經過組合后,整個板子非常堅固,有3D打印機真的太棒了!!!

2016年7月24日 星期日

[Banana] turn off the wifi

Banana Pro使用在有線的情況下,如果要把Wifi關掉,怎麼做?
很簡單,用這個指令:


sudo ifconfig wlan2 down

如果要在系統startup的時候就執行這個指令,就在直接在 /etc/rc.local加入這一行。

sudo nano /etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

#turn off the wifi
ifconfig wlan2 down

2015年10月11日 星期日

[Banana] send email through gmail server

用ADSL最麻煩就是IP時常會被更換,如果架設server就很頭疼。
我想到一個方法就是每隔一段時間,自動由Banana送出一個email內含IP address就可解決。

由Linux系統送Email經過Gmail的server
http://www.arthurtoday.com/2011/08/ubuntu-google-apps-gmail-smtp-relay.html

安装mail server
sudo apt-get install msmtp

sudo apt-get install sendmail mailutils sendmail-bin

設定msmtp為sendmail
sudo ln -s /usr/bin/msmtp /usr/sbin/sendmail

裝好之後,要人工來建立這個 msmtp 的設定檔,請在 /etc 下建立一個叫做「msmtprc」的檔案,然後,在 /etc/msmtprc 檔案裡,加入下列設定內容。

$ sudo nano /etc/msmtprc

account gmail
host smtp.gmail.com
protocol smtp
port 587
auth on
user arthur.yu@arthurtoday.com
password 12345678
tls on
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
from arthur.yu@arthurtoday.com
account default: gmail

有個問題就是要調整Gmail的權限保護,不然會被Gmail擋住。
然後設定例行工作:
http://linux.vbird.org/linux_basic/0430cron.php#cron
取得外網IP:
http://ipecho.net/plain

實例解說:

2015年6月8日 星期一

[BananaPro] VPN server setup - Lubuntu

架設VPN已經想了很久,因工作的需求,時常出差到大陸,雖然LINE和FB不能用還不會造成困擾,但是許多google service不能用,讓人感覺非常不方便。

今天就把BPro拿出來好好研究如何架上VPN,在網上google許多前人的經驗,發現還真的蠻簡單的,雖然在過程中遇到一些問題,不過運氣好很快就解決。


安裝VPN server首先要確定家裡網路的環境和設備,分為幾個步驟:
Step 1:
《網路結構》
           
         internet ﹣》中華電信 ﹣》 ADSL ﹣》 Wifi AP ﹣》intranet -》BananaPro

2015年6月7日 星期日

[Banana Pro] Device driver study(device_create, misc_register) - LED control

上次第一個《device driver sample 範例》 需要自己建立 node,這並不是太好的做法。

本次提供一個方法是在driver init的時候,自動建立driver node。
有兩個方式:

1. 使用 device_create
2. 使用 misc_register 

/*
 * led_drv:
 * LED device driver test program for banana Pro
 * Copyright (c) 2015 Regis Hsu
 *
 * Thanks to code samples from Gordon Henderson
 ***********************************************************************
 *
 *    led_drv is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    led_drv is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with wiringPi.
 *    If not, see <http://www.gnu.org/licenses/>.
 ***********************************************************************
 */

2015年6月5日 星期五

[BananaPro] Scratch for Arduino on BananaPro

最近一直在找題目,主要是以Arduino和3D printer有相關的方向。
看到MIT的Scratch,哇, 真是好物啊!!

腦海裡,這個App可以結合機械手臂,遙控車,機械獸,規劃一些新的指令,那就可以讓小孩學習如何控制這些機器,進而提升意願往更進階的領域研究,讓我們小孩可以在未來更有競爭力。看到台灣電子”慘“業現狀,真是無言。。。


用幫3DP幫BananaPro穿新裝




2015年6月2日 星期二

[Banana Pro] Device driver study - LED control

There is my first device driver in Banana Pro board.

My idea is using this device driver module to control the LED board as below:


Here is the build process and source code:

Setup the build env:
Step 1. Download and rebuild the bsp 
git clone https://github.com/LeMaker/lemaker-bsp.git
refer to my another bolg - Banana Pro] Building u-boot, script.bin and linux-kernel

2015年5月30日 星期六

[Banana Pro] Building u-boot, script.bin and linux-kernel

這幾天這個link被移掉,好不容易找到,就乾脆把它copy下來。。。
因爲要開始做一些device driver,最好是kernel自己重新build,並把header file和相關files保留給接下來build driver module使用,不然會遇到module無法被kernel接受。


只要照着做,把script.bin, uImage, modules做好copy到SD卡就可開機。


refer to:

BananaPro/Pi:Building u-boot, script.bin and linux-kernel


Note: all of the following instructions (and also in the sections 2, 3 and 4 
following on from this page) have to be carried out on a Linux computer, not the Bananan Pro/Pi itself!!

This page describes the process of combining sunxi u-boot, the Linux kernel and other bits together to create the basis of a bootable OS from scratch, and which can also be the basis for further hacking. 
Of course, we are not building a whole distribution here, we're only building u-boot, the kernel and a handful of tools, and then using an existing root file system to get a usable OS. Depending on the size of the root file system, we suggest you use a 4GB or larger SD card – a type class 10 would be faster and more stable. SD card partitioning and formatting will be taken care of later. 
We'll outline on this page two methods to build what we need: one is step by step, the other is the easiest way by using sunxi BSP.

2015年5月29日 星期五

[BananaPro] How to see the log info in Banana boot stage by uart/debug port

plug-in the usb-to-serial cable to PC/Mac with Ubuntu, and found the device as /dev/ttyUSB0

run the command below
sudo screen /dev/ttyUSB0 115200


2015年5月10日 星期日

[Banana Pro][WiringBP] How to control the GPIO by C/C++



上次用Python做GPIO控制,個人認為用於簡單電路測試或控制足夠。但是,未來有一些計劃會用到interrupt,用C/C++開發應該會是主要做法。


WiringBP是由LeMaker改寫Drogon開發的Wiring Pi
root@bpro:/home/regis# git clone https://github.com/LeMaker/WiringBP -b bananapro
root@bpro:/home/regis# chmod +x ./build
root@bpro:/home/regis# sudo ./build

安裝好之後,查看GPIO的status:
root@bpro:/home/regis/WiringBP/examples# gpio readall 



[Banana Pro][RPi.GPIO_BP]用Python來寫GPIO控制程式


本次實驗是用Python來做GPIO控制,做法非常簡單易用。

先確定系統在更新狀態:
root@bpro:/home/regis# apt-get update
root@bpro:/home/regis# apt-get upgrade

再來安裝 git:
root@bpro:/home/regis# apt-get install git
root@bpro:/home/regis# apt-get install python-dev
root@bpro:/home/regis# apt-get install gcc g++ make git-core vim

裝Python的gpio控制模組:
### For Banana Pro
root@bpro:/home/regis# git clone https://github.com/LeMaker/RPi.GPIO_BP -b bananapro
root@bpro:/home/regis# cd RPi.GPIO_BP
root@bpro:/home/regis# python setup.py install
root@bpro:/home/regis# sudo python setup.py install

[Banana Pro][Wifi]解決Wifi不通 - 1'st step Wifi enable


當Banana Pro板子與HD組好後,就開始要試試他的功能。
我用HDMI接上電視,開機很順利的到login畫面,
但是,我沒usb keyboard,也不知Wifi是否有通,就算通也不知ip address。。。

這一關就卡住了。。。
後來,借了usb keyboard,總算可以login,但是發現Wifi是不運作的,真是糟啊!

[Banana Pro]解決Power Key無法關機










我在使用這系統時,發現按了Power button(左上角)時,系統並不會下指令讓Banana Pro關機。

上網搜尋一番,找到方法如下:














先安裝ACPI 模組:
regis@bpro:~$ sudo apt-get install acpid

[Banana Pro] - boot from SATA 120G HD ﹣﹣ 用HD開機

拿到板子後,做了很多實驗,也安裝不少軟件和設定,其中遇到很多問題,裝了一堆不知名的軟件模組,搞的Linux有點亂。

想了幾個解決方案,那就是用HD切幾個partition,其中2個來做實驗,只留一個乾淨來做使用。而SD卡就專職開機就好,避免開機失敗。

就開始吧~~~~

root@bpro / # fdisk /dev/sda
Command (m for help): p

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders, total 234441648 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00002b91

[Banana Pro]測試Linux的I2C tools [GY-273] [HMC5883]三軸電子羅盤 e-compass




用了Arduino之後,買了不少module都是I2C的interface。
本次實驗就用GY-273電子羅盤來試試Banana Pro的I2C的功能。
因為Bananian Linux是非常乾淨的OS,必須先安裝I2C tools

regis@bpro:~$ sudo apt-get install i2c-tools python-smbus 

安裝成功之後,會有這幾個features
i2cdetect – 用來列舉I2C bus和上面所有的裝置
i2cdump – 顯示裝置上所有register的值
i2cget – 讀取裝置上某個register的值
i2cset – 寫入裝置上某個register