钻石会员
主题
帖子
积分19760
阅读权限50
注册时间2011-11-23
最后登录1970-1-1
在线时间 小时
|
本帖最后由 文句子 于 2015-3-29 23:22 编辑
经过一番甚至几番的折腾,Arduino黑云算是在您手中诞生了吧。强调了一堆术语,这个东西究竟有什么好玩的地方呢?
我觉得没有什文字比直接用几个Dem演示更形象不过的了。
同你往日玩Arduino Uno几乎没有任何区别:打开ide,加载你需要用到的库,完成代码,最后编译上传到目标板。
只是你现在手里的Arduino具备了Bridge接口,
因此现在的目标板可以操作的资源至少翻番了!
与openwrt交互,直接完成网络应用,甚至访问AR9331上扩展的某些硬件资源……
Console-控制台通讯版的点灯
本例的演示目的:
1、体会一下bridge正常运行的一些特征
2、通过与console即ar9331控制台的通讯方式,控制arduino uno的硬件资源。
- #include <Console.h>
- const int ledPin = 13; // the pin that the LED is attached to
- int incomingByte; // a variable to read incoming serial data into
- void setup() {
- // initialize serial communication:
- Bridge.begin(115200);
- Console.begin();
- while (!Console){
- ; // wait for Console port to connect.
- }
- Console.println("You're connected to the Console!!!!");
- Console.println("u can control the LED(13) by Type H or L ");
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- }
- void loop() {
- // see if there's incoming serial data:
- if (Console.available() > 0) {
- // read the oldest byte in the serial buffer:
- incomingByte = Console.read();
- // if it's a capital H (ASCII 72), turn on the LED:
- if (incomingByte == 'H') {
- digitalWrite(ledPin, HIGH);
- }
- // if it's an L (ASCII 76) turn off the LED:
- if (incomingByte == 'L') {
- digitalWrite(ledPin, LOW);
- }
- }
- delay(100);
- }
复制代码 点击“upload
注意:硬件上还要观察一下Uno的串口通讯指示灯,应该是类似长亮的状态。这表示我们BRIDGE运行正常
然后打开“Serial Monitor”工具,
剩下的事情简单了,就像代码里写的:输入“H”+回车就是亮灯,输入“L”+回车就是灭灯。
思考:
你觉得你敲入的字符是怎么一步一步到达Uno的呢?
Process-访问因特网上的资源
asciilogo.txtArduino官网上有一个文本文件"
本例的演示目的:
1、Process类的简单使用
2、通过cURL实现互联网使用
- #include <Process.h>
- #include <Console.h>
- void setup() {
- // Initialize Bridge
- Bridge.begin(115200);
- // Initialize Serial
- Console.begin();
- // Wait until a Serial Monitor is connected.
- while (!Console){
- ;
- }
- // run various example processes
- runCurl();
- }
- void loop() {
- // Do nothing here.
- }
- void runCurl() {
- // Launch "curl" command and get Arduino ascii art logo from the network
- // curl is command line program for transferring data using different internet protocols
- Process p; // Create a process and call it "p"
- p.begin("curl"); // Process that launch the "curl" command
- p.addParameter("http://arduino.cc/asciilogo.txt"); // Add the URL parameter to "curl"
- p.run(); // Run the process and wait for its termination
- // Print arduino logo over the Serial
- // A process output can be read with the stream methods
- while (p.available()>0) {
- char c = p.read();
- Console.print(c);
- }
- // Ensure the last bit of data is sent.
- // Serial.flush();
- }
复制代码 upload
打开Serial Monitor
思考:有没有想到除了这个logo字符图,还可以是什么?乐联,yeelink的api行么?
Bridge-WEB浏览器版的点灯
本例的演示目的:
1、YunServer和YunClient类的使用
- /*
- Arduino Yún Bridge example
- This example for the Arduino Yún shows how to use the
- Bridge library to access the digital and analog pins
- on the board through REST calls. It demonstrates how
- you can create your own API when using REST style
- calls through the browser.
- Possible commands created in this shetch:
- * "/arduino/digital/13" -> digitalRead(13)
- * "/arduino/digital/13/1" -> digitalWrite(13, HIGH)
- * "/arduino/analog/2/123" -> analogWrite(2, 123)
- * "/arduino/analog/2" -> analogRead(2)
- * "/arduino/mode/13/input" -> pinMode(13, INPUT)
- * "/arduino/mode/13/output" -> pinMode(13, OUTPUT)
- This example code is part of the public domain
- http://arduino.cc/en/Tutorial/Bridge
- */
- #include <Bridge.h>
- #include <YunServer.h>
- #include <YunClient.h>
- // Listen to the default port 5555, the Yún webserver
- // will forward there all the HTTP requests you send
- YunServer server;
- void setup() {
- // Bridge startup
- pinMode(13, OUTPUT);
- digitalWrite(13, LOW);
- Bridge.begin(115200);
- digitalWrite(13, HIGH);
- // Listen for incoming connection only from localhost
- // (no one from the external network could connect)
- server.listenOnLocalhost();
- server.begin();
- }
- void loop() {
- // Get clients coming from server
- YunClient client = server.accept();
- // There is a new client?
- if (client) {
- // Process request
- process(client);
- // Close connection and free resources.
- client.stop();
- }
- delay(50); // Poll every 50ms
- }
- void process(YunClient client) {
- // read the command
- String command = client.readStringUntil('/');
- // is "digital" command?
- if (command == "digital") {
- digitalCommand(client);
- }
- // is "analog" command?
- if (command == "analog") {
- analogCommand(client);
- }
- // is "mode" command?
- if (command == "mode") {
- modeCommand(client);
- }
- }
- void digitalCommand(YunClient client) {
- int pin, value;
- // Read pin number
- pin = client.parseInt();
- // If the next character is a '/' it means we have an URL
- // with a value like: "/digital/13/1"
- if (client.read() == '/') {
- value = client.parseInt();
- digitalWrite(pin, value);
- }
- else {
- value = digitalRead(pin);
- }
- // Send feedback to client
- client.print(F("Pin D"));
- client.print(pin);
- client.print(F(" set to "));
- client.println(value);
- // Update datastore key with the current pin value
- String key = "D";
- key += pin;
- Bridge.put(key, String(value));
- }
- void analogCommand(YunClient client) {
- int pin, value;
- // Read pin number
- pin = client.parseInt();
- // If the next character is a '/' it means we have an URL
- // with a value like: "/analog/5/120"
- if (client.read() == '/') {
- // Read value and execute command
- value = client.parseInt();
- analogWrite(pin, value);
- // Send feedback to client
- client.print(F("Pin D"));
- client.print(pin);
- client.print(F(" set to analog "));
- client.println(value);
- // Update datastore key with the current pin value
- String key = "D";
- key += pin;
- Bridge.put(key, String(value));
- }
- else {
- // Read analog pin
- value = analogRead(pin);
- // Send feedback to client
- client.print(F("Pin A"));
- client.print(pin);
- client.print(F(" reads analog "));
- client.println(value);
- // Update datastore key with the current pin value
- String key = "A";
- key += pin;
- Bridge.put(key, String(value));
- }
- }
- void modeCommand(YunClient client) {
- int pin;
- // Read pin number
- pin = client.parseInt();
- // If the next character is not a '/' we have a malformed URL
- if (client.read() != '/') {
- client.println(F("error"));
- return;
- }
- String mode = client.readStringUntil('\r');
- if (mode == "input") {
- pinMode(pin, INPUT);
- // Send feedback to client
- client.print(F("Pin D"));
- client.print(pin);
- client.print(F(" configured as INPUT!"));
- return;
- }
- if (mode == "output") {
- pinMode(pin, OUTPUT);
- // Send feedback to client
- client.print(F("Pin D"));
- client.print(pin);
- client.print(F(" configured as OUTPUT!"));
- return;
- }
- client.print(F("error: invalid mode "));
- client.print(mode);
- }
复制代码 操作:
upload
打开浏览器(注意使用兼容的浏览器,推荐chrome),登陆到黑云的路由管理界面
如下图,修改响应动作的url
应该获得以下结果
说明:
由于gpgkey的细节工作留了个尾巴,因此我们现在的访问url采用的是在luci的token后添加功能url的形式
实际上gpgkey应该是就是跨过luci的token,采用隐式的url认证,如官方说明的一样:IP+功能url 即可
|
|