網頁

顯示具有 Arduino Yun 標籤的文章。 顯示所有文章
顯示具有 Arduino Yun 標籤的文章。 顯示所有文章

2017年5月24日 星期三

Arduino Yun 網路

Arduino Yun 也可同時啟動 AP+STA 模式,但測試時花了很多時間
因為 Arduino Yun 真的比 Linkit 7688 慢很多

回復AP模式或出廠值
1. 使用 WLAN 按鈕
按5秒,wifi 回AP模式 SSID=Arduino-Yun-XXXXXXXXXXXX, IP=192.168.240.1
按30秒,linux 環境回復出廠值,清除板上的 Flash 記憶體
2. 登入作業系統,使用命令
wifi-reset-and-reboot
reset-to-factory-anyway

wifi-reset-and-reboot 中加入下一行,才能正確回復AP模式
$UCI "delete" "wireless.@wifi-iface[1]"

設定檔位於 /etc/config
dhcp, network, wireless, firewall

使用網頁 (Arduino Luci)設定,新增 Wi-Fi Station 網路
Network/Wifi/Scan
Join Network
取消勾選 An additional network will be created if you leave this unchecked.
沒有取消勾選,會導致 AP 網路被取消掉

uci set wireless.@wifi-iface[1].ssid='DigiChance'
uci set wireless.@wifi-iface[1].key='xxxxx'
uci set wireless.@wifi-iface[1].encryption='psk2'
uci set wireless.@wifi-iface[1].bssid='B8:55:10:56:05:20'
uci commit wireless

uci set network.wwan.proto='static'
uci set network.wwan.ipaddr='192.168.1.60'
uci set network.wwan.netmask='255.255.255.0'
uci set network.wwan.gateway='192.168.1.1'
uci set network.wwan.dns='192.168.1.1'
uci commit network

修改完執行
/etc/init.d/network restart

相關命令
wifi up
wifi down
wifi status
ifconfig
ifconfig eth1 up
ifconfig eth1 down
ifconfig eth1 192.168.1.61 netmask 255.255.255.0
ifup EWAN // 參考 /etc/config/network
ifdown EWAN // 參考 /etc/config/network

同時啟動 ap+sta 模式,網路不用密碼,使用 MAC filter,會出問題,一直找不到如何解決
1. 不用密碼命令
uci set wireless.@wifi-iface[1].encryption='none'
2. MAC filter 似乎會檢查 BSSID
uci set wireless.@wifi-iface[1].bssid='B8:55:10:56:05:20'
3. 切開不同的網段,似乎能解決問題,但不同網段
uci set network.wwan.ipaddr='192.168.3.60'

若開機時沒接上有線網路,會導致無線網路開不起來
停止網路
uci set network.EWAN.auto='0'
開機後(無線網路啟動後)再啟動有線網路
編輯 /etc/rc.local
加入
ifconfig eth1 192.168.1.61 netmask 255.255.255.0
或
ifup wan
ifup EWAN

uci delete wireless.@wifi-iface[1]
uci add wireless wifi-iface
uci set wireless.@wifi-iface[1].network='wwan'
uci set wireless.@wifi-iface[1].device='radio0'
uci set wireless.@wifi-iface[1].mode='sta'
uci set wireless.@wifi-iface[1].ssid='DigiChance'
uci set wireless.@wifi-iface[1].key='22463458'
uci set wireless.@wifi-iface[1].encryption='psk2'
uci set wireless.@wifi-iface[1].bssid='B8:55:10:56:05:20'
uci commit wireless

uci delete network.wwan
uci add network interface
uci rename network.@interface[-1]='wwan'
uci set network.wwan._orig_ifname='wlan0-1'
uci set network.wwan._orig_bridge='false'
uci set network.wwan.proto='static'
uci set network.wwan.ipaddr='192.168.1.60'
uci set network.wwan.netmask='255.255.255.0'
uci set network.wwan.gateway='192.168.1.1'
uci set network.wwan.dns='192.168.1.1'
uci commit network

