Arduino Yun 開整
之 通過 Python 控制 D13 指示燈
入手 Arduino Yun 的原因,除了它集成 WIFI 之外,就是它支持與 Python 的交互了。
瞭解 Arduino Uno 的同學,可能玩過驅動 D13 引腳的板載指示燈閃爍的栗子。下面,我們把這個栗子改成,通過 Python 程序開關 D13 指示燈。
概述
Arduino Yun 在一塊板子上包括以下兩個部分,這兩部分的信息交互,由 Bridge 完成。
- 8-Bit AVR MCU (ATmega32u4 with 32kB Flash and 2.5kB RAM running at 16MHz),即我們通常所說的 Arduino 部分。
- 32-Bit MIPS CPU (Atheros AR9331 with 16MB Flash and 64MB RAM running at 400MHz), 運行 OpenWRT(Linino)。
Arduino Yun 上的 Python,運行於 AR9331 的 Linino 上,需要通過 Bridge 包,才能夠和 Arduino 引腳交互。詳情可以參考Understanding Arduino Yún's bridge 這篇文章。
Arduino 端 Sketch
首先,需要在 Arduino IDE 中上傳以下的 Sketch 代碼,存儲爲 python_blink.ino。
#include <Bridge.h>
char D13value[2];
void setup() {
pinMode(13,OUTPUT);
Bridge.begin();
}
void loop() {
Bridge.get("key",D13value,2);
int D13int = atoi(D13value);
digitalWrite(13,D13int);
delay(50);
}
上傳 Sketch 代碼:
Linino 端 Python
其次,SSH 到 Arduino Yun 的 Linino 上:
$ ssh -l root arduinoyun.local
[email protected]'s password:
BusyBox v1.19.4 (2013-08-07 16:16:02 CEST) built-in shell (ash)
Enter 'help' for a list of built-in commands.
___ ___ ___ ___
/\__\ ___ /\__\ ___ /\__\ /\ \
/:/ / /\ \ /::| | /\ \ /::| | /::\ \
/:/ / \:\ \ /:|:| | \:\ \ /:|:| | /:/\:\ \
/:/ / /::\__\ /:/|:| |__ /::\__\ /:/|:| |__ /:/ \:\ \
/:/__/ __/:/\/__/ /:/ |:| /\__\ __/:/\/__/ /:/ |:| /\__\ /:/__/ \:\__\
\:\ \ /\/:/ / \/__|:|/:/ / /\/:/ / \/__|:|/:/ / \:\ \ /:/ /
\:\ \ \::/__/ |:/:/ / \::/__/ |:/:/ / \:\ /:/ /
\:\ \ \:\__\ |::/ / \:\__\ |::/ / \:\/:/ /
\:\__\ \/__/ /:/ / \/__/ /:/ / \::/ /
\/__/ \/__/ \/__/ \/__/
_______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
root@Arduinoyun:~# python -V
Python 2.7.3
root@Arduinoyun:~# which python
/usr/bin/python
root@Arduinoyun:~# cd /mnt/sda1
root@Arduinoyun:/mnt/sda1# cd workspaces/
root@Arduinoyun:/mnt/sda1/workspaces# vi blink.py
編輯 blink.py 內容如下:
#!/usr/bin/python
'''
This file is used with sketch : python_blink.ino
'''
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')
from bridgeclient import BridgeClient as bridgeclient
if __name__ == '__main__':
try:
raw = sys.argv[1]
except IndexError as e1:
raw = raw_input('>>> Input (0 or 1) : ')
_client = bridgeclient()
_client.put('key', raw)
print _client.get('key')
設置此文件爲可執行:
root@Arduinoyun:/mnt/sda1/workspaces# ls
blink.py
root@Arduinoyun:/mnt/sda1/workspaces# chmod +x blink.py
打開 D13 指示燈:
root@Arduinoyun:/mnt/sda1/workspaces# ./blink.py 1
1
關閉 D13 指示燈
root@Arduinoyun:/mnt/sda1/workspaces# ./blink.py 0
0
代碼地址
https://github.com/iascchen/arduino_study/
玩的開心!
轉載請註明出處
Author : iascchen(at)gmail(dot)com
Date : 2014-5-1
Github : https://github.com/iascchen/arduino_study
新浪微博 : @問天鼓
CSDN : http://blog.csdn.net/iascchen