網頁

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月29日 星期三

Arduino 增加 printf

參考 http://playground.arduino.cc/Main/Printf
開啟 Print.h, 加入下列程式

#include <stdarg.h>

#define PRINTF_BUF 80 // define the tmp buffer size (change if desired)

    void printf(const char *format, ...)
    {
        char buf[PRINTF_BUF];
        va_list ap;
        va_start(ap, format);
        vsnprintf(buf, sizeof(buf), format, ap);
        for(char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines
        {
            if(*p == '\n')
                write('\r');
            write(*p);
        }
        va_end(ap);
    }
#ifdef F // check to see if F() macro is available
    void printf(const __FlashStringHelper *format, ...)
    {
        char buf[PRINTF_BUF];
        va_list ap;
        va_start(ap, format);
#ifdef __AVR__
        vsnprintf_P(buf, sizeof(buf), (const char *)format, ap); // progmem for AVR
#else
        vsnprintf(buf, sizeof(buf), (const char *)format, ap); // for the rest of the world
#endif
        for(char *p = &buf[0]; *p; p++) // emulate cooked mode for newlines
        {
            if(*p == '\n')
                write('\r');
            write(*p);
        }
        va_end(ap);
   }
#endif

透過 Google Map API 取得高度

參考 Google Maps Elevation API

https://maps.googleapis.com/maps/api/elevation/json?locations=99.999,99.9999&key=xxxxx

{
   "results" : [
      {
         "elevation" : 123.7428283691406,
         "location" : {
            "lat" : 99.1729,
            "lng" : 999.6859
         },
         "resolution" : 152.7032318115234
      }
   ],
   "status" : "OK"
}

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