uci delete network.ewan
uci add network interface
uci rename network.@interface[-1]='ewan'
uci set network.ewan.ifname='eth1'
uci set network.ewan._orig_ifname='eth1-1'
uci set network.ewan.proto='static'
uci set network.ewan.ipaddr='192.168.1.61'
uci set network.ewan.netmask='255.255.255.0'
uci set network.ewan.gateway='192.168.1.1'
uci set network.ewan.dns='192.168.1.1'
uci commit network

wifi-reset-and-reboot 中加入
$UCI "delete" "wireless.@wifi-iface[1]"

2017年4月17日 星期一

OpenSSL 證書

openssl学习笔记--CA及https网站证书配置

root@arduino:~/openssl# mkdir openssl
root@arduino:~/openssl# cd openssl
root@arduino:~/openssl# mkdir private
// 產生 根私鑰
root@arduino:~/openssl# openssl genrsa -out private/cakey.pem 2048
root@arduino:~/openssl# vi /etc/ssl/openssl.cnf
dir             = /root/openssl         # Where everything is kept
default_md = sha512 # 使用預設(sha1)方法,chrome 會報說此方法已經不安全,不給用
ountryName                     = Country Name (2 letter code)
countryName_default             = TW
stateOrProvinceName             = State or Province Name (full name)
stateOrProvinceName_default     = Taiwan
localityName                    = Locality Name (eg, city)
localityName_default            = Taichung
0.organizationName              = Organization Name (eg, company)
0.organizationName_default      = SDL
organizationalUnitName          = Organizational Unit Name (eg, section)
organizationalUnitName_default  = R&D

// 產生 根證書
root@arduino:~/openssl# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
root@arduino:~/openssl# touch index.txt serial
root@arduino:~/openssl# echo 01 >serial
root@arduino:~/openssl# mkdir newcerts
root@arduino:~/openssl# mkdir web1
// 產生網站要用的私鑰,通常在別台電腦
root@arduino:~/openssl# openssl genrsa -out web1/httpd.key 2048
root@arduinoYun:~/openssl# cp /etc/ssl/openssl.cnf web1/
root@arduinoYun:~/openssl# vi web/openssl.cnf
req_extensions = v3_req # The extensions to add to a certificate request
[ v3_req ]
subjectAltName = @alt_names
# Extensions to add to a certificate request
basicConstraints = CA:FALSE
keyUsage = nonRepudiation, digitalSignature, keyEncipherment
[alt_names]
IP.1 = 192.168.1.61
IP.2 = 192.168.240.1
IP.3 = 192.168.43.201
DNS.1 = arduinoyun.local

// 產生證書申請書
root@arduino:~/openssl# openssl req -new -key web1/httpd.key -out web1/httpd.csr -sha512 -config web1/openssl.cnf
// 填入要簽署的網址
Common Name (e.g. server FQDN or YOUR name) []:Arduino Yun
// 下面兩個不要填
A challenge password []:
An optional company name []:

// 可用下面命令,查看證書申請
root@arduinoYun:~/openssl# openssl req -text -noout -in web1/httpd.csr
// 將證書申請書送到根證書的機器,簽屬證書
root@arduino:~/openssl# openssl ca -in web1/httpd.csr -out web1/httpd.crt -days 3650 -extensions v3_req -extfile web1/openssl.cnf
// 回答兩個 y 即可,若發生下列錯誤,查詢 newcerts 目錄下最後的一個 pem
failed to update database
TXT_DB error number 2
// 使用命令
root@arduinoYun:~/openssl# openssl ca -revoke newcerts/02.pem

root@arduino:~/openssl# cd ..
root@arduino:~# vi pythonWeb.py
  server.socket = ssl.wrap_socket(server.socket,
    keyfile='openssl/web1/httpd.key',
    certfile='openssl/web1/httpd.crt',
    #cert_reqs=ssl.CERT_REQUIRED,
    #ca_certs='openssl/cacert.pem',
    server_side=True,
    ssl_version=ssl.PROTOCOL_TLSv1)

傳送根證書 cacert.pem 到 local, 並改名為 cacert.crt
匯入憑證


更改 arduino.local 網址

Arduino Yun ,不須設定 DNS 即可透過 arduino.local 連接上
它是透過 zeroconf  連上 arduino
目前使用 zeroconf 產品有 Apple 的 Bonjour 和 Linux 的 Avahi
Arduino Yun 就是使用 Avahi, 設定檔在 /etc/avahi/avahi-daemon.conf
其中
#host-name=foo

因為沒有設定,所以是使用主機的 host name
可以透過網頁設定

2017年4月12日 星期三

python https server

root@arduino:~# openssl genrsa -out python.key 2048
root@arduino:~# openssl req -new -key python.key -out python.csr
root@arduino:~# openssl x509 -req -days 365 -in python.csr -signkey python.key -out python.crt
root@arduino:~# cat python.crt python.key > python.pem

  server = HTTPServer(('', PORT_NUMBER), myHandler)
  server.socket = ssl.wrap_socket(server.socket,
    keyfile='python.key',
    certfile='python.pem',
    ca_certs='python.crt',
    server_side=True,
    ssl_version=ssl.PROTOCOL_TLSv1)
  server.serve_forever()


2017年3月31日 星期五

Arduino Yun Bridge

Linino 的 console /dev/ttyATH0
Arduino 使用 YunSerialTerminal

Arduino 使用 Bridge, 在 Bridge.cpp 中
Arduino\libraries\Bridge\src\Bridge.cpp
void BridgeClass::begin() {
  uint8_t quit_cmd[] = {'X', 'X', 'X', 'X', 'X'};
  transfer(quit_cmd, 5);
  stream.print(F("run-bridge\n"));
}

Linino 執行 /usr/bin/run-bridge
/usr/bin/run-bridge
exec python -u bridge.py 2> /tmp/bridge.py-stderr.log

/usr/lib/python2.7/bridge/packet.py
使用 stdin stdout

/usr/lib/python2.7/bridge/mailbox.py
json_server = TCPJSONServer('127.0.0.1', 5700)

/usr/lib/python2.7/bridge/mailbox.py
def __init__(self, port=6571):
  utils.try_bind(server, '127.0.0.1', port)

此時可用
root@arduino:~# ps |grep bridge
  802 root      7376 S    python -u bridge.py
root@arduino:~# lsof -i
python     802   root    5u  inet   1297      0t0  TCP localhost:5700 (LISTEN)
python     802   root    4u  inet   1213      0t0  TCP localhost:6571 (LISTEN)
root@arduino:~# netstat -tulpn
tcp        0      0 127.0.0.1:5700          0.0.0.0:*               LISTEN      802/python
tcp        0      0 127.0.0.1:6571          0.0.0.0:*               LISTEN      802/python

/usr/lib/python2.7/bridge/bridge.py
def process(seld, data):
  if data == 'XXXXX':
    print 'Goodbye...'
    self.finished = True
    return ''



/tmp/sketch.hex

/etc/init.d/mcuio
mcu-reset
ldattach -s $BAUDRATE 29 /dev/ttyATH0

/dev/ttySPI0


/usr/bin/detect-console
ttySPI0 ttyATH0


2017年3月22日 星期三

luci 之 MVC 模組

參考 寫個 LuCI 模組

control 模組
root@arduino:/# cd /usr/lib/lua/luci/controller/
root@arduino:/usr/lib/lua/luci/controller# mkdir myapp
root@arduino:/usr/lib/lua/luci/controller# cd myapp
root@arduino:/usr/lib/lua/luci/controller/myapp# vi mymodule.lua
-- Save this to /usr/lib/lua/luci/controller/myapp/mymodule.lua
-- Browse to: /cgi-bin/luci/myapp/mymodule/time.htm

module("luci.controller.myapp.mymodule", package.seeall)

function index()
  -- 定義節點, request 時會呼叫 action_time
  page = entry({"myapp", "mymodule", "time.htm"}, call("action_time"))

  -- 沒有上層節點
  page.dependent = false
  -- 假如是葉節點, 就設成 true 避免錯誤
  page.leaf = true
end

function action_time()
  luci.http.prepare_content("text/html")
  luci.http.write("<h1>Hello LuCi</h1>")
  luci.http.write("<h2>Current time: " .. os.date("%D %T") .. "</h2>")
end

http://arduino.local/cgi-bin/luci/myapp/mymodule/time.htm

control - view 模組
root@arduino:/usr/lib/lua/luci/controller/myapp# cd /usr/lib/lua/luci/view/
root@arduino:/usr/lib/lua/luci/view# mkdir myapp_mymodule
root@arduino:/usr/lib/lua/luci/view# cd myapp_mymodule/
root@arduino:/usr/lib/lua/luci/view/myapp_mymodule# vi time.htm
<!-- Save this to /usr/lib/lua/luci/view/myapp_mymodule/time.htm -->
<%+header%>
<h1>Hello LuCI</h1>
<h2>Current Time: <%= os.date("%D %T")%></h2>
<%+footer%>

root@arduino:/usr/lib/lua/luci/view/myapp_mymodule# cd /usr/lib/lua/luci/controller/myapp/
root@arduino:/usr/lib/lua/luci/controller/myapp# vi mymodule2.lua
-- Save this to /usr/lib/lua/luci/controller/myapp/mymodule2.lua
-- Browse to: /cgi-bin/luci/myapp/mymodule/
-- Browse to: /cgi-bin/luci/myapp/mymodule/time.html

module("luci.controller.myapp.mymodule2", package.seeall)

function index()
  -- 定義節點, 當作 myapp.mymodule.time.html 的別名
  page = entry({"myapp", "mymodule"},
               alias("myapp", "mymodule", "time.html"), "My Module")
  page.dependent = false

  -- 定義節點, request 時會 render view/myapp_mymodule/time.html
  page = entry({"myapp", "mymodule", "time.html"},
               template("myapp_mymodule/time"), "Time")
  page.dependent = false; page.leaf = true
end

http://arduino.local/cgi-bin/luci/myapp/mymodule
http://arduino.local/cgi-bin/luci/myapp/mymodule/time.html



uhttpd 的 CGI

參考 寫個 uhttpd 的 CGI

root@arduino:/# cd /osjs/dist/cgi-bin
root@arduino:/osjs/dist/cig-bin# vi helloCGI
#!/usr/bin/lua
-- HTTP header
print("Content-Type: text/html")
print("")          -- An empty line
-- body
print("<h1>Hello CGI</h1>")
print("<h2>Current time: " .. os.date("%D %T") .. "</h2>")

root@arduino:/osjs/dist/cig-bin# chmod +x helloCGI

http://arduino.local/cgi-bin/helloCGI

Arduino Yun 的 Bridge

使用 Arduino IDE 開啟 File/Examples/Bridge/Bridge, 並上傳

另外登入 Yun
vi bridge_get.py
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
client = bridgeclient()
value = client.get("D13")
print value

python bridge_get.py

http://arduino.local/arduino/mode/13/output
http://arduino.local/arduino/digital/13/1
http://arduino.local/arduino/digital/13/

Understanding Arduino Yún's bridge
http://arduino.local/data/get
http://arduino.local/data/get/D13


http://arduino.local/data/put/D13

https://github.com/arduino/YunWebUI/tree/master/www/keystore_manager_example


Python 的 SimpleHTTPServer

參考 Arduino Yún 小試(2)透過瀏覽器點光明燈

vi lightUpL13Http.py
import sys
import SimpleHTTPServer
import SocketServer
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient

class LedRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/ledOn':
            BridgeClient().put('state', '1')
        elif self.path == '/ledOff':
            BridgeClient().put('state', '0')
        self.path = '/led.html'
        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

server = SocketServer.TCPServer(('0.0.0.0', 8000), LedRequestHandler)
server.serve_forever()

vi led.html
<html>
  <head>
    <meta content="text/html; charset=UTF-8" http-equiv="content-type">
    <title></title>
  </head>
  <body>
     <p><a href="ledOn">On</a>&nbsp; <a href="ledOff">Off</a></p>
  </body>
</html>

python lightUpL13Http.py

http://arduino.local:8000/led.html


2017年3月10日 星期五

Arduino Yun

Yún Shield 不是 Yún
Yún Shield 是用來擴充 Arduino Leonardo 成為 Yún 的

Yún 有三個 Reset 按鈕
網路邊: 32U4 RST
USB邊: WLAN, 作用有兩種
  1. 按5秒,wifi 回AP模式 SSID=Arduino-Yun-XXXXXXXXXXXX, IP=192.168.240.1
      命令: wifi-reset-and-reboot
  2. 按30秒,linux 環境回復出廠值,清除板上的 Flash 記憶體
      命令: reset-to-factory-anyway
另一角: YUN RST

預設的密碼不是 arduino 就是 doghunter

WiFi: Linino-xxxxxxxxxxxx
IP: 192.168.240.1
PASSWORD: doghunter

opkg install xxxx.ipk
opkg remove xxxx
opkg remove xxxx --force-remove
opkg list-installed


編輯 /lib/functions.sh 加入
default_postinst() {
  return 0
}
default_prerm() {
  return 0
}

透過網路連到 colsole
root@arduino:~# telnet localhost 6571

使用 SD卡 擴充 Arduino Yun 的儲存空間

參考 Auto-Expanding of your File System using Pivot Overlay

df -h
查詢 Filesystem 使用狀態

插入 SD卡 會顯示在 /mnt/sda1

執行 overlay-only -i 命令, 第一次執行會要求安裝 Fdisk
在執行一次 overlay-only -i 命令, 會顯示下列選項
1. Pivot Overlay on SDCard (Experimental)
2. Revert Pivot Overlay on SDCard (Experimental)
3. Format SDCard
4. Exit and reboot
0. Exit

若沒有安裝 overlay-only, 使用下列命令
wget https://raw.githubusercontent.com/linino/linino_distro/lininoIO/package/linino/yun-scripts/files/usr/bin/overlay-only
chmod +x overlay-only
mv overlay-only /usr/bin

注意:若是你已經擴充了你的檔案系統,並且你想要更新 Arduino Yun 板,執行下列步驟
- uninstall all programs;
- execute the Revert Pivot Overlay;

Arduino Yun Graphoto2 2.5.2

cd /tmp
wget http://www.ibuyopenwrt.com/images/gphoto2_2.5.2-1_ar71xx.ipk
wget http://www.ibuyopenwrt.com/images/libgphoto2-drivers_2.5.2-1_ar71xx.ipk
wget http://www.ibuyopenwrt.com/images/libgphoto2_2.5.2-1_ar71xx.ipk
opkg update
opkg install libgphoto2_2.5.2-1_ar71xx.ipk
opkg install gphoto2_2.5.2-1_ar71xx.ipk
opkg install libgphoto2-drivers_2.5.2-1_ar71xx.ipk

參考網站 Arduino Yun Gphoto2 2.5.2

2017年1月11日 星期三

Arudino Yun 之 OS

Arduino 分裂成 org 和 cc
org 的 Arduino Yun 使用 LininoIO
cc 的 Arduino Yun 使用 OpenWrt
兩者個關係,實在是搞不清楚

電腦開啟 TFTPUtil GUI, 按下 Options
輸入 Path 和 Server IP Address 後按 OK
按 Start TFTP Server

透過 micro USB 連接到電腦
電腦開啟 Arduino IDE
Tools/Board/Arduino Yun
Tools/Port
File/Examples/Bridge/YunSerialTerminal
按 Upload 傳至 Arduino Yun
電腦開啟 Tera Term, 透過 Serial Port連至 Arduino Yun
若發現是一堆亂碼,修改 YunSerialTerminal 的 linuxBaud 為 115200

壓 Arduino Yun 上的 YUN RST(靠 LED 的按鈕)
並且快速透過 Tera Term(Serial Port) 按下 Enter 鍵,進入 U-Boot

以下使用 org 的 LininoIO
等出現 autoboot in 4 seconds (stop with 'lin'), 輸入 lin
此時會出現
linino>
連接 Arduino Yun 的網路(不能使用 wifi)
setenv serverip 192.168.1.70
setenv ipaddr 192.168.1.72
printenv
tftp 0x80060000 u-boot-linino-yun.bin;
請確認正確下載,若有問題請檢查網路設定
erase 0x9f000000 +0x40000;
cp.b $fileaddr 0x9f000000 $filesize;
tftp 0x80060000 lininoIO-generic-linino-yun-kernel.bin
erase 0x9fEa0000 +0x140000;
cp.b $fileaddr 0x9fea0000 $filesize;
tftp 0x80060000 lininoIO-generic-linino-yun-rootfs-squashfs.bin
erase 0x9f050000 +0xE50000;
cp.b $fileaddr 0x9f050000 $filesize;
erase 0x9f040000 +0x10000;
reset
setenv console ttyATH0,250000
saveenv
reset
透過 SSH 連上 ArduinoYun, 執行 lininoio -f stop
設定 ttyATH0 為 console

以下使用 cc 的 OpenWrt
等出現 autoboot in 4 seconds (type 'ard' to enter u-boot console), 輸入 ard
此時會出現
ar7240>
setenv serverip 192.168.1.70
setenv ipaddr 192.168.1.72
ping 192.168.1.70
tftp 0x80060000 openwrt-ar71xx-generic-linino-u-boot.bin
erase 0x9f000000 +0x40000
cp.b $fileaddr 0x9f000000 $filesize
erase 0x9f040000 +0x10000
tftp 0x80060000 openwrt-ar71xx-generic-yun-16M-kernel.bin
erase 0x9fEa0000 +0x140000
cp.b $fileaddr 0x9fea0000 $filesize
tftp 0x80060000 openwrt-ar71xx-generic-yun-16M-rootfs-squashfs.bin
erase 0x9f050000 +0xE50000
cp.b $fileaddr 0x9f050000 $filesize
bootm 0x9fea0000

org 的 LininoIO 參考資料
LininoIO OS upgrade from U-boot
Upgrade to Linino
Server at download.linino.org

使用網頁更新作業系統
下載最新的影像檔 Index of /linino_distro/lininoIO/latest
檔名有 mini 的是給 YunShield 用的,不要下載
連上 ArduinoYun 網頁
左上角 Applications/Arduino/Arduino Luci
輸入帳號密碼
System/"Backup/Flash Firmware"
點選 Flash new firmware image 的 "選擇檔案",選擇剛下載的影像檔

使用命令列更新作業系統
使用 ssh 連入系統
cd /tmp
wget http://download.linino.org/linino_distro/lininoIO/latest/lininoIO-generic-linino-yun-squashfs-sysupgrade.bin
sysupgrade -v -n lininoIO-generic-linino-yun-squashfs-sysupgrade.bin

使用網頁更新作業系統
下載最新的影像檔,並且解壓縮
連上 ArduinoYun 網頁,並登入
點 CONFIGURE
點 advanced configuration panel (luci)
System/"Backup/Flash Firmware"
點選 Flash new firmware image 的 "選擇檔案",選擇剛下載的影像檔

使用命令列更新作業系統
使用 ssh 連入系統,不能使用 YunSerialTerminal
run-sysupgrade /mnt/sda1/openwrt-ar71xx-generic-yun-squashfs-sysupgrade.bin